protected void setConstraintCheckingMode(AtlasDbConstraintCheckingMode mode) { txManager = new TestTransactionManagerImpl(metricsManager, keyValueService, timestampService, timestampService, lockClient, lockService, transactionService, mode); }
@Override public Transaction commitAndStartNewTransaction(Transaction tx) { tx.commit(); return createNewTransaction(); }
@Override public Transaction createNewTransaction() { long startTimestamp = timelockService.getFreshTimestamp(); return new SnapshotTransaction(metricsManager, keyValueService, timelockService, transactionService, NoOpCleaner.INSTANCE, () -> startTimestamp, getConflictDetectionManager(), SweepStrategyManagers.createDefault(keyValueService), startTimestamp, Optional.empty(), PreCommitConditions.NO_OP, AtlasDbConstraintCheckingMode.NO_CONSTRAINT_CHECKING, null, TransactionReadSentinelBehavior.THROW_EXCEPTION, false, timestampValidationReadCache, getRangesExecutor, defaultGetRangesConcurrency, sweepQueueWriter, deleteExecutor, CommitProfileProcessor.createNonLogging(metricsManager), validateLocksOnReads, () -> TRANSACTION_CONFIG); }
byte[] value = new byte[1]; serializableTxManager.runTaskWithRetry(tx -> { tx.put(TABLE, ImmutableMap.of(firstCell, value, secondCell, value)); return null; assertThatThrownBy(() -> serializableTxManager.runTaskWithConditionWithRetry(() -> new PreCommitCondition() { @Override public void throwIfConditionInvalid(long timestamp) { })).isSameAs(conditionFailure); List<Cell> cells = serializableTxManager.runTaskReadOnly(tx -> BatchingVisitableView.of(tx.getRowsColumnRange( TABLE,
@SuppressWarnings("Indentation") // Checkstyle complains about lambda in constructor. public TestTransactionManagerImpl(MetricsManager metricsManager, KeyValueService keyValueService, TimestampService timestampService, TimestampManagementService timestampManagementService, LockClient lockClient, LockService lockService, TransactionService transactionService, AtlasDbConstraintCheckingMode constraintCheckingMode) { super(metricsManager, createAssertKeyValue(keyValueService, lockService), new LegacyTimelockService(timestampService, lockService, lockClient), timestampManagementService, lockService, transactionService, Suppliers.ofInstance(constraintCheckingMode), ConflictDetectionManagers.createWithoutWarmingCache(keyValueService), SweepStrategyManagers.createDefault(keyValueService), NoOpCleaner.INSTANCE, TimestampCache.createForTests(), false, AbstractTransactionTest.GET_RANGES_THREAD_POOL_SIZE, AbstractTransactionTest.DEFAULT_GET_RANGES_CONCURRENCY, MultiTableSweepQueueWriter.NO_OP, MoreExecutors.newDirectExecutorService(), true, () -> TRANSACTION_CONFIG); }
@Override ConflictDetectionManager getConflictDetectionManager() { return TestConflictDetectionManagers.createWithStaticConflictDetection(getConflictHandlerWithOverrides()); }
@Test public void cleanupPreCommitConditionsOnSuccess() { MutableLong counter = new MutableLong(0L); PreCommitCondition succeedsCondition = new PreCommitCondition() { @Override public void throwIfConditionInvalid(long timestamp) {} @Override public void cleanup() { counter.increment(); } }; serializableTxManager.runTaskWithConditionThrowOnConflict(succeedsCondition, (tx, condition) -> { tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value"))); return null; }); assertThat(counter.intValue(), is(1)); serializableTxManager.runTaskWithConditionReadOnly(succeedsCondition, (tx, condition) -> tx.get(TABLE, ImmutableSet.of(TEST_CELL))); assertThat(counter.intValue(), is(2)); }
private long putWriteAndFailOnPreCommitConditionReturningStartTimestamp(WriteReference writeRef) { AtomicLong startTs = new AtomicLong(0); assertThatThrownBy(() -> serializableTxManager .runTaskWithConditionWithRetry( FailingPreCommitCondition::new, (txn, ignore) -> { put(txn, writeRef); startTs.set(txn.getTimestamp()); return null; })).isInstanceOf(RuntimeException.class); return startTs.get(); }
@Test public void readTransactionSucceedsIfConditionSucceeds() { serializableTxManager.runTaskWithConditionReadOnly(PreCommitConditions.NO_OP, (tx, condition) -> tx.get(TABLE, ImmutableSet.of(TEST_CELL))); TransactionOutcomeMetricsAssert.assertThat(transactionOutcomeMetrics) .hasSuccessfulCommits(1); }
@Test public void commitIfPreCommitConditionSucceeds() { serializableTxManager.runTaskWithConditionThrowOnConflict(PreCommitConditions.NO_OP, (tx, condition) -> { tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value"))); return null; }); }
final MutableLong count = new MutableLong(); for (final TableReference name : Lists.newArrayList(tableRef, namespacedTableRef)) { verifyTxManager.runTaskReadOnly((TransactionTask<Void, RuntimeException>) txn -> { BatchingVisitable<RowResult<byte[]>> bv = txn.getRange(name, RangeRequest.all()); bv.batchAccept(1000, AbortingVisitors.batching(
ExecutorService deleteExecutor) { super(metricsManager, createAssertKeyValue(keyValueService, lockService), new LegacyTimelockService(timestampService, lockService, lockClient), timestampManagementService,
@Override ConflictDetectionManager getConflictDetectionManager() { return TestConflictDetectionManagers.createWithStaticConflictDetection(getConflictHandlerWithOverrides()); }
serializableTxManager.runTaskWithConditionThrowOnConflict(failsCondition, (tx, condition) -> { tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value"))); return null; serializableTxManager.runTaskWithConditionReadOnly(failsCondition, (tx, condition) -> tx.get(TABLE, ImmutableSet.of(TEST_CELL))); fail();
@Test public void commitWithPreCommitConditionOnRetry() { Supplier<PreCommitCondition> conditionSupplier = mock(Supplier.class); when(conditionSupplier.get()).thenReturn(ALWAYS_FAILS_CONDITION) .thenReturn(PreCommitConditions.NO_OP); serializableTxManager.runTaskWithConditionWithRetry(conditionSupplier, (tx, condition) -> { tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value"))); return null; }); }
@Test public void readTransactionFailsIfConditionFails() { try { serializableTxManager.runTaskWithConditionReadOnly(ALWAYS_FAILS_CONDITION, (tx, condition) -> tx.get(TABLE, ImmutableSet.of(TEST_CELL))); fail(); } catch (TransactionFailedRetriableException e) { assertThat(e.getMessage(), containsString("Condition failed")); } TransactionOutcomeMetricsAssert.assertThat(transactionOutcomeMetrics) .hasFailedCommits(1); }
@Test public void failToCommitIfPreCommitConditionFails() { try { serializableTxManager.runTaskWithConditionThrowOnConflict(ALWAYS_FAILS_CONDITION, (tx, condition) -> { tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value"))); return null; }); fail(); } catch (TransactionFailedRetriableException e) { assertThat(e.getMessage(), containsString("Condition failed")); } }
protected TransactionManager createManager() { return new TestTransactionManagerImpl( MetricsManagers.createForTests(), keyValueService, timestampService, timestampManagementService, lockClient, lockService, transactionService, conflictDetectionManager, sweepStrategyManager, MultiTableSweepQueueWriter.NO_OP, MoreExecutors.newDirectExecutorService()); }
@Test public void testGetRowsLocalWritesWithColumnSelection() { // This test ensures getRows correctly applies columnSelection when there are local writes byte[] row1 = PtBytes.toBytes("row1"); Cell row1Column1 = Cell.create(row1, PtBytes.toBytes("column1")); Cell row1Column2 = Cell.create(row1, PtBytes.toBytes("column2")); byte[] row1Column1Value = BigInteger.valueOf(1).toByteArray(); byte[] row1Column2Value = BigInteger.valueOf(2).toByteArray(); Transaction snapshotTx = serializableTxManager.createNewTransaction(); snapshotTx.put(TABLE, ImmutableMap.of( row1Column1, row1Column1Value, row1Column2, row1Column2Value)); ColumnSelection column1Selection = ColumnSelection.create(ImmutableList.of(row1Column1.getColumnName())); // local writes still apply columnSelection RowResult<byte[]> rowResult1 = snapshotTx.getRows(TABLE, ImmutableList.of(row1), column1Selection).get(row1); assertThat(rowResult1.getColumns(), hasEntry(row1Column1.getColumnName(), row1Column1Value)); assertThat(rowResult1.getColumns(), not(hasEntry(row1Column2.getColumnName(), row1Column2Value))); RowResult<byte[]> rowResult2 = snapshotTx.getRows(TABLE, ImmutableList.of(row1), ColumnSelection.all()) .get(row1); assertThat(rowResult2.getColumns(), hasEntry(row1Column1.getColumnName(), row1Column1Value)); assertThat(rowResult2.getColumns(), hasEntry(row1Column2.getColumnName(), row1Column2Value)); }
@SuppressWarnings("Indentation") // Checkstyle complains about lambda in constructor. public TestTransactionManagerImpl(MetricsManager metricsManager, KeyValueService keyValueService, TimestampService timestampService, TimestampManagementService timestampManagementService, LockClient lockClient, LockService lockService, TransactionService transactionService, AtlasDbConstraintCheckingMode constraintCheckingMode) { super(metricsManager, createAssertKeyValue(keyValueService, lockService), new LegacyTimelockService(timestampService, lockService, lockClient), timestampManagementService, lockService, transactionService, Suppliers.ofInstance(constraintCheckingMode), ConflictDetectionManagers.createWithoutWarmingCache(keyValueService), SweepStrategyManagers.createDefault(keyValueService), NoOpCleaner.INSTANCE, TimestampCache.createForTests(), false, AbstractTransactionTest.GET_RANGES_THREAD_POOL_SIZE, AbstractTransactionTest.DEFAULT_GET_RANGES_CONCURRENCY, MultiTableSweepQueueWriter.NO_OP, MoreExecutors.newDirectExecutorService(), true, () -> TRANSACTION_CONFIG); }