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;
}
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;
}
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;
});
}
Aggregations