Search in sources :

Example 41 with Transaction

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

the class FDBReverseDirectoryCacheTest method testUniqueCachePerDatabase.

@Test
@Tag(Tags.WipesFDB)
@Disabled("this got broken while it was disabled, I think")
public void testUniqueCachePerDatabase() throws Exception {
    final Pair<String, Long>[] initialEntries = createRandomDirectoryEntries(fdb, 3);
    FDBReverseDirectoryCache cache = fdb.getReverseDirectoryCache();
    fdb.clearForwardDirectoryCache();
    // Populate the cache
    for (Pair<String, Long> pair : initialEntries) {
        assertEquals(pair.getLeft(), cache.get(globalScope.wrap(pair.getRight())).get());
    }
    assertEquals(initialEntries.length, cache.getPersistentCacheHitCount());
    // Ensure that the cache is populated
    for (Pair<String, Long> pair : initialEntries) {
        assertEquals(pair.getLeft(), cache.get(globalScope.wrap(pair.getRight())).get());
    }
    assertEquals(0, cache.getPersistentCacheMissCount());
    // Wipe FDB!!!!!
    try (FDBRecordContext ctx = fdb.openContext()) {
        Transaction tr = ctx.ensureActive();
        tr.clear(new byte[] { (byte) 0x00 }, new byte[] { (byte) 0xff });
        commit(ctx);
    }
    // Force the creation of a new FDB instance
    FDBDatabaseFactory.instance().clear();
    // Get a fresh new one
    fdb = FDBDatabaseFactory.instance().getDatabase();
    cache = fdb.getReverseDirectoryCache();
    // In the hopes to ensure that re-creating the entries that we previously created
    // will result in new id's, create some initial filler entries.
    final Pair<String, Long>[] fillerEntries = createRandomDirectoryEntries(fdb, 10);
    // Just make sure the filler entries are populated
    for (Pair<String, Long> pair : fillerEntries) {
        assertEquals(pair.getLeft(), cache.get(globalScope.wrap(pair.getRight())).get());
    }
    assertEquals(fillerEntries.length, cache.getPersistentCacheHitCount());
    // Re-create the initial entries again
    final Pair<String, Long>[] newEntries;
    try (FDBRecordContext context = fdb.openContext()) {
        List<String> keys = new ArrayList<>();
        List<CompletableFuture<Long>> futures = Arrays.stream(initialEntries).map(entry -> {
            keys.add(entry.getLeft());
            return entry.getLeft();
        }).map(key -> globalScope.resolve(context.getTimer(), key)).collect(Collectors.toList());
        List<Long> values = AsyncUtil.getAll(futures).get();
        newEntries = zipKeysAndValues(keys, values);
        commit(context);
    }
    for (int i = 0; i < newEntries.length; i++) {
        Pair<String, Long> initialEntry = initialEntries[i];
        Pair<String, Long> newEntry = newEntries[i];
        assertEquals(initialEntry.getLeft(), newEntry.getRight());
        assertNotEquals(initialEntry.getLeft(), newEntry.getRight());
        assertEquals(newEntry.getLeft(), cache.get(globalScope.wrap(newEntry.getRight())).get());
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ScopedValue(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedValue) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Random(java.util.Random) Disabled(org.junit.jupiter.api.Disabled) LocatableResolver(com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver) Transaction(com.apple.foundationdb.Transaction) Tuple(com.apple.foundationdb.tuple.Tuple) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) TestHelpers(com.apple.foundationdb.record.TestHelpers) Pair(org.apache.commons.lang3.tuple.Pair) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Map(java.util.Map) Tag(org.junit.jupiter.api.Tag) BiMap(com.google.common.collect.BiMap) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) ResolvedKeySpacePath(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolvedKeySpacePath) IntStream(java.util.stream.IntStream) Assertions.fail(org.junit.jupiter.api.Assertions.fail) KeySpace(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpace) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) Logger(org.slf4j.Logger) KeySpaceDirectory(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory) Iterator(java.util.Iterator) Executor(java.util.concurrent.Executor) Tags(com.apple.test.Tags) Semaphore(java.util.concurrent.Semaphore) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ExecutionException(java.util.concurrent.ExecutionException) PathUtil(com.apple.foundationdb.directory.PathUtil) ForkJoinPool(java.util.concurrent.ForkJoinPool) ScopedDirectoryLayer(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedDirectoryLayer) Collections(java.util.Collections) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) Transaction(com.apple.foundationdb.Transaction) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag) Disabled(org.junit.jupiter.api.Disabled)

Example 42 with Transaction

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

