-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Pull index routing into strategy object #77211
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1b2b4d1
Pull index routing into strategy object
nik9000 2645ede
Merge branch 'master' into index_routing
nik9000 8f059d1
WIP
nik9000 5ba5ad2
Merge branch 'master' into index_routing
nik9000 c893bb0
Merge branch 'master' into index_routing
nik9000 d3d12f6
Switch build check
nik9000 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
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
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
119 changes: 119 additions & 0 deletions
119
server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java
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 |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.cluster.routing; | ||
|
|
||
| import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.core.Nullable; | ||
|
|
||
| import java.util.function.IntConsumer; | ||
|
|
||
| /** | ||
| * Generates the shard id for {@code (id, routing)} pairs. | ||
| */ | ||
| public abstract class IndexRouting { | ||
| /** | ||
| * Build the routing from {@link IndexMetadata}. | ||
| */ | ||
| public static IndexRouting fromIndexMetadata(IndexMetadata indexMetadata) { | ||
| if (indexMetadata.isRoutingPartitionedIndex()) { | ||
| return new Partitioned( | ||
| indexMetadata.getRoutingNumShards(), | ||
| indexMetadata.getRoutingFactor(), | ||
| indexMetadata.getRoutingPartitionSize() | ||
| ); | ||
| } | ||
| return new Unpartitioned(indexMetadata.getRoutingNumShards(), indexMetadata.getRoutingFactor()); | ||
| } | ||
|
|
||
| private final int routingNumShards; | ||
| private final int routingFactor; | ||
|
|
||
| private IndexRouting(int routingNumShards, int routingFactor) { | ||
| this.routingNumShards = routingNumShards; | ||
| this.routingFactor = routingFactor; | ||
| } | ||
|
|
||
| /** | ||
| * Generate the single shard id that should contain a document with the | ||
| * provided {@code id} and {@code routing}. | ||
| */ | ||
| public abstract int shardId(String id, @Nullable String routing); | ||
|
|
||
| /** | ||
| * Collect all of the shard ids that *may* contain documents with the | ||
| * provided {@code routing}. Indices with a {@code routing_partition} | ||
| * will collect more than one shard. Indices without a partition | ||
| * will collect the same shard id as would be returned | ||
| * by {@link #shardId}. | ||
| */ | ||
| public abstract void collectSearchShards(String routing, IntConsumer consumer); | ||
|
|
||
| /** | ||
| * Convert a hash generated from an {@code (id, routing}) pair into a | ||
| * shard id. | ||
| */ | ||
| protected final int hashToShardId(int hash) { | ||
| return Math.floorMod(hash, routingNumShards) / routingFactor; | ||
| } | ||
|
|
||
| /** | ||
| * Convert a routing value into a hash. | ||
| */ | ||
| private static int effectiveRoutingToHash(String effectiveRouting) { | ||
| return Murmur3HashFunction.hash(effectiveRouting); | ||
| } | ||
|
|
||
| /** | ||
| * Strategy for indices that are not partitioned. | ||
| */ | ||
| private static class Unpartitioned extends IndexRouting { | ||
| Unpartitioned(int routingNumShards, int routingFactor) { | ||
| super(routingNumShards, routingFactor); | ||
| } | ||
|
|
||
| @Override | ||
| public int shardId(String id, @Nullable String routing) { | ||
| return hashToShardId(effectiveRoutingToHash(routing == null ? id : routing)); | ||
| } | ||
|
|
||
| @Override | ||
| public void collectSearchShards(String routing, IntConsumer consumer) { | ||
| consumer.accept(hashToShardId(effectiveRoutingToHash(routing))); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Strategy for partitioned indices. | ||
| */ | ||
| private static class Partitioned extends IndexRouting { | ||
| private final int routingPartitionSize; | ||
|
|
||
| Partitioned(int routingNumShards, int routingFactor, int routingPartitionSize) { | ||
| super(routingNumShards, routingFactor); | ||
| this.routingPartitionSize = routingPartitionSize; | ||
| } | ||
|
|
||
| @Override | ||
| public int shardId(String id, @Nullable String routing) { | ||
| if (routing == null) { | ||
| throw new IllegalArgumentException("A routing value is required for gets from a partitioned index"); | ||
| } | ||
| int offset = Math.floorMod(effectiveRoutingToHash(id), routingPartitionSize); | ||
| return hashToShardId(effectiveRoutingToHash(routing) + offset); | ||
| } | ||
|
|
||
| @Override | ||
| public void collectSearchShards(String routing, IntConsumer consumer) { | ||
| int hash = effectiveRoutingToHash(routing); | ||
| for (int i = 0; i < routingPartitionSize; i++) { | ||
| consumer.accept(hashToShardId(hash + i)); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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.
As a future refinement (not necessarily in this PR), I wonder if
OperationRoutingshould create theIndexRoutingand theIndexRoutingobject then should have theindexShards(and more) method(s)? So the interaction here would be something like:I do see the problem in doing so for
ShardSplittingQuerythough. And it may be affected by your future work too, where you may want bulk to only create a newIndexRoutinginstance when the base parameters are different. So I am OK leaving this as is for now if you prefer to not tackle that now.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.
Moving the methods like
indexShardsinto makes sense. I liked that the class as it stands now doesn't need to know about stuff like the routing table. I think which was is right will show up more clearly after I add the source based routing stuff so I'll keep it as it is for now. Once I have a proposal for source based routing.