Search in sources :

Example 11 with FDBException

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

Example 12 with FDBException

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

Example 13 with FDBException

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

Example 14 with FDBException

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);
    }
}
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 15 with FDBException

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;
        });
    }
}
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) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) 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