Search in sources :

Example 1 with ConjureStartTransactionsResponse

use of com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse in project atlasdb by palantir.

the class SnapshotTransactionTest method startTransactionWithWatches.

private ConjureStartTransactionsResponse startTransactionWithWatches() {
    ConjureStartTransactionsResponse conjureResponse = inMemoryTimeLockRule.get().getLockLeaseService().startTransactionsWithWatches(Optional.empty(), 1);
    Set<Long> startTimestamps = conjureResponse.getTimestamps().stream().boxed().collect(Collectors.toSet());
    inMemoryTimeLockRule.getLockWatchManager().getCache().processStartTransactionsUpdate(startTimestamps, conjureResponse.getLockWatchUpdate());
    return conjureResponse;
}
Also used : ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) MutableLong(org.apache.commons.lang3.mutable.MutableLong) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong)

Example 2 with ConjureStartTransactionsResponse

use of com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse in project atlasdb by palantir.

the class SnapshotTransactionTest method getSortedColumnsThrowsIfLockIsLostMidway.

@Test
public void getSortedColumnsThrowsIfLockIsLostMidway() {
    List<byte[]> rows = LongStream.range(0, 10).mapToObj(PtBytes::toBytes).collect(Collectors.toList());
    List<Cell> cells = rows.stream().map(row -> Cell.create(row, COL_A)).collect(Collectors.toList());
    putCellsInTable(cells, TABLE_SWEPT_THOROUGH);
    ConjureStartTransactionsResponse conjureResponse = startTransactionWithWatches();
    LockImmutableTimestampResponse res = conjureResponse.getImmutableTimestamp();
    long transactionTs = conjureResponse.getTimestamps().start();
    Transaction transaction = getSnapshotTransactionWith(timelockService, () -> transactionTs, res, PreCommitConditions.NO_OP, true);
    int batchHint = 5;
    Iterator<Map.Entry<Cell, byte[]>> sortedColumns = transaction.getSortedColumns(TABLE_SWEPT_THOROUGH, rows, BatchColumnRangeSelection.create(PtBytes.EMPTY_BYTE_ARRAY, PtBytes.EMPTY_BYTE_ARRAY, batchHint));
    assertThat(sortedColumns.next().getKey()).isEqualTo(cells.get(0));
    // lock lost after getting first batch
    timelockService.unlock(ImmutableSet.of(res.getLock()));
    // should still be able to get all but last element of the elements for the first batch;
    // next batch is preemptively fetched when last element of curr batch is retrieved
    List<Cell> retrievedEntries = IntStream.range(1, batchHint - 1).mapToObj(_unused -> sortedColumns.next().getKey()).collect(Collectors.toList());
    assertThat(retrievedEntries).hasSameElementsAs(cells.subList(1, batchHint - 1));
    // should throw while fetching the next batch
    assertThatThrownBy(sortedColumns::next).isInstanceOf(TransactionLockTimeoutException.class);
}
Also used : Arrays(java.util.Arrays) AutoDelegate_KeyValueService(com.palantir.atlasdb.keyvalue.api.AutoDelegate_KeyValueService) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Throwables(com.palantir.common.base.Throwables) LockImmutableTimestampResponse(com.palantir.lock.v2.LockImmutableTimestampResponse) Mockito.doThrow(org.mockito.Mockito.doThrow) Future(java.util.concurrent.Future) Pair(org.apache.commons.lang3.tuple.Pair) ImmutableGetRangesQuery(com.palantir.atlasdb.transaction.api.ImmutableGetRangesQuery) MutableLong(org.apache.commons.lang3.mutable.MutableLong) Duration(java.time.Duration) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) BigInteger(java.math.BigInteger) Mockito.doReturn(org.mockito.Mockito.doReturn) AtlasFutures(com.palantir.atlasdb.futures.AtlasFutures) ToplistDeltaFilteringTableLevelMetricsController(com.palantir.atlasdb.transaction.impl.metrics.ToplistDeltaFilteringTableLevelMetricsController) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Set(java.util.Set) LockMode(com.palantir.lock.LockMode) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Assertions.fail(org.assertj.core.api.Assertions.fail) Stream(java.util.stream.Stream) Transaction(com.palantir.atlasdb.transaction.api.Transaction) DefaultMetricsFilterEvaluationContext(com.palantir.atlasdb.transaction.impl.metrics.DefaultMetricsFilterEvaluationContext) TransactionLockTimeoutNonRetriableException(com.palantir.atlasdb.transaction.api.TransactionLockTimeoutNonRetriableException) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Awaitility(org.awaitility.Awaitility) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) NoOpCleaner(com.palantir.atlasdb.cleaner.NoOpCleaner) Mockito.mock(org.mockito.Mockito.mock) Joiner(com.google.common.base.Joiner) ColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.ColumnRangeSelection) Iterables(com.google.common.collect.Iterables) ConflictHandler(com.palantir.atlasdb.transaction.api.ConflictHandler) Expectations(org.jmock.Expectations) TransactionFailedRetriableException(com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException) RunWith(org.junit.runner.RunWith) Synchroniser(org.jmock.lib.concurrent.Synchroniser) Callable(java.util.concurrent.Callable) SimpleTimeDuration(com.palantir.lock.SimpleTimeDuration) Mockito.spy(org.mockito.Mockito.spy) Supplier(java.util.function.Supplier) AtlasDbTestCase(com.palantir.atlasdb.AtlasDbTestCase) PtBytes(com.palantir.atlasdb.encoding.PtBytes) LockAwareTransactionTask(com.palantir.atlasdb.transaction.api.LockAwareTransactionTask) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AbortingVisitors(com.palantir.common.base.AbortingVisitors) StreamSupport(java.util.stream.StreamSupport) LockRequest(com.palantir.lock.LockRequest) BatchingVisitables(com.palantir.common.base.BatchingVisitables) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Before(org.junit.Before) NoOpLockWatchManager(com.palantir.atlasdb.keyvalue.api.watch.NoOpLockWatchManager) TableLevelMetricsController(com.palantir.atlasdb.transaction.impl.metrics.TableLevelMetricsController) LockCollections(com.palantir.lock.LockCollections) LongStream(java.util.stream.LongStream) Mockito.times(org.mockito.Mockito.times) AtlasRowLockDescriptor(com.palantir.lock.AtlasRowLockDescriptor) Test(org.junit.Test) TimelockService(com.palantir.lock.v2.TimelockService) Mockery(org.jmock.Mockery) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) ExecutionException(java.util.concurrent.ExecutionException) TimeDuration(com.palantir.lock.TimeDuration) Mockito.never(org.mockito.Mockito.never) Futures(com.google.common.util.concurrent.Futures) LockService(com.palantir.lock.LockService) LockClient(com.palantir.lock.LockClient) BatchingVisitableView(com.palantir.common.base.BatchingVisitableView) MultiTableSweepQueueWriter(com.palantir.atlasdb.sweep.queue.MultiTableSweepQueueWriter) ConflictTracer(com.palantir.atlasdb.debug.ConflictTracer) TransactionLockTimeoutException(com.palantir.atlasdb.transaction.api.TransactionLockTimeoutException) MutableInt(org.apache.commons.lang3.mutable.MutableInt) BatchColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BiFunction(java.util.function.BiFunction) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) Random(java.util.Random) SettableFuture(com.google.common.util.concurrent.SettableFuture) BatchingVisitable(com.palantir.common.base.BatchingVisitable) ByteBuffer(java.nio.ByteBuffer) CompletionService(java.util.concurrent.CompletionService) ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) TransactionConfig(com.palantir.atlasdb.transaction.TransactionConfig) PTExecutors(com.palantir.common.concurrent.PTExecutors) Assertions(org.assertj.core.api.Assertions) Parameterized(org.junit.runners.Parameterized) LockDescriptor(com.palantir.lock.LockDescriptor) TableMetadata(com.palantir.atlasdb.table.description.TableMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TransactionReadSentinelBehavior(com.palantir.atlasdb.transaction.api.TransactionReadSentinelBehavior) KeyedStream(com.palantir.common.streams.KeyedStream) Collection(java.util.Collection) Streams(com.google.common.collect.Streams) SweepStrategy(com.palantir.atlasdb.protos.generated.TableMetadataPersistence.SweepStrategy) Collectors(java.util.stream.Collectors) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) List(java.util.List) MultiDelegateProxy(com.palantir.common.proxy.MultiDelegateProxy) ForwardingKeyValueService(com.palantir.atlasdb.keyvalue.impl.ForwardingKeyValueService) Optional(java.util.Optional) SortedMap(java.util.SortedMap) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) HeldLocksToken(com.palantir.lock.HeldLocksToken) Iterators(com.google.common.collect.Iterators) TransactionCommitFailedException(com.palantir.atlasdb.transaction.api.TransactionCommitFailedException) AtlasDbConstraintCheckingMode(com.palantir.atlasdb.transaction.api.AtlasDbConstraintCheckingMode) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) Suppliers(com.google.common.base.Suppliers) TransactionOutcomeMetrics(com.palantir.atlasdb.transaction.impl.metrics.TransactionOutcomeMetrics) ImmutableTransactionConfig(com.palantir.atlasdb.transaction.ImmutableTransactionConfig) ExecutorService(java.util.concurrent.ExecutorService) EncodingUtils(com.palantir.atlasdb.ptobject.EncodingUtils) InOrder(org.mockito.InOrder) Value(com.palantir.atlasdb.keyvalue.api.Value) PreCommitCondition(com.palantir.atlasdb.transaction.api.PreCommitCondition) Iterator(java.util.Iterator) TimestampCache(com.palantir.atlasdb.cache.TimestampCache) Mockito.when(org.mockito.Mockito.when) Maps(com.google.common.collect.Maps) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Ordering(com.google.common.collect.Ordering) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) TransactionFailedNonRetriableException(com.palantir.atlasdb.transaction.api.TransactionFailedNonRetriableException) DefaultTimestampCache(com.palantir.atlasdb.cache.DefaultTimestampCache) Comparator(java.util.Comparator) AbortingVisitor(com.palantir.common.base.AbortingVisitor) Collections(java.util.Collections) TransactionOutcomeMetricsAssert(com.palantir.atlasdb.transaction.impl.metrics.TransactionOutcomeMetricsAssert) ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) Transaction(com.palantir.atlasdb.transaction.api.Transaction) LockImmutableTimestampResponse(com.palantir.lock.v2.LockImmutableTimestampResponse) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 3 with ConjureStartTransactionsResponse

