@@ -1118,6 +1118,82 @@ public void testPartialIndexPrimaryDefault() throws Exception {
1118
1118
}
1119
1119
}
1120
1120
1121
+ public void testSearchReplicaDefaultRouting () throws Exception {
1122
+ final int numShards = 1 ;
1123
+ final int numReplicas = 2 ;
1124
+ final int numSearchReplicas = 2 ;
1125
+ final String indexName = "test" ;
1126
+ final String [] indexNames = new String [] { indexName };
1127
+
1128
+ ClusterService clusterService = null ;
1129
+ ThreadPool threadPool = null ;
1130
+
1131
+ try {
1132
+ OperationRouting opRouting = new OperationRouting (
1133
+ Settings .builder ().put (FeatureFlags .READER_WRITER_SPLIT_EXPERIMENTAL , "true" ).build (),
1134
+ new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS )
1135
+ );
1136
+
1137
+ ClusterState state = ClusterStateCreationUtils .stateWithAssignedPrimariesAndReplicas (
1138
+ indexNames ,
1139
+ numShards ,
1140
+ numReplicas ,
1141
+ numSearchReplicas
1142
+ );
1143
+ IndexShardRoutingTable indexShardRoutingTable = state .getRoutingTable ().index (indexName ).getShards ().get (0 );
1144
+ ShardId shardId = indexShardRoutingTable .searchOnlyReplicas ().get (0 ).shardId ();
1145
+
1146
+ threadPool = new TestThreadPool ("testSearchReplicaDefaultRouting" );
1147
+ clusterService = ClusterServiceUtils .createClusterService (threadPool );
1148
+
1149
+ // add a search replica in initializing state:
1150
+ DiscoveryNode node = new DiscoveryNode (
1151
+ "node_initializing" ,
1152
+ OpenSearchTestCase .buildNewFakeTransportAddress (),
1153
+ Collections .emptyMap (),
1154
+ new HashSet <>(DiscoveryNodeRole .BUILT_IN_ROLES ),
1155
+ Version .CURRENT
1156
+ );
1157
+
1158
+ IndexMetadata indexMetadata = IndexMetadata .builder (indexName )
1159
+ .settings (Settings .builder ().put (state .metadata ().index (indexName ).getSettings ()).build ())
1160
+ .numberOfSearchReplicas (3 )
1161
+ .numberOfReplicas (2 )
1162
+ .build ();
1163
+ Metadata .Builder metadataBuilder = Metadata .builder (state .metadata ()).put (indexMetadata , false ).generateClusterUuidIfNeeded ();
1164
+ IndexRoutingTable .Builder indexShardRoutingBuilder = IndexRoutingTable .builder (indexMetadata .getIndex ());
1165
+ indexShardRoutingBuilder .addIndexShard (indexShardRoutingTable );
1166
+ indexShardRoutingBuilder .addShard (
1167
+ TestShardRouting .newShardRouting (shardId , node .getId (), null , false , true , ShardRoutingState .INITIALIZING , null )
1168
+ );
1169
+ state = ClusterState .builder (state )
1170
+ .routingTable (RoutingTable .builder ().add (indexShardRoutingBuilder ).build ())
1171
+ .metadata (metadataBuilder .build ())
1172
+ .build ();
1173
+
1174
+ // Verify default preference is primary only
1175
+ GroupShardsIterator <ShardIterator > groupIterator = opRouting .searchShards (state , indexNames , null , null );
1176
+ assertThat ("one group per shard" , groupIterator .size (), equalTo (numShards ));
1177
+ for (ShardIterator shardIterator : groupIterator ) {
1178
+ assertEquals ("We should have 3 shards returned" , shardIterator .size (), 3 );
1179
+ int i = 0 ;
1180
+ for (ShardRouting shardRouting : shardIterator ) {
1181
+ assertTrue (
1182
+ "Only search replicas should exist with preference SEARCH_REPLICA" ,
1183
+ shardIterator .nextOrNull ().isSearchOnly ()
1184
+ );
1185
+ if (i == shardIterator .size ()) {
1186
+ assertTrue ("Initializing shard should appear last" , shardRouting .initializing ());
1187
+ assertFalse ("Initializing shard should appear last" , shardRouting .active ());
1188
+ }
1189
+ }
1190
+ }
1191
+ } finally {
1192
+ IOUtils .close (clusterService );
1193
+ terminate (threadPool );
1194
+ }
1195
+ }
1196
+
1121
1197
private DiscoveryNode [] setupNodes () {
1122
1198
// Sets up two data nodes in zone-a and one data node in zone-b
1123
1199
List <String > zones = Arrays .asList ("a" , "a" , "b" );
0 commit comments