Search in sources :

Example 61 with RecordCoreException

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());
    }
}
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 62 with RecordCoreException

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

Example 63 with RecordCoreException

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);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) CompletableFuture(java.util.concurrent.CompletableFuture) Index(com.apple.foundationdb.record.metadata.Index) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 64 with RecordCoreException

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"));
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) DescriptorProtos(com.google.protobuf.DescriptorProtos) DELETE_INDEX_ENTRY(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer.Events.DELETE_INDEX_ENTRY) Arrays(java.util.Arrays) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) RecordCoreStorageException(com.apple.foundationdb.record.RecordCoreStorageException) ByteBuffer(java.nio.ByteBuffer) RecordSerializer(com.apple.foundationdb.record.provider.common.RecordSerializer) RecordSerializationException(com.apple.foundationdb.record.provider.common.RecordSerializationException) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) Range(com.apple.foundationdb.Range) FDBError(com.apple.foundationdb.FDBError) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) RecordMetaDataOptionsProto(com.apple.foundationdb.record.RecordMetaDataOptionsProto) Is.is(org.hamcrest.core.Is.is) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) Tag(org.junit.jupiter.api.Tag) MethodSource(org.junit.jupiter.params.provider.MethodSource) TestRecords7Proto(com.apple.foundationdb.record.TestRecords7Proto) ByteArrayUtil(com.apple.foundationdb.tuple.ByteArrayUtil) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Matchers.allOf(org.hamcrest.Matchers.allOf) Set(java.util.Set) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) KeySpacePath(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpacePath) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) List(java.util.List) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) RecordMetaDataProvider(com.apple.foundationdb.record.RecordMetaDataProvider) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) TestRecordsWithHeaderProto(com.apple.foundationdb.record.TestRecordsWithHeaderProto) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.containsString(org.hamcrest.Matchers.containsString) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) TestNoUnionProto(com.apple.foundationdb.record.TestNoUnionProto) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) TestRecordsDuplicateUnionFields(com.apple.foundationdb.record.TestRecordsDuplicateUnionFields) DynamicMessage(com.google.protobuf.DynamicMessage) Descriptors(com.google.protobuf.Descriptors) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CompletableFuture(java.util.concurrent.CompletableFuture) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) ScanProperties(com.apple.foundationdb.record.ScanProperties) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TestRecordsImportProto(com.apple.foundationdb.record.TestRecordsImportProto) Index(com.apple.foundationdb.record.metadata.Index) FDBException(com.apple.foundationdb.FDBException) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) Collections(java.util.Collections) SAVE_INDEX_ENTRY(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer.Events.SAVE_INDEX_ENTRY) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 65 with RecordCoreException

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());
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Key(com.apple.foundationdb.record.metadata.Key) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) Tuple(com.apple.foundationdb.tuple.Tuple) EndpointType(com.apple.foundationdb.record.EndpointType) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScanProperties(com.apple.foundationdb.record.ScanProperties) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) Is.is(org.hamcrest.core.Is.is) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestRecordsWithUnionProto(com.apple.foundationdb.record.TestRecordsWithUnionProto) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) Query(com.apple.foundationdb.record.query.expressions.Query) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) IndexState(com.apple.foundationdb.record.IndexState) Test(org.junit.jupiter.api.Test) RecordMetaDataProvider(com.apple.foundationdb.record.RecordMetaDataProvider) Index(com.apple.foundationdb.record.metadata.Index) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) TestRecordsWithHeaderProto(com.apple.foundationdb.record.TestRecordsWithHeaderProto) Matchers.containsString(org.hamcrest.Matchers.containsString) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Aggregations

RecordCoreException (com.apple.foundationdb.record.RecordCoreException)121 Nonnull (javax.annotation.Nonnull)58 Test (org.junit.jupiter.api.Test)42 Index (com.apple.foundationdb.record.metadata.Index)37 List (java.util.List)35 Nullable (javax.annotation.Nullable)31 Tuple (com.apple.foundationdb.tuple.Tuple)29 ArrayList (java.util.ArrayList)27 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)26 CompletableFuture (java.util.concurrent.CompletableFuture)25 Collectors (java.util.stream.Collectors)24 Collections (java.util.Collections)22 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)19 Set (java.util.Set)19 Function (java.util.function.Function)19 IndexEntry (com.apple.foundationdb.record.IndexEntry)17 TupleRange (com.apple.foundationdb.record.TupleRange)17 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)17 RecordCursor (com.apple.foundationdb.record.RecordCursor)16 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)15