use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class FDBRecordStore method addStoreStateReadConflict.
/**
* Add a read conflict key for the whole record store state.
*/
private void addStoreStateReadConflict() {
if (storeStateReadConflict) {
return;
}
storeStateReadConflict = true;
Transaction tr = ensureContextActive();
byte[] indexStateKey = getSubspace().pack(Tuple.from(INDEX_STATE_SPACE_KEY));
tr.addReadConflictRange(indexStateKey, ByteArrayUtil.strinc(indexStateKey));
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class FDBRecordStore method removeFormerIndex.
public void removeFormerIndex(FormerIndex formerIndex) {
if (LOGGER.isDebugEnabled()) {
KeyValueLogMessage msg = KeyValueLogMessage.build("removing index", subspaceProvider.logKey(), subspaceProvider.toString(context), LogMessageKeys.SUBSPACE_KEY, formerIndex.getSubspaceKey());
if (formerIndex.getFormerName() != null) {
msg.addKeyAndValue(LogMessageKeys.INDEX_NAME, formerIndex.getFormerName());
}
LOGGER.debug(msg.toString());
}
final long startTime = System.nanoTime();
Transaction tr = ensureContextActive();
tr.clear(getSubspace().range(Tuple.from(INDEX_KEY, formerIndex.getSubspaceTupleKey())));
tr.clear(getSubspace().range(Tuple.from(INDEX_SECONDARY_SPACE_KEY, formerIndex.getSubspaceTupleKey())));
tr.clear(getSubspace().range(Tuple.from(INDEX_RANGE_SPACE_KEY, formerIndex.getSubspaceTupleKey())));
tr.clear(getSubspace().pack(Tuple.from(INDEX_STATE_SPACE_KEY, formerIndex.getSubspaceTupleKey())));
tr.clear(getSubspace().range(Tuple.from(INDEX_UNIQUENESS_VIOLATIONS_KEY, formerIndex.getSubspaceTupleKey())));
if (getTimer() != null) {
getTimer().recordSinceNanoTime(FDBStoreTimer.Events.REMOVE_FORMER_INDEX, startTime);
}
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class FDBRecordStore method checkPossiblyRebuildRecordCounts.
protected boolean checkPossiblyRebuildRecordCounts(@Nonnull RecordMetaData metaData, @Nonnull RecordMetaDataProto.DataStoreInfo.Builder info, @Nonnull List<CompletableFuture<Void>> work, int oldFormatVersion) {
KeyExpression countKeyExpression = metaData.getRecordCountKey();
boolean rebuildRecordCounts = (oldFormatVersion > 0 && oldFormatVersion < RECORD_COUNT_ADDED_FORMAT_VERSION) || (countKeyExpression != null && formatVersion >= RECORD_COUNT_KEY_ADDED_FORMAT_VERSION && (!info.hasRecordCountKey() || !KeyExpression.fromProto(info.getRecordCountKey()).equals(countKeyExpression)));
if (rebuildRecordCounts) {
// We want to clear all record counts.
final Transaction tr = ensureContextActive();
tr.clear(getSubspace().range(Tuple.from(RECORD_COUNT_KEY)));
// Set the new record count key if we have one.
if (formatVersion >= RECORD_COUNT_KEY_ADDED_FORMAT_VERSION) {
if (countKeyExpression != null) {
info.setRecordCountKey(countKeyExpression.toKeyExpression());
} else {
info.clearRecordCountKey();
}
}
// Add the record rebuild job.
addRebuildRecordCountsJob(work);
}
return rebuildRecordCounts;
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class FDBRecordStore method markIndexReadable.
/**
* Marks an index as readable. See the version of
* {@link #markIndexReadable(String) markIndexReadable()}
* that takes a {@link String} as a parameter for more details.
*
* @param index the index to mark readable
* @return a future that will either complete exceptionally if the index can not
* be made readable or will contain <code>true</code> if the store was modified
* and <code>false</code> otherwise
*/
@Nonnull
public CompletableFuture<Boolean> markIndexReadable(@Nonnull Index index) {
if (recordStoreStateRef.get() == null) {
return preloadRecordStoreStateAsync().thenCompose(vignore -> markIndexReadable(index));
}
addIndexStateReadConflict(index.getName());
beginRecordStoreStateWrite();
boolean haveFuture = false;
try {
Transaction tr = ensureContextActive();
byte[] indexKey = indexStateSubspace().pack(index.getName());
CompletableFuture<Boolean> future = tr.get(indexKey).thenCompose(previous -> {
if (previous != null) {
CompletableFuture<Optional<Range>> builtFuture = firstUnbuiltRange(index);
CompletableFuture<Optional<RecordIndexUniquenessViolation>> uniquenessFuture = whenAllIndexUniquenessCommitChecks(index).thenCompose(vignore -> scanUniquenessViolations(index, 1).first());
return CompletableFuture.allOf(builtFuture, uniquenessFuture).thenApply(vignore -> {
Optional<Range> firstUnbuilt = context.join(builtFuture);
Optional<RecordIndexUniquenessViolation> uniquenessViolation = context.join(uniquenessFuture);
if (firstUnbuilt.isPresent()) {
throw new IndexNotBuiltException("Attempted to make unbuilt index readable", firstUnbuilt.get(), LogMessageKeys.INDEX_NAME, index.getName(), "unbuiltRangeBegin", ByteArrayUtil2.loggable(firstUnbuilt.get().begin), "unbuiltRangeEnd", ByteArrayUtil2.loggable(firstUnbuilt.get().end), subspaceProvider.logKey(), subspaceProvider.toString(context), LogMessageKeys.SUBSPACE_KEY, index.getSubspaceKey());
} else if (uniquenessViolation.isPresent()) {
RecordIndexUniquenessViolation wrapped = new RecordIndexUniquenessViolation("Uniqueness violation when making index readable", uniquenessViolation.get());
wrapped.addLogInfo(LogMessageKeys.INDEX_NAME, index.getName(), subspaceProvider.logKey(), subspaceProvider.toString(context));
throw wrapped;
} else {
updateIndexState(index.getName(), indexKey, IndexState.READABLE);
clearReadableIndexBuildData(tr, index);
return true;
}
});
} else {
return AsyncUtil.READY_FALSE;
}
}).whenComplete((b, t) -> endRecordStoreStateWrite()).thenApply(this::addRemoveReplacedIndexesCommitCheckIfChanged);
haveFuture = true;
return future;
} finally {
if (!haveFuture) {
endRecordStoreStateWrite();
}
}
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class FDBRecordStore method clearIndexData.
// Clear the data associated with a given index. This is only safe to do if one is
// either going to rebuild it or disable it. It is therefore package private.
// TODO: Better to go through the index maintainer?
void clearIndexData(@Nonnull Index index) {
Transaction tr = ensureContextActive();
// startsWith to handle ungrouped aggregate indexes
tr.clear(Range.startsWith(indexSubspace(index).pack()));
tr.clear(indexSecondarySubspace(index).range());
tr.clear(indexRangeSubspace(index).range());
tr.clear(indexUniquenessViolationsSubspace(index).range());
// Under the index build subspace, there are 3 lower level subspaces, the lock space, the scanned records
// subspace, and the type/stamp subspace. We are not supposed to clear the lock subspace, which is used to
// run online index jobs which may invoke this method. We should clear:
// * the scanned records subspace. Which, roughly speaking, counts how many records of this store are covered in
// index range subspace.
// * the type/stamp subspace. Which indicates which type of indexing is in progress.
tr.clear(Range.startsWith(OnlineIndexer.indexBuildScannedRecordsSubspace(this, index).pack()));
tr.clear(Range.startsWith(OnlineIndexer.indexBuildTypeSubspace(this, index).pack()));
}
Aggregations