/** {@inheritDoc} */ @Override public Lock lock(String path) { return cache.lock(path); } }
@Override public Object call() throws Exception { return jcache().lock("1").tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS); } }, CacheException.class, "Locks are not supported");
/** {@inheritDoc} */ @Override public Object call() throws Exception { Lock lock = cache.lock(1); assert lock.tryLock(1000L, TimeUnit.MILLISECONDS); info("Locked key from thread: " + thread()); Thread.sleep(50); info("Unlocking key from thread: " + thread()); lock.unlock(); info("Unlocked key from thread: " + thread()); return null; } }, 10, "basic-lock-thread");
@Override public Void call() throws Exception { Lock lock = ignite0.cache(DEFAULT_CACHE_NAME).lock(key); lock.lock(); return null; } }, "lock-thread1");
@Override public Object call() throws Exception { try (Transaction tx = ignite(0).transactions().txStart()) { jcache(0).lock("key").lock(); } return null; } },
@Nullable @Override public Object call() throws Exception { info("Waiting for latch1..."); l1.await(); Lock lock = cache2.lock(kv); lock.lock(); try { String v = cache2.get(kv); assert v != null : "Value is null for key: " + kv; assertEquals(Integer.toString(kv), v); } finally { lock.unlock(); info("Unlocked key in thread 2: " + kv); } assert !cache2.isLocalLocked(kv, true); return null; } });
@Override public void run() { Lock lock = c.lock(CNTR_KEY); try { lock.lock(); int cntr = c.get(CNTR_KEY); info("*** Cntr in lock thread: " + cntr); c.put(CNTR_KEY, --cntr); } catch (Exception e) { error("Failed lock thread", e); } finally { lock.unlock(); } } }));
@Override public Object call() throws Exception { IgniteCache<String, Integer> cache = jcache(0); try (Transaction tx = ignite(0).transactions().txStart()) { cache.lock("key").lock(); } return null; } },
@Override public Void call() throws Exception { Lock lock = ignite1.cache(DEFAULT_CACHE_NAME).lock(key); log.info("Start lock."); lock.lock(); log.info("Locked."); return null; } }, "lock-thread2");
/** * Stops TensorFlow cluster. * * @param clusterId TensorFlow cluster identifier. */ public void stopClusterIfExists(UUID clusterId) { Lock clusterMgrCacheLock = cache.lock(clusterId); clusterMgrCacheLock.lock(); try { TensorFlowCluster cluster = cache.get(clusterId); if (cluster != null) { stopChief(clusterId); stopUserScript(clusterId); srvProcMgr.stop(cluster.getProcesses(), true); clusterRslvr.releasePorts(cluster.getSpec()); cache.remove(clusterId); } } finally { clusterMgrCacheLock.unlock(); } }
/** * * @throws Exception If test failed. */ @Test public void testBasicLock() throws Exception { IgniteCache<Integer, String> cache = ignite1.cache(DEFAULT_CACHE_NAME); Lock lock = cache.lock(1); lock.lock(); assert cache.isLocalLocked(1, false); assert cache.isLocalLocked(1, true); lock.unlock(); checkUnlocked(cache, 1); }
/** * * @throws Exception If test failed. */ @Test public void testBasicLock() throws Exception { MvccFeatureChecker.failIfNotSupported(MvccFeatureChecker.Feature.ENTRY_LOCK); IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME); Lock lock = cache.lock(1); assert lock.tryLock(); assert cache.isLocalLocked(1, false); lock.unlock(); assert !cache.isLocalLocked(1, false); }
/** * @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); }
/** * @throws IgniteCheckedException If test failed. */ @Test public void testSingleLockReentry() throws IgniteCheckedException { MvccFeatureChecker.failIfNotSupported(MvccFeatureChecker.Feature.ENTRY_LOCK); IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME); Lock lock = cache.lock(1); lock.lock(); try { assert cache.isLocalLocked(1, true); lock.lock(); lock.unlock(); assert cache.isLocalLocked(1, true); } finally { lock.unlock(); } assert !cache.isLocalLocked(1, true); assert !cache.isLocalLocked(1, false); }
/** * @throws Exception If failed. */ @Test public void testLockCache() throws Exception { if (atomicityMode() != TRANSACTIONAL) return; MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.ENTRY_LOCK); final Ignite ignite = grid(0); final String key = "key"; Lock lock = ignite.cache(DEFAULT_CACHE_NAME).lock(key); lock.lock(); GridTestUtils.assertThrows(log, new Callable<Object>() { @Override public Object call() throws Exception { ignite.createCache("NEW_CACHE"); return null; } }, IgniteException.class, EXPECTED_MSG); lock.unlock(); } }
/** @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(); } }
/** * @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(); } }
/** * @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(); } }