Java Code Examples for org.apache.ignite.IgniteCache#lock()

The following examples show how to use org.apache.ignite.IgniteCache#lock() . 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: GridCacheMultiNodeLockAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws IgniteCheckedException If test failed.
 */
@Test
public void testLockReentry() throws IgniteCheckedException {
    IgniteCache<Integer, String> cache = ignite1.cache(DEFAULT_CACHE_NAME);

    Lock lock = cache.lock(1);

    lock.lock();

    try {
        checkLocked(cache, 1);

        lock.lock();

        checkLocked(cache, 1);

        lock.unlock();

        checkLocked(cache, 1);
    }
    finally {
        lock.unlock();
    }

    checkUnlocked(cache, 1);
}
 
Example 2
Source File: GridCacheColocatedDebugTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testExplicitLocks() throws Exception {
    storeEnabled = false;

    startGrid();

    try {
        IgniteCache<Object, Object> cache = jcache();

        Lock lock = cache.lock(1);

        lock.lock();

        assertNull(cache.getAndPut(1, "key1"));
        assertEquals("key1", cache.getAndPut(1, "key2"));
        assertEquals("key2", cache.get(1));

        lock.unlock();
    }
    finally {
        stopAllGrids();
    }
}
 
Example 3
Source File: GridCacheMultiNodeLockAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testTwoCaches() throws Exception {
    IgniteCache<Integer, String> cache1 = ignite1.cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, String> cache2 = ignite1.cache(CACHE2);

    final Integer key = primaryKey(cache1);

    Lock lock = cache1.lock(key);

    lock.lock();

    try {
        assertTrue(cache1.isLocalLocked(key, true));
        assertTrue(cache1.isLocalLocked(key, false));

        assertFalse(cache2.isLocalLocked(key, true));
        assertFalse(cache2.isLocalLocked(key, false));
    }
    finally {
        lock.unlock();
    }
}
 
Example 4
Source File: AtomicOperationsInTxTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param isAtomicCacheAllowedInTx If true - atomic operation allowed.
 * Otherwise - it should throw exception.
 */
private void checkLock(boolean isAtomicCacheAllowedInTx) {
    IgniteCache<Integer, Integer> cache;
    Class<? extends Throwable> eCls;
    String eMsg;

    if (isAtomicCacheAllowedInTx) {
        cache = grid(0).cache(DEFAULT_CACHE_NAME).withAllowAtomicOpsInTx();
        eCls = CacheException.class;
        eMsg = "Explicit lock can't be acquired within a transaction.";
    } else {
        cache = grid(0).cache(DEFAULT_CACHE_NAME);
        eCls = IgniteException.class;
        eMsg = "Transaction spans operations on atomic cache";
    }

    Lock lock = cache.lock(1);

    GridTestUtils.assertThrows(log, (Callable<Void>)() -> {
        try (Transaction tx = grid(0).transactions().txStart()) {
            lock.lock();
        }

        return null;
    }, eCls, eMsg);
}
 
Example 5
Source File: GridCacheNearOneNodeSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testSingleLockPut() throws Exception {
    IgniteCache<Integer, String> near = jcache();

    Lock lock = near.lock(1);

    lock.lock();

    try {
        near.put(1, "1");
        near.put(2, "2");

        String one = near.getAndPut(1, "3");

        assertNotNull(one);
        assertEquals("1", one);
    }
    finally {
        lock.unlock();
    }
}
 
Example 6
Source File: GridCacheNearOneNodeSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testSingleLock() throws Exception {
    IgniteCache<Integer, String> near = jcache();

    Lock lock = near.lock(1);

    lock.lock();

    try {
        near.put(1, "1");

        assertEquals("1", near.localPeek(1, CachePeekMode.ONHEAP));
        assertEquals("1", dhtPeek(1));

        assertEquals("1", near.get(1));
        assertEquals("1", near.getAndRemove(1));

        assertNull(near.localPeek(1, CachePeekMode.ONHEAP));
        assertNull(dhtPeek(1));

        assertTrue(near.isLocalLocked(1, false));
        assertTrue(near.isLocalLocked(1, true));
    }
    finally {
        lock.unlock();
    }

    assertFalse(near.isLocalLocked(1, false));
    assertFalse(near.isLocalLocked(1, true));
}
 
