Skip to content

Commit 51f67eb

Browse files
committed
Add integration test for timeouts
1 parent fcf4dc6 commit 51f67eb

File tree

3 files changed

+133
-18
lines changed

3 files changed

+133
-18
lines changed

server/src/internalClusterTest/java/org/elasticsearch/synonyms/SynonymsManagementAPIServiceIT.java

Lines changed: 118 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
import org.apache.logging.log4j.Logger;
1313
import org.elasticsearch.action.ActionListener;
14+
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
1415
import org.elasticsearch.action.bulk.BulkResponse;
16+
import org.elasticsearch.cluster.ClusterName;
17+
import org.elasticsearch.cluster.ClusterState;
1518
import org.elasticsearch.index.mapper.extras.MapperExtrasPlugin;
19+
import org.elasticsearch.indices.IndexCreationException;
1620
import org.elasticsearch.plugins.Plugin;
1721
import org.elasticsearch.reindex.ReindexPlugin;
1822
import org.elasticsearch.test.ESIntegTestCase;
@@ -137,7 +141,7 @@ public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonym
137141
SynonymRule[] rules = randomSynonymsSet(atLeast(rulesToUpdate + 1));
138142
CountDownLatch updatedRulesLatch = new CountDownLatch(rulesToUpdate);
139143
for (int i = 0; i < rulesToUpdate; i++) {
140-
synonymsManagementAPIService.putSynonymRule(synonymSetId, rules[i], new ActionListener<>() {
144+
synonymsManagementAPIService.putSynonymRule(synonymSetId, rules[i], DEFAULT_TIMEOUT, new ActionListener<>() {
141145
@Override
142146
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
143147
updatedRulesLatch.countDown();
@@ -147,7 +151,7 @@ public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonym
147151
public void onFailure(Exception e) {
148152
fail(e);
149153
}
150-
}, DEFAULT_TIMEOUT);
154+
});
151155
}
152156
try {
153157
updatedRulesLatch.await(5, TimeUnit.SECONDS);
@@ -163,7 +167,7 @@ public void onFailure(Exception e) {
163167
// Error here
164168
synonymSetId,
165169
rules[i],
166-
new ActionListener<>() {
170+
DEFAULT_TIMEOUT, new ActionListener<>() {
167171
@Override
168172
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
169173
fail("Shouldn't have been able to update a rule");
@@ -176,8 +180,8 @@ public void onFailure(Exception e) {
176180
}
177181
updatedRulesLatch.countDown();
178182
}
179-
},
180-
DEFAULT_TIMEOUT);
183+
}
184+
);
181185
}
182186
try {
183187
insertRulesLatch.await(5, TimeUnit.SECONDS);
@@ -206,7 +210,7 @@ public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonym
206210
synonymsManagementAPIService.putSynonymRule(
207211
synonymSetId,
208212
synonymsSet[randomIntBetween(0, maxSynonymSets - 1)],
209-
new ActionListener<>() {
213+
DEFAULT_TIMEOUT, new ActionListener<>() {
210214
@Override
211215
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
212216
latch.countDown();
@@ -216,8 +220,8 @@ public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonym
216220
public void onFailure(Exception e) {
217221
fail("Should update a rule that already exists at max capcity");
218222
}
219-
},
220-
DEFAULT_TIMEOUT);
223+
}
224+
);
221225
}
222226

223227
@Override
@@ -238,7 +242,7 @@ public void testCreateRuleWithMaxSynonyms() throws InterruptedException {
238242
@Override
239243
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
240244
// Updating a rule fails
241-
synonymsManagementAPIService.putSynonymRule(synonymSetId, randomSynonymRule(ruleId), new ActionListener<>() {
245+
synonymsManagementAPIService.putSynonymRule(synonymSetId, randomSynonymRule(ruleId), DEFAULT_TIMEOUT, new ActionListener<>() {
242246
@Override
243247
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
244248
fail("Should not create a new rule that does not exist when at max capacity");
@@ -248,7 +252,7 @@ public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonym
248252
public void onFailure(Exception e) {
249253
latch.countDown();
250254
}
251-
}, DEFAULT_TIMEOUT);
255+
});
252256
}
253257

