Search in sources :

Example 1 with RecordCoreStorageException

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

the class IndexingThrottle method throttledRunAsync.

@Nonnull
<R> CompletableFuture<R> throttledRunAsync(@Nonnull final Function<FDBRecordStore, CompletableFuture<R>> function, @Nonnull final BiFunction<R, Throwable, Pair<R, Throwable>> handlePostTransaction, @Nullable final BiConsumer<FDBException, List<Object>> handleLessenWork, @Nullable final List<Object> additionalLogMessageKeyValues) {
    List<Object> onlineIndexerLogMessageKeyValues = new ArrayList<>(Arrays.asList(LogMessageKeys.INDEX_NAME, common.getTargetIndexesNames(), LogMessageKeys.INDEXER_ID, common.getUuid()));
    if (additionalLogMessageKeyValues != null) {
        onlineIndexerLogMessageKeyValues.addAll(additionalLogMessageKeyValues);
    }
    AtomicInteger tries = new AtomicInteger(0);
    CompletableFuture<R> ret = new CompletableFuture<>();
    AtomicLong toWait = new AtomicLong(common.getRunner().getDatabase().getFactory().getInitialDelayMillis());
    AsyncUtil.whileTrue(() -> {
        loadConfig();
        return common.getRunner().runAsync(context -> common.getRecordStoreBuilder().copyBuilder().setContext(context).openAsync().thenCompose(store -> {
            for (Index index : common.getTargetIndexes()) {
                IndexState indexState = store.getIndexState(index);
                if (indexState != expectedIndexState) {
                    throw new RecordCoreStorageException("Unexpected index state", LogMessageKeys.INDEX_NAME, index.getName(), common.getRecordStoreBuilder().getSubspaceProvider().logKey(), common.getRecordStoreBuilder().getSubspaceProvider().toString(context), LogMessageKeys.INDEX_STATE, indexState, LogMessageKeys.INDEX_STATE_PRECONDITION, expectedIndexState);
                }
            }
            return function.apply(store);
        }), handlePostTransaction, onlineIndexerLogMessageKeyValues).handle((value, e) -> {
            if (e == null) {
                ret.complete(value);
                return AsyncUtil.READY_FALSE;
            } else {
                int currTries = tries.getAndIncrement();
                FDBException fdbE = getFDBException(e);
                if (currTries < common.config.getMaxRetries() && fdbE != null && lessenWorkCodes.contains(fdbE.getCode())) {
                    if (handleLessenWork != null) {
                        handleLessenWork.accept(fdbE, onlineIndexerLogMessageKeyValues);
                    }
                    long delay = (long) (Math.random() * toWait.get());
                    toWait.set(Math.min(toWait.get() * 2, common.getRunner().getDatabase().getFactory().getMaxDelayMillis()));
                    if (LOGGER.isWarnEnabled()) {
                        final KeyValueLogMessage message = KeyValueLogMessage.build("Retrying Runner Exception", LogMessageKeys.INDEXER_CURR_RETRY, currTries, LogMessageKeys.INDEXER_MAX_RETRIES, common.config.getMaxRetries(), LogMessageKeys.DELAY, delay, LogMessageKeys.LIMIT, limit);
                        message.addKeysAndValues(onlineIndexerLogMessageKeyValues);
                        LOGGER.warn(message.toString(), e);
                    }
                    return MoreAsyncUtil.delayedFuture(delay, TimeUnit.MILLISECONDS).thenApply(vignore3 -> true);
                } else {
                    return completeExceptionally(ret, e, onlineIndexerLogMessageKeyValues);
                }
            }
        }).thenCompose(Function.identity());
    }, common.getRunner().getExecutor()).whenComplete((vignore, e) -> {
        if (e != null) {
            // Just update ret and ignore the returned future.
            completeExceptionally(ret, e, onlineIndexerLogMessageKeyValues);
        }
    });
    return ret;
}
Also used : Arrays(java.util.Arrays) LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) RecordCoreStorageException(com.apple.foundationdb.record.RecordCoreStorageException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Pair(org.apache.commons.lang3.tuple.Pair) FDBError(com.apple.foundationdb.FDBError) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoggableException(com.apple.foundationdb.util.LoggableException) BiConsumer(java.util.function.BiConsumer) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MoreAsyncUtil(com.apple.foundationdb.async.MoreAsyncUtil) Logger(org.slf4j.Logger) IndexState(com.apple.foundationdb.record.IndexState) Set(java.util.Set) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Index(com.apple.foundationdb.record.metadata.Index) FDBException(com.apple.foundationdb.FDBException) API(com.apple.foundationdb.annotation.API) ArrayList(java.util.ArrayList) FDBException(com.apple.foundationdb.FDBException) Index(com.apple.foundationdb.record.metadata.Index) IndexState(com.apple.foundationdb.record.IndexState) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) RecordCoreStorageException(com.apple.foundationdb.record.RecordCoreStorageException) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nonnull(javax.annotation.Nonnull)

