Search in sources :

Example 6 with StartIdentifiedAtlasDbTransactionResponse

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

the class TransactionStarterTest method shouldDeriveStartTransactionResponseFromBatchedResponse_multipleTransactions.

@Test
public void shouldDeriveStartTransactionResponseFromBatchedResponse_multipleTransactions() {
    ConjureStartTransactionsResponse batchResponse = StartTransactionsTestUtils.getStartTransactionResponse(40, 3);
    when(lockLeaseService.startTransactionsWithWatches(version, 3)).thenReturn(batchResponse);
    List<StartIdentifiedAtlasDbTransactionResponse> responses = requestSingularBatches(3);
    assertThat(responses).satisfies(StartTransactionsTestUtils::assertThatStartTransactionResponsesAreUnique).hasSize(3).allSatisfy(startTxnResponse -> assertDerivableFromBatchedResponse(startTxnResponse, batchResponse));
}
Also used : StartIdentifiedAtlasDbTransactionResponse(com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse) ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) Test(org.junit.Test)

Example 7 with StartIdentifiedAtlasDbTransactionResponse

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

the class MultiClientTransactionStarter method getResponseHandlers.

private static Map<Namespace, ResponseHandler> getResponseHandlers(List<BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>>> batch) {
    Map<Namespace, ResponseHandler> responseHandlers = new HashMap<>();
    for (BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>> element : batch) {
        NamespaceAndRequestParams requestParams = element.argument();
        Namespace namespace = requestParams.namespace();
        responseHandlers.computeIfAbsent(namespace, _unused -> new ResponseHandler(requestParams.params().lockCleanupService(), requestParams.params().cache())).addPendingFuture(SettableResponse.of(requestParams.params().numTransactions(), element.result()));
    }
    return responseHandlers;
}
Also used : DisruptorFuture(com.palantir.atlasdb.autobatch.DisruptorAutobatcher.DisruptorFuture) ConjureLockRequests.toConjure(com.palantir.lock.client.ConjureLockRequests.toConjure) HashMap(java.util.HashMap) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) ConjureStartTransactionsRequest(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsRequest) ImmutableList(com.google.common.collect.ImmutableList) Value(org.immutables.value.Value) Duration(java.time.Duration) Map(java.util.Map) LockWatchCache(com.palantir.lock.watch.LockWatchCache) DisruptorAutobatcher(com.palantir.atlasdb.autobatch.DisruptorAutobatcher) AtlasFutures(com.palantir.atlasdb.futures.AtlasFutures) Autobatchers(com.palantir.atlasdb.autobatch.Autobatchers) KeyedStream(com.palantir.common.streams.KeyedStream) UUID(java.util.UUID) Namespace(com.palantir.atlasdb.timelock.api.Namespace) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) StartIdentifiedAtlasDbTransactionResponse(com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Preconditions(com.palantir.logsafe.Preconditions) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Namespace(com.palantir.atlasdb.timelock.api.Namespace)

Example 8 with StartIdentifiedAtlasDbTransactionResponse

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

the class SnapshotTransactionManager method startTransactions.

@Override
public List<OpenTransaction> startTransactions(List<? extends PreCommitCondition> conditions) {
    if (conditions.isEmpty()) {
        return ImmutableList.of();
    }
    final List<StartIdentifiedAtlasDbTransactionResponse> responses;
    try {
        responses = timelockService.startIdentifiedAtlasDbTransactionBatch(conditions.size());
    } catch (StartTransactionFailedException e) {
        throw new TransactionFailedRetriableException("Failed to start a batch of transactions", e);
    }
    Preconditions.checkState(conditions.size() == responses.size(), "Different number of responses and conditions");
    try {
        long immutableTs = responses.stream().mapToLong(response -> response.immutableTimestamp().getImmutableTimestamp()).max().getAsLong();
        recordImmutableTimestamp(immutableTs);
        cleaner.punch(responses.get(0).startTimestampAndPartition().timestamp());
        List<OpenTransaction> transactions = Streams.zip(responses.stream(), conditions.stream(), (response, condition) -> {
            LockToken immutableTsLock = response.immutableTimestamp().getLock();
            Supplier<Long> startTimestampSupplier = Suppliers.ofInstance(response.startTimestampAndPartition().timestamp());
            Transaction transaction = createTransaction(immutableTs, startTimestampSupplier, immutableTsLock, condition);
            transaction.onSuccess(() -> lockWatchManager.onTransactionCommit(transaction.getTimestamp()));
            return new OpenTransactionImpl(transaction, immutableTsLock);
        }).collect(Collectors.toList());
        openTransactionCounter.inc(transactions.size());
        return transactions;
    } catch (Throwable t) {
        responses.forEach(response -> lockWatchManager.removeTransactionStateFromCache(response.startTimestampAndPartition().timestamp()));
        timelockService.tryUnlock(responses.stream().map(response -> response.immutableTimestamp().getLock()).collect(Collectors.toSet()));
        throw Throwables.rewrapAndThrowUncheckedException(t);
    }
}
Also used : TransactionFailedRetriableException(com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException) TransactionService(com.palantir.atlasdb.transaction.service.TransactionService) Throwables(com.palantir.common.base.Throwables) TransactionConfig(com.palantir.atlasdb.transaction.TransactionConfig) MetricsManager(com.palantir.atlasdb.util.MetricsManager) Duration(java.time.Duration) Counter(com.codahale.metrics.Counter) ConditionAwareTransactionTask(com.palantir.atlasdb.transaction.api.ConditionAwareTransactionTask) TimestampService(com.palantir.timestamp.TimestampService) LockWatchManager(com.palantir.atlasdb.keyvalue.api.watch.LockWatchManager) ToplistDeltaFilteringTableLevelMetricsController(com.palantir.atlasdb.transaction.impl.metrics.ToplistDeltaFilteringTableLevelMetricsController) ImmutableSet(com.google.common.collect.ImmutableSet) TimestampManagementService(com.palantir.timestamp.TimestampManagementService) TransactionReadSentinelBehavior(com.palantir.atlasdb.transaction.api.TransactionReadSentinelBehavior) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) Streams(com.google.common.collect.Streams) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) MemoizingTableLevelMetricsController(com.palantir.atlasdb.transaction.impl.metrics.MemoizingTableLevelMetricsController) List(java.util.List) MetricsFilterEvaluationContext(com.palantir.atlasdb.transaction.impl.metrics.MetricsFilterEvaluationContext) Transaction(com.palantir.atlasdb.transaction.api.Transaction) Timer(com.codahale.metrics.Timer) StartIdentifiedAtlasDbTransactionResponse(com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse) Optional(java.util.Optional) NoOpCleaner(com.palantir.atlasdb.cleaner.NoOpCleaner) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Iterables(com.google.common.collect.Iterables) SafeLoggerFactory(com.palantir.logsafe.logger.SafeLoggerFactory) Cleaner(com.palantir.atlasdb.cleaner.api.Cleaner) TransactionFailedRetriableException(com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) SafeShutdownRunner(com.palantir.util.SafeShutdownRunner) Supplier(java.util.function.Supplier) SafeLogger(com.palantir.logsafe.logger.SafeLogger) ClusterAvailabilityStatus(com.palantir.atlasdb.keyvalue.api.ClusterAvailabilityStatus) AtlasDbConstraintCheckingMode(com.palantir.atlasdb.transaction.api.AtlasDbConstraintCheckingMode) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Suppliers(com.google.common.base.Suppliers) ExecutorService(java.util.concurrent.ExecutorService) NoOpLockWatchManager(com.palantir.atlasdb.keyvalue.api.watch.NoOpLockWatchManager) KeyValueServiceStatus(com.palantir.atlasdb.transaction.api.KeyValueServiceStatus) TableLevelMetricsController(com.palantir.atlasdb.transaction.impl.metrics.TableLevelMetricsController) PreCommitCondition(com.palantir.atlasdb.transaction.api.PreCommitCondition) LockToken(com.palantir.lock.v2.LockToken) TimestampTracker(com.palantir.atlasdb.monitoring.TimestampTracker) TimestampCache(com.palantir.atlasdb.cache.TimestampCache) LockWatchManagerInternal(com.palantir.atlasdb.keyvalue.api.watch.LockWatchManagerInternal) TimelockService(com.palantir.lock.v2.TimelockService) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) LockService(com.palantir.lock.LockService) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) TransactionTask(com.palantir.atlasdb.transaction.api.TransactionTask) MultiTableSweepQueueWriter(com.palantir.atlasdb.sweep.queue.MultiTableSweepQueueWriter) StartTransactionFailedException(com.palantir.lock.client.StartTransactionFailedException) ConflictTracer(com.palantir.atlasdb.debug.ConflictTracer) Preconditions(com.palantir.logsafe.Preconditions) TransactionType(com.palantir.atlasdb.transaction.api.Transaction.TransactionType) LockToken(com.palantir.lock.v2.LockToken) StartTransactionFailedException(com.palantir.lock.client.StartTransactionFailedException) StartIdentifiedAtlasDbTransactionResponse(com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse) Transaction(com.palantir.atlasdb.transaction.api.Transaction) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction) Supplier(java.util.function.Supplier) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction)

