Skip to content

Commit a6d6d3e

Browse files
authored
Resharding: Route requests to source shard while target is pre-handoff (#131306)
This PR for the Autosharding project, routes DocWriteRequests (index/delete/update requests) that are meant for the target shards, to source shards, until the target shard is ready.
1 parent 330deba commit a6d6d3e

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.elasticsearch.action.RoutingMissingException;
1616
import org.elasticsearch.action.index.IndexRequest;
1717
import org.elasticsearch.cluster.metadata.IndexMetadata;
18+
import org.elasticsearch.cluster.metadata.IndexReshardingMetadata;
19+
import org.elasticsearch.cluster.metadata.IndexReshardingState;
1820
import org.elasticsearch.cluster.metadata.MappingMetadata;
1921
import org.elasticsearch.common.ParsingException;
2022
import org.elasticsearch.common.Strings;
@@ -73,11 +75,13 @@ public static IndexRouting fromIndexMetadata(IndexMetadata metadata) {
7375
protected final String indexName;
7476
private final int routingNumShards;
7577
private final int routingFactor;
78+
private final IndexReshardingMetadata indexReshardingMetadata;
7679

7780
private IndexRouting(IndexMetadata metadata) {
7881
this.indexName = metadata.getIndex().getName();
7982
this.routingNumShards = metadata.getRoutingNumShards();
8083
this.routingFactor = metadata.getRoutingFactor();
84+
this.indexReshardingMetadata = metadata.getReshardingMetadata();
8185
}
8286

8387
/**
@@ -149,6 +153,23 @@ private static int effectiveRoutingToHash(String effectiveRouting) {
149153
*/
150154
public void checkIndexSplitAllowed() {}
151155

156+
/**
157+
* If this index is in the process of resharding, and the shard to which this request is being routed,
158+
* is a target shard that is not yet in HANDOFF state, then route it to the source shard.
159+
* @param shardId shardId to which the current document is routed based on hashing
160+
* @return Updated shardId
161+
*/
162+
protected final int rerouteIfResharding(int shardId) {
163+
if (indexReshardingMetadata != null && indexReshardingMetadata.getSplit().isTargetShard(shardId)) {
164+
assert indexReshardingMetadata.isSplit() : "Index resharding state is not a split";
165+
if (indexReshardingMetadata.getSplit()
166+
.targetStateAtLeast(shardId, IndexReshardingState.Split.TargetShardState.HANDOFF) == false) {
167+
return indexReshardingMetadata.getSplit().sourceShard(shardId);
168+
}
169+
}
170+
return shardId;
171+
}
172+
152173
private abstract static class IdAndRoutingOnly extends IndexRouting {
153174
private final boolean routingRequired;
154175
private final IndexVersion creationVersion;
@@ -195,19 +216,22 @@ public int indexShard(String id, @Nullable String routing, XContentType sourceTy
195216
throw new IllegalStateException("id is required and should have been set by process");
196217
}
197218
checkRoutingRequired(id, routing);
198-
return shardId(id, routing);
219+
int shardId = shardId(id, routing);
220+
return rerouteIfResharding(shardId);
199221
}
200222

201223
@Override
202224
public int updateShard(String id, @Nullable String routing) {
203225
checkRoutingRequired(id, routing);
204-
return shardId(id, routing);
226+
int shardId = shardId(id, routing);
227+
return rerouteIfResharding(shardId);
205228
}
206229

207230
@Override
208231
public int deleteShard(String id, @Nullable String routing) {
209232
checkRoutingRequired(id, routing);
210-
return shardId(id, routing);
233+
int shardId = shardId(id, routing);
234+
return rerouteIfResharding(shardId);
211235
}
212236

213237
@Override
@@ -314,7 +338,8 @@ public int indexShard(String id, @Nullable String routing, XContentType sourceTy
314338
assert Transports.assertNotTransportThread("parsing the _source can get slow");
315339
checkNoRouting(routing);
316340
hash = hashSource(sourceType, source).buildHash(IndexRouting.ExtractFromSource::defaultOnEmpty);
317-
return hashToShardId(hash);
341+
int shardId = hashToShardId(hash);
342+
return (rerouteIfResharding(shardId));
318343
}
319344

320345
public String createId(XContentType sourceType, BytesReference source, byte[] suffix) {
@@ -454,13 +479,15 @@ public int updateShard(String id, @Nullable String routing) {
454479
@Override
455480
public int deleteShard(String id, @Nullable String routing) {
456481
checkNoRouting(routing);
457-
return idToHash(id);
482+
int shardId = idToHash(id);
483+
return (rerouteIfResharding(shardId));
458484
}
459485

460486
@Override
461487
public int getShard(String id, @Nullable String routing) {
462488
checkNoRouting(routing);
463-
return idToHash(id);
489+
int shardId = idToHash(id);
490+
return (rerouteIfResharding(shardId));
464491
}
465492

466493
private void checkNoRouting(@Nullable String routing) {

0 commit comments

Comments
 (0)