use of com.apple.foundationdb.FDBException 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.FDBException 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.FDBException in project fdb-record-layer by FoundationDB.
the class FDBDatabaseRunnerTest method runAsyncRetryNoSuccess.
@Test
public void runAsyncRetryNoSuccess() {
// 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);
runner.runAsync(context -> {
assertTrue(iteration.get() < runner.getMaxAttempts());
iteration.incrementAndGet();
throw new RecordCoreRetriableTransactionException("Have to try again!", new FDBException("not_committed", 1020));
}).handle((ignore, e) -> {
assertNotNull(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());
return null;
}).join();
assertEquals(runner.getMaxAttempts(), iteration.get());
}
}
use of com.apple.foundationdb.FDBException in project fdb-record-layer by FoundationDB.
the class FDBRecordContextTest method setReadVersionOutOfBandThenSet.
@Test
public void setReadVersionOutOfBandThenSet() {
try (FDBRecordContext context = fdb.openContext()) {
context.ensureActive().setReadVersion(1066L);
assertEquals(1459L, context.setReadVersion(1459L));
assertEquals(1459L, context.getReadVersion());
FDBExceptions.FDBStoreException err = assertThrows(FDBExceptions.FDBStoreException.class, context::commit);
assertNotNull(err.getCause());
assertThat(err.getCause(), instanceOf(FDBException.class));
FDBException fdbE = (FDBException) err.getCause();
assertEquals(FDBError.READ_VERSION_ALREADY_SET.code(), fdbE.getCode());
}
}
use of com.apple.foundationdb.FDBException in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreCrudTest method readPreloaded.
@Test
public void readPreloaded() throws Exception {
byte[] versionstamp;
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context);
TestRecords1Proto.MySimpleRecord rec = TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(1066L).build();
recordStore.saveRecord(rec);
commit(context);
versionstamp = context.getVersionStamp();
assertNotNull(versionstamp);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context);
// ensure loaded in context
recordStore.preloadRecordAsync(Tuple.from(1066L)).get();
// ensure no more I/O done through the transaction
context.ensureActive().cancel();
FDBStoredRecord<Message> record = recordStore.loadRecord(Tuple.from(1066L));
assertNotNull(record);
assertSame(TestRecords1Proto.MySimpleRecord.getDescriptor(), record.getRecordType().getDescriptor());
assertEquals(1066L, record.getRecord().getField(TestRecords1Proto.MySimpleRecord.getDescriptor().findFieldByNumber(TestRecords1Proto.MySimpleRecord.REC_NO_FIELD_NUMBER)));
assertEquals(FDBRecordVersion.complete(versionstamp, 0), record.getVersion());
FDBExceptions.FDBStoreException e = assertThrows(FDBExceptions.FDBStoreException.class, context::commit);
assertNotNull(e.getCause());
assertThat(e.getCause(), instanceOf(FDBException.class));
FDBException fdbE = (FDBException) e.getCause();
assertEquals(FDBError.TRANSACTION_CANCELLED.code(), fdbE.getCode());
}
}
Aggregations