|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.engine; |
| 10 | + |
| 11 | +import org.opensearch.common.CheckedBiFunction; |
| 12 | +import org.opensearch.common.CheckedFunction; |
| 13 | +import org.opensearch.common.lucene.uid.Versions; |
| 14 | +import org.opensearch.core.index.shard.ShardId; |
| 15 | +import org.opensearch.index.engine.exec.bridge.Indexer; |
| 16 | +import org.opensearch.index.seqno.SequenceNumbers; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.function.BiFunction; |
| 20 | +import java.util.function.Predicate; |
| 21 | + |
| 22 | +public class DeletionStrategyPlanner implements OperationStrategyPlanner { |
| 23 | + |
| 24 | + private final EngineConfig engineConfig; |
| 25 | + private final ShardId shardId; |
| 26 | + private final Predicate<Engine.Operation> hasBeenProcessedBefore; |
| 27 | + private final CheckedFunction<Engine.Operation, Indexer.OpVsEngineDocStatus, IOException> opVsEngineDocStatusFunction; |
| 28 | + private final CheckedBiFunction<Engine.Operation, Boolean, VersionValue, IOException> docVersionSupplier; |
| 29 | + private final BiFunction<Engine.Operation, Integer, Exception> tryAcquireInFlightDocs; |
| 30 | + |
| 31 | + public DeletionStrategyPlanner( |
| 32 | + EngineConfig engineConfig, |
| 33 | + ShardId shardId, |
| 34 | + Predicate<Engine.Operation> hasBeenProcessedBefore, |
| 35 | + CheckedFunction<Engine.Operation, Indexer.OpVsEngineDocStatus, IOException> opVsEngineDocStatusFunction, |
| 36 | + CheckedBiFunction<Engine.Operation, Boolean, VersionValue, IOException> docVersionSupplier, |
| 37 | + BiFunction<Engine.Operation, Integer, Exception> tryAcquireInFlightDocs |
| 38 | + ) { |
| 39 | + this.engineConfig = engineConfig; |
| 40 | + this.shardId = shardId; |
| 41 | + this.hasBeenProcessedBefore = hasBeenProcessedBefore; |
| 42 | + this.opVsEngineDocStatusFunction = opVsEngineDocStatusFunction; |
| 43 | + this.docVersionSupplier = docVersionSupplier; |
| 44 | + this.tryAcquireInFlightDocs = tryAcquireInFlightDocs; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public DeletionStrategy planOperationAsPrimary(Engine.Operation operation) throws IOException { |
| 49 | + final Engine.Delete delete = (Engine.Delete) operation; |
| 50 | + assert delete.origin() == Engine.Operation.Origin.PRIMARY : "planing as primary but got " + delete.origin(); |
| 51 | + // resolve operation from external to internal |
| 52 | + final VersionValue versionValue = docVersionSupplier.apply(delete, delete.getIfSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO); |
| 53 | + // TODO - assert incrementVersionLookup(); |
| 54 | + final long currentVersion; |
| 55 | + final boolean currentlyDeleted; |
| 56 | + if (versionValue == null) { |
| 57 | + currentVersion = Versions.NOT_FOUND; |
| 58 | + currentlyDeleted = true; |
| 59 | + } else { |
| 60 | + currentVersion = versionValue.version; |
| 61 | + currentlyDeleted = versionValue.isDelete(); |
| 62 | + } |
| 63 | + final DeletionStrategy plan; |
| 64 | + if (delete.getIfSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO && currentlyDeleted) { |
| 65 | + final VersionConflictEngineException e = new VersionConflictEngineException( |
| 66 | + shardId, |
| 67 | + delete.id(), |
| 68 | + delete.getIfSeqNo(), |
| 69 | + delete.getIfPrimaryTerm(), |
| 70 | + SequenceNumbers.UNASSIGNED_SEQ_NO, |
| 71 | + SequenceNumbers.UNASSIGNED_PRIMARY_TERM |
| 72 | + ); |
| 73 | + plan = DeletionStrategy.skipDueToVersionConflict(e, currentVersion, true); |
| 74 | + } else if (delete.getIfSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO && (versionValue.seqNo != delete.getIfSeqNo() |
| 75 | + || versionValue.term != delete.getIfPrimaryTerm())) { |
| 76 | + final VersionConflictEngineException e = new VersionConflictEngineException( |
| 77 | + shardId, |
| 78 | + delete.id(), |
| 79 | + delete.getIfSeqNo(), |
| 80 | + delete.getIfPrimaryTerm(), |
| 81 | + versionValue.seqNo, |
| 82 | + versionValue.term |
| 83 | + ); |
| 84 | + plan = DeletionStrategy.skipDueToVersionConflict(e, currentVersion, currentlyDeleted); |
| 85 | + } else if (delete.versionType().isVersionConflictForWrites(currentVersion, delete.version(), currentlyDeleted)) { |
| 86 | + final VersionConflictEngineException e = new VersionConflictEngineException(shardId, delete, currentVersion, currentlyDeleted); |
| 87 | + plan = DeletionStrategy.skipDueToVersionConflict(e, currentVersion, currentlyDeleted); |
| 88 | + } else { |
| 89 | + final Exception reserveError = tryAcquireInFlightDocs.apply(delete, 1); |
| 90 | + if (reserveError != null) { |
| 91 | + plan = DeletionStrategy.failAsTooManyDocs(reserveError); |
| 92 | + } else { |
| 93 | + final long versionOfDeletion = delete.versionType().updateVersion(currentVersion, delete.version()); |
| 94 | + plan = DeletionStrategy.processNormally(currentlyDeleted, versionOfDeletion, 1); |
| 95 | + } |
| 96 | + } |
| 97 | + return plan; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public DeletionStrategy planOperationAsNonPrimary(Engine.Operation operation) throws IOException { |
| 102 | + final Engine.Delete delete = (Engine.Delete) operation; |
| 103 | + assert operation.origin() != Engine.Operation.Origin.PRIMARY : "planing as primary but got " + operation.origin(); |
| 104 | + final DeletionStrategy plan; |
| 105 | + if (hasBeenProcessedBefore.test(delete)) { |
| 106 | + // the operation seq# was processed thus this operation was already put into lucene |
| 107 | + // this can happen during recovery where older operations are sent from the translog that are already |
| 108 | + // part of the lucene commit (either from a peer recovery or a local translog) |
| 109 | + // or due to concurrent indexing & recovery. For the former it is important to skip lucene as the operation in |
| 110 | + // question may have been deleted in an out of order op that is not replayed. |
| 111 | + // See testRecoverFromStoreWithOutOfOrderDelete for an example of local recovery |
| 112 | + // See testRecoveryWithOutOfOrderDelete for an example of peer recovery |
| 113 | + plan = DeletionStrategy.processButSkipLucene(false, delete.version()); |
| 114 | + } else { |
| 115 | + boolean segRepEnabled = engineConfig.getIndexSettings().isSegRepEnabledOrRemoteNode(); |
| 116 | + final Indexer.OpVsEngineDocStatus opVsLucene = opVsEngineDocStatusFunction.apply(delete); |
| 117 | + if (opVsLucene == Indexer.OpVsEngineDocStatus.OP_STALE_OR_EQUAL) { |
| 118 | + if (segRepEnabled) { |
| 119 | + // For segrep based indices, we can't completely rely on localCheckpointTracker |
| 120 | + // as the preserved checkpoint may not have all the operations present in lucene |
| 121 | + // we don't need to index it again as stale op as it would create multiple documents for same seq no |
| 122 | + plan = DeletionStrategy.processButSkipLucene(false, delete.version()); |
| 123 | + } else { |
| 124 | + plan = DeletionStrategy.processAsStaleOp(delete.version()); |
| 125 | + } |
| 126 | + } else { |
| 127 | + plan = DeletionStrategy.processNormally(opVsLucene == Indexer.OpVsEngineDocStatus.DOC_NOT_FOUND, delete.version(), 0); |
| 128 | + } |
| 129 | + } |
| 130 | + return plan; |
| 131 | + } |
| 132 | +} |
0 commit comments