Example 9 with StartIdentifiedAtlasDbTransactionResponse

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

the class MultiClientTransactionStarterTest method shouldNotFreeResourcesWithinNamespaceIfRequestIsServed.

@Test
public void shouldNotFreeResourcesWithinNamespaceIfRequestIsServed() {
    Namespace omega = Namespace.of("omega" + UUID.randomUUID());
    BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>> requestForOmega = batchElementForNamespace(omega, PARTITIONED_TIMESTAMPS_LIMIT_PER_SERVER_CALL - 1);
    BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>> secondRequestForOmega = batchElementForNamespace(omega, 2);
    UUID requestorId = UUID.randomUUID();
    List<BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>>> requests = ImmutableList.of(requestForOmega, secondRequestForOmega);
    Map<Namespace, ConjureStartTransactionsResponse> responseMap = startTransactionsResponse(requests, requestorId);
    when(timelockService.startTransactions(any())).thenReturn(responseMap).thenThrow(EXCEPTION);
    assertThatThrownBy(() -> processBatch(timelockService, requestorId, requests)).isEqualTo(EXCEPTION);
    // assert the first request made by client omega is served
    assertSanityOfRequestBatch(ImmutableList.of(requestForOmega), ImmutableMap.of(omega, ImmutableList.of(responseMap.get(omega))));
    @SuppressWarnings({ "unchecked", "rawtypes" }) ArgumentCaptor<Set<LockToken>> refreshArgumentCaptor = (ArgumentCaptor<Set<LockToken>>) ArgumentCaptor.forClass((Class) Set.class);
    verify(LOCK_CLEANUP_SERVICE_MAP.get(omega)).refreshLockLeases(refreshArgumentCaptor.capture());
    verify(LOCK_CLEANUP_SERVICE_MAP.get(omega)).unlock(eq(Collections.emptySet()));
    verify(NAMESPACE_CACHE_MAP.get(omega)).removeTransactionStateFromCache(anyLong());
    Set<LockToken> refreshedTokens = refreshArgumentCaptor.getValue();
    LockToken tokenShare = Futures.getUnchecked(requestForOmega.result()).get(0).immutableTimestamp().getLock();
    assertThat(tokenShare).isInstanceOf(LockTokenShare.class).satisfies(token -> {
        LockTokenShare share = ((LockTokenShare) token);
        assertThat(share.sharedLockToken()).isIn(refreshedTokens);
    });
}
Also used : Set(java.util.Set) ArgumentCaptor(org.mockito.ArgumentCaptor) LockToken(com.palantir.lock.v2.LockToken) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) Namespace(com.palantir.atlasdb.timelock.api.Namespace) 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) UUID(java.util.UUID) Test(org.junit.Test)

