1111
1212import org .apache .logging .log4j .Logger ;
1313import org .elasticsearch .action .ActionListener ;
14+ import org .elasticsearch .action .admin .cluster .health .ClusterHealthResponse ;
1415import org .elasticsearch .action .bulk .BulkResponse ;
16+ import org .elasticsearch .cluster .ClusterName ;
17+ import org .elasticsearch .cluster .ClusterState ;
1518import org .elasticsearch .index .mapper .extras .MapperExtrasPlugin ;
19+ import org .elasticsearch .indices .IndexCreationException ;
1620import org .elasticsearch .plugins .Plugin ;
1721import org .elasticsearch .reindex .ReindexPlugin ;
1822import 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}
0 commit comments