Search in sources :

Example 6 with GridNearLockRequest

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockRequest in project ignite by apache.

the class IgniteCacheClientNodeChangingTopologyTest method pessimisticTx.

/**
     * @param nearCfg Near cache configuration.
     * @throws Exception If failed.
     */
private void pessimisticTx(NearCacheConfiguration nearCfg) throws Exception {
    ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setCacheMode(PARTITIONED);
    ccfg.setBackups(1);
    ccfg.setAtomicityMode(TRANSACTIONAL);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setRebalanceMode(SYNC);
    ccfg.setNearConfiguration(nearCfg);
    IgniteEx ignite0 = startGrid(0);
    IgniteEx ignite1 = startGrid(1);
    awaitPartitionMapExchange();
    client = true;
    final Ignite ignite2 = startGrid(2);
    assertTrue(ignite2.configuration().isClientMode());
    final Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < 100; i++) map.put(i, i);
    TestCommunicationSpi spi = (TestCommunicationSpi) ignite2.configuration().getCommunicationSpi();
    spi.blockMessages(GridNearLockRequest.class, ignite0.localNode().id());
    spi.blockMessages(GridNearLockRequest.class, ignite1.localNode().id());
    spi.record(GridNearLockRequest.class);
    final IgniteCache<Integer, Integer> cache = ignite2.cache(DEFAULT_CACHE_NAME);
    IgniteInternalFuture<?> putFut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Thread.currentThread().setName("put-thread");
            try (Transaction tx = ignite2.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.putAll(map);
                tx.commit();
            }
            return null;
        }
    });
    assertFalse(putFut.isDone());
    client = false;
    IgniteEx ignite3 = startGrid(3);
    log.info("Stop block1.");
    spi.stopBlock();
    putFut.get();
    spi.record(null);
    checkData(map, null, cache, 4);
    List<Object> msgs = spi.recordedMessages();
    assertTrue(((GridNearLockRequest) msgs.get(0)).firstClientRequest());
    assertTrue(((GridNearLockRequest) msgs.get(1)).firstClientRequest());
    for (int i = 2; i < msgs.size(); i++) assertFalse(((GridNearLockRequest) msgs.get(i)).firstClientRequest());
    ignite3.close();
    for (int i = 0; i < 100; i++) map.put(i, i + 1);
    spi.blockMessages(GridNearLockRequest.class, ignite0.localNode().id());
    spi.blockMessages(GridNearLockRequest.class, ignite1.localNode().id());
    putFut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Thread.currentThread().setName("put-thread");
            try (Transaction tx = ignite2.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                for (Map.Entry<Integer, Integer> e : map.entrySet()) cache.put(e.getKey(), e.getValue());
                tx.commit();
            }
            return null;
        }
    });
    startGrid(3);
    log.info("Stop block2.");
    spi.stopBlock();
    putFut.get();
    checkData(map, null, cache, 4);
    for (int i = 0; i < 100; i++) map.put(i, i + 2);
    try (Transaction tx = ignite2.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        cache.putAll(map);
        tx.commit();
    }
    checkData(map, null, cache, 4);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GridNearLockRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockRequest) TimeoutException(java.util.concurrent.TimeoutException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) CacheException(javax.cache.CacheException) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

GridNearLockRequest (org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockRequest)6 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)3 Transaction (org.apache.ignite.transactions.Transaction)3 LinkedHashMap (java.util.LinkedHashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Ignite (org.apache.ignite.Ignite)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)2 GridNearLockMapping (org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockMapping)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 UUID (java.util.UUID)1 Callable (java.util.concurrent.Callable)1 TimeoutException (java.util.concurrent.TimeoutException)1 CacheException (javax.cache.CacheException)1