-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add Initial Routing and Metadata Groundwork for Virtual Shards #20745
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
47bfa50
Add Virtual Shards routing and mapping overrides
atris 2b48195
Add Virtual Shards routing and mapping overrides
atris 2ccbc63
Add Virtual Shards routing and mapping overrides
atris 2144e22
Refactor Virtual Shards to use range-based routing
atris 8095046
Add fail-safe to VirtualShardRoutingHelper and valid configuration tr…
atris d13a28c
Fixed review comments and move to range based sharding
atris 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
66 changes: 66 additions & 0 deletions
66
server/src/main/java/org/opensearch/cluster/metadata/VirtualShardRoutingHelper.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,66 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.metadata; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.common.annotation.PublicApi; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Resolves virtual shard routing to physical shard IDs. | ||
| */ | ||
| @PublicApi(since = "3.6.0") | ||
| public final class VirtualShardRoutingHelper { | ||
|
|
||
| private VirtualShardRoutingHelper() {} | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(VirtualShardRoutingHelper.class); | ||
|
|
||
| /** | ||
| * Custom Metadata key for storing virtual shard routing overrides. | ||
| */ | ||
| public static final String VIRTUAL_SHARDS_CUSTOM_METADATA_KEY = "virtual_shards_routing"; | ||
|
|
||
| /** | ||
| * Resolves the physical shard for a virtual shard id. | ||
| */ | ||
| public static int resolvePhysicalShardId(IndexMetadata indexMetadata, int vShardId) { | ||
|
Member
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. what would be the access pattern, would it be called during the context of every request or cached? |
||
| int numVirtualShards = indexMetadata.getNumberOfVirtualShards(); | ||
| int numPhysicalShards = indexMetadata.getNumberOfShards(); | ||
|
|
||
| if (numVirtualShards < numPhysicalShards || numVirtualShards % numPhysicalShards != 0) { | ||
| throw new IllegalArgumentException( | ||
| "Virtual shards must be enabled and be a multiple of the number of physical shards to resolve routing." | ||
| ); | ||
| } | ||
|
|
||
| vShardId = Math.floorMod(vShardId, numVirtualShards); | ||
|
|
||
| Map<String, String> overrides = indexMetadata.getCustomData(VIRTUAL_SHARDS_CUSTOM_METADATA_KEY); | ||
atris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (overrides != null) { | ||
| String pShardIdStr = overrides.get(String.valueOf(vShardId)); | ||
| if (pShardIdStr != null) { | ||
| try { | ||
| int pShardId = Integer.parseInt(pShardIdStr); | ||
| if (pShardId >= 0 && pShardId < numPhysicalShards) { | ||
| return pShardId; | ||
| } | ||
| logger.trace("Invalid override value [{}] for vShard [{}]: out of bounds", pShardId, vShardId); | ||
| } catch (NumberFormatException e) { | ||
| logger.trace("Invalid override value [{}] for vShard [{}]: not a number", pShardIdStr, vShardId); | ||
|
Member
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. why are we swallowing the exception? |
||
| } | ||
| } | ||
| } | ||
|
|
||
| int virtualShardsPerPhysical = numVirtualShards / numPhysicalShards; | ||
| return vShardId / virtualShardsPerPhysical; | ||
| } | ||
| } | ||
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
123 changes: 123 additions & 0 deletions
123
server/src/test/java/org/opensearch/cluster/metadata/VirtualShardRoutingHelperTests.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,123 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.metadata; | ||
|
|
||
| import org.opensearch.Version; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class VirtualShardRoutingHelperTests extends OpenSearchTestCase { | ||
|
|
||
| public void testResolvePhysicalShardIdDefaultRangeBased() { | ||
| int numPhysicalShards = 5; | ||
| IndexMetadata metadata = IndexMetadata.builder("test") | ||
| .settings( | ||
| Settings.builder() | ||
| .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_VIRTUAL_SHARDS, 20) | ||
| ) | ||
| .numberOfShards(numPhysicalShards) | ||
| .numberOfReplicas(1) | ||
| .build(); | ||
|
|
||
| assertEquals(0, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 0)); | ||
| assertEquals(0, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 3)); | ||
| assertEquals(1, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 4)); | ||
| assertEquals(1, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 7)); | ||
| assertEquals(4, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 19)); | ||
| } | ||
|
|
||
| public void testResolvePhysicalShardIdWithOverrides() { | ||
| int numPhysicalShards = 5; | ||
| Map<String, String> overrides = new HashMap<>(); | ||
| overrides.put("7", "1"); | ||
| overrides.put("8", "2"); | ||
|
|
||
| IndexMetadata.Builder builder = IndexMetadata.builder("test") | ||
| .settings( | ||
| Settings.builder() | ||
| .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_VIRTUAL_SHARDS, 20) | ||
| ) | ||
| .numberOfShards(numPhysicalShards) | ||
| .numberOfReplicas(1); | ||
| builder.putCustom(VirtualShardRoutingHelper.VIRTUAL_SHARDS_CUSTOM_METADATA_KEY, overrides); | ||
|
|
||
| IndexMetadata metadata = builder.build(); | ||
|
|
||
| assertEquals(1, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 7)); | ||
| assertEquals(2, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 8)); | ||
|
|
||
| assertEquals(0, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 0)); | ||
| assertEquals(2, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 9)); | ||
| } | ||
|
|
||
| public void testInvalidOverridesFallBackToRangeBased() { | ||
| int numPhysicalShards = 5; | ||
| Map<String, String> overrides = new HashMap<>(); | ||
| overrides.put("7", "not_a_number"); | ||
| overrides.put("8", "-1"); | ||
| overrides.put("19", "5"); | ||
|
|
||
| IndexMetadata.Builder builder = IndexMetadata.builder("test") | ||
| .settings( | ||
| Settings.builder() | ||
| .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_VIRTUAL_SHARDS, 20) | ||
| ) | ||
| .numberOfShards(numPhysicalShards) | ||
| .numberOfReplicas(1); | ||
| builder.putCustom(VirtualShardRoutingHelper.VIRTUAL_SHARDS_CUSTOM_METADATA_KEY, overrides); | ||
|
|
||
| IndexMetadata metadata = builder.build(); | ||
|
|
||
| assertEquals(1, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 7)); | ||
| assertEquals(2, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 8)); | ||
| assertEquals(4, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 19)); | ||
| } | ||
|
|
||
| public void testResolvePhysicalShardIdInvalidConfigurations() { | ||
| int numPhysicalShards = 5; | ||
|
|
||
| IndexMetadata metadataDisabled = org.mockito.Mockito.mock(IndexMetadata.class); | ||
| org.mockito.Mockito.when(metadataDisabled.getNumberOfVirtualShards()).thenReturn(-1); | ||
| org.mockito.Mockito.when(metadataDisabled.getNumberOfShards()).thenReturn(numPhysicalShards); | ||
|
|
||
| IllegalArgumentException e1 = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> VirtualShardRoutingHelper.resolvePhysicalShardId(metadataDisabled, 0) | ||
| ); | ||
| assertTrue(e1.getMessage().contains("must be enabled and be a multiple")); | ||
|
|
||
| IndexMetadata metadataInvalid = org.mockito.Mockito.mock(IndexMetadata.class); | ||
| org.mockito.Mockito.when(metadataInvalid.getNumberOfVirtualShards()).thenReturn(13); | ||
| org.mockito.Mockito.when(metadataInvalid.getNumberOfShards()).thenReturn(numPhysicalShards); | ||
|
|
||
| IllegalArgumentException e2 = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> VirtualShardRoutingHelper.resolvePhysicalShardId(metadataInvalid, 0) | ||
| ); | ||
| assertTrue(e2.getMessage().contains("must be enabled and be a multiple")); | ||
| } | ||
|
|
||
| public void testResolvePhysicalShardIdOutOfBoundsNormalization() { | ||
| int numPhysicalShards = 5; | ||
| IndexMetadata metadata = org.mockito.Mockito.mock(IndexMetadata.class); | ||
| org.mockito.Mockito.when(metadata.getNumberOfVirtualShards()).thenReturn(20); | ||
| org.mockito.Mockito.when(metadata.getNumberOfShards()).thenReturn(numPhysicalShards); | ||
|
|
||
| assertEquals(4, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, -1)); | ||
|
|
||
| assertEquals(0, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 20)); | ||
| assertEquals(1, VirtualShardRoutingHelper.resolvePhysicalShardId(metadata, 25)); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.