use of com.palantir.lock.LockRequest in project atlasdb by palantir.
the class SynchronousLockClient method lock.
@Override
public LockRefreshToken lock(String client, String lockName) throws InterruptedException {
LockDescriptor descriptor = StringLockDescriptor.of(lockName);
LockRequest request = LockRequest.builder(ImmutableSortedMap.of(descriptor, LockMode.WRITE)).doNotBlock().build();
return lockService.lock(client, request);
}
use of com.palantir.lock.LockRequest in project atlasdb by palantir.
the class SynchronousLockClientTest method lockRequestIsWrite.
@Test
public void lockRequestIsWrite() throws InterruptedException {
when(LOCK_CLIENT.lock(CLIENT, LOCK_NAME)).thenAnswer((invocation) -> {
LockRequest request = (LockRequest) invocation.getArguments()[1];
assertThat(request.getLocks(), contains(hasProperty("lockMode", is(LockMode.WRITE))));
return null;
});
LOCK_CLIENT.lock(CLIENT, LOCK_NAME);
}
use of com.palantir.lock.LockRequest in project atlasdb by palantir.
the class SynchronousLockClientTest method lockRequestIsNonBlocking.
@Test
public void lockRequestIsNonBlocking() throws InterruptedException {
when(LOCK_CLIENT.lock(CLIENT, LOCK_NAME)).thenAnswer((invocation) -> {
LockRequest request = (LockRequest) invocation.getArguments()[1];
assertThat(request.getBlockingMode(), is(BlockingMode.DO_NOT_BLOCK));
return null;
});
LOCK_CLIENT.lock(CLIENT, LOCK_NAME);
}
use of com.palantir.lock.LockRequest in project atlasdb by palantir.
the class AssertLockedKeyValueService method put.
@Override
public void put(TableReference tableRef, Map<Cell, byte[]> values, long timestamp) {
if (tableRef.equals(TransactionConstants.TRANSACTION_TABLE)) {
SortedMap<LockDescriptor, LockMode> mapToAssertLockHeld = Maps.newTreeMap();
SortedMap<LockDescriptor, LockMode> mapToAssertLockNotHeld = Maps.newTreeMap();
for (Map.Entry<Cell, byte[]> e : values.entrySet()) {
LockDescriptor descriptor = AtlasRowLockDescriptor.of(tableRef.getQualifiedName(), e.getKey().getRowName());
if (Arrays.equals(e.getValue(), TransactionConstants.getValueForTimestamp(TransactionConstants.FAILED_COMMIT_TS))) {
mapToAssertLockNotHeld.put(descriptor, LockMode.READ);
} else {
mapToAssertLockHeld.put(descriptor, LockMode.READ);
}
}
try {
if (!mapToAssertLockHeld.isEmpty()) {
LockRequest request = LockRequest.builder(mapToAssertLockHeld).doNotBlock().lockAsManyAsPossible().build();
LockRefreshToken lock = lockService.lock(LockClient.ANONYMOUS.getClientId(), request);
Validate.isTrue(lock == null, "these should already be held");
}
if (!mapToAssertLockNotHeld.isEmpty()) {
LockRequest request = LockRequest.builder(mapToAssertLockNotHeld).doNotBlock().build();
LockRefreshToken lock = lockService.lock(LockClient.ANONYMOUS.getClientId(), request);
Validate.isTrue(lock != null, "these should already be waited for");
}
} catch (InterruptedException e) {
throw Throwables.throwUncheckedException(e);
}
}
super.put(tableRef, values, timestamp);
}
use of com.palantir.lock.LockRequest in project atlasdb by palantir.
the class LockServiceStateLoggerTest method setUp.
@BeforeClass
public static void setUp() throws Exception {
LockServiceTestUtils.cleanUpLogStateDir();
LockClient clientA = LockClient.of("Client A");
LockClient clientB = LockClient.of("Client B");
LockDescriptor descriptor1 = StringLockDescriptor.of("logger-lock");
LockDescriptor descriptor2 = StringLockDescriptor.of("logger-AAA");
LockRequest request1 = LockRequest.builder(LockCollections.of(ImmutableSortedMap.of(descriptor1, LockMode.WRITE))).blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)).build();
LockRequest request2 = LockRequest.builder(LockCollections.of(ImmutableSortedMap.of(descriptor2, LockMode.WRITE))).blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)).build();
outstandingLockRequestMultimap.put(clientA, request1);
outstandingLockRequestMultimap.put(clientB, request2);
outstandingLockRequestMultimap.put(clientA, request2);
HeldLocksToken token = LockServiceTestUtils.getFakeHeldLocksToken("client A", "Fake thread", new BigInteger("1"), "held-lock-1", "logger-lock");
HeldLocksToken token2 = LockServiceTestUtils.getFakeHeldLocksToken("client B", "Fake thread 2", new BigInteger("2"), "held-lock-2", "held-lock-3");
heldLocksTokenMap.putIfAbsent(token, LockServiceImpl.HeldLocks.of(token, LockCollections.of()));
heldLocksTokenMap.putIfAbsent(token2, LockServiceImpl.HeldLocks.of(token2, LockCollections.of()));
LockServiceStateLogger logger = new LockServiceStateLogger(heldLocksTokenMap, outstandingLockRequestMultimap, LockServiceTestUtils.TEST_LOG_STATE_DIR);
logger.logLocks();
}
Aggregations