@Override public boolean replaceLock(final String entryId, final long oldLockId, final LockType newLock) { return connector.retryTransaction( (handle, transactionStatus) -> { int numDeletedRows = removeLock(handle, oldLockId); if (numDeletedRows != 1) { transactionStatus.setRollbackOnly(); final String message = numDeletedRows == 0 ? StringUtils.format("Cannot find lock[%d]", oldLockId) : StringUtils.format("Found multiple locks for lockId[%d]", oldLockId); throw new RuntimeException(message); } return addLock(handle, entryId, newLock); }, 3, SQLMetadataConnector.DEFAULT_MAX_TRIES ); }
@Test public void testGetLockId() throws EntryExistsException { final String entryId = "ABC123"; Map<String, Integer> entry = ImmutableMap.of("a", 1); Map<String, Integer> status = ImmutableMap.of("count", 42); handler.insert(entryId, DateTimes.of("2014-01-01"), "test", entry, true, status); Assert.assertEquals( ImmutableMap.<Long, Map<String, Integer>>of(), handler.getLocks("non_exist_entry") ); Assert.assertEquals( ImmutableMap.<Long, Map<String, Integer>>of(), handler.getLocks(entryId) ); final ImmutableMap<String, Integer> lock1 = ImmutableMap.of("lock", 1); final ImmutableMap<String, Integer> lock2 = ImmutableMap.of("lock", 2); Assert.assertTrue(handler.addLock(entryId, lock1)); Assert.assertNotNull(handler.getLockId(entryId, lock1)); Assert.assertNull(handler.getLockId(entryId, lock2)); }
@Test public void testReplaceLock() throws EntryExistsException { final String entryId = "ABC123"; Map<String, Integer> entry = ImmutableMap.of("a", 1); Map<String, Integer> status = ImmutableMap.of("count", 42); handler.insert(entryId, DateTimes.of("2014-01-01"), "test", entry, true, status); Assert.assertEquals( ImmutableMap.<Long, Map<String, Integer>>of(), handler.getLocks("non_exist_entry") ); Assert.assertEquals( ImmutableMap.<Long, Map<String, Integer>>of(), handler.getLocks(entryId) ); final ImmutableMap<String, Integer> lock1 = ImmutableMap.of("lock", 1); final ImmutableMap<String, Integer> lock2 = ImmutableMap.of("lock", 2); Assert.assertTrue(handler.addLock(entryId, lock1)); final Long lockId1 = handler.getLockId(entryId, lock1); Assert.assertNotNull(lockId1); Assert.assertTrue(handler.replaceLock(entryId, lockId1, lock2)); }
@Override public boolean replaceLock(final String entryId, final long oldLockId, final LockType newLock) { return connector.retryTransaction( (handle, transactionStatus) -> { int numDeletedRows = removeLock(handle, oldLockId); if (numDeletedRows != 1) { transactionStatus.setRollbackOnly(); final String message = numDeletedRows == 0 ? StringUtils.format("Cannot find lock[%d]", oldLockId) : StringUtils.format("Found multiple locks for lockId[%d]", oldLockId); throw new RuntimeException(message); } return addLock(handle, entryId, newLock); }, 3, SQLMetadataConnector.DEFAULT_MAX_TRIES ); }