the class FDBRecordStoreRepairTest method mutateRecordKeys.

private void mutateRecordKeys(Function<Tuple, Tuple> mutator) throws Exception {
    try (FDBRecordContext context = openContext()) {
        final Transaction tr = context.ensureActive();
        openUnsplitRecordStore(context);
        RecordCursor<KeyValue> cursor = RecordCursor.fromIterator(tr.getRange(recordStore.recordsSubspace().range()).iterator());
        cursor.forEach(keyValue -> {
            Tuple keyTuple = Tuple.fromBytes(keyValue.getKey());
            long suffix = keyTuple.getLong(keyTuple.size() - 1);
            // Skip record versions
            if (suffix != SplitHelper.RECORD_VERSION) {
                Tuple mutatedKey = mutator.apply(keyTuple);
                if (!mutatedKey.equals(keyTuple)) {
                    tr.clear(keyValue.getKey());
                    tr.set(mutatedKey.pack(), keyValue.getValue());
                }
            }
        }).get();
        commit(context);
    }
}
Also used : IsolationLevel(com.apple.foundationdb.record.IsolationLevel) KeyValue(com.apple.foundationdb.KeyValue) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Tags(com.apple.test.Tags) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Key(com.apple.foundationdb.record.metadata.Key) Test(org.junit.jupiter.api.Test) Transaction(com.apple.foundationdb.Transaction) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) Tuple(com.apple.foundationdb.tuple.Tuple) List(java.util.List) TestHelpers(com.apple.foundationdb.record.TestHelpers) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScanProperties(com.apple.foundationdb.record.ScanProperties) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) KeyValue(com.apple.foundationdb.KeyValue) Transaction(com.apple.foundationdb.Transaction) Tuple(com.apple.foundationdb.tuple.Tuple)

Example 43 with Transaction

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

the class FDBReverseDirectoryCacheTest method runParallelCodeOnEmptyDB.

private void runParallelCodeOnEmptyDB(int parallelism, TestHelpers.DangerousRunnable setup, TestHelpers.DangerousConsumer<Semaphore> parallelCode) throws Exception {
    // Wipe FDB!!!!!
    try (FDBRecordContext ctx = fdb.openContext()) {
        Transaction tr = ctx.ensureActive();
        tr.clear(new byte[] { (byte) 0x00 }, new byte[] { (byte) 0xff });
        commit(ctx);
    }
    // Force the creation of a new FDB instance
    FDBDatabaseFactory.instance().clear();
    // Get a fresh new one
    fdb = FDBDatabaseFactory.instance().getDatabase();
    final Executor executor = new ForkJoinPool(parallelism + 1);
    final Semaphore lock = new Semaphore(parallelism);
    setup.run();
    final List<CompletableFuture<Exception>> futures = IntStream.range(0, parallelism).mapToObj(k -> CompletableFuture.supplyAsync(() -> {
        try {
            parallelCode.accept(lock);
            return null;
        } catch (Exception e) {
            return e;
        }
    }, executor)).collect(Collectors.toList());
    lock.release(parallelism);
    final List<Exception> exceptions = AsyncUtil.getAll(futures).get();
    exceptions.removeIf(Objects::isNull);
    exceptions.forEach(Throwable::printStackTrace);
    if (exceptions.size() > 0) {
        throw exceptions.get(0);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ScopedValue(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedValue) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Random(java.util.Random) Disabled(org.junit.jupiter.api.Disabled) LocatableResolver(com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver) Transaction(com.apple.foundationdb.Transaction) Tuple(com.apple.foundationdb.tuple.Tuple) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) TestHelpers(com.apple.foundationdb.record.TestHelpers) Pair(org.apache.commons.lang3.tuple.Pair) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Map(java.util.Map) Tag(org.junit.jupiter.api.Tag) BiMap(com.google.common.collect.BiMap) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) ResolvedKeySpacePath(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolvedKeySpacePath) IntStream(java.util.stream.IntStream) Assertions.fail(org.junit.jupiter.api.Assertions.fail) KeySpace(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpace) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) Logger(org.slf4j.Logger) KeySpaceDirectory(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory) Iterator(java.util.Iterator) Executor(java.util.concurrent.Executor) Tags(com.apple.test.Tags) Semaphore(java.util.concurrent.Semaphore) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ExecutionException(java.util.concurrent.ExecutionException) PathUtil(com.apple.foundationdb.directory.PathUtil) ForkJoinPool(java.util.concurrent.ForkJoinPool) ScopedDirectoryLayer(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedDirectoryLayer) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) Executor(java.util.concurrent.Executor) Transaction(com.apple.foundationdb.Transaction) Objects(java.util.Objects) Semaphore(java.util.concurrent.Semaphore) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) NoSuchElementException(java.util.NoSuchElementException) ExecutionException(java.util.concurrent.ExecutionException) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 44 with Transaction

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

