Skip to content

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
merged 20 commits into from
Jul 18, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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)) {
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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.

Copy link
Contributor

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

assert indexReshardingMetadata.isSplit() : "Index resharding state is not a split";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be before the line with getSplit()?

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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down