Example 7
Source File: GridCacheEvictionLockUnlockSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** @throws Exception If failed. */
private void doTest() throws Exception {
    try {
        startGridsMultiThreaded(gridCnt);

        for (int i = 0; i < gridCnt; i++)
            grid(i).events().localListen(new EvictListener(), EVT_CACHE_ENTRY_EVICTED);

        for (int i = 0; i < gridCnt; i++) {
            reset();

            IgniteCache<Object, Object> cache = jcache(i);

            Lock lock = cache.lock("key");

            lock.lock();
            lock.unlock();

            assertTrue(evictLatch.await(3, SECONDS));

            assertEquals(gridCnt, evictCnt.get());
            assertEquals(gridCnt, touchCnt.get());

            for (int j = 0; j < gridCnt; j++)
                assertEquals(0, jcache(j).size());
        }
    }
    finally {
        stopAllGrids();
    }
}
 
Example 8
Source File: GridCacheNearMultiNodeSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param key Key.
 * @throws Exception If failed.
 */
private void checkSingleLockReentry(int key) throws Throwable {
    if (!transactional())
        return;

    IgniteCache<Integer, String> near = jcache(0);

    String val = Integer.toString(key);

    Lock lock = near.lock(key);

    lock.lock();

    try {
        near.put(key, val);

        assertEquals(val, near.localPeek(key));
        assertEquals(val, localPeek(dht(primaryGrid(key)), key));

        assertTrue(near.isLocalLocked(key, false));
        assertTrue(near.isLocalLocked(key, true));

        lock.lock(); // Reentry.

        try {
            assertEquals(val, near.get(key));
            assertEquals(val, near.getAndRemove(key));

            assertNull(near.localPeek(key));
            assertNull(localPeek(dht(primaryGrid(key)), key));

            assertTrue(near.isLocalLocked(key, false));
            assertTrue(near.isLocalLocked(key, true));
        }
        finally {
            lock.unlock();
        }

        assertTrue(near.isLocalLocked(key, false));
        assertTrue(near.isLocalLocked(key, true));
    }
    catch (Throwable t) {
        error("Test failed.", t);

        throw t;
    }
    finally {
        lock.unlock();
    }

    assertFalse(near(0).isLockedNearOnly(key));
    assertFalse(near.isLocalLocked(key, true));
}
 
Example 9
Source File: CacheGetInsideLockChangingTopologyTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param ignite Node.
 * @param cacheName Cache name.
 * @throws Exception If failed.
 */
private void getInsideLockStopPrimary(Ignite ignite, String cacheName) throws Exception {
    IgniteCache<Integer, Integer> lockCache = ignite.cache(TX_CACHE1);

    IgniteCache<Integer, Integer> getCache = ignite.cache(cacheName);

    final int NEW_NODE = SRVS + CLIENTS;

    Ignite srv = startGrid(NEW_NODE);

    awaitPartitionMapExchange();

    try {
        Integer key = primaryKey(srv.cache(cacheName));

        getCache.put(key, 1);

        IgniteInternalFuture<?> stopFut = GridTestUtils.runAsync(new Callable<Void>() {
            @Override public Void call() throws Exception {
                U.sleep(500);

                log.info("Stop node.");

                stopGrid(NEW_NODE);

                log.info("Node stopped.");

                return null;
            }
        }, "stop-thread");

        Lock lock = lockCache.lock(key + 1);

        lock.lock();

        try {
            while (!stopFut.isDone())
                assertEquals(1, (Object)getCache.get(key));
        }
        finally {
            lock.unlock();
        }

        stopFut.get();
    }
    finally {
        stopGrid(NEW_NODE);
    }
}
 
Example 10
Source File: GridCacheColocatedDebugTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testExplicitLocksDistributed() throws Exception {
    storeEnabled = false;

    startGridsMultiThreaded(3);

    Ignite g0 = grid(0);
    Ignite g1 = grid(1);
    Ignite g2 = grid(2);

    try {
        Integer k0 = forPrimary(g0);
        Integer k1 = forPrimary(g1);
        Integer k2 = forPrimary(g2);

        IgniteCache<Object, Object> cache = jcache(0);

        Lock lock0 = cache.lock(k0);
        Lock lock1 = cache.lock(k1);
        Lock lock2 = cache.lock(k2);

        lock0.lock();
        lock1.lock();
        lock2.lock();

        cache.put(k0, "val0");

        cache.putAll(F.asMap(k1, "val1", k2, "val2"));

        assertEquals("val0", cache.get(k0));
        assertEquals("val1", cache.get(k1));
        assertEquals("val2", cache.get(k2));

        lock0.unlock();
        lock1.unlock();
        lock2.unlock();
    }
    finally {
        stopAllGrids();
    }
}
 
