Java Code Examples for org.apache.ignite.internal.IgniteEx#cachex()

The following examples show how to use org.apache.ignite.internal.IgniteEx#cachex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridIndexRebuildSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Check versions presence in index tree.
 *
 * @param srv Node.
 * @param afterRebuild Whether index rebuild has occurred.
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings({"ConstantConditions", "unchecked"})
protected void checkDataState(IgniteEx srv, boolean afterRebuild) throws IgniteCheckedException {
    IgniteInternalCache icache = srv.cachex(CACHE_NAME);

    IgniteCache cache = srv.cache(CACHE_NAME);

    assertNotNull(icache);

    for (IgniteCacheOffheapManager.CacheDataStore store : icache.context().offheap().cacheDataStores()) {
        GridCursor<? extends CacheDataRow> cur = store.cursor();

        while (cur.next()) {
            CacheDataRow row = cur.get();

            int key = row.key().value(icache.context().cacheObjectContext(), false);

            if (!afterRebuild || key <= AMOUNT / 2)
                assertEquals(key, cache.get(key));
            else
                assertEquals(-1, cache.get(key));
        }
    }
}
 
Example 2
Source File: GridIndexRebuildWithMvccEnabledSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void checkDataState(IgniteEx srv, boolean afterRebuild) throws IgniteCheckedException {
    IgniteInternalCache icache = srv.cachex(CACHE_NAME);

    assertNotNull(icache);

    CacheObjectContext coCtx = icache.context().cacheObjectContext();

    for (IgniteCacheOffheapManager.CacheDataStore store : icache.context().offheap().cacheDataStores()) {
        GridCursor<? extends CacheDataRow> cur = store.cursor();

        while (cur.next()) {
            CacheDataRow row = cur.get();

            int key = row.key().value(coCtx, false);

            List<IgniteBiTuple<Object, MvccVersion>> vers = store.mvccFindAllVersions(icache.context(), row.key());

            if (!afterRebuild || key <= AMOUNT / 2)
                assertEquals(key, vers.size());
            else {
                // For keys affected by concurrent put there are two versions -
                // -1 (concurrent put mark) and newest restored value as long as put cleans obsolete versions.
                assertEquals(2, vers.size());

                Object val0 = ((CacheObject)vers.get(0).getKey()).value(coCtx, false);
                Object val1 = ((CacheObject)vers.get(1).getKey()).value(coCtx, false);

                assertEquals(-1, val0);
                assertEquals(key, val1);
            }

        }
    }
}
 
Example 3
Source File: IgniteLostPartitionsOnLeaveBaselineSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if failed.
 */
@Test
public void testLostPartitionsOnLeaveBaseline() throws Exception {
    try {
        final IgniteEx gridFirst = startGrid(0);
        startGrid(1);

        gridFirst.cluster().active(true);

        gridFirst.getOrCreateCaches(Arrays.asList(
            cacheConfiguration("cache-no-persistence", PARTITIONED, ATOMIC, "no-persistence"),
            cacheConfiguration("cache-persistence", PARTITIONED, ATOMIC, null)
        ));

        IgniteInternalCache<Object, Object> cacheNoPersistence = gridFirst.cachex("cache-no-persistence");
        IgniteInternalCache<Object, Object> cachePersistence = gridFirst.cachex("cache-persistence");

        for (int i = 0; i < 10; i++) {
            cacheNoPersistence.put(i, i);
            cachePersistence.put(i, i);
        }

        stopGrid(1);

        resetBaselineTopology();

        assertTrue("List of lost partitions for cache without persistence should not be empty.",
            !cacheNoPersistence.context().topology().lostPartitions().isEmpty());

        assertTrue("List of lost partitions for cache with persistence should not be empty.",
            !cachePersistence.context().topology().lostPartitions().isEmpty());
    } finally {
        stopAllGrids();
    }
}
 
Example 4
Source File: GridIoManagerFileTransmissionSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param ignite An ignite instance.
 * @param cacheName Cache name.
 * @return The cache working directory.
 */
