@@ -39,8 +39,8 @@ public void testResolvePhysicalShardIdDefaultRangeBased() {
3939 public void testResolvePhysicalShardIdWithOverrides () {
4040 int numPhysicalShards = 5 ;
4141 Map <String , String > overrides = new HashMap <>();
42- overrides .put ("7" , "1" ); // mapped out of standard routing
43- overrides .put ("8" , "2" ); // mapped out of standard routing
42+ overrides .put ("7" , "1" );
43+ overrides .put ("8" , "2" );
4444
4545 IndexMetadata .Builder builder = IndexMetadata .builder ("test" )
4646 .settings (
@@ -57,7 +57,6 @@ public void testResolvePhysicalShardIdWithOverrides() {
5757 assertEquals (1 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 7 ));
5858 assertEquals (2 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 8 ));
5959
60- // Default falls back to range-based formula (20 / 5 = 4 vshards per pshard)
6160 assertEquals (0 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 0 ));
6261 assertEquals (2 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 9 ));
6362 }
@@ -81,19 +80,14 @@ public void testInvalidOverridesFallBackToRangeBased() {
8180
8281 IndexMetadata metadata = builder .build ();
8382
84- // Standard range-based routing expects 4 vshards per physical shard
85- // vShard 7 -> 7 / 4 = 1
8683 assertEquals (1 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 7 ));
87- // vShard 8 -> 8 / 4 = 2
8884 assertEquals (2 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 8 ));
89- // vShard 19 -> 19 / 4 = 4
9085 assertEquals (4 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 19 ));
9186 }
9287
9388 public void testResolvePhysicalShardIdInvalidConfigurations () {
9489 int numPhysicalShards = 5 ;
9590
96- // Disabled virtual shards
9791 IndexMetadata metadataDisabled = org .mockito .Mockito .mock (IndexMetadata .class );
9892 org .mockito .Mockito .when (metadataDisabled .getNumberOfVirtualShards ()).thenReturn (-1 );
9993 org .mockito .Mockito .when (metadataDisabled .getNumberOfShards ()).thenReturn (numPhysicalShards );
@@ -104,7 +98,6 @@ public void testResolvePhysicalShardIdInvalidConfigurations() {
10498 );
10599 assertTrue (e1 .getMessage ().contains ("must be enabled and be a multiple" ));
106100
107- // Invalid multiple
108101 IndexMetadata metadataInvalid = org .mockito .Mockito .mock (IndexMetadata .class );
109102 org .mockito .Mockito .when (metadataInvalid .getNumberOfVirtualShards ()).thenReturn (13 );
110103 org .mockito .Mockito .when (metadataInvalid .getNumberOfShards ()).thenReturn (numPhysicalShards );
@@ -115,4 +108,16 @@ public void testResolvePhysicalShardIdInvalidConfigurations() {
115108 );
116109 assertTrue (e2 .getMessage ().contains ("must be enabled and be a multiple" ));
117110 }
111+
112+ public void testResolvePhysicalShardIdOutOfBoundsNormalization () {
113+ int numPhysicalShards = 5 ;
114+ IndexMetadata metadata = org .mockito .Mockito .mock (IndexMetadata .class );
115+ org .mockito .Mockito .when (metadata .getNumberOfVirtualShards ()).thenReturn (20 );
116+ org .mockito .Mockito .when (metadata .getNumberOfShards ()).thenReturn (numPhysicalShards );
117+
118+ assertEquals (4 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , -1 ));
119+
120+ assertEquals (0 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 20 ));
121+ assertEquals (1 , VirtualShardRoutingHelper .resolvePhysicalShardId (metadata , 25 ));
122+ }
118123}
0 commit comments