Example 11
Source File: GridCacheNearOneNodeSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testSingleLockReentry() throws Exception {
    IgniteCache<Integer, String> near = jcache();

    Lock lock = near.lock(1);

    lock.lock();

    try {
        near.put(1, "1");

        assertEquals("1", near.localPeek(1, CachePeekMode.ONHEAP));
        assertEquals("1", dhtPeek(1));

        assertTrue(near.isLocalLocked(1, false));
        assertTrue(near.isLocalLocked(1, true));

        lock.lock(); // Reentry.

        try {
            assertEquals("1", near.get(1));
            assertEquals("1", near.getAndRemove(1));

            assertNull(near.localPeek(1, CachePeekMode.ONHEAP));
            assertNull(dhtPeek(1));

            assertTrue(near.isLocalLocked(1, false));
            assertTrue(near.isLocalLocked(1, true));
        }
        finally {
            lock.unlock();
        }

        assertTrue(near.isLocalLocked(1, false));
        assertTrue(near.isLocalLocked(1, true));
    }
    finally {
        lock.unlock();
    }

    assertFalse(near.isLocalLocked(1, false));
    assertFalse(near.isLocalLocked(1, true));
}
 
Example 12
Source File: GridCacheNearReadersSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testExplicitLockReaders() throws Exception {
    if (atomicityMode() == ATOMIC)
        return;

    grids = 3;
    aff.reset(grids, 1);

    startGrids();

    int key1 = 3;
    String val1 = Integer.toString(key1);

    assertEquals(grid(0).localNode(), F.first(aff.nodes(aff.partition(key1), grid(0).cluster().nodes())));

    int key2 = 1;
    String val2 = Integer.toString(key2);

    assertEquals(grid(1).localNode(), F.first(aff.nodes(aff.partition(key2), grid(1).cluster().nodes())));

    IgniteCache<Integer, String> cache = jcache(0);

    Lock lock1 = cache.lock(key1);

    lock1.lock();

    try {
        // Nested lock.
        Lock lock2 = cache.lock(key2);

        lock2.lock();

        try {
            assertNull(cache.getAndPut(key1, val1));

            assertEquals(val1, dhtPeek(0, key1));
            assertEquals(val1, dhtPeek(1, key1));
            assertNull(dhtPeek(2, key1));

            // Since near entry holds the lock, it should
            // contain correct value.
            assertEquals(val1, near(0).peekEx(key1).wrap().getValue());

            assertNull(near(1).peekEx(key1));
            assertNull(near(2).peekEx(key1));

            cache.put(key2, val2);

            assertNull(dhtPeek(0, key2));
            assertEquals(val2, dhtPeek(1, key2));
            assertEquals(val2, dhtPeek(2, key2));

            assertEquals(val2, near(0).peekEx(key2).wrap().getValue());
            assertNull(near(1).peekEx(key2));
            assertNull(near(2).peekEx(key2));

            String val22 = val2 + "2";

            cache.put(key2, val22);

            assertNull(dhtPeek(0, key2));
            assertEquals(val22, dhtPeek(1, key2));
            assertEquals(val22, dhtPeek(2, key2));

            assertEquals(val22, near(0).peekEx(key2).wrap().getValue());
            assertNull(near(1).peekEx(key2));
            assertNull(near(2).peekEx(key2));

            cache.remove(key2);

            assertNull(dhtPeek(0, key2));
            assertNull(dhtPeek(1, key2));
            assertNull(dhtPeek(2, key2));

            assertNull(dht(0).peekEx(key2));
            assertNotNull(dht(1).peekEx(key2));
            assertNotNull(dht(2).peekEx(key2));

            assertNotNull(near(0).peekEx(key2));
            assertNull(near(1).peekEx(key2));
            assertNull(near(2).peekEx(key2));
        }
        finally {
            lock2.unlock();
        }
    }
    finally {
        lock1.unlock();
    }
}
 