private File cacheWorkDir(IgniteEx ignite, String cacheName) {
    // Resolve cache directory.
    IgniteInternalCache<?, ?> cache = ignite.cachex(cacheName);

    FilePageStoreManager pageStoreMgr = (FilePageStoreManager)cache.context()
        .shared()
        .pageStore();

    return pageStoreMgr.cacheWorkDir(cache.configuration());
}
 
Example 5
Source File: GridIndexRebuildSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Creating a cache, table, index and populating data.
 *
 * @param node Node.
 * @return Cache.
 * @throws Exception if failed.
 */
private IgniteInternalCache createAndFillTableWithIndex(IgniteEx node) throws Exception {
    requireNonNull(node);

    String cacheName = CACHE_NAME;

    execute(node, "CREATE TABLE T(k int primary key, v int) WITH \"cache_name=" + cacheName +
        ",wrap_value=false,atomicity=transactional\"");

    execute(node, "CREATE INDEX IDX ON T(v)");

    IgniteInternalCache cc = node.cachex(cacheName);

    assertNotNull(cc);

    putData(node, false);

    return cc;
}
 
Example 6
Source File: EncryptedCacheGroupCreateTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** */
private KeystoreEncryptionKey createEncryptedCache(String cacheName, String grpName) {
    CacheConfiguration<Long, String> ccfg = new CacheConfiguration<>(cacheName);

    ccfg.setEncryptionEnabled(true);
    ccfg.setGroupName(grpName);

    IgniteEx grid = grid(0);

    grid.createCache(ccfg);

    IgniteInternalCache<Object, Object> enc = grid.cachex(cacheName);

    assertNotNull(enc);

    KeystoreEncryptionKey key =
        (KeystoreEncryptionKey)grid.context().encryption().groupKey(CU.cacheGroupId(cacheName, grpName));

    assertNotNull(key);
    assertNotNull(key.key());

    return key;
}
 
Example 7
Source File: EncryptedCacheGroupCreateTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testCreateEncryptedCacheGroup() throws Exception {
    KeystoreEncryptionKey key = createEncryptedCache(ENCRYPTED_CACHE, ENCRYPTED_GROUP);

    CacheConfiguration<Long, String> ccfg = new CacheConfiguration<>(ENCRYPTED_CACHE + "2");

    ccfg.setEncryptionEnabled(true);
    ccfg.setGroupName(ENCRYPTED_GROUP);

    IgniteEx grid = grid(0);

    grid.createCache(ccfg);

    IgniteInternalCache<Object, Object> encrypted2 = grid.cachex(ENCRYPTED_CACHE + "2");

    GridEncryptionManager encMgr = encrypted2.context().kernalContext().encryption();

    KeystoreEncryptionKey key2 = (KeystoreEncryptionKey)encMgr.groupKey(CU.cacheGroupId(ENCRYPTED_CACHE, ENCRYPTED_GROUP));

    assertNotNull(key2);
    assertNotNull(key2.key());

    assertEquals(key.key(), key2.key());
}
 
Example 8
Source File: EncryptedCacheCreateTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testCreateEncryptedCache() throws Exception {
    CacheConfiguration<Long, String> ccfg = new CacheConfiguration<>(ENCRYPTED_CACHE);

    ccfg.setEncryptionEnabled(true);

    IgniteEx grid = grid(0);

    grid.createCache(ccfg);

    IgniteInternalCache<Object, Object> enc = grid.cachex(ENCRYPTED_CACHE);

    assertNotNull(enc);

    KeystoreEncryptionKey key =
        (KeystoreEncryptionKey)grid.context().encryption().groupKey(CU.cacheGroupId(ENCRYPTED_CACHE, null));

    assertNotNull(key);
    assertNotNull(key.key());
}
 
Example 9
Source File: IgnitePdsCorruptedStoreTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Test node invalidation when meta store is read only.
 */