Example 2 with RecordCoreStorageException

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

the class FDBRecordStore method checkAndParseStoreHeader.

@Nonnull
private RecordMetaDataProto.DataStoreInfo checkAndParseStoreHeader(@Nullable KeyValue firstKeyValue, @Nonnull StoreExistenceCheck existenceCheck) {
    RecordMetaDataProto.DataStoreInfo info;
    if (firstKeyValue == null) {
        info = RecordMetaDataProto.DataStoreInfo.getDefaultInstance();
    } else if (!checkFirstKeyIsHeader(firstKeyValue, getContext(), getSubspaceProvider(), getSubspace(), existenceCheck)) {
        // Treat as brand new, although there is no way to be sure that what was written is compatible
        // with the current default versions.
        info = RecordMetaDataProto.DataStoreInfo.getDefaultInstance();
    } else {
        try {
            info = RecordMetaDataProto.DataStoreInfo.parseFrom(firstKeyValue.getValue());
        } catch (InvalidProtocolBufferException ex) {
            throw new RecordCoreStorageException("Error reading version", ex).addLogInfo(subspaceProvider.logKey(), subspaceProvider.toString(context));
        }
    }
    checkStoreHeaderInternal(info, getContext(), getSubspaceProvider(), existenceCheck);
    return info;
}
Also used : RecordCoreStorageException(com.apple.foundationdb.record.RecordCoreStorageException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) Nonnull(javax.annotation.Nonnull)

Example 3 with RecordCoreStorageException

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

the class TimeWindowLeaderboardIndexMaintainer method loadSubDirectory.

@Nonnull
protected CompletableFuture<TimeWindowLeaderboardSubDirectory> loadSubDirectory(@Nonnull TimeWindowLeaderboardDirectory directory, @Nonnull Tuple group) {
    TimeWindowLeaderboardSubDirectory subdirectory = directory.getSubDirectory(group);
    if (subdirectory != null) {
        return CompletableFuture.completedFuture(subdirectory);
    }
    final Subspace extraSubspace = getSecondarySubspace();
    return state.transaction.get(extraSubspace.pack(SUB_DIRECTORY_PREFIX.addAll(group))).thenApply(bytes -> {
        final TimeWindowLeaderboardSubDirectory newsub;
        if (bytes == null) {
            newsub = new TimeWindowLeaderboardSubDirectory(group, directory.isHighScoreFirst());
        } else {
            TimeWindowLeaderboardProto.TimeWindowLeaderboardSubDirectory.Builder builder = TimeWindowLeaderboardProto.TimeWindowLeaderboardSubDirectory.newBuilder();
            try {
                builder.mergeFrom(bytes);
            } catch (InvalidProtocolBufferException ex) {
                throw new RecordCoreStorageException("error decoding leaderboard sub-directory", ex);
            }
            newsub = new TimeWindowLeaderboardSubDirectory(group, builder.build());
        }
        directory.addSubDirectory(newsub);
        return newsub;
    });
}
Also used : RecordCoreStorageException(com.apple.foundationdb.record.RecordCoreStorageException) Subspace(com.apple.foundationdb.subspace.Subspace) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Nonnull(javax.annotation.Nonnull)

Aggregations

RecordCoreStorageException (com.apple.foundationdb.record.RecordCoreStorageException)3 Nonnull (javax.annotation.Nonnull)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 FDBError (com.apple.foundationdb.FDBError)1 FDBException (com.apple.foundationdb.FDBException)1 API (com.apple.foundationdb.annotation.API)1 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)1 MoreAsyncUtil (com.apple.foundationdb.async.MoreAsyncUtil)1 IndexState (com.apple.foundationdb.record.IndexState)1 RecordMetaDataProto (com.apple.foundationdb.record.RecordMetaDataProto)1 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)1 LogMessageKeys (com.apple.foundationdb.record.logging.LogMessageKeys)1 Index (com.apple.foundationdb.record.metadata.Index)1 Subspace (com.apple.foundationdb.subspace.Subspace)1 LoggableException (com.apple.foundationdb.util.LoggableException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1