Search in sources :

Example 16 with FDBException

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());
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordCoreRetriableTransactionException(com.apple.foundationdb.record.RecordCoreRetriableTransactionException) FDBException(com.apple.foundationdb.FDBException) Test(org.junit.jupiter.api.Test)

Example 17 with FDBException

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());
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) FDBException(com.apple.foundationdb.FDBException) Test(org.junit.jupiter.api.Test)

Example 18 with FDBException

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());
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) BeforeEach(org.junit.jupiter.api.BeforeEach) FDBDatabaseTest.testStoreAndRetrieveSimpleRecord(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseTest.testStoreAndRetrieveSimpleRecord) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) AtomicReference(java.util.concurrent.atomic.AtomicReference) Tuple(com.apple.foundationdb.tuple.Tuple) Vector(java.util.Vector) RecordCoreRetriableTransactionException(com.apple.foundationdb.record.RecordCoreRetriableTransactionException) ImmutableList(com.google.common.collect.ImmutableList) FDBError(com.apple.foundationdb.FDBError) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ThreadContext(org.apache.logging.log4j.ThreadContext) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) ImmutableMap(com.google.common.collect.ImmutableMap) Executor(java.util.concurrent.Executor) Tags(com.apple.test.Tags) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) FDBException(com.apple.foundationdb.FDBException) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Message(com.google.protobuf.Message) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordCoreRetriableTransactionException(com.apple.foundationdb.record.RecordCoreRetriableTransactionException) FDBException(com.apple.foundationdb.FDBException) Test(org.junit.jupiter.api.Test)

Example 19 with FDBException

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());
    }
}
Also used : FDBException(com.apple.foundationdb.FDBException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with FDBException

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());
    }
}
Also used : TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Message(com.google.protobuf.Message) FDBException(com.apple.foundationdb.FDBException) Test(org.junit.jupiter.api.Test)

Aggregations

FDBException (com.apple.foundationdb.FDBException)28 Test (org.junit.jupiter.api.Test)25 FDBError (com.apple.foundationdb.FDBError)16 CompletableFuture (java.util.concurrent.CompletableFuture)16 List (java.util.List)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)15 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)14 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)14 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)13 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)13 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)13 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)13 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)12 Tuple (com.apple.foundationdb.tuple.Tuple)12 RecordCoreRetriableTransactionException (com.apple.foundationdb.record.RecordCoreRetriableTransactionException)11 Assertions.fail (org.junit.jupiter.api.Assertions.fail)11 Tags (com.apple.test.Tags)10 Message (com.google.protobuf.Message)10 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)10