@Test
public void testReadOnlyMetaStore() throws Exception {
    IgniteEx ignite0 = startGrid(0);

    AtomicReference<File> readOnlyFile = new AtomicReference<>();

    failingFileIOFactory.createClosure((file, options) -> {
        if (Arrays.asList(options).contains(StandardOpenOption.WRITE) && file.equals(readOnlyFile.get()))
            throw new IOException("File is readonly.");

        return null;
    });

    ignite0.cluster().active(true);

    IgniteInternalCache<Integer, Integer> cache = ignite0.cachex(CACHE_NAME1);

    cache.put(1, 1);

    ignite0.cluster().active(false);

    FilePageStoreManager storeMgr = ((FilePageStoreManager)ignite0.context().cache().context().pageStore());

    File workDir = storeMgr.workDir();
    File metaStoreDir = new File(workDir, MetaStorage.METASTORAGE_CACHE_NAME.toLowerCase());
    File metaStoreFile = new File(metaStoreDir, String.format(FilePageStoreManager.PART_FILE_TEMPLATE, 0));

    readOnlyFile.set(metaStoreFile);

    IgniteInternalFuture fut = GridTestUtils.runAsync(new Runnable() {
        @Override public void run() {
            try {
                ignite0.cluster().active(true);
            }
            catch (Exception ignore) {
                // No-op.
            }
        }
    });

    waitFailure(IOException.class);

    fut.cancel();
}
 
Example 10
Source File: WalPreloadingConcurrentTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public final void testConcurrentReserveReleaseHistoryForPreloading() throws Exception {
    IgniteEx ig = startGrid(0);

    final int entryCnt = 3200;

    ig.cluster().active(true);

    IgniteInternalCache<Object, Object> cache = ig.cachex(DEFAULT_CACHE_NAME);

    for (int k = 0; k < entryCnt; k++)
        cache.put(k, k);

    forceCheckpoint();

    GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager)ig.context().cache().context().database();

    AtomicBoolean stop = new AtomicBoolean(false);

    //Millis
    long duration = 5000L;

    IgniteInternalFuture fut1 = multithreadedAsync(new Callable<Object>() {
        @Override public Object call() {
            int randomPart = ThreadLocalRandom.current().nextInt(cache.configuration().getAffinity().partitions());

            while (!stop.get()) {
                db.reserveHistoryForPreloading(Collections.singletonMap(
                    new T2<Integer, Integer>(cache.context().groupId(), randomPart),
                    0L
                ));
            }

            return null;
        }
    }, THREADS, "reserve-history-thread");

    IgniteInternalFuture fut2 = multithreadedAsync(new Callable<Object>() {
        @Override public Object call() {
            while (!stop.get())
                db.releaseHistoryForPreloading();

            return null;
        }
    }, THREADS, "release-history-thread");

    Thread.sleep(duration);

    stop.set(true);

    fut1.get();

    fut2.get();
}
 
Example 11
Source File: GridCacheFullTextQuerySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Fill cache.
 *
 * @throws IgniteCheckedException if failed.
 */
private static Set<Integer> populateCache(IgniteEx ignite, boolean loc, int cnt,
    IgnitePredicate<Integer> expectedEntryFilter) throws IgniteCheckedException {
    IgniteInternalCache<Integer, Person> cache = ignite.cachex(PERSON_CACHE);

    assertNotNull(cache);

    Random rand = new Random();

    HashSet<Integer> exp = new HashSet<>();

    Affinity<Integer> aff = cache.affinity();

    ClusterNode localNode = cache.context().localNode();

    for (int i = 0; i < cnt; i++) {
        int val = rand.nextInt(cnt);

        cache.put(val, new Person(String.valueOf(val), val));

        if (expectedEntryFilter.apply(val) && (!loc || aff.isPrimary(localNode, val)))
            exp.add(val);
    }

    return exp;
}
 
Example 12
Source File: GridCacheFullTextQuerySelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Check if there is missed keys.
 *
 * @throws IgniteCheckedException if failed.
 */