254258
@Override
@@ -299,4 +303,108 @@ public void onFailure(Exception e) {
299303
readLatch.await(5, TimeUnit.SECONDS);
300304
verify(logger).warn(anyString(), eq(synonymSetId));
301305
}
306+
307+
public void testCreateSynonymsWithYellowSynonymsIndex() throws Exception {
308+
309+
// Override health method check to simulate a timeout in checking the synonyms index
310+
synonymsManagementAPIService = new SynonymsManagementAPIService(client()) {
311+
@Override
312+
void checkSynonymsIndexHealth(int timeout, ActionListener<ClusterHealthResponse> listener) {
313+
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).build();
314+
ClusterHealthResponse response = new ClusterHealthResponse(randomIdentifier(),
315+
new String[]{SynonymsManagementAPIService.SYNONYMS_INDEX_CONCRETE_NAME},
316+
clusterState);
317+
response.setTimedOut(true);
318+
listener.onResponse(response);
319+
}
320+
};
321+
322+
// Create a rule fails
323+
CountDownLatch putLatch = new CountDownLatch(1);
324+
String synonymSetId = randomIdentifier();
325+
synonymsManagementAPIService.putSynonymsSet(
326+
synonymSetId,
327+
randomSynonymsSet(1, 1),
328+
DEFAULT_TIMEOUT,
329+
new ActionListener<>() {
330+
@Override
331+
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
332+
fail("Shouldn't have been able to create synonyms with a timeout in synonyms index health");
333+
}
334+
335+
@Override
336+
public void onFailure(Exception e) {
337+
// Expected
338+
assertTrue(e instanceof IndexCreationException);
339+
assertTrue(e.getMessage().contains("synonyms index [.synonyms] is not searchable"));
340+
putLatch.countDown();
341+
}
342+
});
343+
344+
putLatch.await(5, TimeUnit.SECONDS);
345+
346+
// Update a rule fails
347+
CountDownLatch updateLatch = new CountDownLatch(1);
348+
synonymsManagementAPIService.putSynonymRule(
349+
synonymSetId,
350+
randomSynonymRule(randomIdentifier()),
351+
DEFAULT_TIMEOUT, new ActionListener<>() {
352+
@Override
353+
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
354+
fail("Shouldn't have been able to update synonyms with a timeout in synonyms index health");
355+
}
356+
357+
@Override
358+
public void onFailure(Exception e) {
359+
// Expected
360+
assertTrue(e instanceof IndexCreationException);
361+
assertTrue(e.getMessage().contains("synonyms index [.synonyms] is not searchable"));
362+
updateLatch.countDown();
363+
}
364+
});
365+
366+
updateLatch.await(5, TimeUnit.SECONDS);
367+
368+
// But, we can still create a synonyms set with timeout 0
369+
CountDownLatch putWithoutTimeoutLatch = new CountDownLatch(1);
370+
synonymsManagementAPIService.putSynonymsSet(
371+
synonymSetId,
372+
randomSynonymsSet(1, 1),
373+
0,
374+
new ActionListener<>() {
375+
@Override
376+
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
377+
// Expected
378+
putLatch.countDown();
379+
}
380+
381+
@Override
382+
public void onFailure(Exception e) {
383+
fail(e);
384+
}
385+
});
386+
387+
putWithoutTimeoutLatch.await(5, TimeUnit.SECONDS);
388+
389+
// Same for update
390+
CountDownLatch putRuleWithoutTimeoutLatch = new CountDownLatch(1);
391+
synonymsManagementAPIService.putSynonymRule(
392+
synonymSetId,
393+
randomSynonymRule(randomIdentifier()),
394+
0,
395+
new ActionListener<>() {
396+
@Override
397+
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
398+
// Expected
399+
putRuleWithoutTimeoutLatch.countDown();
400+
}
401+
402+
@Override
403+
public void onFailure(Exception e) {
404+
fail(e);
405+
}
406+
});
407+
408+
putRuleWithoutTimeoutLatch.await(5, TimeUnit.SECONDS);
409+
}
302410
}

server/src/main/java/org/elasticsearch/action/synonyms/TransportPutSynonymRuleAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected void doExecute(Task task, PutSynonymRuleAction.Request request, Action
4141
synonymsManagementAPIService.putSynonymRule(
4242
request.synonymsSetId(),
4343
request.synonymRule(),
44-
listener.map(SynonymUpdateResponse::new),
45-
request.timeout());
44+
request.timeout(), listener.map(SynonymUpdateResponse::new)
45+
);
4646
}
4747
}

