|
8 | 8 |
|
9 | 9 | package org.opensearch.indices.settings;
|
10 | 10 |
|
| 11 | +import org.opensearch.action.search.SearchPhaseExecutionException; |
11 | 12 | import org.opensearch.action.search.SearchResponse;
|
12 | 13 | import org.opensearch.action.support.WriteRequest;
|
13 | 14 | import org.opensearch.cluster.ClusterState;
|
|
30 | 31 | import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE;
|
31 | 32 | import static org.opensearch.cluster.routing.UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING;
|
32 | 33 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount;
|
| 34 | +import static org.hamcrest.Matchers.anyOf; |
| 35 | +import static org.hamcrest.core.IsEqual.equalTo; |
33 | 36 |
|
34 | 37 | @OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0)
|
35 | 38 | public class SearchOnlyReplicaIT extends RemoteStoreBaseIntegTestCase {
|
@@ -236,6 +239,80 @@ public void testSearchReplicaRoutingPreference() throws IOException {
|
236 | 239 | assertEquals(nodeId, indexShardRoutingTable.searchOnlyReplicas().get(0).currentNodeId());
|
237 | 240 | }
|
238 | 241 |
|
| 242 | + public void testSearchReplicaRoutingPreferenceWhenSearchReplicaUnassigned() { |
| 243 | + internalCluster().startClusterManagerOnlyNode(); |
| 244 | + internalCluster().startDataOnlyNode(); |
| 245 | + createIndex(TEST_INDEX, Settings.builder().put(indexSettings()).put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1).build()); |
| 246 | + ensureYellow(TEST_INDEX); |
| 247 | + client().prepareIndex(TEST_INDEX).setId("1").setSource("foo", "bar").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); |
| 248 | + |
| 249 | + // By default cluster.routing.search_only.strict is set as true |
| 250 | + // When cluster.routing.search_only.strict is set as true, and no assigned search replica is available, |
| 251 | + // search request will fail since it will route only to search replica but it's not available |
| 252 | + Throwable throwable = assertThrows( |
| 253 | + SearchPhaseExecutionException.class, |
| 254 | + () -> client().prepareSearch(TEST_INDEX).setPreference(null).setQuery(QueryBuilders.matchAllQuery()).get() |
| 255 | + ); |
| 256 | + |
| 257 | + assertEquals("all shards failed", throwable.getMessage()); |
| 258 | + |
| 259 | + // Set cluster.routing.search_only.strict as false |
| 260 | + client().admin() |
| 261 | + .cluster() |
| 262 | + .prepareUpdateSettings() |
| 263 | + .setTransientSettings(Settings.builder().put("cluster.routing.search_only.strict", false)) |
| 264 | + .get(); |
| 265 | + |
| 266 | + // When cluster.routing.search_only.strict is set as false, and no assigned search replica is available; |
| 267 | + // search request will fall back to querying writers |
| 268 | + SearchResponse response = client().prepareSearch(TEST_INDEX).setPreference(null).setQuery(QueryBuilders.matchAllQuery()).get(); |
| 269 | + |
| 270 | + String nodeId = response.getHits().getAt(0).getShard().getNodeId(); |
| 271 | + IndexShardRoutingTable indexShardRoutingTable = getIndexShardRoutingTable(); |
| 272 | + assertEquals(nodeId, indexShardRoutingTable.primaryShard().currentNodeId()); |
| 273 | + } |
| 274 | + |
| 275 | + public void testSearchReplicaRoutingPreferenceWhenSearchReplicaAssigned() { |
| 276 | + internalCluster().startClusterManagerOnlyNode(); |
| 277 | + internalCluster().startDataOnlyNode(); |
| 278 | + createIndex(TEST_INDEX, Settings.builder().put(indexSettings()).put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1).build()); |
| 279 | + ensureYellow(TEST_INDEX); |
| 280 | + client().prepareIndex(TEST_INDEX).setId("1").setSource("foo", "bar").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); |
| 281 | + |
| 282 | + internalCluster().startSearchOnlyNode(); |
| 283 | + ensureGreen(TEST_INDEX); |
| 284 | + |
| 285 | + // By default cluster.routing.search_only.strict is set as true |
| 286 | + // When cluster.routing.search_only.strict is set as true, and assigned search replica is available; |
| 287 | + // search request will succeed |
| 288 | + SearchResponse response = client().prepareSearch(TEST_INDEX).setPreference(null).setQuery(QueryBuilders.matchAllQuery()).get(); |
| 289 | + |
| 290 | + String nodeId = response.getHits().getAt(0).getShard().getNodeId(); |
| 291 | + IndexShardRoutingTable indexShardRoutingTable = getIndexShardRoutingTable(); |
| 292 | + assertEquals(nodeId, indexShardRoutingTable.searchOnlyReplicas().get(0).currentNodeId()); |
| 293 | + |
| 294 | + // Set cluster.routing.search_only.strict as false |
| 295 | + client().admin() |
| 296 | + .cluster() |
| 297 | + .prepareUpdateSettings() |
| 298 | + .setTransientSettings(Settings.builder().put("cluster.routing.search_only.strict", false)) |
| 299 | + .get(); |
| 300 | + |
| 301 | + // When cluster.routing.search_only.strict is set as false, and assigned search replica is available; |
| 302 | + // search request can land on either writer or reader |
| 303 | + response = client().prepareSearch(TEST_INDEX).setPreference(null).setQuery(QueryBuilders.matchAllQuery()).get(); |
| 304 | + |
| 305 | + nodeId = response.getHits().getAt(0).getShard().getNodeId(); |
| 306 | + indexShardRoutingTable = getIndexShardRoutingTable(); |
| 307 | + assertThat( |
| 308 | + nodeId, |
| 309 | + anyOf( |
| 310 | + equalTo(indexShardRoutingTable.primaryShard().currentNodeId()), |
| 311 | + equalTo(indexShardRoutingTable.searchOnlyReplicas().get(0).currentNodeId()) |
| 312 | + ) |
| 313 | + ); |
| 314 | + } |
| 315 | + |
239 | 316 | public void testUnableToAllocateSearchReplicaWontBlockRegularReplicaAllocation() {
|
240 | 317 | int numSearchReplicas = 1;
|
241 | 318 | int numWriterReplicas = 1;
|
|
0 commit comments