-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Adjust translog operation assertions for synthetic source #119330
Open
dnhatn
wants to merge
1
commit into
elastic:main
Choose a base branch
from
dnhatn:relax-translog-assertion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 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
This file contains 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
This file contains 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
This file contains 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
127 changes: 127 additions & 0 deletions
127
server/src/main/java/org/elasticsearch/index/engine/TranslogOperationAsserter.java
This file contains 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,127 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.index.engine; | ||
|
||
import org.elasticsearch.index.mapper.DocumentParser; | ||
import org.elasticsearch.index.mapper.MappingLookup; | ||
import org.elasticsearch.index.shard.ShardId; | ||
import org.elasticsearch.index.translog.Translog; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
/** | ||
* | ||
* A utility class to assert that translog operations with the same sequence number | ||
* in the same generation are either identical or equivalent when synthetic sources are used. | ||
*/ | ||
public abstract class TranslogOperationAsserter { | ||
public static final TranslogOperationAsserter DEFAULT = new TranslogOperationAsserter() { | ||
}; | ||
|
||
private TranslogOperationAsserter() { | ||
|
||
} | ||
|
||
public static TranslogOperationAsserter withEngineConfig(EngineConfig engineConfig) { | ||
return new TranslogOperationAsserter() { | ||
@Override | ||
boolean sameIndexOperation(Translog.Index o1, Translog.Index o2) throws IOException { | ||
if (super.sameIndexOperation(o1, o2)) { | ||
return true; | ||
} | ||
if (engineConfig.getIndexSettings().isRecoverySourceSyntheticEnabled()) { | ||
return super.sameIndexOperation(synthesizeSource(engineConfig, o1), o2) | ||
|| super.sameIndexOperation(o1, synthesizeSource(engineConfig, o2)); | ||
} | ||
return false; | ||
} | ||
}; | ||
} | ||
|
||
static Translog.Index synthesizeSource(EngineConfig engineConfig, Translog.Index op) throws IOException { | ||
final ShardId shardId = engineConfig.getShardId(); | ||
MappingLookup mappingLookup = engineConfig.getMapperService().mappingLookup(); | ||
DocumentParser documentParser = engineConfig.getMapperService().documentParser(); | ||
try (var reader = new TranslogDirectoryReader(shardId, op, mappingLookup, documentParser, engineConfig, () -> {})) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, I was wondering whether opening a translog as a Lucene index was possible. This is direction I was thinking as well 👍 |
||
final Engine.Searcher searcher = new Engine.Searcher( | ||
"assert_translog", | ||
reader, | ||
engineConfig.getSimilarity(), | ||
engineConfig.getQueryCache(), | ||
engineConfig.getQueryCachingPolicy(), | ||
() -> {} | ||
); | ||
try ( | ||
LuceneSyntheticSourceChangesSnapshot snapshot = new LuceneSyntheticSourceChangesSnapshot( | ||
mappingLookup, | ||
searcher, | ||
LuceneSyntheticSourceChangesSnapshot.DEFAULT_BATCH_SIZE, | ||
Integer.MAX_VALUE, | ||
op.seqNo(), | ||
op.seqNo(), | ||
true, | ||
false, | ||
engineConfig.getIndexSettings().getIndexVersionCreated() | ||
) | ||
) { | ||
final Translog.Operation normalized = snapshot.next(); | ||
assert normalized != null : "expected one operation; got zero"; | ||
return (Translog.Index) normalized; | ||
} | ||
} | ||
} | ||
|
||
boolean sameIndexOperation(Translog.Index o1, Translog.Index o2) throws IOException { | ||
// TODO: We haven't had timestamp for Index operations in Lucene yet, we need to loosen this check without timestamp. | ||
return Objects.equals(o1.id(), o2.id()) | ||
&& Objects.equals(o1.source(), o2.source()) | ||
&& Objects.equals(o1.routing(), o2.routing()) | ||
&& o1.primaryTerm() == o2.primaryTerm() | ||
&& o1.seqNo() == o2.seqNo() | ||
&& o1.version() == o2.version(); | ||
} | ||
|
||
public boolean assertSameOperations( | ||
long seqNo, | ||
long generation, | ||
Translog.Operation newOp, | ||
Translog.Operation prvOp, | ||
Exception prevFailure | ||
) throws IOException { | ||
final boolean sameOp; | ||
if (newOp instanceof final Translog.Index o2 && prvOp instanceof final Translog.Index o1) { | ||
sameOp = sameIndexOperation(o1, o2); | ||
} else if (newOp instanceof final Translog.Delete o1 && prvOp instanceof final Translog.Delete o2) { | ||
sameOp = Objects.equals(o1.id(), o2.id()) | ||
&& o1.primaryTerm() == o2.primaryTerm() | ||
&& o1.seqNo() == o2.seqNo() | ||
&& o1.version() == o2.version(); | ||
} else { | ||
sameOp = false; | ||
} | ||
if (sameOp == false) { | ||
throw new AssertionError( | ||
"seqNo [" | ||
+ seqNo | ||
+ "] was processed twice in generation [" | ||
+ generation | ||
+ "], with different data. " | ||
+ "prvOp [" | ||
+ prvOp | ||
+ "], newOp [" | ||
+ newOp | ||
+ "]", | ||
prevFailure | ||
); | ||
} | ||
return true; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a dedicated unit test class for
TranslogOperationAsserter
?