Skip to content

Commit 44d99d4

Browse files
committed
Add back primary shard preference for queries
Signed-off-by: Kunal Kotwani <kkotwani@amazon.com>
1 parent 18390f9 commit 44d99d4

25 files changed

Lines changed: 255 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010
- Allow mmap to use new JDK-19 preview APIs in Apache Lucene 9.4+ ([#5151](https://github.com/opensearch-project/OpenSearch/pull/5151))
1111
- Add events correlation engine plugin ([#6854](https://github.com/opensearch-project/OpenSearch/issues/6854))
1212
- Add connectToNodeAsExtension in TransportService ([#6866](https://github.com/opensearch-project/OpenSearch/pull/6866))
13+
- Add back primary shard preference for queries ([#7375](https://github.com/opensearch-project/OpenSearch/pull/7375))
1314

1415
### Dependencies
1516
- Bump `log4j-core` from 2.18.0 to 2.19.0
@@ -102,4 +103,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
102103
### Security
103104

104105
[Unreleased 3.0]: https://github.com/opensearch-project/OpenSearch/compare/2.x...HEAD
105-
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.5...2.x
106+
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.5...2.x

rest-api-spec/src/main/resources/rest-api-spec/test/bulk/20_list_of_strings.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
- do:
1313
count:
14+
# we count through the primary in case there is a replica that has not yet fully recovered
15+
preference: _primary
1416
index: test_index
1517

1618
- match: {count: 2}

server/src/internalClusterTest/java/org/opensearch/cluster/coordination/RareClusterStateIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public void testDelayedMappingPropagationOnReplica() throws Exception {
394394
assertNotNull(mapper.mappers().getMapper("field2"));
395395
});
396396

397-
assertBusy(() -> assertTrue(client().prepareGet("index", "2").get().isExists()));
397+
assertBusy(() -> assertTrue(client().prepareGet("index", "2").setPreference("_primary").get().isExists()));
398398

399399
// The mappings have not been propagated to the replica yet as a consequence the document count not be indexed
400400
// We wait on purpose to make sure that the document is not indexed because the shard operation is stalled

server/src/internalClusterTest/java/org/opensearch/search/basic/SearchWhileCreatingIndexIT.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ private void searchWhileCreatingIndex(boolean createIndex, int numberOfReplicas)
8888
ClusterHealthStatus status = client().admin().cluster().prepareHealth("test").get().getStatus();
8989
while (status != ClusterHealthStatus.GREEN) {
9090
// first, verify that search normal search works
91-
SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.termQuery("field", "test")).get();
91+
SearchResponse searchResponse = client().prepareSearch("test")
92+
.setPreference("_primary")
93+
.setQuery(QueryBuilders.termQuery("field", "test"))
94+
.execute()
95+
.actionGet();
9296
assertHitCount(searchResponse, 1);
97+
9398
Client client = client();
9499
searchResponse = client.prepareSearch("test")
95100
.setPreference(preference + Integer.toString(counter++))

server/src/internalClusterTest/java/org/opensearch/search/fetch/subphase/MatchedQueriesIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ public void testMatchedWithShould() throws Exception {
328328
.should(queryStringQuery("dolor").queryName("dolor"))
329329
.should(queryStringQuery("elit").queryName("elit"))
330330
)
331+
.setPreference("_primary")
331332
.get();
332333

333334
assertHitCount(searchResponse, 2L);

server/src/internalClusterTest/java/org/opensearch/search/functionscore/RandomScoreFunctionIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void testConsistentHitsWithSameSeed() throws Exception {
117117
for (int o = 0; o < outerIters; o++) {
118118
final int seed = randomInt();
119119
String preference = randomRealisticUnicodeOfLengthBetween(1, 10); // at least one char!!
120-
// randomPreference should not start with '_' (reserved for known preference types (e.g. _shards)
120+
// randomPreference should not start with '_' (reserved for known preference types (e.g. _shards, _primary)
121121
while (preference.startsWith("_")) {
122122
preference = randomRealisticUnicodeOfLengthBetween(1, 10);
123123
}

server/src/internalClusterTest/java/org/opensearch/search/preference/SearchPreferenceIT.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public void testStopOneNodePreferenceWithRedState() throws IOException {
8989
internalCluster().stopRandomDataNode();
9090
client().admin().cluster().prepareHealth().setWaitForStatus(ClusterHealthStatus.RED).get();
9191
String[] preferences = new String[] {
92+
"_primary",
9293
"_local",
94+
"_primary_first",
9395
"_prefer_nodes:somenode",
9496
"_prefer_nodes:server2",
9597
"_prefer_nodes:somenode,server2" };
@@ -140,13 +142,32 @@ public void testSimplePreference() {
140142
client().prepareIndex("test").setSource("field1", "value1").get();
141143
refresh();
142144

143-
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).get();
145+
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_local").execute().actionGet();
146+
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
147+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_local").execute().actionGet();
148+
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
149+
150+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_primary").execute().actionGet();
151+
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
152+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_primary").execute().actionGet();
144153
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
145154

146-
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_local").get();
155+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_replica").execute().actionGet();
156+
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
157+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_replica").execute().actionGet();
158+
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
159+
160+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_replica_first").execute().actionGet();
161+
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
162+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_replica_first").execute().actionGet();
163+
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
164+
165+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("1234").execute().actionGet();
166+
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
167+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("1234").execute().actionGet();
147168
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
148169

149-
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("1234").get();
170+
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).get();
150171
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
151172
}
152173

server/src/internalClusterTest/java/org/opensearch/search/profile/query/QueryProfilerIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,15 @@ public void testProfileMatchesRegular() throws Exception {
148148
.setProfile(false)
149149
.addSort("id.keyword", SortOrder.ASC)
150150
.setSearchType(SearchType.QUERY_THEN_FETCH)
151+
.setPreference("_primary")
151152
.setRequestCache(false);
152153

153154
SearchRequestBuilder profile = client().prepareSearch("test")
154155
.setQuery(q)
155156
.setProfile(true)
156157
.addSort("id.keyword", SortOrder.ASC)
157158
.setSearchType(SearchType.QUERY_THEN_FETCH)
159+
.setPreference("_primary")
158160
.setRequestCache(false);
159161

160162
MultiSearchResponse.Item[] responses = client().prepareMultiSearch().add(vanilla).add(profile).get().getResponses();

server/src/internalClusterTest/java/org/opensearch/search/simple/SimpleSearchIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void testSearchRandomPreference() throws InterruptedException, ExecutionE
9999
int iters = scaledRandomIntBetween(10, 20);
100100
for (int i = 0; i < iters; i++) {
101101
String randomPreference = randomUnicodeOfLengthBetween(0, 4);
102-
// randomPreference should not start with '_' (reserved for known preference types (e.g. _shards)
102+
// randomPreference should not start with '_' (reserved for known preference types (e.g. _shards, _primary)
103103
while (randomPreference.startsWith("_")) {
104104
randomPreference = randomUnicodeOfLengthBetween(0, 4);
105105
}

server/src/main/java/org/opensearch/action/admin/cluster/shards/ClusterSearchShardsRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ public ClusterSearchShardsRequest routing(String... routings) {
152152

153153
/**
154154
* Sets the preference to execute the search. Defaults to randomize across shards. Can be set to
155-
* {@code _local} to prefer local shards or a custom value, which guarantees that the same order
155+
* {@code _local} to prefer local shards, {@code _primary} to execute only on primary shards,
156+
* or a custom value, which guarantees that the same order
156157
* will be used across different requests.
157158
*/
158159
public ClusterSearchShardsRequest preference(String preference) {

0 commit comments

Comments
 (0)