Search in sources :

Example 1 with LockRequest

use of com.palantir.lock.LockRequest 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);
    }
}
Also used : LockService(com.palantir.lock.LockService) LockClient(com.palantir.lock.LockClient) SingleBackendCliTestRunner(com.palantir.atlasdb.cli.runner.SingleBackendCliTestRunner) DaggerTestAtlasDbServices(com.palantir.atlasdb.services.test.DaggerTestAtlasDbServices) TestAtlasDbServices(com.palantir.atlasdb.services.test.TestAtlasDbServices) GlobalClock(com.palantir.atlasdb.cleaner.GlobalClock) Clock(com.palantir.common.time.Clock) LockRequest(com.palantir.lock.LockRequest) TimestampService(com.palantir.timestamp.TimestampService) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 2 with LockRequest

use of com.palantir.lock.LockRequest 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());
}
Also used : StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) LockService(com.palantir.lock.LockService) HeldLocksToken(com.palantir.lock.HeldLocksToken) LockRequest(com.palantir.lock.LockRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LockRefreshToken(com.palantir.lock.LockRefreshToken) Test(org.junit.Test)

Example 3 with LockRequest

use of com.palantir.lock.LockRequest in project atlasdb by palantir.

the class ClientSplitLockService method lock.

private LockRefreshToken lock(LockClient client, LockRequest request) throws InterruptedException {
    if (request.getBlockingMode() == BlockingMode.DO_NOT_BLOCK) {
        if (client == LockClient.ANONYMOUS) {
            return nonBlockingClient.lock(LockClient.ANONYMOUS.getClientId(), request);
        } else {
            return nonBlockingClient.lock(client.getClientId(), request);
        }
    }
    // Let's try sending this request as a non-blocking request.
    if ((request.getLockGroupBehavior() == LockGroupBehavior.LOCK_ALL_OR_NONE) && (request.getBlockingMode() != BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE)) {
        LockRequest.Builder newRequest = LockRequest.builder(request.getLockDescriptors());
        newRequest.doNotBlock();
        newRequest.timeoutAfter(request.getLockTimeout());
        if (request.getVersionId() != null) {
            newRequest.withLockedInVersionId(request.getVersionId());
        }
        final LockRefreshToken response;
        if (client == LockClient.ANONYMOUS) {
            response = nonBlockingClient.lock(LockClient.ANONYMOUS.getClientId(), request);
        } else {
            response = nonBlockingClient.lock(client.getClientId(), request);
        }
        if (response != null) {
            return response;
        }
    }
    // No choice but to send it as a blocking request.
    if (client == LockClient.ANONYMOUS) {
        return blockingClient.lock(LockClient.ANONYMOUS.getClientId(), request);
    } else {
        return blockingClient.lock(client.getClientId(), request);
    }
}
Also used : LockRequest(com.palantir.lock.LockRequest) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 4 with LockRequest

use of com.palantir.lock.LockRequest in project atlasdb by palantir.

the class TransactionManagersTest method setsGlobalDefaultLockTimeout.

@Test
public void setsGlobalDefaultLockTimeout() {
    TimeDuration expectedTimeout = SimpleTimeDuration.of(47, TimeUnit.SECONDS);
    AtlasDbConfig atlasDbConfig = ImmutableAtlasDbConfig.builder().keyValueService(new InMemoryAtlasDbConfig()).defaultLockTimeoutSeconds((int) expectedTimeout.getTime()).build();
    TransactionManagers.builder().config(atlasDbConfig).userAgent("test").globalMetricsRegistry(new MetricRegistry()).globalTaggedMetricRegistry(DefaultTaggedMetricRegistry.getDefault()).registrar(environment).build().serializable();
    assertEquals(expectedTimeout, LockRequest.getDefaultLockTimeout());
    LockRequest lockRequest = LockRequest.builder(ImmutableSortedMap.of(StringLockDescriptor.of("foo"), LockMode.WRITE)).build();
    assertEquals(expectedTimeout, lockRequest.getLockTimeout());
}
Also used : DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) MetricRegistry(com.codahale.metrics.MetricRegistry) SimpleTimeDuration(com.palantir.lock.SimpleTimeDuration) TimeDuration(com.palantir.lock.TimeDuration) ImmutableAtlasDbConfig(com.palantir.atlasdb.config.ImmutableAtlasDbConfig) InMemoryAtlasDbConfig(com.palantir.atlasdb.memory.InMemoryAtlasDbConfig) AtlasDbConfig(com.palantir.atlasdb.config.AtlasDbConfig) InMemoryAtlasDbConfig(com.palantir.atlasdb.memory.InMemoryAtlasDbConfig) LockRequest(com.palantir.lock.LockRequest) Test(org.junit.Test)

Example 5 with LockRequest

use of com.palantir.lock.LockRequest in project atlasdb by palantir.

the class SerializableErrorDecoderTest method resilientToValidJsonBodyThatIsNotASerializableError.

@Test
public void resilientToValidJsonBodyThatIsNotASerializableError() throws JsonProcessingException {
    LockRequest lockRequest = LockRequest.builder(ImmutableSortedMap.of(StringLockDescriptor.of(LOCK_ID), LockMode.WRITE)).build();
    Response response = createResponseForEntity(lockRequest);
    assertCanDecodeRuntimeException(response);
}
Also used : Response(feign.Response) LockRequest(com.palantir.lock.LockRequest) Test(org.junit.Test)

Aggregations

LockRequest (com.palantir.lock.LockRequest)10 Test (org.junit.Test)5 LockDescriptor (com.palantir.lock.LockDescriptor)4 LockRefreshToken (com.palantir.lock.LockRefreshToken)4 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)3 HeldLocksToken (com.palantir.lock.HeldLocksToken)2 LockClient (com.palantir.lock.LockClient)2 LockService (com.palantir.lock.LockService)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GlobalClock (com.palantir.atlasdb.cleaner.GlobalClock)1 SingleBackendCliTestRunner (com.palantir.atlasdb.cli.runner.SingleBackendCliTestRunner)1 AtlasDbConfig (com.palantir.atlasdb.config.AtlasDbConfig)1 ImmutableAtlasDbConfig (com.palantir.atlasdb.config.ImmutableAtlasDbConfig)1 Cell (com.palantir.atlasdb.keyvalue.api.Cell)1 InMemoryAtlasDbConfig (com.palantir.atlasdb.memory.InMemoryAtlasDbConfig)1 DaggerTestAtlasDbServices (com.palantir.atlasdb.services.test.DaggerTestAtlasDbServices)1 TestAtlasDbServices (com.palantir.atlasdb.services.test.TestAtlasDbServices)1 Clock (com.palantir.common.time.Clock)1 AtlasRowLockDescriptor (com.palantir.lock.AtlasRowLockDescriptor)1