Search in sources :

Example 16 with FDBStoreTimer

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

the class TextIndexTest method getTimer.

@Nonnull
private static FDBStoreTimer getTimer(@Nonnull FDBRecordStore recordStore) {
    final FDBStoreTimer timer = recordStore.getTimer();
    assertNotNull(timer, "store has not been initialized with a timer");
    return timer;
}
Also used : FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Nonnull(javax.annotation.Nonnull)

Example 17 with FDBStoreTimer

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

the class ProbableIntersectionCursorTest method basicIntersection.

/**
 * Show that a basic intersection succeeds.
 */
@Test
public void basicIntersection() {
    final FDBStoreTimer timer = new FDBStoreTimer();
    final Iterator<Integer> iterator1 = IntStream.iterate(0, x -> x + 2).limit(150).iterator();
    final Iterator<Integer> iterator2 = IntStream.iterate(0, x -> x + 3).limit(100).iterator();
    final FirableCursor<Integer> cursor1 = new FirableCursor<>(RecordCursor.fromIterator(iterator1));
    final FirableCursor<Integer> cursor2 = new FirableCursor<>(RecordCursor.fromIterator(iterator2));
    final RecordCursor<Integer> intersectionCursor = ProbableIntersectionCursor.create(Collections::singletonList, Arrays.asList(bignore -> cursor1, bignore -> cursor2), null, timer);
    // Intersection consumes first cursor
    cursor1.fireAll();
    CompletableFuture<RecordCursorResult<Integer>> firstFuture = intersectionCursor.onNext();
    cursor2.fire();
    RecordCursorResult<Integer> firstResult = firstFuture.join();
    assertEquals(0, (int) firstResult.get());
    assertThat(firstResult.hasNext(), is(true));
    assertEquals(cursor1.getNext().getNoNextReason(), RecordCursor.NoNextReason.SOURCE_EXHAUSTED);
    // Intersection consumes second cursor as they come
    cursor2.fireAll();
    AtomicInteger falsePositives = new AtomicInteger();
    AsyncUtil.whileTrue(() -> intersectionCursor.onNext().thenApply(result -> {
        if (result.hasNext()) {
            int value = result.get();
            // every result *must* be divisible by 3
            assertEquals(0, value % 3);
            if (value % 2 != 0) {
                // most results should be divisible by 2
                falsePositives.incrementAndGet();
            }
            assertThat(result.getContinuation().isEnd(), is(false));
            assertNotNull(result.getContinuation().toBytes());
            try {
                RecordCursorProto.ProbableIntersectionContinuation protoContinuation = RecordCursorProto.ProbableIntersectionContinuation.parseFrom(result.getContinuation().toBytes());
                assertEquals(2, protoContinuation.getChildStateCount());
                assertThat(protoContinuation.getChildState(0).getExhausted(), is(true));
                assertThat(protoContinuation.getChildState(0).hasContinuation(), is(false));
                assertThat(protoContinuation.getChildState(1).getExhausted(), is(false));
                assertThat(protoContinuation.getChildState(1).hasContinuation(), is(true));
            } catch (InvalidProtocolBufferException e) {
                throw new RecordCoreException("error parsing proto continuation", e);
            }
        } else {
            assertThat(result.getNoNextReason().isSourceExhausted(), is(true));
            assertThat(result.getContinuation().isEnd(), is(true));
            assertNull(result.getContinuation().toBytes());
        }
        return result.hasNext();
    }), intersectionCursor.getExecutor()).join();
    assertThat(falsePositives.get(), lessThan(5));
    assertEquals(50 + falsePositives.get(), timer.getCount(FDBStoreTimer.Counts.QUERY_INTERSECTION_PLAN_MATCHES));
    assertEquals(200 - falsePositives.get(), timer.getCount(FDBStoreTimer.Counts.QUERY_INTERSECTION_PLAN_NONMATCHES));
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) RowLimitedCursor(com.apple.foundationdb.record.cursors.RowLimitedCursor) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) HashSet(java.util.HashSet) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) TestLogMessageKeys(com.apple.foundationdb.record.logging.TestLogMessageKeys) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FirableCursor(com.apple.foundationdb.record.cursors.FirableCursor) Matchers.lessThan(org.hamcrest.Matchers.lessThan) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) BloomFilter(com.google.common.hash.BloomFilter) Set(java.util.Set) Collectors(java.util.stream.Collectors) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Stream(java.util.stream.Stream) RecordCursorProto(com.apple.foundationdb.record.RecordCursorProto) RecordCursor(com.apple.foundationdb.record.RecordCursor) RecordCursorTest(com.apple.foundationdb.record.RecordCursorTest) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) FirableCursor(com.apple.foundationdb.record.cursors.FirableCursor) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Collections(java.util.Collections) RecordCursorProto(com.apple.foundationdb.record.RecordCursorProto) Test(org.junit.jupiter.api.Test) RecordCursorTest(com.apple.foundationdb.record.RecordCursorTest)