Example 13
Source File: CacheNoAffinityExchangeTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testNoAffinityChangeOnClientLeftWithMergedExchanges() throws Exception {
    System.setProperty(IgniteSystemProperties.IGNITE_EXCHANGE_MERGE_DELAY, "1000");

    try {
        Ignite ig = startGridsMultiThreaded(4);

        ig.cluster().active(true);

        IgniteCache<Integer, Integer> atomicCache = ig.createCache(new CacheConfiguration<Integer, Integer>()
            .setName("atomic").setAtomicityMode(CacheAtomicityMode.ATOMIC).setCacheMode(CacheMode.REPLICATED));

        IgniteCache<Integer, Integer> txCache = ig.createCache(new CacheConfiguration<Integer, Integer>()
            .setName("tx").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL).setCacheMode(CacheMode.REPLICATED));

        Ignite client = startClientGrid("client");

        stopGrid(1);
        stopGrid(2);
        stopGrid(3);

        awaitPartitionMapExchange();

        atomicCache.put(-1, -1);
        txCache.put(-1, -1);

        TestRecordingCommunicationSpi.spi(ig).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {
            @Override public boolean apply(ClusterNode node, Message msg) {
                return msg instanceof GridDhtPartitionSupplyMessageV2;
            }
        });

        startGridsMultiThreaded(1, 3);

        CountDownLatch latch = new CountDownLatch(1);
        for (Ignite ignite : G.allGrids()) {
            if (ignite.cluster().localNode().order() == 9) {
                TestDiscoverySpi discoSpi =
                    (TestDiscoverySpi)((IgniteEx)ignite).context().discovery().getInjectedDiscoverySpi();

                discoSpi.latch = latch;

                break;
            }
        }

        client.close();

        for (int k = 0; k < 100; k++) {
            atomicCache.put(k, k);
            txCache.put(k, k);

            Lock lock = txCache.lock(k);
            lock.lock();
            lock.unlock();
        }

        for (int k = 0; k < 100; k++) {
            assertEquals(Integer.valueOf(k), atomicCache.get(k));
            assertEquals(Integer.valueOf(k), txCache.get(k));
        }

        latch.countDown();
    }
    finally {
        System.clearProperty(IgniteSystemProperties.IGNITE_EXCHANGE_MERGE_DELAY);
    }
}
 
Example 14
Source File: CrossCacheLockTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testLockUnlock() throws Exception {
    for (int i = 0; i < GRID_CNT; i++) {
        Ignite ignite = ignite(i);

        log.info("Check node: " + ignite.name());

        IgniteCache<Integer, Integer> cache1 = ignite.cache(CACHE1);
        IgniteCache<Integer, Integer> cache2 = ignite.cache(CACHE2);

        for (int k = 0; k < 1000; k++) {
            Lock lock1 = null;
            Lock lock2 = null;

            try {
                lock1 = cache1.lock(k);

                assertTrue(lock1.tryLock());

                assertTrue(cache1.isLocalLocked(k, true));
                assertFalse(cache2.isLocalLocked(k, true));

                lock2 = cache2.lock(k);

                assertTrue(lock2.tryLock());

                assertTrue(cache1.isLocalLocked(k, true));
                assertTrue(cache2.isLocalLocked(k, true));

                lock2.unlock();

                lock2 = null;

                assertTrue(cache1.isLocalLocked(k, true));
                assertFalse(cache2.isLocalLocked(k, true));

                lock1.unlock();

                lock1 = null;

                assertFalse(cache1.isLocalLocked(k, true));
                assertFalse(cache2.isLocalLocked(k, true));
            }
            finally {
                if (lock1 != null)
                    lock1.unlock();

                if (lock2 != null)
                    lock2.unlock();
            }
        }
    }
}
 
Example 15
Source File: AsyncChannelTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Test that client channel works in async mode.
 */
