use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.
the class FDBDatabaseRunnerTest method runRetryNoSuccess.
@Test
public void runRetryNoSuccess() {
// The rest of the tests retry all of the way, so set guards to make sure they don't take forever.
try (FDBDatabaseRunner runner = database.newRunner()) {
runner.setMaxAttempts(5);
runner.setMaxDelayMillis(100);
runner.setInitialDelayMillis(5);
AtomicInteger iteration = new AtomicInteger(0);
try {
runner.run(context -> {
assertTrue(iteration.get() < runner.getMaxAttempts());
iteration.incrementAndGet();
throw new RecordCoreRetriableTransactionException("Have to try again!", new FDBException("not_committed", 1020));
});
fail("Did not catch retriable error that hit maximum retry limit");
} catch (RecordCoreException e) {
assertEquals("Have to try again!", e.getMessage());
assertNotNull(e.getCause());
assertTrue(e.getCause() instanceof FDBException);
assertEquals("not_committed", e.getCause().getMessage());
assertEquals(FDBError.NOT_COMMITTED.code(), ((FDBException) e.getCause()).getCode());
}
assertEquals(runner.getMaxAttempts(), iteration.get());
}
}
use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.
the class FDBDatabaseRunnerTest method runNonRetriableException.
@Test
public void runNonRetriableException() {
try (FDBDatabaseRunner runner = database.newRunner()) {
runner.run(context -> {
throw new RecordCoreException("Encountered an I/O error", new FDBException("io_error", 1510));
});
fail("Did not error on second non-retriable exception");
} catch (RecordCoreException e) {
assertEquals("Encountered an I/O error", e.getMessage());
assertNotNull(e.getCause());
assertTrue(e.getCause() instanceof FDBException);
assertEquals("io_error", e.getCause().getMessage());
assertEquals(FDBError.IO_ERROR.code(), ((FDBException) e.getCause()).getCode());
}
try (FDBDatabaseRunner runner = database.newRunner()) {
runner.run(context -> {
throw new RecordCoreException("Internal error");
});
fail("Did not catch third non-retriable exception");
} catch (RecordCoreException e) {
assertEquals("Internal error", e.getMessage());
assertNull(e.getCause());
}
}
use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreUniqueIndexTest method uniquenessChecks.
@Test
public void uniquenessChecks() throws Exception {
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context);
Index index1 = recordStore.getRecordMetaData().getIndex("MySimpleRecord$str_value_indexed");
Index index2 = recordStore.getRecordMetaData().getIndex("MySimpleRecord$num_value_unique");
AtomicBoolean check1Run = new AtomicBoolean(false);
CompletableFuture<Void> check1 = MoreAsyncUtil.delayedFuture(1L, TimeUnit.MILLISECONDS).thenRun(() -> check1Run.set(true));
recordStore.addIndexUniquenessCommitCheck(index1, check1);
CompletableFuture<Void> check2 = new CompletableFuture<>();
RecordCoreException err = new RecordCoreException("unable to run check");
check2.completeExceptionally(err);
recordStore.addIndexUniquenessCommitCheck(index2, check2);
// Checks For index 1 should complete successfully and mark the "check1Run" boolean as completed. It
// should not throw an error from check 2 completing exceptionally.
recordStore.whenAllIndexUniquenessCommitChecks(index1).get();
assertTrue(check1Run.get(), "check1 should have marked check1Run as having completed");
// For index 2, the error should be caught when the uniqueness checks are waited on
ExecutionException thrownExecutionException = assertThrows(ExecutionException.class, () -> recordStore.whenAllIndexUniquenessCommitChecks(index2).get());
assertSame(err, thrownExecutionException.getCause());
// The error from the "uniqueness check" should block the transaction from committing
RecordCoreException thrownRecordCoreException = assertThrows(RecordCoreException.class, context::commit);
assertSame(err, thrownRecordCoreException);
}
}
use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreTest method testUserVersionMonotonic.
@Test
public void testUserVersionMonotonic() {
final FDBRecordStoreBase.UserVersionChecker userVersion1 = (oldUserVersion, oldMetaDataVersion, metaData) -> CompletableFuture.completedFuture(101);
final FDBRecordStoreBase.UserVersionChecker userVersion2 = (oldUserVersion, oldMetaDataVersion, metaData) -> CompletableFuture.completedFuture(102);
final RecordMetaDataBuilder builder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
try (FDBRecordContext context = openContext()) {
recordStore = FDBRecordStore.newBuilder().setContext(context).setKeySpacePath(path).setMetaDataProvider(builder).setUserVersionChecker(userVersion1).create();
assertEquals(101, recordStore.getUserVersion());
context.commit();
}
try (FDBRecordContext context = openContext()) {
recordStore = FDBRecordStore.newBuilder().setContext(context).setKeySpacePath(path).setMetaDataProvider(builder).setUserVersionChecker(userVersion2).open();
assertEquals(102, recordStore.getUserVersion());
context.commit();
}
try (FDBRecordContext context = openContext()) {
FDBRecordStore.Builder storeBuilder = FDBRecordStore.newBuilder().setContext(context).setKeySpacePath(path).setMetaDataProvider(builder).setUserVersionChecker(userVersion1);
RecordCoreException ex = assertThrows(RecordCoreException.class, storeBuilder::open);
assertThat(ex.getMessage(), containsString("Stale user version"));
}
}
use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreCountRecordsTest method addCountKey.
@Test
@SuppressWarnings("deprecation")
public void addCountKey() throws Exception {
RecordMetaDataHook removeCountHook = metaData -> metaData.removeIndex(COUNT_INDEX.getName());
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, removeCountHook);
for (int i = 0; i < 10; i++) {
recordStore.saveRecord(makeRecord(i, 1066, i % 5));
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, removeCountHook);
recordStore.getSnapshotRecordCount().get();
fail("evaluated count without index or key");
} catch (RecordCoreException e) {
assertThat(e.getMessage(), containsString("requires appropriate index"));
}
AtomicInteger versionCounter = new AtomicInteger(recordStore.getRecordMetaData().getVersion());
RecordMetaDataHook hook = md -> {
md.setRecordCountKey(field("num_value_3_indexed"));
md.setVersion(md.getVersion() + versionCounter.incrementAndGet());
};
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertThat(timer.getCount(FDBStoreTimer.Events.RECOUNT_RECORDS), equalTo(1));
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertThat(timer.getCount(FDBStoreTimer.Events.RECOUNT_RECORDS), equalTo(0));
assertEquals(10L, recordStore.getSnapshotRecordCount().get().longValue());
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
// Before it was deprecated, this is how a key would have been written.
RecordMetaDataProto.DataStoreInfo.Builder infoBuilder = recordStore.getRecordStoreState().getStoreHeader().toBuilder();
infoBuilder.getRecordCountKeyBuilder().getFieldBuilder().clearNullInterpretation();
recordStore.saveStoreHeader(infoBuilder.build());
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertThat(timer.getCount(FDBStoreTimer.Events.RECOUNT_RECORDS), equalTo(0));
assertEquals(10L, recordStore.getSnapshotRecordCount().get().longValue());
}
}
Aggregations