Example 10 with StartIdentifiedAtlasDbTransactionResponse

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

the class MultiClientTransactionStarterTest method assertSanityOfRequestBatch.

private void assertSanityOfRequestBatch(List<BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>>> requestsForClients, Map<Namespace, List<ConjureStartTransactionsResponse>> responseMap) {
    assertCompletedWithCorrectNumberOfTransactions(requestsForClients);
    Map<Namespace, List<BatchElement<NamespaceAndRequestParams, List<StartIdentifiedAtlasDbTransactionResponse>>>> partitionedResponses = requestsForClients.stream().collect(Collectors.groupingBy(e -> e.argument().namespace()));
    responseMap.forEach((namespace, responses) -> {
        int startInd = 0;
        List<StartIdentifiedAtlasDbTransactionResponse> startedTransactions = partitionedResponses.get(namespace).stream().map(response -> Futures.getUnchecked(response.result())).flatMap(Collection::stream).collect(Collectors.toList());
        for (ConjureStartTransactionsResponse conjureResponse : responses) {
            int toIndex = Math.min(startInd + conjureResponse.getTimestamps().count(), startedTransactions.size());
            List<StartIdentifiedAtlasDbTransactionResponse> responseList = startedTransactions.subList(startInd, toIndex);
            startInd = toIndex;
            assertThat(responseList).satisfies(StartTransactionsTestUtils::assertThatStartTransactionResponsesAreUnique).allSatisfy(startTxnResponse -> StartTransactionsTestUtils.assertDerivableFromBatchedResponse(startTxnResponse, conjureResponse));
        }
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DisruptorFuture(com.palantir.atlasdb.autobatch.DisruptorAutobatcher.DisruptorFuture) IntStream(java.util.stream.IntStream) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) ConjureStartTransactionsResponse(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse) ArrayList(java.util.ArrayList) ConjureStartTransactionsRequest(com.palantir.atlasdb.timelock.api.ConjureStartTransactionsRequest) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) LockWatchCache(com.palantir.lock.watch.LockWatchCache) ImmutableMap(com.google.common.collect.ImmutableMap) NamespaceAndRequestParams(com.palantir.lock.client.MultiClientTransactionStarter.NamespaceAndRequestParams) KeyedStream(com.palantir.common.streams.KeyedStream) LockToken(com.palantir.lock.v2.LockToken) Collection(java.util.Collection) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Namespace(com.palantir.atlasdb.timelock.api.Namespace) Collectors(java.util.stream.Collectors) MultiClientTransactionStarter.processBatch(com.palantir.lock.client.MultiClientTransactionStarter.processBatch) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) RequestParams(com.palantir.lock.client.MultiClientTransactionStarter.RequestParams) StartIdentifiedAtlasDbTransactionResponse(com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse) LockWatchCacheImpl(com.palantir.lock.watch.LockWatchCacheImpl) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) 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) Namespace(com.palantir.atlasdb.timelock.api.Namespace)

Aggregations

StartIdentifiedAtlasDbTransactionResponse (com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse)14 ConjureStartTransactionsResponse (com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse)10 Test (org.junit.Test)10 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)9 BatchElement (com.palantir.atlasdb.autobatch.BatchElement)7 ArrayList (java.util.ArrayList)6 Namespace (com.palantir.atlasdb.timelock.api.Namespace)5 NamespaceAndRequestParams (com.palantir.lock.client.MultiClientTransactionStarter.NamespaceAndRequestParams)5 UUID (java.util.UUID)5 LockToken (com.palantir.lock.v2.LockToken)4 LockWatchCache (com.palantir.lock.watch.LockWatchCache)4 Collectors (java.util.stream.Collectors)4 DisruptorAutobatcher (com.palantir.atlasdb.autobatch.DisruptorAutobatcher)3 Preconditions (com.palantir.logsafe.Preconditions)3 Duration (java.time.Duration)3 Optional (java.util.Optional)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Iterables (com.google.common.collect.Iterables)2