Search in sources :

Example 6 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class IgniteCacheGroupsTest method testCacheIdSort.

/**
 * @throws Exception If failed.
 */
@Test
public void testCacheIdSort() throws Exception {
    Ignite node = startGrid(0);
    final List<IgniteCache> caches = new ArrayList<>(3);
    caches.add(node.createCache(cacheConfiguration(GROUP1, "c1", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
    caches.add(node.createCache(cacheConfiguration(GROUP1, "c2", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
    caches.add(node.createCache(cacheConfiguration(GROUP1, "c3", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
    Affinity aff = node.affinity("c1");
    final List<Integer> keys = new ArrayList<>();
    for (int i = 0; i < 1_000_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() >= 10_000)
                break;
        }
    }
    assertEquals(10_000, keys.size());
    final long stopTime = System.currentTimeMillis() + 10_000;
    GridTestUtils.runMultiThreaded(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (System.currentTimeMillis() < stopTime) {
                for (int i = 0; i < 100; i++) {
                    IgniteCache cache = caches.get(rnd.nextInt(3));
                    Integer key = keys.get(rnd.nextInt(10_000));
                    if (rnd.nextFloat() > 0.8f)
                        cache.remove(key);
                    else
                        cache.put(key, key);
                }
            }
            return null;
        }
    }, 5, "update-thread");
    CacheGroupContext grp = cacheGroup(node, GROUP1);
    Integer cacheId = null;
    GridIterator<CacheDataRow> it = grp.offheap().partitionIterator(0);
    int c = 0;
    while (it.hasNext()) {
        CacheDataRow row = it.next();
        if (cacheId == null || cacheId != row.cacheId()) {
            cacheId = row.cacheId();
            c++;
        }
    }
    assertEquals(3, c);
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheExistsException(org.apache.ignite.cache.CacheExistsException) CacheException(javax.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Affinity(org.apache.ignite.cache.affinity.Affinity) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 7 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class IgniteCachePeekModesAbstractTest method swapKeys.

/**
 * @param nodeIdx Node index.
 * @return Tuple with primary and backup keys.
 */
private T2<List<Integer>, List<Integer>> swapKeys(int nodeIdx) {
    // TODO: GG-11148.
    // SwapSpaceSpi swap = ignite(nodeIdx).configuration().getSwapSpaceSpi();
    // 
    // IgniteSpiCloseableIterator<KeyCacheObject> it = swap.keyIterator(SPACE_NAME, null);
    IgniteSpiCloseableIterator<KeyCacheObject> it = new GridEmptyCloseableIterator<>();
    assertNotNull(it);
    Affinity aff = ignite(nodeIdx).affinity(DEFAULT_CACHE_NAME);
    ClusterNode node = ignite(nodeIdx).cluster().localNode();
    List<Integer> primary = new ArrayList<>();
    List<Integer> backups = new ArrayList<>();
    CacheObjectContext coctx = ((IgniteEx) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME).context().cacheObjectContext();
    while (it.hasNext()) {
        Integer key = it.next().value(coctx, false);
        if (aff.isPrimary(node, key))
            primary.add(key);
        else {
            assertTrue(aff.isBackup(node, key));
            backups.add(key);
        }
    }
    return new T2<>(primary, backups);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteEx(org.apache.ignite.internal.IgniteEx) ArrayList(java.util.ArrayList) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) Affinity(org.apache.ignite.cache.affinity.Affinity) T2(org.apache.ignite.internal.util.typedef.T2)

Example 8 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class GridCacheInterceptorAbstractSelfTest method nearKey.

/**
 * @param idx Grid index.
 * @return Key which does not belong to the grid.
 */
private String nearKey(int idx) {
    Affinity aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
    String key = null;
    for (int i = 0; i < 10_000; i++) {
        if (!aff.isPrimaryOrBackup(grid(idx).localNode(), String.valueOf(i))) {
            key = String.valueOf(i);
            break;
        }
    }
    assertNotNull(key);
    return key;
}
Also used : Affinity(org.apache.ignite.cache.affinity.Affinity)

Example 9 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class GridCacheInterceptorAbstractSelfTest method backupKey.

/**
 * @param idx Grid index.
 * @return Primary key for grid.
 */
private String backupKey(int idx) {
    Affinity aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
    String key = null;
    for (int i = 0; i < 10_000; i++) {
        if (aff.isBackup(grid(idx).localNode(), String.valueOf(i))) {
            key = String.valueOf(i);
            break;
        }
    }
    assertNotNull(key);
    return key;
}
Also used : Affinity(org.apache.ignite.cache.affinity.Affinity)

Example 10 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class CacheMvccSqlTxQueriesAbstractTest method testUpdateExplicitPartitionsWithReducer.

/**
 * @throws Exception If failed.
 */
@Test
public void testUpdateExplicitPartitionsWithReducer() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, 10).setIndexedTypes(Integer.class, Integer.class);
    Ignite ignite = startGridsMultiThreaded(4);
    awaitPartitionMapExchange();
    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
    Affinity<Object> affinity = internalCache0(cache).affinity();
    int keysCnt = 10, retryCnt = 0;
    Integer test = 0;
    Map<Integer, Integer> vals = new LinkedHashMap<>();
    while (vals.size() < keysCnt) {
        int partition = affinity.partition(test);
        if (partition == 1 || partition == 2)
            vals.put(test, 0);
        else
            assertTrue("Maximum retry number exceeded", ++retryCnt < 1000);
        test++;
    }
    cache.putAll(vals);
    SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE Integer set _val=(SELECT 2 FROM DUAL)").setPartitions(1, 2);
    List<List<?>> all = cache.query(qry).getAll();
    assertEquals(Long.valueOf(keysCnt), all.stream().findFirst().orElseThrow(AssertionError::new).get(0));
    List<List<?>> rows = cache.query(new SqlFieldsQuery("SELECT _val FROM Integer")).getAll();
    assertEquals(keysCnt, rows.size());
    assertTrue(rows.stream().map(r -> r.get(0)).map(Integer.class::cast).allMatch(v -> v == 2));
}
Also used : SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Transaction(org.apache.ignite.transactions.Transaction) Random(java.util.Random) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) EntryProcessor(javax.cache.processor.EntryProcessor) GridCursor(org.apache.ignite.internal.util.lang.GridCursor) GridTestUtils.runAsync(org.apache.ignite.testframework.GridTestUtils.runAsync) MutableEntry(javax.cache.processor.MutableEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Cache(javax.cache.Cache) CyclicBarrier(java.util.concurrent.CyclicBarrier) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TransactionConfiguration(org.apache.ignite.configuration.TransactionConfiguration) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) CacheDataRowAdapter(org.apache.ignite.internal.processors.cache.persistence.CacheDataRowAdapter) IgniteCache(org.apache.ignite.IgniteCache) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) Serializable(java.io.Serializable) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) SQL_SUM(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL_SUM) Objects(java.util.Objects) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) MvccFeatureChecker(org.apache.ignite.testframework.MvccFeatureChecker) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) DML(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.WriteMode.DML) GridTestUtils.runMultiThreaded(org.apache.ignite.testframework.GridTestUtils.runMultiThreaded) Affinity(org.apache.ignite.cache.affinity.Affinity) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) EntryProcessorException(javax.cache.processor.EntryProcessorException) SQL(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL) LinkedHashMap(java.util.LinkedHashMap) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) S(org.apache.ignite.internal.util.typedef.internal.S) CacheException(javax.cache.CacheException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) TransactionDuplicateKeyException(org.apache.ignite.transactions.TransactionDuplicateKeyException) F(org.apache.ignite.internal.util.typedef.F) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) Iterator(java.util.Iterator) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) IgniteQueryErrorCode(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) TimeUnit(java.util.concurrent.TimeUnit) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Phaser(java.util.concurrent.Phaser) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) LinkedHashMap(java.util.LinkedHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) List(java.util.List) Test(org.junit.Test)

Aggregations

Affinity (org.apache.ignite.cache.affinity.Affinity)49 Ignite (org.apache.ignite.Ignite)30 IgniteCache (org.apache.ignite.IgniteCache)29 ClusterNode (org.apache.ignite.cluster.ClusterNode)28 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)18 Collection (java.util.Collection)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Transaction (org.apache.ignite.transactions.Transaction)16 List (java.util.List)15 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)14 Map (java.util.Map)13 Cache (javax.cache.Cache)13 HashMap (java.util.HashMap)10 IgniteEx (org.apache.ignite.internal.IgniteEx)10 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)10 Collections (java.util.Collections)8 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)8 Ignition (org.apache.ignite.Ignition)8 ScanQuery (org.apache.ignite.cache.query.ScanQuery)8