Example 18 with FDBStoreTimer

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

the class ProbableIntersectionCursorTest method loopIterationWithLimit.

@Test
public void loopIterationWithLimit() throws ExecutionException, InterruptedException {
    FDBStoreTimer timer = new FDBStoreTimer();
    FirableCursor<Integer> secondCursor = new FirableCursor<>(RecordCursor.fromList(Arrays.asList(2, 1)));
    RecordCursor<Integer> cursor = ProbableIntersectionCursor.create(Collections::singletonList, Arrays.asList(continuation -> RecordCursor.fromList(Arrays.asList(1, 2), continuation).limitRowsTo(1), continuation -> secondCursor), null, timer);
    CompletableFuture<RecordCursorResult<Integer>> cursorResultFuture = cursor.onNext();
    secondCursor.fire();
    assertFalse(cursorResultFuture.isDone());
    secondCursor.fire();
    RecordCursorResult<Integer> cursorResult = cursorResultFuture.get();
    assertEquals(1, (int) cursorResult.get());
    secondCursor.fire();
    cursorResult = cursor.getNext();
    assertEquals(RecordCursor.NoNextReason.RETURN_LIMIT_REACHED, cursorResult.getNoNextReason());
    assertThat(timer.getCount(FDBStoreTimer.Events.QUERY_INTERSECTION), lessThanOrEqualTo(5));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) RowLimitedCursor(com.apple.foundationdb.record.cursors.RowLimitedCursor) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) HashSet(java.util.HashSet) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) TestLogMessageKeys(com.apple.foundationdb.record.logging.TestLogMessageKeys) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FirableCursor(com.apple.foundationdb.record.cursors.FirableCursor) Matchers.lessThan(org.hamcrest.Matchers.lessThan) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) BloomFilter(com.google.common.hash.BloomFilter) Set(java.util.Set) Collectors(java.util.stream.Collectors) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Stream(java.util.stream.Stream) RecordCursorProto(com.apple.foundationdb.record.RecordCursorProto) RecordCursor(com.apple.foundationdb.record.RecordCursor) RecordCursorTest(com.apple.foundationdb.record.RecordCursorTest) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) FirableCursor(com.apple.foundationdb.record.cursors.FirableCursor) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) Collections(java.util.Collections) Test(org.junit.jupiter.api.Test) RecordCursorTest(com.apple.foundationdb.record.RecordCursorTest)

Example 19 with FDBStoreTimer

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

the class UnionIntersectionTest method nonIntersectingReasons.

