private Thread createThread(final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock, final Collection<String> toLock) { final Thread thread; thread = new Thread(() -> { LockState<String> locked = mutuallyExclusiveSetLock.lockOnObjects(toLock); try { //stuff } finally { mutuallyExclusiveSetLock.unlock(locked); } }); return thread; } }
@Test public void testDoubleLock() throws Exception { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("whatup", "dog")); try { mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("anything")); } catch(Exception e) { return; //expected } finally { mutuallyExclusiveSetLock.unlock(lockOnObjects); } Assert.fail(); //should have thrown }
@Test public void testSimpleNotBlock() throws Exception { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("whatup", "dog")); final Thread thread; try { thread = createThread(mutuallyExclusiveSetLock, Arrays.asList("heyo")); thread.setDaemon(true); thread.start(); thread.join(10 * 1000); } finally { mutuallyExclusiveSetLock.unlock(lockOnObjects); } }
@Test public void testSimpleBlock() throws Exception { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("whatup", "dog")); final Thread thread; try { thread = createThread(mutuallyExclusiveSetLock, Arrays.asList("whatup")); thread.setDaemon(true); thread.start(); Thread.sleep(100); Assert.assertTrue(thread.isAlive()); // assertEquals(2, mutuallyExclusiveSetLock.syncMap.size()); } finally { mutuallyExclusiveSetLock.unlock(lockOnObjects); } thread.join(10 * 1000); // assertFalse(mutuallyExclusiveSetLock.syncMap.get("dog").isLocked()); // assertEquals(0, mutuallyExclusiveSetLock.threadSet.size()); }
@Test public void testInterface() throws Exception { MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("whatev", "dog")); try { //stuff } finally { // assertEquals(2, mutuallyExclusiveSetLock.syncMap.size()); // assertEquals(1, mutuallyExclusiveSetLock.threadSet.size()); mutuallyExclusiveSetLock.unlock(lockOnObjects); // assertFalse(mutuallyExclusiveSetLock.syncMap.get("whatev").isHeldByCurrentThread()); // assertEquals(0, mutuallyExclusiveSetLock.threadSet.size()); } }
@Test public void testThreadOwnsLocks() { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); List<String> asList = Arrays.asList("whatup", "dog"); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(asList); try { Assert.assertTrue(mutuallyExclusiveSetLock.isLocked(asList)); } finally { mutuallyExclusiveSetLock.unlock(lockOnObjects); } }
LockState<String> locked = setLock.lockOnObjects(toLock); try { while (!unlock) {
@Override public void putUnlessExists(TableReference tableRef, Map<Cell, byte[]> values) throws KeyAlreadyExistsException { LockState<Cell> locks = lockSet.lockOnObjects(values.keySet()); try (Disposer d = new Disposer(); ColumnFamily table = columnFamilies.get(tableRef.getQualifiedName())) { Set<Cell> alreadyExists = Sets.newHashSetWithExpectedSize(0); WriteOptions options = d.register(new WriteOptions().setSync(writeOptions.fsyncCommit())); WriteBatch batch = d.register(new WriteBatch()); RocksIterator iter = d.register(getDb().newIterator(table.getHandle())); for (Entry<Cell, byte[]> entry : values.entrySet()) { byte[] key = RocksDbKeyValueServices.getKey(entry.getKey(), PUT_UNLESS_EXISTS_TS); if (RocksDbKeyValueServices.keyExists(iter, key)) { alreadyExists.add(entry.getKey()); } else { batch.put(table.getHandle(), key, entry.getValue()); } } getDb().write(options, batch); if (!alreadyExists.isEmpty()) { throw new KeyAlreadyExistsException("key already exists", alreadyExists); } } catch (RocksDBException e) { throw Throwables.propagate(e); } finally { locks.unlock(); } }