Search in sources :

Example 66 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreFormatVersionTest method testAccessUserFieldAtOldFormatVersion.

/**
 * Test that accessing the header user fields at earlier format versions is disallowed.
 */
@Test
public void testAccessUserFieldAtOldFormatVersion() {
    final String expectedErrMsg = "cannot access header user fields at current format version";
    final RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    FDBRecordStore.Builder storeBuilder = FDBRecordStore.newBuilder().setKeySpacePath(path).setMetaDataProvider(metaData).setFormatVersion(FDBRecordStore.HEADER_USER_FIELDS_FORMAT_VERSION - 1);
    try (FDBRecordContext context = openContext()) {
        recordStore = storeBuilder.setContext(context).create();
        RecordCoreException err = assertThrows(RecordCoreException.class, () -> recordStore.getHeaderUserField("foo"));
        assertEquals(expectedErrMsg, err.getMessage());
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        recordStore = storeBuilder.setContext(context).open();
        RecordCoreException err = assertThrows(RecordCoreException.class, () -> recordStore.setHeaderUserField("foo", "bar".getBytes(Charsets.UTF_8)));
        assertEquals(expectedErrMsg, err.getMessage());
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        recordStore = storeBuilder.setFormatVersion(FDBRecordStore.INFO_ADDED_FORMAT_VERSION).setContext(context).open();
        assertEquals(FDBRecordStore.HEADER_USER_FIELDS_FORMAT_VERSION - 1, recordStore.getFormatVersion());
        RecordCoreException err = assertThrows(RecordCoreException.class, () -> recordStore.clearHeaderUserField("foo"));
        assertEquals(expectedErrMsg, err.getMessage());
        commit(context);
    }
    // Now try upgrading the format version and validate that the fields can be read
    try (FDBRecordContext context = openContext()) {
        recordStore = storeBuilder.setFormatVersion(FDBRecordStore.HEADER_USER_FIELDS_FORMAT_VERSION).setContext(context).open();
        assertEquals(FDBRecordStore.HEADER_USER_FIELDS_FORMAT_VERSION, recordStore.getFormatVersion());
        recordStore.setHeaderUserField("foo", "bar".getBytes(Charsets.UTF_8));
        String val = recordStore.getHeaderUserField("foo").toStringUtf8();
        assertEquals("bar", val);
        recordStore.clearHeaderUserField("foo");
        assertNull(recordStore.getHeaderUserField("foo"));
        commit(context);
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Test(org.junit.jupiter.api.Test)

Example 67 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreIndexTest method minMaxIndex.

@ParameterizedTest(name = "minMaxIndex({0})")
@EnumSource(MinMaxIndexTypes.class)
public void minMaxIndex(MinMaxIndexTypes indexTypes) throws Exception {
    final FieldKeyExpression recno = field("rec_no");
    final GroupingKeyExpression byKey = recno.groupBy(field("num_value_3_indexed"));
    final RecordMetaDataHook hook = md -> {
        RecordTypeBuilder type = md.getRecordType("MySimpleRecord");
        md.addIndex(type, new Index("min", byKey, indexTypes.min()));
        md.addIndex(type, new Index("max", byKey, indexTypes.max()));
    };
    final IndexAggregateFunction minOverall = new IndexAggregateFunction(FunctionNames.MIN_EVER, recno, null);
    final IndexAggregateFunction maxOverall = new IndexAggregateFunction(FunctionNames.MAX_EVER, recno, null);
    final IndexAggregateFunction minByKey = new IndexAggregateFunction(FunctionNames.MIN_EVER, byKey, null);
    final IndexAggregateFunction maxByKey = new IndexAggregateFunction(FunctionNames.MAX_EVER, byKey, null);
    List<String> types = Collections.singletonList("MySimpleRecord");
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        assertNull(recordStore.evaluateAggregateFunction(types, minOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        assertNull(recordStore.evaluateAggregateFunction(types, maxOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        assertNull(recordStore.evaluateAggregateFunction(types, minByKey, Key.Evaluated.scalar(1), IsolationLevel.SNAPSHOT).join());
        assertNull(recordStore.evaluateAggregateFunction(types, maxByKey, Key.Evaluated.scalar(1), IsolationLevel.SNAPSHOT).join());
        for (int i = 0; i < 100; i++) {
            TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
            recBuilder.setRecNo(i);
            recBuilder.setNumValue3Indexed(i % 5);
            recordStore.saveRecord(recBuilder.build());
        }
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        assertEquals(0, recordStore.evaluateAggregateFunction(types, minOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join().getLong(0));
        assertEquals(99, recordStore.evaluateAggregateFunction(types, maxOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join().getLong(0));
        assertEquals(1, recordStore.evaluateAggregateFunction(types, minByKey, Key.Evaluated.scalar(1), IsolationLevel.SNAPSHOT).join().getLong(0));
        assertEquals(96, recordStore.evaluateAggregateFunction(types, maxByKey, Key.Evaluated.scalar(1), IsolationLevel.SNAPSHOT).join().getLong(0));
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        recordStore.deleteRecord(Tuple.from(0));
        recordStore.deleteRecord(Tuple.from(99));
        assertEquals(0, recordStore.evaluateAggregateFunction(types, minOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join().getLong(0));
        assertEquals(99, recordStore.evaluateAggregateFunction(types, maxOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join().getLong(0));
        commit(context);
    }
    // verify that negatives do not appear in min/max
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
        recBuilder.setRecNo(-1);
        recBuilder.setNumValue3Indexed(1);
        recordStore.saveRecord(recBuilder.build());
        if (!indexTypes.shouldAllowNegative()) {
            fail("should have thrown exception");
        }
    } catch (RecordCoreException e) {
        if (indexTypes.shouldAllowNegative()) {
            throw e;
        }
        assertEquals(e.getMessage(), "Attempted update of MAX_EVER_LONG or MIN_EVER_LONG index with negative value");
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) IndexScanType(com.apple.foundationdb.record.IndexScanType) Pair(org.apache.commons.lang3.tuple.Pair) FDBError(com.apple.foundationdb.FDBError) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) RecordIndexUniquenessViolation(com.apple.foundationdb.record.RecordIndexUniquenessViolation) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) Set(java.util.Set) FanType(com.apple.foundationdb.record.metadata.expressions.KeyExpression.FanType) TupleRange(com.apple.foundationdb.record.TupleRange) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) RecordMetaDataProvider(com.apple.foundationdb.record.RecordMetaDataProvider) RecordStoreState(com.apple.foundationdb.record.RecordStoreState) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) InvalidIndexEntry(com.apple.foundationdb.record.provider.foundationdb.indexes.InvalidIndexEntry) AutoContinuingCursor(com.apple.foundationdb.record.cursors.AutoContinuingCursor) Matchers.is(org.hamcrest.Matchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) FunctionNames(com.apple.foundationdb.record.FunctionNames) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) CloseableAsyncIterator(com.apple.foundationdb.async.CloseableAsyncIterator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FDBRecordStoreBase.indexEntryKey(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase.indexEntryKey) Nullable(javax.annotation.Nullable) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) Tags(com.apple.test.Tags) TestRecords1EvolvedProto(com.apple.foundationdb.record.TestRecords1EvolvedProto) ExecutionException(java.util.concurrent.ExecutionException) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Index(com.apple.foundationdb.record.metadata.Index) FDBException(com.apple.foundationdb.FDBException) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) IndexEntry(com.apple.foundationdb.record.IndexEntry) StoreTimer(com.apple.foundationdb.record.provider.common.StoreTimer) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Random(java.util.Random) Tuple(com.apple.foundationdb.tuple.Tuple) Range(com.apple.foundationdb.Range) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) ImmutableSet(com.google.common.collect.ImmutableSet) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) ImmutableMap(com.google.common.collect.ImmutableMap) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) IndexQueryabilityFilter(com.apple.foundationdb.record.query.IndexQueryabilityFilter) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Optional(java.util.Optional) TestNoIndexesProto(com.apple.foundationdb.record.TestNoIndexesProto) LazyCursor(com.apple.foundationdb.record.cursors.LazyCursor) EnumSource(org.junit.jupiter.params.provider.EnumSource) CompletableFuture(java.util.concurrent.CompletableFuture) Iterators(com.google.common.collect.Iterators) Key(com.apple.foundationdb.record.metadata.Key) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) HashSet(java.util.HashSet) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) ScanProperties(com.apple.foundationdb.record.ScanProperties) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) BooleanSource(com.apple.test.BooleanSource) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Description(org.hamcrest.Description) Matchers.oneOf(org.hamcrest.Matchers.oneOf) Logger(org.slf4j.Logger) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) IndexState(com.apple.foundationdb.record.IndexState) TestRecordsIndexFilteringProto(com.apple.foundationdb.record.TestRecordsIndexFilteringProto) Message(com.google.protobuf.Message) Collections(java.util.Collections) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Index(com.apple.foundationdb.record.metadata.Index) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 68 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class OnlineIndexerBuildRankIndexTest method rankRebuild.