private static void checkForMissedKeys(IgniteEx ignite, Collection<Integer> exp,
    List<Cache.Entry<Integer, ?>> all) throws IgniteCheckedException {
    if (exp.size() == 0)
        return;

    IgniteInternalCache<Integer, Person> cache = ignite.cachex(PERSON_CACHE);

    assertNotNull(cache);

    StringBuilder sb = new StringBuilder();

    Affinity<Integer> aff = cache.affinity();

    for (Integer key : exp) {
        Integer part = aff.partition(key);

        sb.append(
            String.format("Query did not return expected key '%d' (exists: %s), partition '%d', partition nodes: ",
                key, cache.get(key) != null, part));

        Collection<ClusterNode> partNodes = aff.mapPartitionToPrimaryAndBackups(part);

        for (ClusterNode node : partNodes)
            sb.append(node).append("  ");

        sb.append(";\n");
    }

    sb.append("Returned keys: ");

    for (Cache.Entry e : all)
        sb.append(e.getKey()).append(" ");

    sb.append(";\n");

    fail(sb.toString());
}
 
Example 13
Source File: IgnitePdsCorruptedStoreTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Test node invalidation when cache meta is corrupted.
 */
@Test
public void testCacheMetaCorruption() throws Exception {
    IgniteEx ignite = startGrid(0);

    ignite.cluster().active(true);

    IgniteInternalCache cache = ignite.cachex(CACHE_NAME1);

    cache.put(1, 1);

    int partId = cache.affinity().partition(1);

    int grpId = cache.context().group().groupId();

    corruptTreeRoot(ignite, (PageMemoryEx)cache.context().dataRegion().pageMemory(), grpId, partId);

    ignite.cluster().active(false);

    stopGrid(0);

    try {
        startGrid(0);

        ignite.cluster().active(true);

        cache.put(1, 1);
    }
    catch (Exception e) {
        // No-op.
    }

    waitFailure(StorageException.class);
}
 
Example 14
Source File: GridIndexRebuildWithMvccEnabledSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/** {@inheritDoc} */
@Test
@Override public void testIndexRebuild() throws Exception {
    IgniteEx srv = startServer();

    execute(srv, "CREATE TABLE T(k int primary key, v int) WITH \"cache_name=T,wrap_value=false," +
        "atomicity=transactional_snapshot\"");

    execute(srv, "CREATE INDEX IDX ON T(v)");

    IgniteInternalCache cc = srv.cachex(CACHE_NAME);

    assertNotNull(cc);

    lockVersion(srv);

    putData(srv, false);

    checkDataState(srv, false);

    File idxPath = indexFile(cc);

    stopAllGrids();

    assertTrue(U.delete(idxPath));

    srv = startServer();

    putData(srv, true);

    checkDataState(srv, true);
}
 
Example 15
Source File: SpringEncryptedCacheRestartTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testCreateEncryptedCacheGroup() throws Exception {
    IgniteEx g0 = (IgniteEx)IgnitionEx.start(
        IgniteUtils.resolveIgnitePath(
            "modules/spring/src/test/config/enc/enc-group.xml").getAbsolutePath(), "grid-0");

    IgniteEx g1 = (IgniteEx)IgnitionEx.start(
        IgniteUtils.resolveIgnitePath(
            "modules/spring/src/test/config/enc/enc-group-2.xml").getAbsolutePath(), "grid-1");

    g1.cluster().active(true);

    awaitPartitionMapExchange();

    IgniteInternalCache<Object, Object> encrypted = g0.cachex("encrypted");

    assertNotNull(encrypted);

    IgniteInternalCache<Object, Object> encrypted2 = g0.cachex("encrypted-2");

    assertNotNull(encrypted2);

    KeystoreEncryptionKey key = (KeystoreEncryptionKey)g0.context().encryption().groupKey(
        CU.cacheGroupId(encrypted.name(), encrypted.configuration().getGroupName()));

    assertNotNull(key);
    assertNotNull(key.key());

    KeystoreEncryptionKey key2 = (KeystoreEncryptionKey)g0.context().encryption().groupKey(
        CU.cacheGroupId(encrypted2.name(), encrypted2.configuration().getGroupName()));

    assertNotNull(key2);
    assertNotNull(key2.key());

    assertEquals(key.key(), key2.key());
}
 
