com.palantir.atlasdb.transaction.api.TransactionManager.runTaskReadOnly()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(98)

本文整理了Java中com.palantir.atlasdb.transaction.api.TransactionManager.runTaskReadOnly()方法的一些代码示例,展示了TransactionManager.runTaskReadOnly()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TransactionManager.runTaskReadOnly()方法的具体详情如下:
包路径:com.palantir.atlasdb.transaction.api.TransactionManager
类名称:TransactionManager
方法名:runTaskReadOnly

TransactionManager.runTaskReadOnly介绍

[英]This will open and run a read-only transaction. Read-only transactions are similar to other transactions, but will throw if any write operations are called. Furthermore, they often make fewer network calls than their read/write counterparts so should be used where possible.
[中]这将打开并运行只读事务。只读事务与其他事务类似,但在调用任何写操作时都会抛出。此外,它们通常比读/写对应的网络调用更少,因此应尽可能使用它们。

代码示例

代码示例来源:origin: palantir/atlasdb

Optional<String> selectTableToCompact() {
  return transactionManager.runTaskReadOnly(this::selectTableToCompactInternal);
}

代码示例来源:origin: palantir/atlasdb

@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);
  }
}

代码示例来源:origin: palantir/atlasdb

@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;
    });
  }
}

代码示例来源:origin: palantir/atlasdb

@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());
}

代码示例来源:origin: palantir/atlasdb

@Override
public <T, E extends Exception> T runTaskReadOnly(TransactionTask<T, E> task) throws E {
  return delegate().runTaskReadOnly(wrapTask(task));
}

代码示例来源:origin: palantir/atlasdb

public Optional<Long> get() {
  return transactionManager.runTaskReadOnly((transaction) -> new CheckAndSetPersistentValue(transaction).get());
}

代码示例来源:origin: palantir/atlasdb

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);
  });
}

代码示例来源:origin: palantir/atlasdb

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;
}

代码示例来源:origin: palantir/atlasdb

@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
  }
}

代码示例来源:origin: palantir/atlasdb

@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.");
}

代码示例来源:origin: palantir/atlasdb

@Test
public void testLoadEmpty() {
  Assert.assertTrue(txManager.runTaskReadOnly(
      tx -> priorityStore.loadOldPriorities(tx, tx.getTimestamp())).isEmpty());
  Assert.assertTrue(txManager.runTaskReadOnly(
      tx -> priorityStore.loadNewPriorities(tx)).isEmpty());
}

代码示例来源:origin: palantir/atlasdb

@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);
}

代码示例来源:origin: palantir/atlasdb

@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)));
}

代码示例来源:origin: palantir/atlasdb

@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));
}

代码示例来源:origin: palantir/atlasdb

@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)));
}

代码示例来源:origin: palantir/atlasdb

@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)));
}

代码示例来源:origin: com.palantir.atlasdb/atlasdb-impl-shared

@Override
public <T, E extends Exception> T runTaskReadOnly(TransactionTask<T, E> task) throws E {
  return delegate().runTaskReadOnly(wrapTask(task));
}

代码示例来源:origin: palantir/atlasdb

@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));
}

代码示例来源:origin: palantir/atlasdb

@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));
}

代码示例来源:origin: com.palantir.atlasdb/atlasdb-tests-shared

@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
  }
}

相关文章