Optional<String> selectTableToCompact() { return transactionManager.runTaskReadOnly(this::selectTableToCompactInternal); }
@Override protected final void performOneCall() { List<byte[]> results = txnManager.runTaskReadOnly(txn -> getRange(txn, 0L, numRows)); Preconditions.checkState(results.size() == numRows); for (byte[] resultData : results) { Preconditions.checkState(resultData.length == dataSize); } }
@Override public void get(long firstBlock, long numBlocks, OutputStream destination) { if (parent.isUncommitted()) { loadNBlocksToOutputStream(parent, id, firstBlock, numBlocks, destination); } else { txnMgr.runTaskReadOnly(txn -> { loadNBlocksToOutputStream(txn, id, firstBlock, numBlocks, destination); return null; }); } }
@Override public void performOneCall() { List<byte[]> result = txnManager.runTaskReadOnly(txn -> { BlobsTable table = BenchmarksTableFactory.of().getBlobsTable(txn); List<BlobsRow> rowKeys = keys.stream().map(BlobsRow::of).collect(Collectors.toList()); return table.getRows(rowKeys).stream() .map(BlobsTable.BlobsRowResult::getData) .collect(Collectors.toList()); }); Preconditions.checkState(result.size() == keys.size()); }
@Override public <T, E extends Exception> T runTaskReadOnly(TransactionTask<T, E> task) throws E { return delegate().runTaskReadOnly(wrapTask(task)); }
public Optional<Long> get() { return transactionManager.runTaskReadOnly((transaction) -> new CheckAndSetPersistentValue(transaction).get()); }
private Optional<String> getExistingBucketForParameters() { return txnManager.runTaskReadOnly(txn -> { MetadataTable table = BenchmarksTableFactory.of().getMetadataTable(txn); return table.getRow(MetadataTable.MetadataRow.of(getKeyForParameters())) .map(MetadataTable.MetadataRowResult::getData) .map(AbstractRangeScanBenchmark::deserialize) .map(mdata -> mdata.bucket); }); }
public static TransactionManager mockTxManager() { TransactionManager txManager = mock(TransactionManager.class); Answer runTaskAnswer = inv -> { Object[] args = inv.getArguments(); TransactionTask<?, ?> task = (TransactionTask<?, ?>) args[0]; return task.execute(mock(Transaction.class)); }; doAnswer(runTaskAnswer).when(txManager).runTaskReadOnly(any()); doAnswer(runTaskAnswer).when(txManager).runTaskWithRetry(any()); return txManager; }
@Test public void testWriteFailsOnReadOnly() { try { getManager().runTaskReadOnly((TransactionTask<Void, RuntimeException>) t -> { put(t, "row1", "col1", "v1"); return null; }); fail(); } catch (RuntimeException e) { // we want this to throw } }
@Test public void shouldNotRunTaskReadOnlyWithClosedTransactionManager() { txMgr.close(); assertThatThrownBy(() -> txMgr.runTaskReadOnly((TransactionTask<Void, RuntimeException>) txn -> { put(txn, "row1", "col1", "v1"); return null; })) .isInstanceOf(IllegalStateException.class) .hasMessage("Operations cannot be performed on closed TransactionManager."); }
@Test public void testLoadEmpty() { Assert.assertTrue(txManager.runTaskReadOnly( tx -> priorityStore.loadOldPriorities(tx, tx.getTimestamp())).isEmpty()); Assert.assertTrue(txManager.runTaskReadOnly( tx -> priorityStore.loadNewPriorities(tx)).isEmpty()); }
@Test public void shouldNotMakeRemoteCallsInAReadonlyTransactionIfNoWorkIsDone() { TimestampService mockTimestampService = mock(TimestampService.class); TimestampManagementService mockTimestampManagementService = mock(TimestampManagementService.class); LockService mockLockService = mock(LockService.class); TransactionManager txnManagerWithMocks = SerializableTransactionManager.createForTest( metricsManager, getKeyValueService(), mockTimestampService, mockTimestampManagementService, 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, 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); verifyNoMoreInteractions(mockTimestampManagementService); }
@Test public void smokeTest() throws Exception { createTable(TABLE_1, SweepStrategy.CONSERVATIVE); createTable(TABLE_2, SweepStrategy.THOROUGH); createTable(TABLE_3, SweepStrategy.NOTHING); putManyCells(TABLE_1, 100, 110); putManyCells(TABLE_1, 103, 113); putManyCells(TABLE_1, 105, 115); putManyCells(TABLE_2, 101, 111); putManyCells(TABLE_2, 104, 114); putManyCells(TABLE_3, 120, 130); try (SingleLockService sweepLocks = backgroundSweeper.createSweepLocks()) { for (int i = 0; i < 50; ++i) { backgroundSweeper.checkConfigAndRunSweep(sweepLocks); } } verifyTableSwept(TABLE_1, 75, true); verifyTableSwept(TABLE_2, 58, false); List<SweepPriority> priorities = txManager.runTaskReadOnly( tx -> SweepPriorityStoreImpl.create(kvs, SweepTableFactory.of(), false).loadNewPriorities(tx)); Assert.assertTrue(priorities.stream().anyMatch(p -> p.tableRef().equals(TABLE_1))); Assert.assertTrue(priorities.stream().anyMatch(p -> p.tableRef().equals(TABLE_2))); }
@Test public void testDelete() throws Exception { txManager.runTaskWithRetry(tx -> { priorityStore.update( tx, TableReference.createFromFullyQualifiedName("foo.bar"), fullUpdate(0)); priorityStore.update( tx, TableReference.createFromFullyQualifiedName("qwe.rty"), fullUpdate(1)); return null; }); assertThat(txManager.runTaskReadOnly(priorityStore::loadNewPriorities)) .containsExactlyInAnyOrder(priority("foo.bar", 0), priority("qwe.rty", 1)); txManager.runTaskWithRetry(tx -> { priorityStore.delete(tx, ImmutableList.of(TableReference.createFromFullyQualifiedName("foo.bar"))); return null; }); Assert.assertEquals( ImmutableList.of(priority("qwe.rty", 1)), txManager.runTaskReadOnly(priorityStore::loadNewPriorities)); }
@Test public void testStoreAndLoadNew() throws Exception { txManager.runTaskWithRetry(tx -> { priorityStore.update( tx, TableReference.createFromFullyQualifiedName("foo.bar"), fullUpdate(0)); priorityStore.update( tx, TableReference.createFromFullyQualifiedName("qwe.rty"), fullUpdate(1)); return null; }); Assert.assertEquals( ImmutableSet.of(priority("foo.bar", 0), priority("qwe.rty", 1)), ImmutableSet.copyOf(txManager.runTaskReadOnly(priorityStore::loadNewPriorities))); }
@Test public void testUpdateAndLoad() { long oldTs = txManager.runTaskWithRetry(tx -> { priorityStore.update( tx, TableReference.createFromFullyQualifiedName("foo.bar"), fullUpdate(0)); return tx.getTimestamp(); }); txManager.runTaskWithRetry(tx -> { priorityStore.update( tx, TableReference.createFromFullyQualifiedName("foo.bar"), fullUpdate(1)); return null; }); Assert.assertEquals( ImmutableList.of(priority("foo.bar", 1)), txManager.runTaskReadOnly(priorityStore::loadNewPriorities)); // TODO(gbonik): This currently fails because the getTimestamp override hack never worked. // We should create a ticket to track this. //Assert.assertEquals( // ImmutableList.of(priority("foo.bar", 0)), // txManager.runTaskReadOnly(tx -> priorityStore.loadOldPrioritites(tx, oldTs + 1))); }
@Override public <T, E extends Exception> T runTaskReadOnly(TransactionTask<T, E> task) throws E { return delegate().runTaskReadOnly(wrapTask(task)); }
@Test public void testLoadDefaultsIfFieldMissing() { txManager.runTaskWithRetry(tx -> { priorityStore.update( tx, TableReference.createFromFullyQualifiedName("foo.bar"), ImmutableUpdateSweepPriority.builder() .newStaleValuesDeleted(1) .build()); return null; }); Assert.assertEquals( ImmutableList.of(ImmutableSweepPriority.builder() .tableRef(TableReference.createFromFullyQualifiedName("foo.bar")) .staleValuesDeleted(1) .cellTsPairsExamined(0) .lastSweepTimeMillis(OptionalLong.empty()) .minimumSweptTimestamp(Long.MIN_VALUE) .writeCount(0) .build()), txManager.runTaskReadOnly(priorityStore::loadNewPriorities)); }
@Test public void testPartialUpdate() { txManager.runTaskWithRetry(tx -> { priorityStore.update( tx, TableReference.createFromFullyQualifiedName("foo.bar"), fullUpdate(0)); return null; }); txManager.runTaskWithRetry(tx -> { priorityStore.update( tx, TableReference.createFromFullyQualifiedName("foo.bar"), ImmutableUpdateSweepPriority.builder() .newStaleValuesDeleted(555) .build()); return null; }); Assert.assertEquals( ImmutableList.of(ImmutableSweepPriority.builder() .tableRef(TableReference.createFromFullyQualifiedName("foo.bar")) .staleValuesDeleted(555) .cellTsPairsExamined(10) .lastSweepTimeMillis(123) .minimumSweptTimestamp(456) .writeCount(5) .build()), txManager.runTaskReadOnly(priorityStore::loadNewPriorities)); }
@Test public void testWriteFailsOnReadOnly() { try { getManager().runTaskReadOnly((TransactionTask<Void, RuntimeException>) t -> { put(t, "row1", "col1", "v1"); return null; }); fail(); } catch (RuntimeException e) { // we want this to throw } }