Search in sources :

Example 6 with GridCacheAffinityManager

use of org.apache.ignite.internal.processors.cache.GridCacheAffinityManager in project ignite by apache.

the class TxOptimisticOnPartitionExchangeTest method doTest.

/**
 * @param isolation {@link TransactionIsolation}.
 * @param txInitiatorPrimary False If the transaction does not use the keys of the node that initiated it.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
public void doTest(final TransactionIsolation isolation, boolean txInitiatorPrimary) throws Exception {
    final CountDownLatch txStarted = new CountDownLatch(1);
    final IgniteCache cache = ignite(0).cache(CACHE_NAME);
    final Map<Integer, Integer> txValues = new TreeMap<>();
    ClusterNode node = ignite(0).cluster().node();
    GridCacheAffinityManager affinity = ((IgniteCacheProxy) cache).context().affinity();
    for (int i = 0; txValues.size() < TX_SIZE; i++) {
        if (!txInitiatorPrimary && node.equals(affinity.primaryByKey(i, NONE)))
            continue;
        txValues.put(i, i);
    }
    TestCommunicationSpi.init();
    msgInterception = true;
    IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() {
            try (Transaction tx = ignite(0).transactions().txStart(OPTIMISTIC, isolation)) {
                info(">>> TX started.");
                txStarted.countDown();
                cache.putAll(txValues);
                tx.commit();
                info(">>> TX committed.");
            }
            return null;
        }
    });
    txStarted.await();
    try {
        info(">>> Grid starting.");
        IgniteEx ignite = startGrid(NODES_CNT);
        info(">>> Grid started.");
        fut.get();
        awaitPartitionMapExchange();
        msgInterception = false;
        IgniteCache<Object, Object> cacheStartedNode = ignite.cache(CACHE_NAME);
        try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            Set<Object> keys = cacheStartedNode.getAll(txValues.keySet()).keySet();
            assertEquals(txValues.keySet(), new TreeSet<>(keys));
            tx.commit();
        }
    } finally {
        msgInterception = false;
        stopGrid(NODES_CNT);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) TreeMap(java.util.TreeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheAffinityManager(org.apache.ignite.internal.processors.cache.GridCacheAffinityManager) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx)

Aggregations

GridCacheAffinityManager (org.apache.ignite.internal.processors.cache.GridCacheAffinityManager)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Cache (javax.cache.Cache)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2 Transaction (org.apache.ignite.transactions.Transaction)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeoutException (java.util.concurrent.TimeoutException)1 CacheException (javax.cache.CacheException)1 Ignite (org.apache.ignite.Ignite)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)1