Search in sources :

Example 1 with FDBDatabaseRunner

use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseRunner in project fdb-record-layer by FoundationDB.

the class AutoContinuingCursorTest method testAutoContinuingCursorGivenCursorGenerator.

private void testAutoContinuingCursorGivenCursorGenerator(BiFunction<FDBRecordContext, byte[], RecordCursor<Integer>> nextCursorGenerator, int maxRetriesOnRetriableException, List<Integer> expectedList) {
    try (FDBDatabaseRunner runner = database.newRunner()) {
        RecordCursor<Integer> cursor = new AutoContinuingCursor<>(runner, nextCursorGenerator, maxRetriesOnRetriableException);
        List<Integer> returnedList = cursor.asList().join();
        assertEquals(expectedList, returnedList);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FDBDatabaseRunner(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseRunner)

Example 2 with FDBDatabaseRunner

use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseRunner in project fdb-record-layer by FoundationDB.

the class LocatableResolver method runAsyncBorrowingReadVersion.

@Nonnull
private <T> CompletableFuture<T> runAsyncBorrowingReadVersion(@Nonnull FDBRecordContext parentContext, @Nonnull Function<FDBRecordContext, CompletableFuture<T>> retriable, Object... additionalLogMessageKeyValues) {
    final FDBDatabaseRunner runner = parentContext.newRunner();
    boolean started = false;
    try {
        final AtomicBoolean first = new AtomicBoolean(true);
        CompletableFuture<T> future = runner.runAsync(childContext -> {
            if (parentContext.isClosed()) {
                // If the parent context is closed, stop this runner, too
                throw new FDBDatabaseRunner.RunnerClosed();
            }
            final CompletableFuture<Long> readVersionFuture;
            if (first.get()) {
                // The first time around, borrow a read version
                first.set(false);
                readVersionFuture = parentContext.getReadVersionAsync().thenApply(readVersion -> {
                    childContext.setReadVersion(readVersion);
                    return readVersion;
                });
            } else {
                // Other times, get a new read version from the database (explicit here for instrumentation purposes)
                readVersionFuture = childContext.getReadVersionAsync();
            }
            return readVersionFuture.thenCompose(ignore -> retriable.apply(childContext));
        }, Arrays.asList(additionalLogMessageKeyValues));
        started = true;
        return future.whenComplete((valIgnore, errIgnore) -> runner.close());
    } finally {
        if (!started) {
            runner.close();
        }
    }
}
Also used : Arrays(java.util.Arrays) LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) LazyCursor(com.apple.foundationdb.record.cursors.LazyCursor) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) Subspace(com.apple.foundationdb.subspace.Subspace) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) ScanProperties(com.apple.foundationdb.record.ScanProperties) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ByteArrayUtil2(com.apple.foundationdb.tuple.ByteArrayUtil2) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Logger(org.slf4j.Logger) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) KeyValueCursor(com.apple.foundationdb.record.provider.foundationdb.KeyValueCursor) List(java.util.List) RecordCursor(com.apple.foundationdb.record.RecordCursor) Optional(java.util.Optional) API(com.apple.foundationdb.annotation.API) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FDBDatabaseRunner(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseRunner) Cache(com.google.common.cache.Cache) ResolverStateProto(com.apple.foundationdb.record.ResolverStateProto) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FDBDatabaseRunner(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseRunner) Nonnull(javax.annotation.Nonnull)

Aggregations

FDBDatabaseRunner (com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseRunner)2 API (com.apple.foundationdb.annotation.API)1 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)1 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)1 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 RecordCursor (com.apple.foundationdb.record.RecordCursor)1 ResolverStateProto (com.apple.foundationdb.record.ResolverStateProto)1 ScanProperties (com.apple.foundationdb.record.ScanProperties)1 LazyCursor (com.apple.foundationdb.record.cursors.LazyCursor)1 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)1 LogMessageKeys (com.apple.foundationdb.record.logging.LogMessageKeys)1 FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)1 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)1 FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)1 KeyValueCursor (com.apple.foundationdb.record.provider.foundationdb.KeyValueCursor)1 Subspace (com.apple.foundationdb.subspace.Subspace)1 ByteArrayUtil2 (com.apple.foundationdb.tuple.ByteArrayUtil2)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Cache (com.google.common.cache.Cache)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1