use of com.palantir.lock.LockRefreshToken in project atlasdb by palantir.
the class PaxosTimeLockServerIntegrationTest method lockAndUnlockAndCountExceptions.
private int lockAndUnlockAndCountExceptions(List<LockService> lockServices, int numRequestsPerClient) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(lockServices.size() * numRequestsPerClient);
Map<LockService, LockRefreshToken> tokenMap = new HashMap<>();
for (LockService service : lockServices) {
LockRefreshToken token = service.lock(CLIENT_1, REQUEST_LOCK_WITH_LONG_TIMEOUT);
assertNotNull(token);
tokenMap.put(service, token);
}
List<Future<LockRefreshToken>> futures = Lists.newArrayList();
for (LockService lockService : lockServices) {
for (int i = 0; i < numRequestsPerClient; i++) {
int currentTrial = i;
futures.add(executorService.submit(() -> lockService.lock(CLIENT_2 + String.valueOf(currentTrial), REQUEST_LOCK_WITH_LONG_TIMEOUT)));
}
}
executorService.shutdown();
executorService.awaitTermination(2, TimeUnit.SECONDS);
AtomicInteger exceptionCounter = new AtomicInteger(0);
futures.forEach(future -> {
try {
assertNull(future.get());
} catch (ExecutionException e) {
Throwable cause = e.getCause();
assertThat(cause.getClass().getName()).contains("RetryableException");
assertRemoteExceptionWithStatus(cause.getCause(), HttpStatus.TOO_MANY_REQUESTS_429);
exceptionCounter.getAndIncrement();
} catch (InterruptedException e) {
throw Throwables.propagate(e);
}
});
tokenMap.forEach((service, token) -> assertTrue(service.unlock(token)));
return exceptionCounter.get();
}
use of com.palantir.lock.LockRefreshToken in project atlasdb by palantir.
the class PaxosTimeLockServerIntegrationTest method lockServiceShouldAllowUsToTakeOutLocks.
@Test
public void lockServiceShouldAllowUsToTakeOutLocks() throws InterruptedException {
LockService lockService = getLockService(CLIENT_1);
LockRefreshToken token = lockService.lock(LOCK_CLIENT_NAME, com.palantir.lock.LockRequest.builder(LOCK_MAP).doNotBlock().build());
assertThat(token).isNotNull();
lockService.unlock(token);
}
use of com.palantir.lock.LockRefreshToken in project atlasdb by palantir.
the class TestTimestampCommand method runAndVerifyCli.
private void runAndVerifyCli(Verifier verifier) throws Exception {
try (SingleBackendCliTestRunner runner = makeRunner(cliArgs.toArray(new String[0]))) {
TestAtlasDbServices services = runner.connect(moduleFactory);
LockService lockService = services.getLockService();
TimestampService tss = services.getTimestampService();
LockClient client = services.getTestLockClient();
Clock clock = GlobalClock.create(lockService);
long prePunch = clock.getTimeMillis();
punch(services, tss, clock);
long postPunch = clock.getTimeMillis();
long immutableTs = tss.getFreshTimestamp();
LockRequest request = LockRequest.builder(ImmutableSortedMap.of(lock, LockMode.WRITE)).withLockedInVersionId(immutableTs).doNotBlock().build();
LockRefreshToken token = lockService.lock(client.getClientId(), request);
long lastFreshTs = tss.getFreshTimestamps(1000).getUpperBound();
verifier.verify(runner, tss, immutableTs, prePunch, postPunch, lastFreshTs, true);
lockService.unlock(token);
lastFreshTs = tss.getFreshTimestamps(1000).getUpperBound();
// there are no locks so we now expect immutable to just be a fresh
runner.freshCommand();
verifier.verify(runner, tss, immutableTs, prePunch, postPunch, lastFreshTs, false);
}
}
use of com.palantir.lock.LockRefreshToken in project atlasdb by palantir.
the class LockRemotingTest method testLock.
@Test
public void testLock() throws InterruptedException, IOException {
ObjectMapper mapper = new ObjectMapper();
LockDescriptor desc = StringLockDescriptor.of("1234");
String writeValueAsString = mapper.writeValueAsString(desc);
LockDescriptor desc2 = mapper.readValue(writeValueAsString, LockDescriptor.class);
long minVersion = 123;
LockRequest request = LockRequest.builder(ImmutableSortedMap.of(desc, LockMode.WRITE)).doNotBlock().withLockedInVersionId(minVersion).build();
writeValueAsString = mapper.writeValueAsString(request);
LockRequest request2 = mapper.readValue(writeValueAsString, LockRequest.class);
LockRefreshToken lockResponse = rawLock.lock(LockClient.ANONYMOUS.getClientId(), request);
rawLock.unlock(lockResponse);
writeValueAsString = mapper.writeValueAsString(lockResponse);
LockRefreshToken lockResponse2 = mapper.readValue(writeValueAsString, LockRefreshToken.class);
LockService lock = AtlasDbFeignTargetFactory.createProxy(Optional.empty(), lockService.baseUri().toString(), LockService.class, UserAgents.DEFAULT_USER_AGENT);
String lockClient = "23234";
LockRefreshToken token = lock.lock(lockClient, request);
long minLockedInVersionId = lock.getMinLockedInVersionId(lockClient);
Assert.assertEquals(minVersion, minLockedInVersionId);
lock.unlock(token);
token = lock.lock(LockClient.ANONYMOUS.getClientId(), request);
Set<LockRefreshToken> refreshed = lock.refreshLockRefreshTokens(ImmutableList.of(token));
Assert.assertEquals(1, refreshed.size());
lock.unlock(token);
try {
lock.logCurrentState();
} finally {
LockServiceTestUtils.cleanUpLogStateDir();
}
lock.currentTimeMillis();
HeldLocksToken token1 = lock.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), request);
HeldLocksToken token2 = lock.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), request2);
Assert.assertNull(token2);
lock.unlock(token1.getLockRefreshToken());
token2 = lock.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), request2);
Assert.assertNotNull(token2);
lock.unlock(token2.getLockRefreshToken());
}
use of com.palantir.lock.LockRefreshToken in project atlasdb by palantir.
the class LegacyTimelockServiceTest method lockImmutableTimestampLocksFreshTimestamp.
@Test
public void lockImmutableTimestampLocksFreshTimestamp() throws InterruptedException {
long immutableTs = 3L;
LockRefreshToken expectedToken = mockImmutableTsLockResponse();
mockMinLockedInVersionIdResponse(immutableTs);
LockImmutableTimestampResponse expectedResponse = LockImmutableTimestampResponse.of(immutableTs, toTokenV2(expectedToken));
assertEquals(expectedResponse, timelock.lockImmutableTimestamp(LockImmutableTimestampRequest.create()));
}
Aggregations