Search in sources :

Example 1 with TransactionManager

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

the class AtlasDbEteServer method run.

@Override
public void run(AtlasDbEteConfiguration config, final Environment environment) throws Exception {
    TransactionManager transactionManager = tryToCreateTransactionManager(config, environment);
    environment.jersey().register(new SimpleTodoResource(new TodoClient(transactionManager)));
    environment.jersey().register(new SimpleCheckAndSetResource(new CheckAndSetClient(transactionManager)));
    environment.jersey().register(HttpRemotingJerseyFeature.INSTANCE);
    environment.jersey().register(new NotInitializedExceptionMapper());
    environment.jersey().register(new CleanupMetadataResourceImpl(transactionManager, config.getAtlasDbConfig().initializeAsync()));
}
Also used : CleanupMetadataResourceImpl(com.palantir.atlasdb.schema.CleanupMetadataResourceImpl) NotInitializedExceptionMapper(com.palantir.atlasdb.http.NotInitializedExceptionMapper) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) TodoClient(com.palantir.atlasdb.todo.TodoClient) SimpleCheckAndSetResource(com.palantir.atlasdb.cas.SimpleCheckAndSetResource) CheckAndSetClient(com.palantir.atlasdb.cas.CheckAndSetClient) SimpleTodoResource(com.palantir.atlasdb.todo.SimpleTodoResource)

Example 2 with TransactionManager

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

the class Scrubber method runBackgroundScrubTask.

@VisibleForTesting
void runBackgroundScrubTask(final TransactionManager txManager) {
    log.debug("Starting scrub task");
    // Warning: Let T be the hard delete transaction that triggered a scrub, and let S be its
    // start timestamp.  If the locks for T happen to time out right after T checks that its
    // locks are held but right before T writes its commit timestamp (extremely rare case), AND
    // the unreadable timestamp is greater than S, then the scrub task could actually roll back
    // the hard delete transaction (forcing it to abort or retry).  Note that this doesn't affect
    // correctness, but could be an annoying edge cause that causes hard delete to take longer
    // than it otherwise would have.
    Long immutableTimestamp = immutableTimestampSupplier.get();
    Long unreadableTimestamp = unreadableTimestampSupplier.get();
    final long maxScrubTimestamp = aggressiveScrub ? immutableTimestamp : Math.min(unreadableTimestamp, immutableTimestamp);
    log.debug("Scrub task immutableTimestamp: {}, unreadableTimestamp: {}, maxScrubTimestamp: {}", immutableTimestamp, unreadableTimestamp, maxScrubTimestamp);
    final int batchSize = (int) Math.ceil(batchSizeSupplier.get() * ((double) threadCount / readThreadCount));
    List<byte[]> rangeBoundaries = Lists.newArrayList();
    rangeBoundaries.add(PtBytes.EMPTY_BYTE_ARRAY);
    if (readThreadCount > 1) {
        // This will actually partition into the closest higher power of 2 number of ranges.
        rangeBoundaries.addAll(Ordering.from(UnsignedBytes.lexicographicalComparator()).sortedCopy(new UniformRowNamePartitioner(ValueType.BLOB).getPartitions(readThreadCount - 1)));
    }
    rangeBoundaries.add(PtBytes.EMPTY_BYTE_ARRAY);
    List<Future<Void>> readerFutures = Lists.newArrayList();
    final AtomicInteger totalCellsRead = new AtomicInteger(0);
    for (int i = 0; i < rangeBoundaries.size() - 1; i++) {
        final byte[] startRow = rangeBoundaries.get(i);
        final byte[] endRow = rangeBoundaries.get(i + 1);
        readerFutures.add(readerExec.submit(() -> {
            BatchingVisitable<SortedMap<Long, Multimap<TableReference, Cell>>> scrubQueue = scrubberStore.getBatchingVisitableScrubQueue(maxScrubTimestamp, startRow, endRow);
            scrubQueue.batchAccept(batchSize, batch -> {
                for (SortedMap<Long, Multimap<TableReference, Cell>> cells : batch) {
                    // We may actually get more cells than the batch size. The batch size is used
                    // for pulling off the scrub queue, and a single entry in the scrub queue may
                    // match multiple tables. These will get broken down into smaller batches later
                    // on when we actually do deletes.
                    int numCellsRead = scrubSomeCells(cells, txManager, maxScrubTimestamp);
                    int totalRead = totalCellsRead.addAndGet(numCellsRead);
                    log.debug("Scrub task processed {} cells in a batch, total {} processed so far.", numCellsRead, totalRead);
                    if (!isScrubEnabled.get()) {
                        log.debug("Stopping scrub for banned hours.");
                        break;
                    }
                }
                return isScrubEnabled.get();
            });
            return null;
        }));
    }
    for (Future<Void> readerFuture : readerFutures) {
        Futures.getUnchecked(readerFuture);
    }
    log.debug("Scrub background task running at timestamp {} processed a total of {} cells", maxScrubTimestamp, totalCellsRead.get());
    log.debug("Finished scrub task");
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) TransactionService(com.palantir.atlasdb.transaction.service.TransactionService) Throwables(com.palantir.common.base.Throwables) LoggerFactory(org.slf4j.LoggerFactory) BatchingVisitable(com.palantir.common.base.BatchingVisitable) Future(java.util.concurrent.Future) HashMultimap(com.google.common.collect.HashMultimap) PTExecutors(com.palantir.common.concurrent.PTExecutors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) ImmutableSet(com.google.common.collect.ImmutableSet) NamedThreadFactory(com.palantir.common.concurrent.NamedThreadFactory) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Collection(java.util.Collection) Set(java.util.Set) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) Maps2(com.palantir.common.collect.Maps2) GuardedBy(javax.annotation.concurrent.GuardedBy) Sets(com.google.common.collect.Sets) UniformRowNamePartitioner(com.palantir.atlasdb.table.description.UniformRowNamePartitioner) List(java.util.List) ValueType(com.palantir.atlasdb.table.description.ValueType) Transaction(com.palantir.atlasdb.transaction.api.Transaction) Entry(java.util.Map.Entry) Builder(com.google.common.collect.ImmutableMultimap.Builder) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) SortedMap(java.util.SortedMap) Iterables(com.google.common.collect.Iterables) Supplier(com.google.common.base.Supplier) TransactionFailedRetriableException(com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException) Callable(java.util.concurrent.Callable) Multimap(com.google.common.collect.Multimap) PtBytes(com.palantir.atlasdb.encoding.PtBytes) Multimaps(com.google.common.collect.Multimaps) Lists(com.google.common.collect.Lists) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ExecutorService(java.util.concurrent.ExecutorService) UnsignedBytes(com.google.common.primitives.UnsignedBytes) Logger(org.slf4j.Logger) Value(com.palantir.atlasdb.keyvalue.api.Value) ExecutorInheritableThreadLocal(com.palantir.common.concurrent.ExecutorInheritableThreadLocal) Maps(com.google.common.collect.Maps) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) TransactionConstants(com.palantir.atlasdb.transaction.impl.TransactionConstants) Ordering(com.google.common.collect.Ordering) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) TransactionType(com.palantir.atlasdb.transaction.api.Transaction.TransactionType) UniformRowNamePartitioner(com.palantir.atlasdb.table.description.UniformRowNamePartitioner) BatchingVisitable(com.palantir.common.base.BatchingVisitable) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) HashMultimap(com.google.common.collect.HashMultimap) Multimap(com.google.common.collect.Multimap) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SortedMap(java.util.SortedMap) Future(java.util.concurrent.Future) Cell(com.palantir.atlasdb.keyvalue.api.Cell) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with TransactionManager

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