use of com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse in project atlasdb by palantir.

the class SnapshotTransactionTest method commitDoesNotThrowIfAlreadySuccessfullyCommitted.

@Test
public void commitDoesNotThrowIfAlreadySuccessfullyCommitted() {
    final Cell cell = Cell.create(PtBytes.toBytes("row1"), PtBytes.toBytes("column1"));
    TimelockService spiedTimeLockService = spy(timelockService);
    ConjureStartTransactionsResponse conjureResponse = startTransactionWithWatches();
    LockImmutableTimestampResponse res = conjureResponse.getImmutableTimestamp();
    long transactionTs = conjureResponse.getTimestamps().start();
    Transaction snapshot = getSnapshotTransactionWith(spiedTimeLockService, () -> transactionTs, res, PreCommitConditions.NO_OP);
    when(spiedTimeLockService.getFreshTimestamp()).thenReturn(transactionTs + 1);
    doReturn(transactionTs + 1).when(spiedTimeLockService).getCommitTimestamp(anyLong(), any());
    // forcing to try to commit a transaction that is already committed
    transactionService.putUnlessExists(transactionTs, spiedTimeLockService.getFreshTimestamp());
    snapshot.put(TABLE, ImmutableMap.of(cell, PtBytes.toBytes("value")));
    snapshot.commit();
    spiedTimeLockService.unlock(Collections.singleton(res.getLock()));
}
Also used : ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) Transaction(com.palantir.atlasdb.transaction.api.Transaction) LockImmutableTimestampResponse(com.palantir.lock.v2.LockImmutableTimestampResponse) TimelockService(com.palantir.lock.v2.TimelockService) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 4 with ConjureStartTransactionsResponse

