-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Resharding: Route requests to source shard while target is pre-handoff #131306
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
ankikuma
merged 20 commits into
elastic:main
from
ankikuma:07/13/2025/ReshardRouteIndexingRequest
Jul 18, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
89fc052
Route documents based on resharding metadata
ankikuma 718ed14
spotless
ankikuma 9650bb3
minor name change
ankikuma c7689d2
Merge remote-tracking branch 'upstream/main' into 07/13/2025/ReshardR…
ankikuma faed81f
Merge remote-tracking branch 'upstream/main' into 07/13/2025/ReshardR…
ankikuma 91fa459
Merge remote-tracking branch 'upstream/main' into 07/13/2025/ReshardR…
ankikuma 84c8b45
review comments
ankikuma 6633f3b
Merge remote-tracking branch 'upstream/main' into 07/13/2025/ReshardR…
ankikuma 34d1625
Merge remote-tracking branch 'upstream/main' into 07/13/2025/ReshardR…
ankikuma 7b154ca
Merge remote-tracking branch 'upstream/main' into 07/13/2025/ReshardR…
ankikuma 464b292
bwc changes
ankikuma 2862158
[CI] Auto commit changes from spotless
8bb99a9
bwc change
ankikuma 25a7807
Merge remote-tracking branch 'upstream/main' into 07/13/2025/ReshardR…
ankikuma 755cb7a
remove index version
ankikuma 2260547
Merge remote-tracking branch 'upstream/main' into 07/13/2025/ReshardR…
ankikuma 94cb58c
remove index version change
ankikuma e641a47
remove index version change
ankikuma 8570ad5
remove index version change
ankikuma b21d779
spotless
ankikuma 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
import org.elasticsearch.action.RoutingMissingException; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
import org.elasticsearch.cluster.metadata.IndexReshardingMetadata; | ||
import org.elasticsearch.cluster.metadata.IndexReshardingState; | ||
import org.elasticsearch.cluster.metadata.MappingMetadata; | ||
import org.elasticsearch.common.ParsingException; | ||
import org.elasticsearch.common.Strings; | ||
|
@@ -73,11 +75,13 @@ public static IndexRouting fromIndexMetadata(IndexMetadata metadata) { | |
protected final String indexName; | ||
private final int routingNumShards; | ||
private final int routingFactor; | ||
private final IndexReshardingMetadata indexReshardingMetadata; | ||
|
||
private IndexRouting(IndexMetadata metadata) { | ||
this.indexName = metadata.getIndex().getName(); | ||
this.routingNumShards = metadata.getRoutingNumShards(); | ||
this.routingFactor = metadata.getRoutingFactor(); | ||
this.indexReshardingMetadata = metadata.getReshardingMetadata(); | ||
} | ||
|
||
/** | ||
|
@@ -149,6 +153,23 @@ private static int effectiveRoutingToHash(String effectiveRouting) { | |
*/ | ||
public void checkIndexSplitAllowed() {} | ||
|
||
/** | ||
* If this index is in the process of resharding, and the shard to which this request is being routed, | ||
* is a target shard that is not yet in HANDOFF state, then route it to the source shard. | ||
* @param shardId shardId to which the current document is routed based on hashing | ||
* @return Updated shardId | ||
*/ | ||
protected final int rerouteIfResharding(int shardId) { | ||
if (indexReshardingMetadata != null && indexReshardingMetadata.getSplit().isTargetShard(shardId)) { | ||
assert indexReshardingMetadata.isSplit() : "Index resharding state is not a split"; | ||
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. Shouldn't this be before the line with |
||
if (indexReshardingMetadata.getSplit() | ||
.targetStateAtLeast(shardId, IndexReshardingState.Split.TargetShardState.HANDOFF) == false) { | ||
return indexReshardingMetadata.getSplit().sourceShard(shardId); | ||
} | ||
} | ||
return shardId; | ||
} | ||
|
||
private abstract static class IdAndRoutingOnly extends IndexRouting { | ||
private final boolean routingRequired; | ||
private final IndexVersion creationVersion; | ||
|
@@ -195,19 +216,22 @@ public int indexShard(String id, @Nullable String routing, XContentType sourceTy | |
throw new IllegalStateException("id is required and should have been set by process"); | ||
} | ||
checkRoutingRequired(id, routing); | ||
return shardId(id, routing); | ||
int shardId = shardId(id, routing); | ||
return rerouteIfResharding(shardId); | ||
} | ||
|
||
@Override | ||
public int updateShard(String id, @Nullable String routing) { | ||
checkRoutingRequired(id, routing); | ||
return shardId(id, routing); | ||
int shardId = shardId(id, routing); | ||
return rerouteIfResharding(shardId); | ||
} | ||
|
||
@Override | ||
public int deleteShard(String id, @Nullable String routing) { | ||
checkRoutingRequired(id, routing); | ||
return shardId(id, routing); | ||
int shardId = shardId(id, routing); | ||
return rerouteIfResharding(shardId); | ||
} | ||
|
||
@Override | ||
|
@@ -314,7 +338,8 @@ public int indexShard(String id, @Nullable String routing, XContentType sourceTy | |
assert Transports.assertNotTransportThread("parsing the _source can get slow"); | ||
checkNoRouting(routing); | ||
hash = hashSource(sourceType, source).buildHash(IndexRouting.ExtractFromSource::defaultOnEmpty); | ||
return hashToShardId(hash); | ||
int shardId = hashToShardId(hash); | ||
return (rerouteIfResharding(shardId)); | ||
} | ||
|
||
public String createId(XContentType sourceType, BytesReference source, byte[] suffix) { | ||
|
@@ -454,13 +479,15 @@ public int updateShard(String id, @Nullable String routing) { | |
@Override | ||
public int deleteShard(String id, @Nullable String routing) { | ||
checkNoRouting(routing); | ||
return idToHash(id); | ||
int shardId = idToHash(id); | ||
return (rerouteIfResharding(shardId)); | ||
} | ||
|
||
@Override | ||
public int getShard(String id, @Nullable String routing) { | ||
checkNoRouting(routing); | ||
return idToHash(id); | ||
int shardId = idToHash(id); | ||
return (rerouteIfResharding(shardId)); | ||
} | ||
|
||
private void checkNoRouting(@Nullable String routing) { | ||
|
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.
nit: should we check for
indexReshardingMetadata.isSplit()
? It's technically possible.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.
➕
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.
possibly we should assert it is split, because I don't know right now what the correct behaviour here would be if it isn't. Good chance that we'll also need some logic here for any other resharding type.
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.
yes i am okay with assert