Search in sources :

Example 1 with LockService

use of com.palantir.lock.LockService 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();
}
Also used : LockService(com.palantir.lock.LockService) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 2 with LockService

use of com.palantir.lock.LockService 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);
}
Also used : LockService(com.palantir.lock.LockService) LockRefreshToken(com.palantir.lock.LockRefreshToken) Test(org.junit.Test)

Example 3 with LockService

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

the class TransactionManagerTest method shouldNotMakeRemoteCallsInAReadonlyTransactionIfNoWorkIsDone.

@Test
public void shouldNotMakeRemoteCallsInAReadonlyTransactionIfNoWorkIsDone() {
    TimestampService mockTimestampService = mock(TimestampService.class);
    LockService mockLockService = mock(LockService.class);
    TransactionManager txnManagerWithMocks = SerializableTransactionManager.createForTest(getKeyValueService(), mockTimestampService, LockClient.of("foo"), mockLockService, transactionService, () -> AtlasDbConstraintCheckingMode.FULL_CONSTRAINT_CHECKING_THROWS_EXCEPTIONS, conflictDetectionManager, sweepStrategyManager, NoOpCleaner.INSTANCE, AbstractTransactionTest.GET_RANGES_THREAD_POOL_SIZE, AbstractTransactionTest.DEFAULT_GET_RANGES_CONCURRENCY, () -> AtlasDbConstants.DEFAULT_TIMESTAMP_CACHE_SIZE, MultiTableSweepQueueWriter.NO_OP);
    // fetch an immutable timestamp once so it's cached
    when(mockTimestampService.getFreshTimestamp()).thenReturn(1L);
    when(mockLockService.getMinLockedInVersionId("foo")).thenReturn(1L);
    txnManagerWithMocks.getImmutableTimestamp();
    verify(mockTimestampService).getFreshTimestamp();
    verify(mockLockService).getMinLockedInVersionId("foo");
    // now execute a read transaction
    txnManagerWithMocks.runTaskReadOnly(txn -> null);
    verifyNoMoreInteractions(mockLockService);
    verifyNoMoreInteractions(mockTimestampService);
}
Also used : LockService(com.palantir.lock.LockService) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) TimestampService(com.palantir.timestamp.TimestampService) Test(org.junit.Test)

Example 4 with LockService

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

the class TransactionManagerTest method shouldNotConflictIfImmutableTimestampLockExpiresIfNoReadsOrWrites.

@Test
public void shouldNotConflictIfImmutableTimestampLockExpiresIfNoReadsOrWrites() {
    TimelockService timelock = mock(TimelockService.class);
    LockService mockLockService = mock(LockService.class);
    TransactionManager txnManagerWithMocks = new SerializableTransactionManager(keyValueService, timelock, mockLockService, transactionService, () -> AtlasDbConstraintCheckingMode.FULL_CONSTRAINT_CHECKING_THROWS_EXCEPTIONS, conflictDetectionManager, sweepStrategyManager, NoOpCleaner.INSTANCE, TimestampTrackerImpl.createNoOpTracker(), () -> AtlasDbConstants.DEFAULT_TIMESTAMP_CACHE_SIZE, false, () -> AtlasDbConstants.DEFAULT_TRANSACTION_LOCK_ACQUIRE_TIMEOUT_MS, AbstractTransactionTest.GET_RANGES_THREAD_POOL_SIZE, AbstractTransactionTest.DEFAULT_GET_RANGES_CONCURRENCY, MultiTableSweepQueueWriter.NO_OP);
    when(timelock.getFreshTimestamp()).thenReturn(1L);
    when(timelock.lockImmutableTimestamp(any())).thenReturn(LockImmutableTimestampResponse.of(2L, LockToken.of(UUID.randomUUID())));
    txnManagerWithMocks.runTaskThrowOnConflict(txn -> null);
}
Also used : LockService(com.palantir.lock.LockService) TimelockService(com.palantir.lock.v2.TimelockService) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) Test(org.junit.Test)

Example 5 with LockService

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

the class TransactionManagerTest method shouldConflictIfImmutableTimestampLockExpiresEvenIfNoWritesOnThoroughSweptTable.

@Test
public void shouldConflictIfImmutableTimestampLockExpiresEvenIfNoWritesOnThoroughSweptTable() {
    TimelockService timelock = mock(TimelockService.class);
    LockService mockLockService = mock(LockService.class);
    TransactionManager txnManagerWithMocks = new SerializableTransactionManager(keyValueService, timelock, mockLockService, transactionService, () -> AtlasDbConstraintCheckingMode.FULL_CONSTRAINT_CHECKING_THROWS_EXCEPTIONS, conflictDetectionManager, sweepStrategyManager, NoOpCleaner.INSTANCE, TimestampTrackerImpl.createNoOpTracker(), () -> AtlasDbConstants.DEFAULT_TIMESTAMP_CACHE_SIZE, false, () -> AtlasDbConstants.DEFAULT_TRANSACTION_LOCK_ACQUIRE_TIMEOUT_MS, AbstractTransactionTest.GET_RANGES_THREAD_POOL_SIZE, AbstractTransactionTest.DEFAULT_GET_RANGES_CONCURRENCY, MultiTableSweepQueueWriter.NO_OP);
    when(timelock.getFreshTimestamp()).thenReturn(1L);
    when(timelock.lockImmutableTimestamp(any())).thenReturn(LockImmutableTimestampResponse.of(2L, LockToken.of(UUID.randomUUID())));
    assertThatThrownBy(() -> txnManagerWithMocks.runTaskThrowOnConflict(txn -> {
        get(txn, TEST_TABLE_THOROUGH, "row1", "col1");
        return null;
    })).isInstanceOf(TransactionFailedRetriableException.class);
}
Also used : LockService(com.palantir.lock.LockService) TimelockService(com.palantir.lock.v2.TimelockService) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) Test(org.junit.Test)

Aggregations

LockService (com.palantir.lock.LockService)22 Test (org.junit.Test)11 LockRefreshToken (com.palantir.lock.LockRefreshToken)7 LegacyTimelockService (com.palantir.lock.impl.LegacyTimelockService)7 TimestampService (com.palantir.timestamp.TimestampService)6 KvsBackedPersistentLockService (com.palantir.atlasdb.persistentlock.KvsBackedPersistentLockService)5 NoOpPersistentLockService (com.palantir.atlasdb.persistentlock.NoOpPersistentLockService)5 PersistentLockService (com.palantir.atlasdb.persistentlock.PersistentLockService)5 LockRefreshingLockService (com.palantir.lock.client.LockRefreshingLockService)5 TimelockService (com.palantir.lock.v2.TimelockService)5 LockClient (com.palantir.lock.LockClient)4 LockRequest (com.palantir.lock.LockRequest)4 KeyValueService (com.palantir.atlasdb.keyvalue.api.KeyValueService)3 TransactionManager (com.palantir.atlasdb.transaction.api.TransactionManager)3 Cleaner (com.palantir.atlasdb.cleaner.Cleaner)2 NoOpCleaner (com.palantir.atlasdb.cleaner.NoOpCleaner)2 Cell (com.palantir.atlasdb.keyvalue.api.Cell)2 ForwardingKeyValueService (com.palantir.atlasdb.keyvalue.impl.ForwardingKeyValueService)2 TrackingKeyValueService (com.palantir.atlasdb.keyvalue.impl.TrackingKeyValueService)2 ManagedTimestampService (com.palantir.atlasdb.timelock.paxos.ManagedTimestampService)2