use of com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse in project atlasdb by palantir.

the class MultiClientTransactionStarterTest method servesRequestsAsSoonAsResponseIsReceived.

@Test
public void servesRequestsAsSoonAsResponseIsReceived() {
    Namespace namespace = Namespace.of("Test" + UUID.randomUUID());
    UUID requestorId = UUID.randomUUID();
    BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>> requestToBeServed = batchElementForNamespace(namespace, 1);
    BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>> requestNotToBeServed = batchElementForNamespace(namespace, PARTITIONED_TIMESTAMPS_LIMIT_PER_SERVER_CALL * 5);
    ImmutableList<BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>>> requests = ImmutableList.of(requestToBeServed, requestNotToBeServed);
    Map<Namespace, ConjureStartTransactionsResponse> responseMap = startTransactionsResponse(requests, requestorId);
    when(timelockService.startTransactions(any())).thenReturn(responseMap).thenThrow(EXCEPTION);
    assertThatThrownBy(() -> processBatch(timelockService, requestorId, requests)).isEqualTo(EXCEPTION);
    // assert first request is served even if server throws on next request
    assertSanityOfRequestBatch(ImmutableList.of(requestToBeServed), ImmutableMap.of(namespace, ImmutableList.of(responseMap.get(namespace))));
    assertThat(requestNotToBeServed.result().isDone()).isFalse();
    LockCleanupService relevantLockCleanupService = LOCK_CLEANUP_SERVICE_MAP.get(namespace);
    verify(relevantLockCleanupService).refreshLockLeases(any());
    verify(relevantLockCleanupService).unlock(any());
    /*
         * For one space, all requests are accumulated, and we attempt to fetch as many respones as possible from the
         * server. Concretely, the queue here contains 25 demands for a response. The first one succeeds, but the
         * next four fail (and each batch size is five), and thus we get four calls to clean up.
         */
    verify(NAMESPACE_CACHE_MAP.get(namespace), times(4)).removeTransactionStateFromCache(anyLong());
}
Also used : StartIdentifiedAtlasDbTransactionResponse(com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse) ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) NamespaceAndRequestParams(com.palantir.lock.client.MultiClientTransactionStarter.NamespaceAndRequestParams) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) UUID(java.util.UUID) Namespace(com.palantir.atlasdb.timelock.api.Namespace) Test(org.junit.Test)

