Search in sources :

Example 1 with Lease

use of com.palantir.lock.v2.Lease in project atlasdb by palantir.

the class MultiClientConjureTimelockResourceTest method createAsyncTimeLockServiceForClient.

private AsyncTimelockService createAsyncTimeLockServiceForClient(String client) {
    AsyncTimelockService timelockService = mock(AsyncTimelockService.class);
    LeadershipId leadershipId = namespaceToLeaderMap.computeIfAbsent(client, _u -> LeadershipId.random());
    LeaderTime leaderTime = LeaderTime.of(leadershipId, NanoTime.createForTests(1L));
    when(timelockService.leaderTime()).thenReturn(Futures.immediateFuture(leaderTime));
    when(timelockService.startTransactionsWithWatches(any())).thenReturn(Futures.immediateFuture(ConjureStartTransactionsResponse.builder().immutableTimestamp(lockImmutableTimestampResponse).lease(Lease.of(leaderTime, Duration.ofSeconds(977))).timestamps(partitionedTimestamps).lockWatchUpdate(lockWatchStateUpdate).build()));
    when(timelockService.getCommitTimestamps(anyInt(), any())).thenReturn(Futures.immediateFuture(getCommitTimestampResponse(client)));
    return timelockService;
}
Also used : LeaderTime(com.palantir.lock.v2.LeaderTime) AsyncTimelockService(com.palantir.atlasdb.timelock.AsyncTimelockService) LeadershipId(com.palantir.lock.v2.LeadershipId)

Example 2 with Lease

use of com.palantir.lock.v2.Lease in project atlasdb by palantir.

the class LeasedLockTokenTest method ignoreRefreshIfNewLeaseGoesBackInTime.

@Test
public void ignoreRefreshIfNewLeaseGoesBackInTime() {
    Lease oldLease = getLease();
    advance(Duration.ofNanos(123));
    LeasedLockToken token = LeasedLockToken.of(LOCK_TOKEN, getLease());
    assertExpiresExactlyAfter(token, LEASE_TIMEOUT);
    token.updateLease(oldLease);
    assertExpiresExactlyAfter(token, LEASE_TIMEOUT);
}
Also used : Lease(com.palantir.lock.v2.Lease) Test(org.junit.Test)

Example 3 with Lease

use of com.palantir.lock.v2.Lease in project atlasdb by palantir.

the class LeasedLockTokenTest method throwIfRefreshedWithDifferentLeaderId.

@Test
public void throwIfRefreshedWithDifferentLeaderId() {
    LeasedLockToken token = LeasedLockToken.of(LOCK_TOKEN, getLease());
    Lease otherLease = Lease.of(LeaderTime.of(OTHER_LEADER_ID, currentTime), LEASE_TIMEOUT);
    assertThatThrownBy(() -> token.updateLease(otherLease)).hasMessageStartingWith("Lock leases can only be refreshed by lease owners.");
}
Also used : Lease(com.palantir.lock.v2.Lease) Test(org.junit.Test)

Example 4 with Lease

use of com.palantir.lock.v2.Lease in project atlasdb by palantir.

the class LockLeaseServiceTest method startTransactionsResponseHasCorrectLeasedLock.

@Test
public void startTransactionsResponseHasCorrectLeasedLock() {
    Lease lease = getLease();
    when(timelock.startTransactions(any())).thenReturn(startTransactionsResponseWith(LOCK_TOKEN, lease));
    StartTransactionResponseV4 clientResponse = lockLeaseService.startTransactions(2);
    verify(timelock).startTransactions(any());
    LeasedLockToken leasedLock = (LeasedLockToken) clientResponse.immutableTimestamp().getLock();
    assertThat(leasedLock.serverToken()).isEqualTo(LOCK_TOKEN);
    assertThat(leasedLock.getLease()).isEqualTo(lease);
}
Also used : StartTransactionResponseV4(com.palantir.lock.v2.StartTransactionResponseV4) Lease(com.palantir.lock.v2.Lease) Test(org.junit.Test)

Example 5 with Lease

use of com.palantir.lock.v2.Lease in project atlasdb by palantir.

the class LockLeaseService method startTransactions.

StartTransactionResponseV4 startTransactions(int batchSize) {
    ConjureStartTransactionsRequest request = ConjureStartTransactionsRequest.builder().requestorId(clientId).requestId(UUID.randomUUID()).numTransactions(batchSize).lastKnownVersion(Optional.empty()).build();
    ConjureStartTransactionsResponse conjureResponse = delegate.startTransactions(request);
    StartTransactionResponseV4 response = StartTransactionResponseV4.of(conjureResponse.getImmutableTimestamp(), conjureResponse.getTimestamps(), conjureResponse.getLease());
    Lease lease = response.lease();
    LeasedLockToken leasedLockToken = LeasedLockToken.of(ConjureLockToken.of(response.immutableTimestamp().getLock().getRequestId()), lease);
    long immutableTs = response.immutableTimestamp().getImmutableTimestamp();
    return StartTransactionResponseV4.of(LockImmutableTimestampResponse.of(immutableTs, leasedLockToken), response.timestamps(), lease);
}
Also used : StartTransactionResponseV4(com.palantir.lock.v2.StartTransactionResponseV4) ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) Lease(com.palantir.lock.v2.Lease) ConjureStartTransactionsRequest(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsRequest)

Aggregations

Lease (com.palantir.lock.v2.Lease)12 Test (org.junit.Test)9 StartTransactionResponseV4 (com.palantir.lock.v2.StartTransactionResponseV4)4 ConjureStartTransactionsRequest (com.palantir.atlasdb.timelock.api.ConjureStartTransactionsRequest)3 LockResponse (com.palantir.lock.v2.LockResponse)3 Lease (io.fabric8.kubernetes.api.model.coordination.v1.Lease)3 ConjureStartTransactionsResponse (com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse)2 LeaderTime (com.palantir.lock.v2.LeaderTime)2 LockImmutableTimestampResponse (com.palantir.lock.v2.LockImmutableTimestampResponse)2 LockToken (com.palantir.lock.v2.LockToken)2 WaitForLocksRequest (com.palantir.lock.v2.WaitForLocksRequest)2 Optional (java.util.Optional)2 Set (java.util.Set)2 UUID (java.util.UUID)2 Test (org.junit.jupiter.api.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Sets (com.google.common.collect.Sets)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1