@Test
public void nonIntersectingReasons() {
    final List<Integer> leftList = Arrays.asList(0, 2, 4, 6);
    final Function<byte[], RecordCursor<Integer>> left = continuation -> RecordCursor.fromList(leftList, continuation).limitRowsTo(1);
    final List<Integer> rightList = Arrays.asList(1, 3, 5, 7);
    final Function<byte[], RecordCursor<Integer>> right = continuation -> RecordCursor.fromList(rightList, continuation).limitRowsTo(1);
    FDBStoreTimer timer = new FDBStoreTimer();
    boolean done = false;
    byte[] continuation = null;
    List<Integer> results = new ArrayList<>();
    while (!done) {
        IntersectionCursor<Integer> intersectionCursor = IntersectionCursor.create(Collections::singletonList, false, left, right, continuation, timer);
        intersectionCursor.forEach(results::add).join();
        RecordCursorResult<Integer> noNextResult = intersectionCursor.getNext();
        done = noNextResult.getNoNextReason().isSourceExhausted();
        continuation = noNextResult.getContinuation().toBytes();
        if (!done) {
            assertEquals(RecordCursor.NoNextReason.RETURN_LIMIT_REACHED, noNextResult.getNoNextReason());
        }
    }
    assertEquals(Collections.emptyList(), results);
    System.out.println(timer.getKeysAndValues());
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) FDBRecord(com.apple.foundationdb.record.provider.foundationdb.FDBRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) EndpointType(com.apple.foundationdb.record.EndpointType) ScanProperties(com.apple.foundationdb.record.ScanProperties) PipelineOperation(com.apple.foundationdb.record.PipelineOperation) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FDBStoredRecord(com.apple.foundationdb.record.provider.foundationdb.FDBStoredRecord) ValueSource(org.junit.jupiter.params.provider.ValueSource) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) LongStream(java.util.stream.LongStream) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Tags(com.apple.test.Tags) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) FDBRecordStoreTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase) Test(org.junit.jupiter.api.Test) PrimitiveIterator(java.util.PrimitiveIterator) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Index(com.apple.foundationdb.record.metadata.Index) TestHelpers.assertDiscardedAtMost(com.apple.foundationdb.record.TestHelpers.assertDiscardedAtMost) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) RecordCursorTest(com.apple.foundationdb.record.RecordCursorTest) Collections(java.util.Collections) RecordCursor(com.apple.foundationdb.record.RecordCursor) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) ArrayList(java.util.ArrayList) Collections(java.util.Collections) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) RecordCursorTest(com.apple.foundationdb.record.RecordCursorTest)

Example 20 with FDBStoreTimer

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

the class LocatableResolverTest method testResolveUseCacheCommits.

@Test
public void testResolveUseCacheCommits() {
    FDBDatabaseFactory factory = FDBDatabaseFactory.instance();
    factory.setDirectoryCacheSize(10);
    FDBStoreTimer timer = new FDBStoreTimer();
    String key = "hello " + UUID.randomUUID();
    FDBDatabase fdb = factory.getDatabase();
    assertEquals(0, timer.getCount(FDBStoreTimer.Events.COMMIT));
    try (FDBRecordContext context = fdb.openContext()) {
        context.setTimer(timer);
        context.asyncToSync(FDBStoreTimer.Waits.WAIT_DIRECTORY_RESOLVE, globalScope.resolve(context.getTimer(), key));
    }
    // initial resolve may commit twice, once for the key and once to initialize the reverse directory cache
    assertThat(timer.getCount(FDBStoreTimer.Events.COMMIT), is(greaterThanOrEqualTo(1)));
    timer.reset();
    try (FDBRecordContext context = fdb.openContext()) {
        context.setTimer(timer);
        context.asyncToSync(FDBStoreTimer.Waits.WAIT_DIRECTORY_RESOLVE, globalScope.resolve(context.getTimer(), "a-new-key"));
    }
    assertEquals(1, timer.getCount(FDBStoreTimer.Events.COMMIT));
    timer.reset();
    assertEquals(0, timer.getCount(FDBStoreTimer.Events.COMMIT));
    try (FDBRecordContext context = fdb.openContext()) {
        context.setTimer(timer);
        context.asyncToSync(FDBStoreTimer.Waits.WAIT_DIRECTORY_RESOLVE, globalScope.resolve(context.getTimer(), key));
    }
    assertEquals(0, timer.getCount(FDBStoreTimer.Events.COMMIT));
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)29 Test (org.junit.jupiter.api.Test)21 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)14 FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)9 Nonnull (javax.annotation.Nonnull)9 Arrays (java.util.Arrays)8 List (java.util.List)8 Function (java.util.function.Function)8 Collections (java.util.Collections)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 Collectors (java.util.stream.Collectors)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)7 RecordCursor (com.apple.foundationdb.record.RecordCursor)6 FirableCursor (com.apple.foundationdb.record.cursors.FirableCursor)6 StoreTimer (com.apple.foundationdb.record.provider.common.StoreTimer)6 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)6 Iterator (java.util.Iterator)6 ExecutionException (java.util.concurrent.ExecutionException)6