Search in sources :

Example 16 with FDBDatabase

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);
        }
    });
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 17 with FDBDatabase

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

Example 18 with FDBDatabase

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

Example 19 with FDBDatabase

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

Example 20 with FDBDatabase

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);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) BiFunction(java.util.function.BiFunction) Matchers.not(org.hamcrest.Matchers.not) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) LocatableResolverLockedException(com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver.LocatableResolverLockedException) TestHelpers.consistently(com.apple.foundationdb.record.TestHelpers.consistently) Matchers.hasKey(org.hamcrest.Matchers.hasKey) ExceptionMessageMatcher.hasMessageContaining(com.apple.foundationdb.record.TestHelpers.ExceptionMessageMatcher.hasMessageContaining) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Tuple(com.apple.foundationdb.tuple.Tuple) Pair(org.apache.commons.lang3.tuple.Pair) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) Tag(org.junit.jupiter.api.Tag) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Matchers.allOf(org.hamcrest.Matchers.allOf) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FDBRecordContextConfig(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContextConfig) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) CacheBuilder(com.google.common.cache.CacheBuilder) DEFAULT_CHECK(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.DEFAULT_CHECK) CacheStats(com.google.common.cache.CacheStats) FDBDatabaseFactoryImpl(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactoryImpl) IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) MetadataHook(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Supplier(java.util.function.Supplier) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) TestHelpers.eventually(com.apple.foundationdb.record.TestHelpers.eventually) KeyType(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType) ImmutableList(com.google.common.collect.ImmutableList) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ScanProperties(com.apple.foundationdb.record.ScanProperties) BooleanSource(com.apple.test.BooleanSource) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) MoreAsyncUtil(com.apple.foundationdb.async.MoreAsyncUtil) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Tags(com.apple.test.Tags) TimeUnit(java.util.concurrent.TimeUnit) DEFAULT_HOOK(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.DEFAULT_HOOK) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) PreWriteCheck(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.PreWriteCheck) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) Cache(com.google.common.cache.Cache) Collections(java.util.Collections) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) FDBDatabaseFactoryImpl(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactoryImpl) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)42 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)39 Test (org.junit.jupiter.api.Test)38 Tuple (com.apple.foundationdb.tuple.Tuple)19 FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)14 Transaction (com.apple.foundationdb.Transaction)12 FDBDatabaseFactory (com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory)11 ScanProperties (com.apple.foundationdb.record.ScanProperties)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 List (java.util.List)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)7 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)7 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)7 RecordCursor (com.apple.foundationdb.record.RecordCursor)7 FDBTestBase (com.apple.foundationdb.record.provider.foundationdb.FDBTestBase)7 Tags (com.apple.test.Tags)7