Search in sources :

Example 1 with STRING

use of com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType.STRING in project fdb-record-layer by FoundationDB.

the class ExtendedDirectoryLayerTest method testDefaultCanAllocateIndependentKeysInParallel.

@Test
public void testDefaultCanAllocateIndependentKeysInParallel() {
    final ScopedDirectoryLayer directoryLayer = ScopedDirectoryLayer.global(database);
    Map<String, Long> mappingsFromOld = new ConcurrentHashMap<>();
    Map<String, Long> mappingsFromNew = new ConcurrentHashMap<>();
    List<CompletableFuture<?>> operations = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        String oldDirLayerKey = i + "-old-dl-key";
        String newDirLayerKey = i + "-new-dl-key";
        operations.add(directoryLayer.resolve(oldDirLayerKey).thenApply(value -> mappingsFromOld.put(oldDirLayerKey, value)));
        operations.add(globalScope.resolve(newDirLayerKey).thenApply(value -> mappingsFromNew.put(newDirLayerKey, value)));
    }
    CompletableFuture.allOf(operations.toArray(new CompletableFuture<?>[0])).join();
    // Implicitly checks that there are no duplicate keys or values in the two maps
    BiMap<String, Long> completeMapping = ImmutableBiMap.<String, Long>builder().putAll(mappingsFromOld).putAll(mappingsFromNew).build();
    for (Map.Entry<String, Long> entry : completeMapping.entrySet()) {
        try (FDBRecordContext context = database.openContext()) {
            assertThat("the FDB directory layer sees the mapping", directoryLayer.mustResolve(context, entry.getKey()).join(), is(entry.getValue()));
            assertThat("the ScopedDirectoryLayer sees the mapping", globalScope.mustResolve(context, entry.getKey()).join(), is(entry.getValue()));
        }
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MetadataHook(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Tuple(com.apple.foundationdb.tuple.Tuple) STRING(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType.STRING) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BiMap(com.google.common.collect.BiMap) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Example 2 with STRING

use of com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType.STRING in project fdb-record-layer by FoundationDB.

the class ExtendedDirectoryLayerTest method testParallelAllocation.

private static void testParallelAllocation(boolean checkDirectoryLayer, FDBDatabase database, Function<String, CompletableFuture<Long>> resolveCall1, Function<String, CompletableFuture<Long>> resolveCall2, LocatableResolver resolver1, LocatableResolver resolver2) {
    Map<String, Long> mappings1 = new ConcurrentHashMap<>();
    Map<String, Long> mappings2 = new ConcurrentHashMap<>();
    List<CompletableFuture<?>> operations = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        String key = "key-" + i;
        operations.add(resolveCall1.apply(key).thenAccept(value -> mappings1.put(key, value)));
        operations.add(resolveCall2.apply(key).thenAccept(value -> mappings2.put(key, value)));
    }
    CompletableFuture.allOf(operations.toArray(new CompletableFuture<?>[0])).join();
    for (Map.Entry<String, Long> entry : mappings1.entrySet()) {
        assertThat(String.format("the mappings for %s are identical", entry.getKey()), mappings2.get(entry.getKey()), is(entry.getValue()));
        try (FDBRecordContext context = database.openContext()) {
            if (checkDirectoryLayer) {
                // only applies when the scope is global
                final DirectoryLayer directoryLayer = DirectoryLayer.getDefault();
                Long value = directoryLayer.open(context.ensureActive(), Collections.singletonList(entry.getKey())).thenApply(subspace -> Tuple.fromBytes(subspace.pack()).getLong(0)).join();
                assertThat("the FDB directory layer sees the mapping", value, is(entry.getValue()));
            }
            assertThat(resolver1.getClass().getName() + " sees the mapping", resolver1.mustResolve(context, entry.getKey()).join(), is(entry.getValue()));
            assertThat(resolver2.getClass().getName() + " sees the mapping", resolver2.mustResolve(context, entry.getKey()).join(), is(entry.getValue()));
            checkMappingInReverseCache(resolver1, entry.getKey(), entry.getValue());
            checkMappingInReverseCache(resolver2, entry.getKey(), entry.getValue());
        }
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MetadataHook(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Tuple(com.apple.foundationdb.tuple.Tuple) STRING(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType.STRING) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BiMap(com.google.common.collect.BiMap) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

AsyncUtil (com.apple.foundationdb.async.AsyncUtil)2 DirectoryLayer (com.apple.foundationdb.directory.DirectoryLayer)2 FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)2 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)2 FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)2 STRING (com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType.STRING)2 MetadataHook (com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook)2 Tuple (com.apple.foundationdb.tuple.Tuple)2 BiMap (com.google.common.collect.BiMap)2 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Function (java.util.function.Function)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2