private void rankRebuild(@Nonnull List<TestRecords1Proto.MySimpleRecord> records, @Nullable List<TestRecords1Proto.MySimpleRecord> recordsWhileBuilding, int agents, boolean overlap) {
    final Index index = new Index("newRankIndex", field("num_value_2").ungrouped(), IndexTypes.RANK);
    final IndexRecordFunction<Long> recordFunction = (IndexRecordFunction<Long>) Query.rank("num_value_2").getFunction();
    final Runnable beforeBuild = new Runnable() {

        @SuppressWarnings("try")
        @Override
        public void run() {
            try (FDBRecordContext context = openContext()) {
                for (TestRecords1Proto.MySimpleRecord record : records) {
                    try {
                        recordStore.evaluateRecordFunction(recordFunction, createStoredMessage(record)).join();
                        fail("Somehow evaluated rank");
                    } catch (RecordCoreException e) {
                        assertEquals("Record function rank(Field { 'num_value_2' None} group 1) requires appropriate index on MySimpleRecord", e.getMessage());
                    }
                }
                // Either we get an exception, or the record store is empty.
                try {
                    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.rank("num_value_2").equalsValue(0L)).build();
                    RecordQueryPlan plan = planner.plan(query);
                    assertEquals("Scan(<,>) | [MySimpleRecord] | rank(Field { 'num_value_2' None} group 1) EQUALS 0", plan.toString());
                    Optional<?> first = recordStore.executeQuery(plan).first().join();
                    assertTrue(!first.isPresent(), "non-empty range with rank rebuild");
                } catch (CompletionException e) {
                    assertNotNull(e.getCause());
                    assertThat(e.getCause(), instanceOf(RecordCoreException.class));
                    assertEquals("Record function rank(Field { 'num_value_2' None} group 1) requires appropriate index on MySimpleRecord", e.getCause().getMessage());
                }
            }
        }
    };
    List<TestRecords1Proto.MySimpleRecord> updatedRecords;
    if (recordsWhileBuilding == null || recordsWhileBuilding.size() == 0) {
        updatedRecords = records;
    } else {
        updatedRecords = updated(records, recordsWhileBuilding);
    }
    final Runnable afterBuild = new Runnable() {

        @SuppressWarnings("try")
        @Override
        public void run() {
            try (FDBRecordContext context = openContext()) {
                for (TestRecords1Proto.MySimpleRecord record : updatedRecords) {
                    try {
                        recordStore.evaluateRecordFunction(recordFunction, createStoredMessage(record)).join();
                        fail("Somehow evaluated rank");
                    } catch (RecordCoreException e) {
                        assertEquals("Record function rank(Field { 'num_value_2' None} group 1) requires appropriate index on MySimpleRecord", e.getMessage());
                    }
                }
                // Either we get an exception, or the record store is empty.
                try {
                    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.rank("num_value_2").equalsValue(0L)).build();
                    RecordQueryPlan plan = planner.plan(query);
                    assertEquals("Scan(<,>) | [MySimpleRecord] | rank(Field { 'num_value_2' None} group 1) EQUALS 0", plan.toString());
                    Optional<?> first = recordStore.executeQuery(plan).first().join();
                    assertTrue(!first.isPresent(), "non-empty range with rank rebuild");
                } catch (CompletionException e) {
                    assertNotNull(e.getCause());
                    assertThat(e.getCause(), instanceOf(RecordCoreException.class));
                    assertEquals("Record function rank(Field { 'num_value_2' None} group 1) requires appropriate index on MySimpleRecord", e.getCause().getMessage());
                }
            }
        }
    };
    TreeSet<Integer> values = new TreeSet<>();
    boolean hasNull = false;
    for (TestRecords1Proto.MySimpleRecord record : updatedRecords) {
        if (!record.hasNumValue2()) {
            hasNull = true;
        } else {
            values.add(record.getNumValue2());
        }
    }
    long curr = 0;
    Map<Integer, Long> ranks = new HashMap<>();
    if (hasNull) {
        curr += 1;
    }
    for (Integer value : values) {
        ranks.put(value, curr);
        curr += 1;
    }
    final Runnable afterReadable = new Runnable() {

        @SuppressWarnings("try")
        @Override
        public void run() {
            try (FDBRecordContext context = openContext()) {
                for (TestRecords1Proto.MySimpleRecord record : updatedRecords) {
                    Long rank = recordStore.evaluateRecordFunction(recordFunction, createStoredMessage(record)).join();
                    if (!record.hasNumValue2()) {
                        assertEquals(0L, rank.longValue());
                    } else {
                        assertEquals(ranks.get(record.getNumValue2()), rank);
                    }
                    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.rank("num_value_2").equalsValue(rank)).build();
                    RecordQueryPlan plan = planner.plan(query);
                    assertEquals("Index(newRankIndex [[" + rank + "],[" + rank + "]] BY_RANK)", plan.toString());
                    Optional<TestRecords1Proto.MySimpleRecord> retrieved = recordStore.executeQuery(plan).map(rec -> TestRecords1Proto.MySimpleRecord.newBuilder().mergeFrom(rec.getRecord()).build()).filter(rec -> rec.getRecNo() == record.getRecNo()).first().join();
                    assertTrue(retrieved.isPresent(), "empty range after rank index build");
                    assertEquals(record, retrieved.get());
                }
            }
        }
    };
    singleRebuild(records, recordsWhileBuilding, agents, overlap, false, index, beforeBuild, afterBuild, afterReadable);
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) HashMap(java.util.HashMap) Random(java.util.Random) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) TreeSet(java.util.TreeSet) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Map(java.util.Map) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) IndexRecordFunction(com.apple.foundationdb.record.metadata.IndexRecordFunction) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) Nullable(javax.annotation.Nullable) Query(com.apple.foundationdb.record.query.expressions.Query) LongStream(java.util.stream.LongStream) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Tags(com.apple.test.Tags) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Stream(java.util.stream.Stream) Index(com.apple.foundationdb.record.metadata.Index) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexRecordFunction(com.apple.foundationdb.record.metadata.IndexRecordFunction) HashMap(java.util.HashMap) Index(com.apple.foundationdb.record.metadata.Index) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) TreeSet(java.util.TreeSet) CompletionException(java.util.concurrent.CompletionException) RecordQuery(com.apple.foundationdb.record.query.RecordQuery)

