use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class ChainedCursorTest method testHatesReverse.
@Test
public void testHatesReverse() {
// The chained cursor cannot implement a reverse scan
assertThrows(RecordCoreArgumentException.class, () -> {
FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().build(), true);
new ChainedCursor<>(context, (lastKey) -> CompletableFuture.completedFuture(Optional.of(10L)), (key) -> new byte[0], (prevContinuation) -> 10L, null, props);
}
});
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase 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));
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class LocatableResolverTest method testDirectoryCache.
@Test
public void testDirectoryCache() {
FDBDatabaseFactory factory = FDBDatabaseFactory.instance();
factory.setDirectoryCacheSize(10);
FDBStoreTimer timer = new FDBStoreTimer();
FDBDatabase fdb = factory.getDatabase();
// Make sure cache is fresh.
fdb.close();
String key = "world";
Long value;
try (FDBRecordContext context = fdb.openContext()) {
context.setTimer(timer);
value = context.asyncToSync(FDBStoreTimer.Waits.WAIT_DIRECTORY_RESOLVE, globalScope.resolve(context.getTimer(), key));
}
int initialReads = timer.getCount(FDBStoreTimer.Events.DIRECTORY_READ);
assertThat(initialReads, is(greaterThanOrEqualTo(1)));
for (int i = 0; i < 10; i++) {
try (FDBRecordContext context = fdb.openContext()) {
context.setTimer(timer);
assertThat("we continue to resolve the same value", context.asyncToSync(FDBStoreTimer.Waits.WAIT_DIRECTORY_RESOLVE, globalScope.resolve(context.getTimer(), key)), is(value));
}
}
assertEquals(timer.getCount(FDBStoreTimer.Events.DIRECTORY_READ), initialReads);
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class LocatableResolverTest method testVersionIncrementInvalidatesCache.
@Test
public void testVersionIncrementInvalidatesCache() {
FDBDatabaseFactory factory = FDBDatabaseFactory.instance();
factory.setDirectoryCacheSize(10);
FDBStoreTimer timer = new FDBStoreTimer();
FDBDatabase fdb = factory.getDatabase();
// Make sure cache is fresh, and resets version
fdb.close();
fdb.setResolverStateRefreshTimeMillis(100);
String key = "some-key";
Long value;
try (FDBRecordContext context = fdb.openContext()) {
context.setTimer(timer);
value = context.asyncToSync(FDBStoreTimer.Waits.WAIT_DIRECTORY_RESOLVE, globalScope.resolve(context.getTimer(), key));
}
assertThat(timer.getCount(FDBStoreTimer.Events.DIRECTORY_READ), is(greaterThanOrEqualTo(1)));
timer.reset();
consistently("we hit the cached value", () -> {
try (FDBRecordContext context = fdb.openContext()) {
context.setTimer(timer);
assertThat("the resolved value is still the same", globalScope.resolve(context.getTimer(), key).join(), is(value));
}
return timer.getCount(FDBStoreTimer.Events.DIRECTORY_READ);
}, is(0), 200, 10);
globalScope.incrementVersion().join();
timer.reset();
eventually("we see the version change and invalidate the cache", () -> {
try (FDBRecordContext context = fdb.openContext()) {
context.setTimer(timer);
assertThat("the resolved value is still the same", globalScope.resolve(context.getTimer(), key).join(), is(value));
}
return timer.getCount(FDBStoreTimer.Events.DIRECTORY_READ);
}, is(1), 120, 10);
timer.reset();
consistently("the value is cached while the version is not changed", () -> {
try (FDBRecordContext context = fdb.openContext()) {
context.setTimer(timer);
assertThat("the resolved value is still the same", globalScope.resolve(context.getTimer(), key).join(), is(value));
}
return timer.getCount(FDBStoreTimer.Events.DIRECTORY_READ);
}, is(0), 200, 10);
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class LocatableResolverTest method testParallelDbAndScopeGetVersion.
@Test
public void testParallelDbAndScopeGetVersion() {
// version is cached for 30 seconds by default
database.setResolverStateRefreshTimeMillis(100);
// sets the timeout for all the db instances we create
final FDBDatabaseFactory parallelFactory = new FDBDatabaseFactoryImpl();
parallelFactory.setStateRefreshTimeMillis(100);
Supplier<FDBDatabase> databaseSupplier = () -> new FDBDatabase(parallelFactory, null);
consistently("uninitialized version is 0", () -> {
try (FDBRecordContext context = database.openContext()) {
return globalScope.getVersion(context.getTimer()).join();
}
}, is(0), 200, 10);
List<Pair<FDBDatabase, LocatableResolver>> simulatedInstances = IntStream.range(0, 20).mapToObj(i -> {
FDBDatabase db = databaseSupplier.get();
return Pair.of(db, resolverFactory.getGlobalScope(db));
}).collect(Collectors.toList());
Supplier<CompletableFuture<Set<Integer>>> supplier = () -> {
List<CompletableFuture<Integer>> parallelOperations = simulatedInstances.stream().map(pair -> {
FDBDatabase db = pair.getKey();
LocatableResolver resolver = pair.getValue();
FDBRecordContext context = db.openContext();
return resolver.getVersion(context.getTimer()).whenComplete((ignore, e) -> context.close());
}).collect(Collectors.toList());
return AsyncUtil.getAll(parallelOperations).thenApply(HashSet::new);
};
consistently("all instances report the version as 0", () -> supplier.get().join(), is(Collections.singleton(0)), 200, 10);
globalScope.incrementVersion().join();
eventually("all instances report the new version once the caches have refreshed", () -> supplier.get().join(), is(Collections.singleton(1)), 120, 10);
}
Aggregations