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