Search in sources :

Example 1 with OpenTransaction

use of com.palantir.atlasdb.transaction.api.OpenTransaction in project atlasdb by palantir.

the class AtlasDbServiceImpl method startTransaction.

@Override
public TransactionToken startTransaction() {
    String id = UUID.randomUUID().toString();
    TransactionToken token = new TransactionToken(id);
    OpenTransaction openTxn = Iterables.getOnlyElement(txManager.startTransactions(ImmutableList.of(PreCommitConditions.NO_OP)));
    transactions.put(token, openTxn);
    return token;
}
Also used : TransactionToken(com.palantir.atlasdb.api.TransactionToken) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction)

Example 2 with OpenTransaction

use of com.palantir.atlasdb.transaction.api.OpenTransaction in project atlasdb by palantir.

the class SnapshotTransactionManagerTest method startEmptyBatchOfTransactionsDoesNotCallTimelockService.

@Test
public void startEmptyBatchOfTransactionsDoesNotCallTimelockService() {
    TimelockService timelockService = spy(services.getLegacyTimelockService());
    SnapshotTransactionManager transactionManager = createSnapshotTransactionManager(timelockService, false);
    List<OpenTransaction> transactions = transactionManager.startTransactions(ImmutableList.of());
    assertThat(transactions).isEmpty();
    verify(timelockService, never()).startIdentifiedAtlasDbTransactionBatch(anyInt());
}
Also used : TimelockService(com.palantir.lock.v2.TimelockService) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction) Test(org.junit.Test)

Example 3 with OpenTransaction

use of com.palantir.atlasdb.transaction.api.OpenTransaction 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 4 with OpenTransaction

use of com.palantir.atlasdb.transaction.api.OpenTransaction in project atlasdb by palantir.

the class AtlasDbServiceImpl method commit.

@Override
public void commit(TransactionToken token) {
    OpenTransaction openTxn = transactions.getIfPresent(token);
    if (openTxn != null) {
        openTxn.finish((TxTask) transaction -> null);
        transactions.invalidate(token);
    }
}
Also used : TableCellVal(com.palantir.atlasdb.api.TableCellVal) TableRowResult(com.palantir.atlasdb.api.TableRowResult) Iterables(com.google.common.collect.Iterables) ConflictHandler(com.palantir.atlasdb.transaction.api.ConflictHandler) BatchingVisitable(com.palantir.common.base.BatchingVisitable) Inject(javax.inject.Inject) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction) RangeToken(com.palantir.atlasdb.api.RangeToken) ImmutableList(com.google.common.collect.ImmutableList) Duration(java.time.Duration) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) RuntimeTransactionTask(com.palantir.atlasdb.transaction.api.RuntimeTransactionTask) BatchingVisitables(com.palantir.common.base.BatchingVisitables) TableRowSelection(com.palantir.atlasdb.api.TableRowSelection) TableRange(com.palantir.atlasdb.api.TableRange) TableMetadataPersistence(com.palantir.atlasdb.protos.generated.TableMetadataPersistence) TableMetadata(com.palantir.atlasdb.table.description.TableMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Collection(java.util.Collection) PreCommitConditions(com.palantir.atlasdb.transaction.impl.PreCommitConditions) Set(java.util.Set) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TableCell(com.palantir.atlasdb.api.TableCell) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) List(java.util.List) TxTask(com.palantir.atlasdb.transaction.impl.TxTask) TransactionToken(com.palantir.atlasdb.api.TransactionToken) ValueType(com.palantir.atlasdb.table.description.ValueType) Transaction(com.palantir.atlasdb.transaction.api.Transaction) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) AtlasDbService(com.palantir.atlasdb.api.AtlasDbService) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Preconditions(com.palantir.logsafe.Preconditions) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction)

Example 5 with OpenTransaction

use of com.palantir.atlasdb.transaction.api.OpenTransaction in project atlasdb by palantir.

the class AtlasDbServiceImpl method abort.

@Override
public void abort(TransactionToken token) {
    OpenTransaction openTxn = transactions.getIfPresent(token);
    if (openTxn != null) {
        openTxn.finish((TxTask) transaction -> {
            transaction.abort();
            return null;
        });
        transactions.invalidate(token);
    }
}
Also used : TableCellVal(com.palantir.atlasdb.api.TableCellVal) TableRowResult(com.palantir.atlasdb.api.TableRowResult) Iterables(com.google.common.collect.Iterables) ConflictHandler(com.palantir.atlasdb.transaction.api.ConflictHandler) BatchingVisitable(com.palantir.common.base.BatchingVisitable) Inject(javax.inject.Inject) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction) RangeToken(com.palantir.atlasdb.api.RangeToken) ImmutableList(com.google.common.collect.ImmutableList) Duration(java.time.Duration) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) RuntimeTransactionTask(com.palantir.atlasdb.transaction.api.RuntimeTransactionTask) BatchingVisitables(com.palantir.common.base.BatchingVisitables) TableRowSelection(com.palantir.atlasdb.api.TableRowSelection) TableRange(com.palantir.atlasdb.api.TableRange) TableMetadataPersistence(com.palantir.atlasdb.protos.generated.TableMetadataPersistence) TableMetadata(com.palantir.atlasdb.table.description.TableMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Collection(java.util.Collection) PreCommitConditions(com.palantir.atlasdb.transaction.impl.PreCommitConditions) Set(java.util.Set) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TableCell(com.palantir.atlasdb.api.TableCell) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) List(java.util.List) TxTask(com.palantir.atlasdb.transaction.impl.TxTask) TransactionToken(com.palantir.atlasdb.api.TransactionToken) ValueType(com.palantir.atlasdb.table.description.ValueType) Transaction(com.palantir.atlasdb.transaction.api.Transaction) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) AtlasDbService(com.palantir.atlasdb.api.AtlasDbService) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Preconditions(com.palantir.logsafe.Preconditions) OpenTransaction(com.palantir.atlasdb.transaction.api.OpenTransaction)

Aggregations

OpenTransaction (com.palantir.atlasdb.transaction.api.OpenTransaction)7 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 Iterables (com.google.common.collect.Iterables)3 TransactionToken (com.palantir.atlasdb.api.TransactionToken)3 KeyValueService (com.palantir.atlasdb.keyvalue.api.KeyValueService)3 Transaction (com.palantir.atlasdb.transaction.api.Transaction)3 Preconditions (com.palantir.logsafe.Preconditions)3 Duration (java.time.Duration)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Test (org.junit.Test)3 Cache (com.google.common.cache.Cache)2 CacheBuilder (com.google.common.cache.CacheBuilder)2 AtlasDbService (com.palantir.atlasdb.api.AtlasDbService)2 RangeToken (com.palantir.atlasdb.api.RangeToken)2 TableCell (com.palantir.atlasdb.api.TableCell)2 TableCellVal (com.palantir.atlasdb.api.TableCellVal)2 TableRange (com.palantir.atlasdb.api.TableRange)2 TableRowResult (com.palantir.atlasdb.api.TableRowResult)2