-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Introducing indexing & deletion strategy planner interfaces #20585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Bukhtawar
merged 1 commit into
opensearch-project:main
from
shank9918:refactored-engine
Feb 27, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
server/src/main/java/org/opensearch/index/engine/DeletionStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.index.engine; | ||
|
|
||
| import org.opensearch.common.lucene.uid.Versions; | ||
| import org.opensearch.index.seqno.SequenceNumbers; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * The deletion strategy | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public final class DeletionStrategy extends OperationStrategy { | ||
|
|
||
| public final boolean currentlyDeleted; | ||
|
|
||
| private DeletionStrategy( | ||
| boolean deleteFromEngine, | ||
| boolean addStaleOpToEngine, | ||
| boolean currentlyDeleted, | ||
| long versionOfDeletion, | ||
| int reservedDocs, | ||
| Engine.DeleteResult earlyResultOnPreflightError | ||
| ) { | ||
| super(deleteFromEngine, addStaleOpToEngine, versionOfDeletion, earlyResultOnPreflightError, reservedDocs); | ||
| assert (deleteFromEngine && earlyResultOnPreflightError != null) == false | ||
| : "can only delete from engine or have a preflight result but not both." | ||
| + "deleteFromEngine: " | ||
| + deleteFromEngine | ||
| + " earlyResultOnPreFlightError:" | ||
| + earlyResultOnPreflightError; | ||
| assert reservedDocs == 0 || deleteFromEngine || addStaleOpToEngine : reservedDocs; | ||
| this.currentlyDeleted = currentlyDeleted; | ||
| } | ||
|
|
||
| static DeletionStrategy skipDueToVersionConflict(VersionConflictEngineException e, long currentVersion, boolean currentlyDeleted) { | ||
| final Engine.DeleteResult deleteResult = new Engine.DeleteResult( | ||
| e, | ||
| currentVersion, | ||
| SequenceNumbers.UNASSIGNED_PRIMARY_TERM, | ||
| SequenceNumbers.UNASSIGNED_SEQ_NO, | ||
| currentlyDeleted == false | ||
| ); | ||
| return new DeletionStrategy(false, false, currentlyDeleted, Versions.NOT_FOUND, 0, deleteResult); | ||
| } | ||
|
|
||
| static DeletionStrategy processNormally(boolean currentlyDeleted, long versionOfDeletion, int reservedDocs) { | ||
| return new DeletionStrategy(true, false, currentlyDeleted, versionOfDeletion, reservedDocs, null); | ||
|
|
||
| } | ||
|
|
||
| static DeletionStrategy processButSkipEngine(boolean currentlyDeleted, long versionOfDeletion) { | ||
| return new DeletionStrategy(false, false, currentlyDeleted, versionOfDeletion, 0, null); | ||
| } | ||
|
|
||
| static DeletionStrategy processAsStaleOp(long versionOfDeletion) { | ||
| return new DeletionStrategy(false, true, false, versionOfDeletion, 0, null); | ||
| } | ||
|
|
||
| static DeletionStrategy failAsTooManyDocs(Exception e) { | ||
| final Engine.DeleteResult deleteResult = new Engine.DeleteResult( | ||
| e, | ||
| Versions.NOT_FOUND, | ||
| SequenceNumbers.UNASSIGNED_PRIMARY_TERM, | ||
| SequenceNumbers.UNASSIGNED_SEQ_NO, | ||
| false | ||
| ); | ||
| return new DeletionStrategy(false, false, false, Versions.NOT_FOUND, 0, deleteResult); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| if (!super.equals(o)) return false; | ||
shank9918 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| DeletionStrategy that = (DeletionStrategy) o; | ||
| return currentlyDeleted == that.currentlyDeleted; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(super.hashCode(), currentlyDeleted); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "DeletionStrategy{" | ||
| + "currentlyDeleted=" | ||
| + currentlyDeleted | ||
| + ", executeOpOnEngine=" | ||
| + executeOpOnEngine | ||
| + ", addStaleOpToEngine=" | ||
| + addStaleOpToEngine | ||
| + ", version=" | ||
| + version | ||
| + ", earlyResultOnPreFlightError=" | ||
| + earlyResultOnPreFlightError | ||
| + ", reservedDocs=" | ||
| + reservedDocs | ||
| + '}'; | ||
| } | ||
| } | ||
146 changes: 146 additions & 0 deletions
146
server/src/main/java/org/opensearch/index/engine/DeletionStrategyPlanner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.index.engine; | ||
|
|
||
| import org.opensearch.common.CheckedBiFunction; | ||
| import org.opensearch.common.CheckedFunction; | ||
| import org.opensearch.common.lucene.uid.Versions; | ||
| import org.opensearch.core.index.shard.ShardId; | ||
| import org.opensearch.index.IndexSettings; | ||
| import org.opensearch.index.seqno.SequenceNumbers; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.function.BiFunction; | ||
| import java.util.function.Predicate; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Plans execution strategies for deletion operations. | ||
| * The planner produces {@link DeletionStrategy} instances that guide the engine's | ||
| * execution of delete operations on both primary and replica shards. | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public class DeletionStrategyPlanner implements OperationStrategyPlanner<Engine.Delete, DeletionStrategy> { | ||
|
|
||
| private final IndexSettings indexSettings; | ||
| private final ShardId shardId; | ||
| private final Predicate<Engine.Operation> hasBeenProcessedBefore; | ||
| private final CheckedFunction<Engine.Operation, OpVsEngineDocStatus, IOException> opVsEngineDocStatusFunction; | ||
| private final CheckedBiFunction<Engine.Operation, Boolean, VersionValue, IOException> docVersionSupplier; | ||
| private final BiFunction<Engine.Operation, Integer, Exception> tryAcquireInFlightDocs; | ||
| private final Supplier<Boolean> incrementVersionLookup; | ||
|
|
||
| public DeletionStrategyPlanner( | ||
| IndexSettings indexSettings, | ||
| ShardId shardId, | ||
| Predicate<Engine.Operation> hasBeenProcessedBefore, | ||
| CheckedFunction<Engine.Operation, OpVsEngineDocStatus, IOException> opVsEngineDocStatusFunction, | ||
| CheckedBiFunction<Engine.Operation, Boolean, VersionValue, IOException> docVersionSupplier, | ||
| BiFunction<Engine.Operation, Integer, Exception> tryAcquireInFlightDocs, | ||
| Supplier<Boolean> incrementVersionLookup | ||
| ) { | ||
| this.indexSettings = indexSettings; | ||
| this.shardId = shardId; | ||
| this.hasBeenProcessedBefore = hasBeenProcessedBefore; | ||
| this.opVsEngineDocStatusFunction = opVsEngineDocStatusFunction; | ||
| this.docVersionSupplier = docVersionSupplier; | ||
| this.tryAcquireInFlightDocs = tryAcquireInFlightDocs; | ||
| this.incrementVersionLookup = incrementVersionLookup; | ||
| } | ||
|
|
||
| @Override | ||
| public DeletionStrategy planOperationAsPrimary(Engine.Delete delete) throws IOException { | ||
| assert delete.origin() == Engine.Operation.Origin.PRIMARY : "planing as primary but got " + delete.origin(); | ||
| // resolve operation from external to internal | ||
| final VersionValue versionValue = docVersionSupplier.apply(delete, delete.getIfSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO); | ||
| assert incrementVersionLookup.get(); | ||
| final long currentVersion; | ||
| final boolean currentlyDeleted; | ||
| if (versionValue == null) { | ||
| currentVersion = Versions.NOT_FOUND; | ||
| currentlyDeleted = true; | ||
| } else { | ||
| currentVersion = versionValue.version; | ||
| currentlyDeleted = versionValue.isDelete(); | ||
| } | ||
| final DeletionStrategy plan; | ||
| if (delete.getIfSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO && currentlyDeleted) { | ||
| final VersionConflictEngineException e = new VersionConflictEngineException( | ||
| shardId, | ||
| delete.id(), | ||
| delete.getIfSeqNo(), | ||
| delete.getIfPrimaryTerm(), | ||
| SequenceNumbers.UNASSIGNED_SEQ_NO, | ||
| SequenceNumbers.UNASSIGNED_PRIMARY_TERM | ||
| ); | ||
| plan = DeletionStrategy.skipDueToVersionConflict(e, currentVersion, true); | ||
| } else if (delete.getIfSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO | ||
| && (versionValue.seqNo != delete.getIfSeqNo() || versionValue.term != delete.getIfPrimaryTerm())) { | ||
| final VersionConflictEngineException e = new VersionConflictEngineException( | ||
| shardId, | ||
| delete.id(), | ||
| delete.getIfSeqNo(), | ||
| delete.getIfPrimaryTerm(), | ||
| versionValue.seqNo, | ||
| versionValue.term | ||
| ); | ||
| plan = DeletionStrategy.skipDueToVersionConflict(e, currentVersion, currentlyDeleted); | ||
| } else if (delete.versionType().isVersionConflictForWrites(currentVersion, delete.version(), currentlyDeleted)) { | ||
| final VersionConflictEngineException e = new VersionConflictEngineException( | ||
| shardId, | ||
| delete, | ||
| currentVersion, | ||
| currentlyDeleted | ||
| ); | ||
| plan = DeletionStrategy.skipDueToVersionConflict(e, currentVersion, currentlyDeleted); | ||
| } else { | ||
| final Exception reserveError = tryAcquireInFlightDocs.apply(delete, 1); | ||
| if (reserveError != null) { | ||
| plan = DeletionStrategy.failAsTooManyDocs(reserveError); | ||
| } else { | ||
| final long versionOfDeletion = delete.versionType().updateVersion(currentVersion, delete.version()); | ||
| plan = DeletionStrategy.processNormally(currentlyDeleted, versionOfDeletion, 1); | ||
| } | ||
| } | ||
| return plan; | ||
| } | ||
|
|
||
| @Override | ||
| public DeletionStrategy planOperationAsNonPrimary(Engine.Delete delete) throws IOException { | ||
| assert assertNonPrimaryOrigin(delete); | ||
| final DeletionStrategy plan; | ||
| if (hasBeenProcessedBefore.test(delete)) { | ||
| // the operation seq# was processed thus this operation was already put into lucene | ||
| // this can happen during recovery where older operations are sent from the translog that are already | ||
| // part of the lucene commit (either from a peer recovery or a local translog) | ||
| // or due to concurrent indexing & recovery. For the former it is important to skip lucene as the operation in | ||
| // question may have been deleted in an out of order op that is not replayed. | ||
| // See testRecoverFromStoreWithOutOfOrderDelete for an example of local recovery | ||
| // See testRecoveryWithOutOfOrderDelete for an example of peer recovery | ||
| plan = DeletionStrategy.processButSkipEngine(false, delete.version()); | ||
| } else { | ||
| boolean segRepEnabled = indexSettings.isSegRepEnabledOrRemoteNode(); | ||
| final OpVsEngineDocStatus opVsLucene = opVsEngineDocStatusFunction.apply(delete); | ||
| if (opVsLucene == OpVsEngineDocStatus.OP_STALE_OR_EQUAL) { | ||
| if (segRepEnabled) { | ||
| // For segrep based indices, we can't completely rely on localCheckpointTracker | ||
| // as the preserved checkpoint may not have all the operations present in lucene | ||
| // we don't need to index it again as stale op as it would create multiple documents for same seq no | ||
| plan = DeletionStrategy.processButSkipEngine(false, delete.version()); | ||
| } else { | ||
| plan = DeletionStrategy.processAsStaleOp(delete.version()); | ||
| } | ||
| } else { | ||
| plan = DeletionStrategy.processNormally(opVsLucene == OpVsEngineDocStatus.DOC_NOT_FOUND, delete.version(), 0); | ||
| } | ||
| } | ||
| return plan; | ||
| } | ||
| } |
122 changes: 122 additions & 0 deletions
122
server/src/main/java/org/opensearch/index/engine/IndexingStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.index.engine; | ||
|
|
||
| import org.opensearch.common.lucene.uid.Versions; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * The indexing strategy | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public final class IndexingStrategy extends OperationStrategy { | ||
|
|
||
| public final boolean currentNotFoundOrDeleted; | ||
| public final boolean useUpdateDocument; | ||
|
|
||
| private IndexingStrategy( | ||
| boolean currentNotFoundOrDeleted, | ||
| boolean useUpdateDocument, | ||
| boolean indexIntoEngine, | ||
| boolean addStaleOpToEngine, | ||
| long versionForIndexing, | ||
| int reservedDocs, | ||
| Engine.IndexResult earlyResultOnPreFlightError | ||
| ) { | ||
| super(indexIntoEngine, addStaleOpToEngine, versionForIndexing, earlyResultOnPreFlightError, reservedDocs); | ||
| assert useUpdateDocument == false || indexIntoEngine : "use update is set to true, but we're not indexing into engine"; | ||
| assert (indexIntoEngine && earlyResultOnPreFlightError != null) == false | ||
| : "can only index into engine or have a preflight result but not both." | ||
| + "indexIntoEngine: " | ||
| + indexIntoEngine | ||
| + " earlyResultOnPreFlightError:" | ||
| + earlyResultOnPreFlightError; | ||
| assert reservedDocs == 0 || indexIntoEngine || addStaleOpToEngine : reservedDocs; | ||
| this.currentNotFoundOrDeleted = currentNotFoundOrDeleted; | ||
| this.useUpdateDocument = useUpdateDocument; | ||
| } | ||
|
|
||
| static IndexingStrategy optimizedAppendOnly(long versionForIndexing, int reservedDocs) { | ||
| return new IndexingStrategy(true, false, true, false, versionForIndexing, reservedDocs, null); | ||
| } | ||
|
|
||
| static IndexingStrategy skipDueToVersionConflict( | ||
| VersionConflictEngineException e, | ||
| boolean currentNotFoundOrDeleted, | ||
| long currentVersion | ||
| ) { | ||
| final Engine.IndexResult result = new Engine.IndexResult(e, currentVersion); | ||
| return new IndexingStrategy(currentNotFoundOrDeleted, false, false, false, Versions.NOT_FOUND, 0, result); | ||
| } | ||
|
|
||
| static IndexingStrategy processNormally(boolean currentNotFoundOrDeleted, long versionForIndexing, int reservedDocs) { | ||
| return new IndexingStrategy( | ||
| currentNotFoundOrDeleted, | ||
| currentNotFoundOrDeleted == false, | ||
| true, | ||
| false, | ||
| versionForIndexing, | ||
| reservedDocs, | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| static IndexingStrategy processButSkipEngine(boolean currentNotFoundOrDeleted, long versionForIndexing) { | ||
| return new IndexingStrategy(currentNotFoundOrDeleted, false, false, false, versionForIndexing, 0, null); | ||
| } | ||
|
|
||
| static IndexingStrategy processAsStaleOp(long versionForIndexing) { | ||
| return new IndexingStrategy(false, false, false, true, versionForIndexing, 0, null); | ||
| } | ||
|
|
||
| static IndexingStrategy failAsTooManyDocs(Exception e) { | ||
| final Engine.IndexResult result = new Engine.IndexResult(e, Versions.NOT_FOUND); | ||
| return new IndexingStrategy(false, false, false, false, Versions.NOT_FOUND, 0, result); | ||
| } | ||
|
|
||
| static IndexingStrategy failAsIndexAppendOnly(Engine.IndexResult result, long versionForIndexing, int reservedDocs) { | ||
| return new IndexingStrategy(false, false, false, true, versionForIndexing, reservedDocs, result); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| if (!super.equals(o)) return false; | ||
| IndexingStrategy that = (IndexingStrategy) o; | ||
| return currentNotFoundOrDeleted == that.currentNotFoundOrDeleted && useUpdateDocument == that.useUpdateDocument; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(super.hashCode(), currentNotFoundOrDeleted, useUpdateDocument); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "IndexingStrategy{" | ||
| + "currentNotFoundOrDeleted=" | ||
| + currentNotFoundOrDeleted | ||
| + ", useUpdateDocument=" | ||
| + useUpdateDocument | ||
| + ", executeOpOnEngine=" | ||
| + executeOpOnEngine | ||
| + ", addStaleOpToEngine=" | ||
| + addStaleOpToEngine | ||
| + ", version=" | ||
| + version | ||
| + ", earlyResultOnPreFlightError=" | ||
| + earlyResultOnPreFlightError | ||
| + ", reservedDocs=" | ||
| + reservedDocs | ||
| + '}'; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.