the class KeyValueServiceMigrator method internalMigrate.

private void internalMigrate() throws InterruptedException {
    Set<TableReference> tables = KeyValueServiceMigrators.getMigratableTableNames(fromKvs, unmigratableTables);
    TransactionManager txManager = toTransactionManager;
    TransactionManager readTxManager = fromTransactionManager;
    GeneralTaskCheckpointer checkpointer = new GeneralTaskCheckpointer(checkpointTable, toKvs, txManager);
    ExecutorService executor = PTExecutors.newFixedThreadPool(threads);
    try {
        migrateTables(tables, readTxManager, txManager, toKvs, migrationTimestampSupplier.get(), executor, checkpointer);
        processMessage("Data migration complete.", KvsMigrationMessageLevel.INFO);
    } catch (Throwable t) {
        processMessage("Migration failed.", t, KvsMigrationMessageLevel.ERROR);
        Throwables.throwUncheckedException(t);
    } finally {
        executor.shutdown();
        executor.awaitTermination(10000L, TimeUnit.MILLISECONDS);
    }
}
Also used : TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) ExecutorService(java.util.concurrent.ExecutorService)

Example 4 with TransactionManager

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

use of com.palantir.atlasdb.transaction.api.TransactionManager 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)

Aggregations

TransactionManager (com.palantir.atlasdb.transaction.api.TransactionManager)13 Test (org.junit.Test)6 LockService (com.palantir.lock.LockService)4 StreamTestTableFactory (com.palantir.atlasdb.performance.schema.generated.StreamTestTableFactory)3 ValueStreamStore (com.palantir.atlasdb.performance.schema.generated.ValueStreamStore)3 TimelockService (com.palantir.lock.v2.TimelockService)3 InputStream (java.io.InputStream)3 Benchmark (org.openjdk.jmh.annotations.Benchmark)3 Measurement (org.openjdk.jmh.annotations.Measurement)3 Threads (org.openjdk.jmh.annotations.Threads)3 Warmup (org.openjdk.jmh.annotations.Warmup)3 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)2 SerializableTransactionManager (com.palantir.atlasdb.transaction.impl.SerializableTransactionManager)2 ExecutorService (java.util.concurrent.ExecutorService)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Supplier (com.google.common.base.Supplier)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 HashMultimap (com.google.common.collect.HashMultimap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 Builder (com.google.common.collect.ImmutableMultimap.Builder)1