Example 5 with ConjureStartTransactionsResponse

use of com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse in project atlasdb by palantir.

the class MultiClientTransactionStarterTest method shouldNotFreeResourcesIfRequestIsServed.

@Test
public void shouldNotFreeResourcesIfRequestIsServed() {
    Namespace alpha = Namespace.of("alpha" + UUID.randomUUID());
    Namespace beta = Namespace.of("beta" + UUID.randomUUID());
    BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>> requestForAlpha = batchElementForNamespace(alpha, PARTITIONED_TIMESTAMPS_LIMIT_PER_SERVER_CALL - 1);
    BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>> requestForBeta = batchElementForNamespace(beta, PARTITIONED_TIMESTAMPS_LIMIT_PER_SERVER_CALL * 5);
    UUID requestorId = UUID.randomUUID();
    List<BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>>> requests = ImmutableList.of(requestForAlpha, requestForBeta);
    Map<Namespace, ConjureStartTransactionsResponse> responseMap = startTransactionsResponse(requests, requestorId);
    when(timelockService.startTransactions(any())).thenReturn(responseMap).thenThrow(EXCEPTION);
    assertThatThrownBy(() -> processBatch(timelockService, requestorId, requests)).isEqualTo(EXCEPTION);
    // assert requests made by client alpha are served
    assertSanityOfRequestBatch(ImmutableList.of(requestForAlpha), ImmutableMap.of(alpha, ImmutableList.of(responseMap.get(alpha))));
    verify(LOCK_CLEANUP_SERVICE_MAP.get(alpha), never()).unlock(any());
    verify(LOCK_CLEANUP_SERVICE_MAP.get(beta)).refreshLockLeases(any());
    verify(LOCK_CLEANUP_SERVICE_MAP.get(beta)).unlock(any());
    verify(NAMESPACE_CACHE_MAP.get(alpha), never()).removeTransactionStateFromCache(anyLong());
    // The size of each batch is 5 here, and thus for a single batch we need to clean up five times
    verify(NAMESPACE_CACHE_MAP.get(beta), times(5)).removeTransactionStateFromCache(anyLong());
}
Also used : StartIdentifiedAtlasDbTransactionResponse(com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse) ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) NamespaceAndRequestParams(com.palantir.lock.client.MultiClientTransactionStarter.NamespaceAndRequestParams) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) UUID(java.util.UUID) Namespace(com.palantir.atlasdb.timelock.api.Namespace) Test(org.junit.Test)

Aggregations

ConjureStartTransactionsResponse (com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse)29 Test (org.junit.Test)22 ImmutableList (com.google.common.collect.ImmutableList)12 List (java.util.List)12 Namespace (com.palantir.atlasdb.timelock.api.Namespace)9 StartIdentifiedAtlasDbTransactionResponse (com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse)9 ArrayList (java.util.ArrayList)9 BatchElement (com.palantir.atlasdb.autobatch.BatchElement)6 Cell (com.palantir.atlasdb.keyvalue.api.Cell)6 ConjureStartTransactionsRequest (com.palantir.atlasdb.timelock.api.ConjureStartTransactionsRequest)6 Transaction (com.palantir.atlasdb.transaction.api.Transaction)6 LockImmutableTimestampResponse (com.palantir.lock.v2.LockImmutableTimestampResponse)6 Optional (java.util.Optional)6 UUID (java.util.UUID)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 Iterables (com.google.common.collect.Iterables)5 Futures (com.google.common.util.concurrent.Futures)5 TimelockService (com.palantir.lock.v2.TimelockService)5