the class FDBStoreTimerTest method testLowLevelIoMetrics.

@Test
public void testLowLevelIoMetrics() {
    final FDBStoreTimer timer = new FDBStoreTimer();
    try (FDBRecordContext context = fdb.openContext(null, timer)) {
        Transaction tr = context.ensureActive();
        tr.clear(subspace.range());
        tr.commit().join();
    }
    assertThat(timer.getCount(FDBStoreTimer.Counts.DELETES), equalTo(1));
    assertThat(timer.getCount(FDBStoreTimer.Events.COMMITS), equalTo(1));
    timer.reset();
    int writeBytes = 0;
    try (FDBRecordContext context = fdb.openContext(null, timer)) {
        Transaction tr = context.ensureActive();
        for (int i = 0; i < 5; i++) {
            byte[] key = subspace.pack(Tuple.from(i));
            byte[] value = subspace.pack(Tuple.from("foo", i));
            tr.set(key, value);
            writeBytes += (key.length + value.length);
        }
        ReadTransaction rtr = tr.snapshot();
        List<KeyValue> values = rtr.getRange(subspace.range()).asList().join();
        assertThat(values.size(), equalTo(5));
        tr.commit().join();
    }
    assertThat(timer.getCount(FDBStoreTimer.Counts.WRITES), equalTo(5));
    assertThat(timer.getCount(FDBStoreTimer.Counts.BYTES_WRITTEN), equalTo(writeBytes));
    assertThat(timer.getCount(FDBStoreTimer.Counts.READS), equalTo(1));
    assertThat(timer.getCount(FDBStoreTimer.Counts.BYTES_READ), equalTo(writeBytes));
    assertThat(timer.getCount(FDBStoreTimer.Events.COMMITS), equalTo(1));
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) Transaction(com.apple.foundationdb.Transaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) Test(org.junit.jupiter.api.Test)

Example 45 with Transaction

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

the class FDBStoreTimerTest method testTransactionMetricListener.

@Test
void testTransactionMetricListener() {
    try (FDBRecordContext context = fdb.openContext(null, null)) {
        Transaction tr = context.ensureActive();
        tr.clear(subspace.range());
        tr.commit().join();
    }
    final TestTransactionListener listener = new TestTransactionListener();
    final FDBStoreTimer timer = new FDBStoreTimer();
    try {
        FDBDatabaseFactory.instance().setTransactionListener(listener);
        for (int i = 0; i < 3; i++) {
            try (FDBRecordContext context = fdb.openContext(null, timer)) {
                Transaction tr = context.ensureActive();
                tr.set(subspace.pack(Tuple.from(1L)), Tuple.from(1L).pack());
                tr.get(subspace.pack(Tuple.from(1L)));
                tr.get(subspace.pack(Tuple.from(1L)));
                // Make sure we get metrics even if there is no commit
                if (i != 1) {
                    tr.commit().join();
                }
            }
        }
        assertThat(listener.transactions, equalTo(3));
        assertThat(listener.reads, equalTo(timer.getCount(FDBStoreTimer.Counts.READS)));
        assertThat(listener.writes, equalTo(timer.getCount(FDBStoreTimer.Counts.WRITES)));
        assertThat(listener.commits, equalTo(2));
        assertThat(listener.closes, equalTo(3));
    } finally {
        FDBDatabaseFactory.instance().setTransactionListener(null);
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) Test(org.junit.jupiter.api.Test)

Aggregations

Transaction (com.apple.foundationdb.Transaction)84 ReadTransaction (com.apple.foundationdb.ReadTransaction)34 Tuple (com.apple.foundationdb.tuple.Tuple)34 Test (org.junit.jupiter.api.Test)33 Nonnull (javax.annotation.Nonnull)28 ArrayList (java.util.ArrayList)26 List (java.util.List)26 CompletableFuture (java.util.concurrent.CompletableFuture)26 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)22 Subspace (com.apple.foundationdb.subspace.Subspace)21 Collectors (java.util.stream.Collectors)19 Nullable (javax.annotation.Nullable)19 KeyValue (com.apple.foundationdb.KeyValue)18 Map (java.util.Map)18 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)17 Range (com.apple.foundationdb.Range)16 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 Collections (java.util.Collections)15 RecordCursor (com.apple.foundationdb.record.RecordCursor)14