@Test
public void testAsyncRequests() throws Exception {
    try (IgniteClient client = startClient(0)) {
        Ignite ignite = grid(0);

        IgniteCache<Integer, Integer> igniteCache = ignite.cache(CACHE_NAME);
        ClientCache<Integer, Integer> clientCache = client.cache(CACHE_NAME);

        clientCache.clear();

        Lock keyLock = igniteCache.lock(0);

        IgniteInternalFuture fut;

        keyLock.lock();

        try {
            CountDownLatch latch = new CountDownLatch(1);

            fut = GridTestUtils.runAsync(() -> {
                latch.countDown();

                // This request is blocked until we explicitly unlock key in another thread.
                clientCache.put(0, 0);

                clientCache.put(1, 1);

                assertEquals(10, clientCache.size(CachePeekMode.PRIMARY));
            });

            latch.await();

            for (int i = 2; i < 10; i++) {
                clientCache.put(i, i);

                assertEquals((Integer)i, igniteCache.get(i));
                assertEquals((Integer)i, clientCache.get(i));
            }

            // Parallel thread must be blocked on key 0.
            assertFalse(clientCache.containsKey(1));
        }
        finally {
            keyLock.unlock();
        }

        fut.get();

        assertTrue(clientCache.containsKey(1));
    }
}
 
Example 16
Source File: ClusterMetricsSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param client Client flag.
 * @throws Exception If failed.
 */
private void checkPmeMetricsOnNodeJoin(boolean client) throws Exception {
    IgniteEx ignite = startGrid(0);

    MetricRegistry reg = ignite.context().metric().registry(PME_METRICS);

    LongMetric currentPMEDuration = reg.findMetric(PME_DURATION);
    LongMetric currentBlockingPMEDuration = reg.findMetric(PME_OPS_BLOCKED_DURATION);

    HistogramMetricImpl durationHistogram = reg.findMetric(PME_DURATION_HISTOGRAM);
    HistogramMetricImpl blockindDurationHistogram = reg.findMetric(PME_OPS_BLOCKED_DURATION_HISTOGRAM);

    IgniteCache<Object, Object> cache = ignite.getOrCreateCache(
        new CacheConfiguration<>(DEFAULT_CACHE_NAME)
            .setAtomicityMode(TRANSACTIONAL));

    cache.put(1, 1);

    awaitPartitionMapExchange();

    int timeout = 5000;

    assertTrue(GridTestUtils.waitForCondition(() -> currentPMEDuration.value() == 0, timeout));
    assertEquals(0, currentBlockingPMEDuration.value());

    // There was two blocking exchange: server node start and cache start.
    assertEquals(2, Arrays.stream(durationHistogram.value()).sum());
    assertEquals(2, Arrays.stream(blockindDurationHistogram.value()).sum());

    Lock lock = cache.lock(1);

    lock.lock();

    TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(ignite);

    spi.blockMessages((node, message) -> message instanceof GridDhtPartitionsFullMessage);

    GridTestUtils.runAsync(() -> client ? startClientGrid("client") : startGrid(1));

    assertTrue(waitForCondition(() ->
        ignite.context().cache().context().exchange().lastTopologyFuture().initialVersion().topologyVersion() == 2,
        timeout));

    if (client)
        assertEquals(0, currentBlockingPMEDuration.value());
    else
        assertTrue(currentBlockingPMEDuration.value() > 0);

    lock.unlock();

    spi.waitForBlocked();
    spi.stopBlock();

    awaitPartitionMapExchange();

    assertTrue(GridTestUtils.waitForCondition(() -> currentPMEDuration.value() == 0, timeout));
    assertEquals(0, currentBlockingPMEDuration.value());

    if (client) {
        // There was non-blocking exchange: client node start.
        assertEquals(3, Arrays.stream(durationHistogram.value()).sum());
        assertEquals(2, Arrays.stream(blockindDurationHistogram.value()).sum());
    }
    else {
        // There was two blocking exchange: server node start and rebalance completing.
        assertEquals(4, Arrays.stream(durationHistogram.value()).sum());
        assertEquals(4, Arrays.stream(blockindDurationHistogram.value()).sum());
    }
}
 
Example 17
Source File: IgniteCacheLockBenchmark.java    From ignite with Apache License 2.0 3 votes vote down vote up
/** {@inheritDoc} */
@Override public void setUp(BenchmarkConfiguration cfg) throws Exception {
    super.setUp(cfg);

    String key = "key";

    IgniteCache<String, Integer> cache = cacheForOperation();

    cache.put(key, 0);

    lock = cache.lock(key);
}
 