server/src/main/java/org/elasticsearch/synonyms/SynonymsManagementAPIService.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.action.DocWriteRequest;
2121
import org.elasticsearch.action.DocWriteResponse;
2222
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
23+
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
2324
import org.elasticsearch.action.admin.cluster.health.TransportClusterHealthAction;
2425
import org.elasticsearch.action.admin.indices.analyze.ReloadAnalyzersRequest;
2526
import org.elasticsearch.action.admin.indices.analyze.ReloadAnalyzersResponse;
@@ -76,7 +77,7 @@ public class SynonymsManagementAPIService {
7677

7778
private static final String SYNONYMS_INDEX_NAME_PATTERN = ".synonyms-*";
7879
private static final int SYNONYMS_INDEX_FORMAT = 2;
79-
private static final String SYNONYMS_INDEX_CONCRETE_NAME = ".synonyms-" + SYNONYMS_INDEX_FORMAT;
80+
static final String SYNONYMS_INDEX_CONCRETE_NAME = ".synonyms-" + SYNONYMS_INDEX_FORMAT;
8081
private static final String SYNONYMS_ALIAS_NAME = ".synonyms";
8182
public static final String SYNONYMS_FEATURE_NAME = "synonyms";
8283
// Stores the synonym set the rule belongs to
@@ -376,7 +377,7 @@ void bulkUpdateSynonymsSet(String synonymSetId, SynonymRule[] synonymsSet, Actio
376377
bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).execute(listener);
377378
}
378379

379-
public void putSynonymRule(String synonymsSetId, SynonymRule synonymRule, ActionListener<SynonymsReloadResult> listener, int timeout) {
380+
public void putSynonymRule(String synonymsSetId, SynonymRule synonymRule, int timeout, ActionListener<SynonymsReloadResult> listener) {
380381
checkSynonymSetExists(synonymsSetId, listener.delegateFailureAndWrap((l1, obj) -> {
381382
// Count synonym rules to check if we're at maximum
382383
BoolQueryBuilder queryFilter = QueryBuilders.boolQuery()
@@ -560,9 +561,7 @@ private <T> void ensureSynonymsSearchableAndReloadAnalyzers(
560561
) {
561562
// Ensure synonyms index is searchable if timeout is present
562563
if (timeout > 0) {
563-
ClusterHealthRequest healthRequest = new ClusterHealthRequest(TimeValue.timeValueSeconds(timeout), SYNONYMS_ALIAS_NAME)
564-
.waitForGreenStatus();
565-
client.execute(TransportClusterHealthAction.TYPE, healthRequest, listener.delegateFailure((l, response) -> {
564+
checkSynonymsIndexHealth(timeout, listener.delegateFailure((l, response) -> {
566565
if (response.isTimedOut()) {
567566
l.onFailure(new IndexCreationException("synonyms index [" + SYNONYMS_ALIAS_NAME + "] is not searchable. "
568567
+ response.getActiveShardsPercent() + "% shards are active", null));
@@ -576,6 +575,14 @@ private <T> void ensureSynonymsSearchableAndReloadAnalyzers(
576575
}
577576
}
578577

578+
// Allows checking failures in tests
579+
void checkSynonymsIndexHealth(int timeout, ActionListener<ClusterHealthResponse> listener) {
580+
ClusterHealthRequest healthRequest = new ClusterHealthRequest(TimeValue.timeValueSeconds(timeout), SYNONYMS_ALIAS_NAME)
581+
.waitForGreenStatus();
582+
583+
client.execute(TransportClusterHealthAction.TYPE, healthRequest, listener);
584+
}
585+
579586
private <T> void reloadAnalyzers(
580587
String synonymSetId,
581588
boolean preview,
@@ -597,7 +604,7 @@ private static String internalSynonymRuleId(String synonymsSetId, String synonym
597604
return synonymsSetId + SYNONYM_RULE_ID_SEPARATOR + synonymRuleId;
598605
}
599606

600-
static Settings settings() {
607+
private static Settings settings() {
601608
return Settings.builder()
602609
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
603610
.put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-1")

0 commit comments

Comments
 (0)