Example 69 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class OnlineIndexerSimpleTest method notReincreaseLimit.

@Test
void notReincreaseLimit() {
    // Non-retriable error that is in lessen work codes.
    Supplier<RuntimeException> createException = () -> new RecordCoreException("Non-retriable", new FDBException("transaction_too_large", 2101));
    Queue<Pair<Integer, Supplier<RuntimeException>>> queue = new LinkedList<>();
    // failures until it hits 42
    for (int i = 100; i > 42; i = (3 * i) / 4) {
        queue.add(Pair.of(i, createException));
    }
    // a whole bunch of successes
    for (int i = 0; i < 100; i++) {
        queue.add(Pair.of(42, null));
    }
    reincreaseLimit(queue, index -> OnlineIndexer.newBuilder().setDatabase(fdb).setMetaData(metaData).setIndex(index).setSubspace(subspace).setLimit(100).setMaxRetries(queue.size() + 3).setRecordsPerSecond(10000).setMdcContext(ImmutableMap.of("mdcKey", "my cool mdc value")).setMaxAttempts(3).build());
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) FDBException(com.apple.foundationdb.FDBException) LinkedList(java.util.LinkedList) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.jupiter.api.Test)

Example 70 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class OnlineIndexerSimpleTest method run.

@Test
public void run() {
    Index index = runAsyncSetup();
    try (OnlineIndexer indexBuilder = OnlineIndexer.newBuilder().setDatabase(fdb).setMetaData(metaData).setIndex(index).setSubspace(subspace).setLimit(100).setMaxRetries(3).setRecordsPerSecond(10000).setMdcContext(ImmutableMap.of("mdcKey", "my cool mdc value")).setMaxAttempts(2).build()) {
        AtomicInteger attempts = new AtomicInteger();
        // Non-FDB error
        attempts.set(0);
        runAndHandleLessenWorkCodes(indexBuilder, store -> {
            attempts.incrementAndGet();
            throw new IllegalStateException("illegal state");
        }).handle((val, e) -> {
            assertNotNull(e);
            assertThat(e, instanceOf(IllegalStateException.class));
            assertEquals("illegal state", e.getMessage());
            assertNull(e.getCause());
            assertEquals(1, attempts.get());
            assertEquals("my cool mdc value", ThreadContext.get("mdcKey"));
            return null;
        }).join();
        // Retriable error that is not in lessen work codes.
        attempts.set(0);
        runAndHandleLessenWorkCodes(indexBuilder, store -> {
            attempts.incrementAndGet();
            throw new RecordCoreRetriableTransactionException("Retriable", new FDBException("commit_unknown_result", 1021));
        }).handle((val, e) -> {
            assertNotNull(e);
            assertThat(e, instanceOf(RecordCoreRetriableTransactionException.class));
            assertEquals("Retriable", e.getMessage());
            assertThat(e.getCause(), instanceOf(FDBException.class));
            assertEquals("commit_unknown_result", e.getCause().getMessage());
            assertEquals(FDBError.COMMIT_UNKNOWN_RESULT.code(), ((FDBException) e.getCause()).getCode());
            assertEquals(2, attempts.get());
            assertEquals("my cool mdc value", ThreadContext.get("mdcKey"));
            return null;
        }).join();
        // Non-retriable error that is in lessen work codes.
        attempts.set(0);
        runAndHandleLessenWorkCodes(indexBuilder, store -> {
            attempts.incrementAndGet();
            throw new RecordCoreException("Non-retriable", new FDBException("transaction_too_large", 2101));
        }).handle((val, e) -> {
            assertNotNull(e);
            assertThat(e, instanceOf(RecordCoreException.class));
            assertEquals("Non-retriable", e.getMessage());
            assertNotNull(e.getCause());
            assertThat(e.getCause(), instanceOf(FDBException.class));
            assertEquals("transaction_too_large", e.getCause().getMessage());
            assertEquals(FDBError.TRANSACTION_TOO_LARGE.code(), ((FDBException) e.getCause()).getCode());
            // lessenWorkCodes is maxRetries
            assertEquals(4, attempts.get());
            assertEquals("my cool mdc value", ThreadContext.get("mdcKey"));
            return null;
        }).join();
        // Retriable error that is in lessen work codes.
        attempts.set(0);
        runAndHandleLessenWorkCodes(indexBuilder, store -> {
            attempts.incrementAndGet();
            throw new RecordCoreRetriableTransactionException("Retriable and lessener", new FDBException("not_committed", 1020));
        }).handle((val, e) -> {
            assertNotNull(e);
            assertThat(e, instanceOf(RecordCoreRetriableTransactionException.class));
            assertEquals("Retriable and lessener", e.getMessage());
            assertNotNull(e.getCause());
            assertThat(e.getCause(), instanceOf(FDBException.class));
            assertEquals("not_committed", e.getCause().getMessage());
            assertEquals(FDBError.NOT_COMMITTED.code(), ((FDBException) e.getCause()).getCode());
            assertEquals(8, attempts.get());
            assertEquals("my cool mdc value", ThreadContext.get("mdcKey"));
            return null;
        }).join();
    }
}
Also used : Arrays(java.util.Arrays) LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Tuple(com.apple.foundationdb.tuple.Tuple) Range(com.apple.foundationdb.Range) Pair(org.apache.commons.lang3.tuple.Pair) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) FDBError(com.apple.foundationdb.FDBError) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadContext(org.apache.logging.log4j.ThreadContext) DO_NOT_RE_INCREASE_LIMIT(com.apple.foundationdb.record.provider.foundationdb.OnlineIndexer.DO_NOT_RE_INCREASE_LIMIT) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) DEFAULT_PROGRESS_LOG_INTERVAL(com.apple.foundationdb.record.provider.foundationdb.OnlineIndexer.DEFAULT_PROGRESS_LOG_INTERVAL) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Queue(java.util.Queue) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) FunctionNames(com.apple.foundationdb.record.FunctionNames) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) AsyncIterator(com.apple.foundationdb.async.AsyncIterator) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) RangeSet(com.apple.foundationdb.async.RangeSet) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Key(com.apple.foundationdb.record.metadata.Key) RecordCoreRetriableTransactionException(com.apple.foundationdb.record.RecordCoreRetriableTransactionException) Matchers.lessThan(org.hamcrest.Matchers.lessThan) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedList(java.util.LinkedList) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) Matchers.oneOf(org.hamcrest.Matchers.oneOf) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) LongStream(java.util.stream.LongStream) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) ExecutionException(java.util.concurrent.ExecutionException) Index(com.apple.foundationdb.record.metadata.Index) FDBException(com.apple.foundationdb.FDBException) Collections(java.util.Collections) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordCoreRetriableTransactionException(com.apple.foundationdb.record.RecordCoreRetriableTransactionException) FDBException(com.apple.foundationdb.FDBException) Index(com.apple.foundationdb.record.metadata.Index) 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