Example 18
Source File: GridCacheBasicApiAbstractTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 *
 */
@Test
public void testInterruptLock() throws InterruptedException {
    MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.ENTRY_LOCK);

    final IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);

    final Lock lock = cache.lock(1);

    lock.lock();

    final AtomicBoolean isOk = new AtomicBoolean(false);

    Thread t = new Thread(new Runnable() {
        @Override public void run() {
            assertFalse(cache.isLocalLocked(1, true));

            lock.lock();

            try {
                assertTrue(cache.isLocalLocked(1, true));
            }
            finally {
                lock.unlock();
            }

            assertTrue(Thread.currentThread().isInterrupted());

            isOk.set(true);
        }
    });

    t.start();

    Thread.sleep(100);

    t.interrupt();

    lock.unlock();

    t.join();

    assertTrue(isOk.get());
}
 
Example 19
Source File: IgniteCacheClientNodeChangingTopologyTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testLockRemoveAfterClientFailed() throws Exception {
    ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);

    ccfg.setCacheMode(PARTITIONED);
    ccfg.setBackups(1);
    ccfg.setAtomicityMode(TRANSACTIONAL);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setRebalanceMode(SYNC);

    IgniteEx ignite0 = startGrid(0);
    IgniteEx ignite1 = startGrid(1);

    Ignite ignite2 = startClientGrid(2);

    assertTrue(ignite2.configuration().isClientMode());

    IgniteCache<Integer, Integer> cache2 = ignite2.cache(DEFAULT_CACHE_NAME);

    final Integer key = 0;

    Lock lock2 = cache2.lock(key);

    lock2.lock();

    ignite2.close();

    IgniteCache<Integer, Integer> cache0 = ignite0.cache(DEFAULT_CACHE_NAME);

    assertFalse(cache0.isLocalLocked(key, false));

    IgniteCache<Integer, Integer> cache1 = ignite1.cache(DEFAULT_CACHE_NAME);

    assertFalse(cache1.isLocalLocked(key, false));

    Lock lock1 = cache1.lock(0);

    assertTrue(lock1.tryLock(5000, TimeUnit.MILLISECONDS));

    lock1.unlock();

    ignite2 = startClientGrid(2);

    assertTrue(ignite2.configuration().isClientMode());

    cache2 = ignite2.cache(DEFAULT_CACHE_NAME);

    lock2 = cache2.lock(0);

    assertTrue(lock2.tryLock(5000, TimeUnit.MILLISECONDS));

    lock2.unlock();
}
 
Example 20
Source File: GridServiceProcessorStopSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testStopDuringHangedDeployment() throws Exception {
    final CountDownLatch depLatch = new CountDownLatch(1);

    final CountDownLatch finishLatch = new CountDownLatch(1);

    final IgniteEx node0 = startGrid(0);
    final IgniteEx node1 = startGrid(1);
    final IgniteEx node2 = startGrid(2);

    final IgniteCache<Object, Object> cache = node2.getOrCreateCache(new CacheConfiguration<Object, Object>("def")
        .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));

    node0.services().deployNodeSingleton("myService", new TestServiceImpl());

    // Guarantee lock owner will never left topology unexpectedly.
    final Integer lockKey = keyForNode(node2.affinity("def"), new AtomicInteger(1),
        node2.cluster().localNode());

    // Lock to hold topology version undone.
    final Lock lock = cache.lock(lockKey);

    // Try to change topology once service has deployed.
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {
        @Override public Void call() throws Exception {
            depLatch.await();

            node1.close();

            return null;
        }
    }, "top-change-thread");

    // Stop node on unstable topology.
    GridTestUtils.runAsync(new Callable<Void>() {
        @Override public Void call() throws Exception {
            depLatch.await();

            Thread.sleep(1000);

            node0.close();

            finishLatch.countDown();

            return null;
        }
    }, "stopping-node-thread");

    assertNotNull(node0.services().service("myService"));

    // Freeze topology changing
    lock.lock();

    depLatch.countDown();

    boolean wait = finishLatch.await(15, TimeUnit.SECONDS);

    if (!wait)
        U.dumpThreads(log);

    assertTrue("Deploy future isn't completed", wait);

    fut.get();

    Ignition.stopAll(true);
}