use of com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer in project fdb-record-layer by FoundationDB.
the class ScopedInterningLayer method readReverse.
@Override
protected CompletableFuture<Optional<String>> readReverse(FDBStoreTimer timer, Long value) {
FDBRecordContext context = database.openContext();
context.setTimer(timer);
return interningLayerFuture.thenCompose(layer -> layer.readReverse(context, value)).whenComplete((ignored, th) -> context.close());
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer in project fdb-record-layer by FoundationDB.
the class RecordCursorTest method pipelineWithOuterLimits.
@ParameterizedTest(name = "pipelineWithOuterLimits [outOfBand = {0}]")
@BooleanSource
public void pipelineWithOuterLimits(boolean outOfBand) {
final RecordCursor.NoNextReason[] possibleNoNextReasons = new RecordCursor.NoNextReason[] { RecordCursor.NoNextReason.SOURCE_EXHAUSTED, outOfBand ? RecordCursor.NoNextReason.TIME_LIMIT_REACHED : RecordCursor.NoNextReason.RETURN_LIMIT_REACHED };
final List<Integer> ints = IntStream.range(0, 10).boxed().collect(Collectors.toList());
final FDBStoreTimer timer = new FDBStoreTimer();
final BiFunction<Integer, byte[], RecordCursor<Pair<Integer, Integer>>> innerFunc = (x, continuation) -> {
final RecordCursor<Integer> intCursor = RecordCursor.fromList(ints, continuation);
final RecordCursor<Integer> limitedCursor;
if (outOfBand) {
limitedCursor = new FakeOutOfBandCursor<>(intCursor, 3);
} else {
limitedCursor = intCursor.limitRowsTo(3);
}
return limitedCursor.filter(y -> y < x).map(y -> Pair.of(x, y));
};
final Function<byte[], RecordCursor<Integer>> outerFunc = continuation -> {
final RecordCursor<Integer> intCursor = RecordCursor.fromList(ints, continuation);
final RecordCursor<Integer> limitedCursor;
if (outOfBand) {
limitedCursor = new FakeOutOfBandCursor<>(intCursor, 3);
} else {
limitedCursor = intCursor.limitRowsTo(3);
}
return limitedCursor.filterInstrumented(x -> x >= 7 && x < 9, timer, FDBStoreTimer.Counts.QUERY_FILTER_GIVEN, FDBStoreTimer.Events.QUERY_FILTER, FDBStoreTimer.Counts.QUERY_FILTER_PASSED, FDBStoreTimer.Counts.QUERY_DISCARDED);
};
int results = iterateGrid(continuation -> RecordCursor.flatMapPipelined(outerFunc, innerFunc, continuation, 5), possibleNoNextReasons);
assertEquals(15, results);
// Note that as only the outer filter is instrumented, these assertions are based on only the outer filter.
// Should be:
// Itr 1: 0, 1, 2
// Itr 2: 3, 4, 5
// Itr 3: 6, 7 (0, 1, 2), 8
// Itr 4: 7 (3, 4, 5), 8, 9
// Itr 5: 7 (6, 7, 8), 8, 9
// Itr 6: 7 (9), 8 (0, 1, 2), 9
// Itr 7: 8 (3, 4, 5), 9
// Itr 8: 8 (6, 7, 8), 9
// Itr 9: 8 (9), 9
assertEquals(24, timer.getCount(FDBStoreTimer.Counts.QUERY_FILTER_GIVEN));
assertEquals(11, timer.getCount(FDBStoreTimer.Counts.QUERY_FILTER_PASSED));
assertEquals(13, timer.getCount(FDBStoreTimer.Counts.QUERY_DISCARDED));
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer in project fdb-record-layer by FoundationDB.
the class UnorderedUnionCursorTest method loopIterationWithLimit.
@Test
public void loopIterationWithLimit() throws ExecutionException, InterruptedException {
FDBStoreTimer timer = new FDBStoreTimer();
FirableCursor<Integer> secondCursor = new FirableCursor<>(RecordCursor.fromList(Arrays.asList(3, 4)));
RecordCursor<Integer> cursor = UnorderedUnionCursor.create(Arrays.asList(continuation -> RecordCursor.fromList(Arrays.asList(1, 2), continuation).limitRowsTo(1), continuation -> secondCursor), null, timer);
RecordCursorResult<Integer> cursorResult = cursor.getNext();
assertEquals(1, (int) cursorResult.get());
CompletableFuture<RecordCursorResult<Integer>> cursorResultFuture = cursor.onNext();
assertFalse(cursorResultFuture.isDone());
secondCursor.fire();
cursorResult = cursorResultFuture.get();
assertEquals(3, (int) cursorResult.get());
cursorResultFuture = cursor.onNext();
assertFalse(cursorResultFuture.isDone());
secondCursor.fire();
cursorResult = cursorResultFuture.get();
assertEquals(4, (int) cursorResult.get());
cursorResultFuture = cursor.onNext();
assertFalse(cursorResultFuture.isDone());
secondCursor.fire();
cursorResult = cursorResultFuture.get();
assertFalse(cursorResult.hasNext());
assertEquals(RecordCursor.NoNextReason.RETURN_LIMIT_REACHED, cursorResult.getNoNextReason());
assertThat(timer.getCount(FDBStoreTimer.Events.QUERY_INTERSECTION), lessThanOrEqualTo(5));
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer in project fdb-record-layer by FoundationDB.
the class ProbableIntersectionCursorTest method resumeFromContinuation.
/**
* Test that the cursor can be resumed by deserializing its state from the continuation object.
*/
@Test
public void resumeFromContinuation() {
final FDBStoreTimer timer = new FDBStoreTimer();
final List<Integer> list1 = Arrays.asList(10, 2, 5, 6, 8, 19, 0);
final List<Integer> list2 = Arrays.asList(9, 1, 3, 5, 2, 4, 8);
final List<Function<byte[], RecordCursor<Integer>>> cursorFuncs = listsToFunctions(Arrays.asList(list1, list2));
final Function<byte[], ProbableIntersectionCursor<Integer>> intersectionCursorFunction = continuation -> ProbableIntersectionCursor.create(Collections::singletonList, cursorFuncs, continuation, timer);
final Iterator<Integer> resultIterator = Iterators.forArray(5, 2, 8);
byte[] continuation = null;
boolean done = false;
List<BloomFilter<List<Object>>> lastBloomFilters = null;
while (!done) {
ProbableIntersectionCursor<Integer> intersectionCursor = intersectionCursorFunction.apply(continuation);
List<BloomFilter<List<Object>>> bloomFilters = intersectionCursor.getCursorStates().stream().map(ProbableIntersectionCursorState::getBloomFilter).collect(Collectors.toList());
if (lastBloomFilters != null) {
assertEquals(lastBloomFilters, bloomFilters);
}
lastBloomFilters = bloomFilters;
RecordCursorResult<Integer> result = intersectionCursor.getNext();
if (resultIterator.hasNext()) {
assertThat(result.hasNext(), is(true));
assertEquals(resultIterator.next(), result.get());
assertThat(result.getContinuation().isEnd(), is(false));
assertNotNull(result.getContinuation().toBytes());
} else {
assertThat(result.hasNext(), is(false));
assertEquals(RecordCursor.NoNextReason.SOURCE_EXHAUSTED, result.getNoNextReason());
assertThat(result.getContinuation().isEnd(), is(true));
assertNull(result.getContinuation().toBytes());
done = true;
}
continuation = result.getContinuation().toBytes();
}
assertEquals(3, timer.getCount(FDBStoreTimer.Counts.QUERY_INTERSECTION_PLAN_MATCHES));
assertEquals(list1.size() + list2.size() - 3, timer.getCount(FDBStoreTimer.Counts.QUERY_INTERSECTION_PLAN_NONMATCHES));
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreStateCacheTest method cacheWithVersionTracking.
/**
* Validate that caching just naturally works.
*/
@ParameterizedTest(name = "cacheWithVersionTracking (test context = {0})")
@MethodSource("testContextSource")
public void cacheWithVersionTracking(@Nonnull StateCacheTestContext testContext) throws Exception {
boolean trackCommitVersions = fdb.isTrackLastSeenVersionOnCommit();
boolean trackReadVersion = fdb.isTrackLastSeenVersionOnCommit();
FDBRecordStoreStateCache currentCache = fdb.getStoreStateCache();
try {
fdb.setStoreStateCache(testContext.getCache(fdb));
fdb.setTrackLastSeenVersion(true);
FDBStoreTimer timer = new FDBStoreTimer();
final FDBDatabase.WeakReadSemantics readSemantics = new FDBDatabase.WeakReadSemantics(0L, 5000, false);
// Load up a read version
try (FDBRecordContext context = fdb.openContext(null, timer, null)) {
context.getReadVersion();
commit(context);
}
// Commit a new meta-data
long commitVersion;
timer.reset();
try (FDBRecordContext context = fdb.openContext(null, timer, readSemantics)) {
openSimpleRecordStore(context);
recordStore.setStateCacheabilityAsync(true).get();
assertEquals(1, timer.getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS));
recordStore.markIndexDisabled("MySimpleRecord$str_value_indexed").get();
commit(context);
commitVersion = context.getCommittedVersion();
}
// Version caching will elect to use the commit version, which is not cached
timer.reset();
try (FDBRecordContext context = fdb.openContext(null, timer, readSemantics)) {
assertEquals(commitVersion, context.getReadVersion());
openSimpleRecordStore(context);
assertEquals(1, timer.getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS));
assertTrue(recordStore.isIndexDisabled("MySimpleRecord$str_value_indexed"));
// should be read only-so won't change commit version
commit(context);
}
// Version caching will still use the commit version from the first (non read-only commit), but now it is in cache
timer.reset();
try (FDBRecordContext context = fdb.openContext(null, timer, readSemantics)) {
assertEquals(commitVersion, context.getReadVersion());
openSimpleRecordStore(context);
assertEquals(1, timer.getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT));
assertTrue(recordStore.isIndexDisabled("MySimpleRecord$str_value_indexed"));
// Add a dummy write to increase the DB version
context.ensureActive().addWriteConflictKey(recordStore.recordsSubspace().pack(UUID.randomUUID()));
commit(context);
assertThat(context.getCommittedVersion(), greaterThan(commitVersion));
commitVersion = context.getCommittedVersion();
}
// The commit version will be from the commit above. This should invalidate the
// read-version cache, but not the meta-data version cache.
timer.reset();
try (FDBRecordContext context = fdb.openContext(null, timer, readSemantics)) {
assertEquals(commitVersion, context.getReadVersion());
openSimpleRecordStore(context);
if (testContext instanceof ReadVersionStateCacheTestContext) {
assertEquals(1, timer.getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS));
} else {
assertEquals(1, timer.getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT));
}
assertTrue(recordStore.isIndexDisabled("MySimpleRecord$str_value_indexed"));
// Add a dummy write to increase the DB version
context.ensureActive().addWriteConflictKey(recordStore.recordsSubspace().pack(UUID.randomUUID()));
commit(context);
assertThat(context.getCommittedVersion(), greaterThan(commitVersion));
commitVersion = context.getCommittedVersion();
}
// Load a new read version.
timer.reset();
final long readVersion;
try (FDBRecordContext context = fdb.openContext(null, timer, null)) {
readVersion = context.getReadVersion();
assertThat(readVersion, greaterThanOrEqualTo(commitVersion));
openSimpleRecordStore(context);
if (testContext instanceof ReadVersionStateCacheTestContext) {
assertEquals(1, timer.getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS));
} else {
assertEquals(1, timer.getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT));
}
assertTrue(recordStore.isIndexDisabled("MySimpleRecord$str_value_indexed"));
}
// Load the meta-data using the cached read version.
timer.reset();
try (FDBRecordContext context = fdb.openContext(null, timer, readSemantics)) {
assertEquals(readVersion, context.getReadVersion());
openSimpleRecordStore(context);
assertEquals(1, timer.getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT));
assertTrue(recordStore.isIndexDisabled("MySimpleRecord$str_value_indexed"));
}
} finally {
fdb.setTrackLastSeenVersionOnCommit(trackCommitVersions);
fdb.setTrackLastSeenVersionOnRead(trackReadVersion);
fdb.setStoreStateCache(currentCache);
}
}
Aggregations