Example 16
Source File: RebuildIndexLogMessageTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/** */
@Test
public void testRebuildIndexLogMessage() throws Exception {
    IgniteEx ignite = startGrids(1);

    String gridName = ignite.name();

    ignite.cluster().active(true);

    IgniteCache<Integer, Person> cacheA = ignite.getOrCreateCache(CACHE_NAME_A);
    IgniteCache<Integer, Person> cacheB = ignite.getOrCreateCache(CACHE_NAME_B);

    IgniteInternalCache<Integer, Person> cacheAEx = ignite.cachex(CACHE_NAME_A);
    IgniteInternalCache<Integer, Person> cacheBEx = ignite.cachex(CACHE_NAME_B);

    for (int i = 0; i < 100; i++)
        cacheA.put(i, new Person(i, i));

    for (int i = 0; i < 100; i++)
        cacheB.put(i, new Person(i, i));

    forceCheckpoint();

    File cacheAWorkDir = ((FilePageStoreManager)cacheAEx.context().shared().pageStore())
        .cacheWorkDir(cacheAEx.configuration());
    File cacheBWorkDir = ((FilePageStoreManager)cacheBEx.context().shared().pageStore())
        .cacheWorkDir(cacheBEx.configuration());

    File idxPathA = cacheAWorkDir.toPath().resolve("index.bin").toFile();
    File idxPathB = cacheBWorkDir.toPath().resolve("index.bin").toFile();

    stopAllGrids();

    assertTrue(U.delete(idxPathA));
    assertTrue(U.delete(idxPathB));

    ignite = startGrid(getConfiguration(gridName));

    ignite.cluster().active(true);

    cacheA = ignite.getOrCreateCache(CACHE_NAME_A);
    cacheB = ignite.getOrCreateCache(CACHE_NAME_B);

    cacheA.put(1000, new Person(1000, 1));
    cacheB.put(1000, new Person(1000, 1));

    checkLatch.await(60, TimeUnit.SECONDS);

    if (checkLatch.getCount() > 0)
        throw new TimeoutException("Test timed out: cannot detect log message about completion of indexes rebuilding");

    assertTrue(logLsnr.check());
}
 
Example 17
Source File: AbstractEncryptionTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/** */
void checkEncryptedCaches(IgniteEx grid0, IgniteEx grid1) {
    Set<String> cacheNames = new HashSet<>(grid0.cacheNames());

    cacheNames.addAll(grid1.cacheNames());

    for (String cacheName : cacheNames) {
        CacheConfiguration ccfg = grid1.cache(cacheName).getConfiguration(CacheConfiguration.class);

        if (!ccfg.isEncryptionEnabled())
            continue;

        IgniteInternalCache<?, ?> encrypted0 = grid0.cachex(cacheName);

        int grpId = CU.cacheGroupId(cacheName, ccfg.getGroupName());

        assertNotNull(encrypted0);

        IgniteInternalCache<?, ?> encrypted1 = grid1.cachex(cacheName);

        assertNotNull(encrypted1);

        assertTrue(encrypted1.configuration().isEncryptionEnabled());

        KeystoreEncryptionKey encKey0 = (KeystoreEncryptionKey)grid0.context().encryption().groupKey(grpId);

        assertNotNull(encKey0);
        assertNotNull(encKey0.key());

        if (!grid1.configuration().isClientMode()) {
            KeystoreEncryptionKey encKey1 = (KeystoreEncryptionKey)grid1.context().encryption().groupKey(grpId);

            assertNotNull(encKey1);
            assertNotNull(encKey1.key());

            assertEquals(encKey0.key(), encKey1.key());
        }
        else
            assertNull(grid1.context().encryption().groupKey(grpId));
    }

    checkData(grid0);
}