use of com.apple.foundationdb.subspace.Subspace in project fdb-record-layer by FoundationDB.
the class HighContentionAllocatorTest method validateAllocation.
private void validateAllocation(FDBRecordContext context, HighContentionAllocator hca, Map<Long, String> allocations) {
Subspace allocationSubspace = hca.getAllocationSubspace();
Transaction transaction = context.ensureActive();
List<KeyValue> keyValueList = transaction.getRange(allocationSubspace.range()).asList().join();
Map<Long, String> storedAllocations = keyValueList.stream().collect(Collectors.toMap(kv -> extractKey(allocationSubspace, kv), this::extractValue));
assertThat("we see the allocated keys in the subspace", allocations.entrySet(), containsInAnyOrder(storedAllocations.entrySet().toArray()));
}
use of com.apple.foundationdb.subspace.Subspace in project fdb-record-layer by FoundationDB.
the class HighContentionAllocatorTest method testCheckForRootConflicts.
@Test
@Tag(Tags.WipesFDB)
void testCheckForRootConflicts() {
Range everything = new Range(new byte[] { (byte) 0x00 }, new byte[] { (byte) 0xFF });
database.run(context -> {
context.ensureActive().clear(everything);
return null;
});
try (FDBRecordContext context = database.openContext()) {
HighContentionAllocator hca = HighContentionAllocator.forRoot(context, keySpace.path("test-path"));
// so write something for all of those keys
for (int i = 0; i < 64; i++) {
byte[] key = Tuple.from(i, "string-" + i).pack();
byte[] value = new byte[0];
context.ensureActive().set(key, value);
}
try {
hca.allocate("some-string").join();
fail("allocate should fail in the same transaction");
} catch (Exception e) {
assertThat("a", e.getCause().getMessage(), is("database already has keys in allocation range"));
}
// check that the hca marks these keys as invalid
// the thing here is that when the post allocation hook fails the allocator still writes a key,
// that's actually a good thing since it will prevent us from trying to allocate that key again
// but we need to make sure we have a way to exclude from the reverse lookup
Subspace allocationSubspace = hca.getAllocationSubspace();
Range initialwindow = new Range(allocationSubspace.getKey(), allocationSubspace.pack(64));
List<KeyValue> allocatedValues = context.ensureActive().getRange(initialwindow).asList().join();
byte[] valueBytes = allocatedValues.get(0).getValue();
assertThat("there's exactly one allocation key", allocatedValues, hasSize(1));
assertArrayEquals(valueBytes, new byte[] { (byte) 0xFD }, "the value is set to the magic byte");
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
HighContentionAllocator hca = HighContentionAllocator.forRoot(context, keySpace.path("test-path"));
try {
hca.allocate("some-string").join();
fail("allocate should fail in new transaction");
} catch (Exception e) {
assertThat("a", e.getCause().getMessage(), is("database already has keys in allocation range"));
}
}
database.run(context -> {
context.ensureActive().clear(everything);
return null;
});
}
use of com.apple.foundationdb.subspace.Subspace in project fdb-record-layer by FoundationDB.
the class ScopedDirectoryLayerTest method validate.
private void validate(FDBRecordContext context, LocatableResolver resolver, DirectoryLayer directoryLayer, String key, Long value) {
List<String> directories = directoryLayer.list(context.ensureActive()).join();
assertThat("entry was added to the appropriate directory layer", directories, hasItem(key));
Subspace resultSubpsace = directoryLayer.open(context.ensureActive(), ImmutableList.of(key)).join();
Long directoryValue = resolver.deserializeValue(resultSubpsace.getKey()).getValue();
assertThat("resolver returned the value of the subspace prefix", directoryValue, is(value));
Long newValue = resolver.resolve(context.getTimer(), key).join();
assertThat("repeated calls to resolve return the same value", newValue, is(value));
}
use of com.apple.foundationdb.subspace.Subspace in project fdb-record-layer by FoundationDB.
the class ScopedDirectoryLayerTest method testDefaultResolverSeesPreviousDefaultDirectoryLayerEntries.
@Test
public void testDefaultResolverSeesPreviousDefaultDirectoryLayerEntries() {
final DirectoryLayer directoryLayer = DirectoryLayer.getDefault();
final List<String> names = IntStream.range(0, 5).mapToObj(number -> String.format("name-%d", number)).collect(Collectors.toList());
Map<String, Long> values = new HashMap<>();
try (FDBRecordContext context = database.openContext()) {
for (String name : names) {
values.put(name, directoryLayer.createOrOpen(context.ensureActive(), ImmutableList.of(name)).thenApply(subspace -> Tuple.fromBytes(subspace.getKey()).getLong(0)).join());
}
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
for (String name : names) {
Long resolvedValue = globalScope.resolve(context.getTimer(), name).join();
assertThat("resolver sees all mappings in directory layer", values.get(name), is(resolvedValue));
}
}
}
use of com.apple.foundationdb.subspace.Subspace in project fdb-record-layer by FoundationDB.
the class FDBDirectoryBaseTest method setUp.
@BeforeEach
public void setUp() {
if (fdb == null) {
fdb = FDBDatabaseFactory.instance().getDatabase();
}
if (subspace == null) {
subspace = fdb.run(context -> TestKeySpace.getKeyspacePath("record-test", "unit", "indexTest", "version").toSubspace(context));
}
fdb.run(context -> {
context.ensureActive().clear(subspace.range());
return null;
});
FDBRecordContext context = fdb.openContext(getContextConfig());
directory = new FDBDirectory(subspace, context);
}
Aggregations