use of com.apple.foundationdb.FDBException in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreCrudTest method readYourWritesPreloaded.
@Test
public void readYourWritesPreloaded() throws Exception {
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context);
TestRecords1Proto.MySimpleRecord rec = TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(1066L).build();
recordStore.saveRecord(rec);
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(rec.toByteString(), record.getRecord().toByteString());
assertEquals(FDBRecordVersion.incomplete(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());
}
}
use of com.apple.foundationdb.FDBException in project fdb-record-layer by FoundationDB.
the class AutoContinuingCursorTest method testContinuesOnRetryableException.
@Test
void testContinuesOnRetryableException() {
final AtomicInteger iteration = new AtomicInteger(0);
testAutoContinuingCursorGivenCursorGenerator((context, continuation) -> new TestingListCursor<>(list, continuation, () -> {
if (iteration.incrementAndGet() % 3 == 0) {
CompletableFuture<Void> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(new FDBException("transaction_too_old", FDBError.TRANSACTION_TOO_OLD.code()));
return failedFuture;
}
return AsyncUtil.DONE;
}), 1, list);
}
use of com.apple.foundationdb.FDBException in project fdb-record-layer by FoundationDB.
the class FDBDatabaseRunnerTest method close.
@Test
public void close() throws Exception {
AtomicInteger iteration = new AtomicInteger(0);
CompletableFuture<Void> future;
try (FDBDatabaseRunner runner = database.newRunner()) {
runner.setMaxAttempts(Integer.MAX_VALUE);
runner.setInitialDelayMillis(100);
runner.setMaxDelayMillis(100);
future = runner.runAsync(context -> {
iteration.incrementAndGet();
throw new RecordCoreRetriableTransactionException("Have to try again!", new FDBException("not_committed", 1020));
});
}
int currentIteration = iteration.get();
assertThat("Should have run at least once", currentIteration, greaterThan(0));
try {
future.join();
fail("Should have stopped exceptionally");
} catch (Exception ex) {
if (!(ex instanceof FDBDatabaseRunner.RunnerClosed || (ex instanceof CompletionException && ex.getCause() instanceof FDBDatabaseRunner.RunnerClosed))) {
throw ex;
}
}
Thread.sleep(150);
assertEquals(currentIteration, iteration.get(), "Should have stopped running");
}
use of com.apple.foundationdb.FDBException in project fdb-record-layer by FoundationDB.
the class FDBDatabaseRunnerTest method runRetryToSuccess.
@Test
public void runRetryToSuccess() {
try (FDBDatabaseRunner runner = database.newRunner()) {
AtomicInteger count = new AtomicInteger(0);
String value = runner.run(context -> {
if (count.getAndIncrement() == 0) {
throw new RecordCoreRetriableTransactionException("Have to try again!", new FDBException("not_committed", 1020));
} else {
return "Success!";
}
});
assertEquals("Success!", value);
assertEquals(2, count.get(), "Should only take one try");
count.set(0);
value = runner.run(context -> {
if (count.getAndIncrement() == 0) {
throw new FDBException("not_committed", 1020);
} else {
return "Success!";
}
});
assertEquals("Success!", value);
assertEquals(2, count.get(), "Should only take one try");
count.set(0);
value = runner.run(context -> {
if (count.getAndIncrement() == 0) {
throw new RecordCoreRetriableTransactionException("Something non-standard");
} else {
return "Success!";
}
});
assertEquals("Success!", value);
assertEquals(2, count.get(), "Should only take one try");
value = runner.run(context -> "Success!");
assertEquals("Success!", value);
}
}
use of com.apple.foundationdb.FDBException in project fdb-record-layer by FoundationDB.
the class FDBDatabaseRunnerTest method runAsyncNonRetriableException.
@Test
public void runAsyncNonRetriableException() {
try (FDBDatabaseRunner runner = database.newRunner()) {
runner.runAsync(context -> {
throw new RecordCoreException("Encountered an I/O error", new FDBException("io_error", 1510));
}).handle((ignore, e) -> {
assertNotNull(e);
assertTrue(e instanceof RecordCoreException);
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());
return null;
}).join();
}
try (FDBDatabaseRunner runner = database.newRunner()) {
runner.runAsync(context -> {
throw new RecordCoreException("Internal error");
}).handle((ignore, e) -> {
assertNotNull(e);
assertTrue(e instanceof RecordCoreException);
assertEquals("Internal error", e.getMessage());
assertNull(e.getCause());
return null;
});
}
}
Aggregations