Skip to content

Commit b9becdd

Browse files
authored
Adapt testPendingTasks() for replicated closed indices (#38326)
Relates to #33888
1 parent a663613 commit b9becdd

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

server/src/test/java/org/elasticsearch/indices/IndicesServiceTests.java

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
import java.util.Map;
8181
import java.util.Optional;
8282
import java.util.concurrent.CountDownLatch;
83-
import java.util.concurrent.TimeUnit;
8483
import java.util.stream.Collectors;
8584
import java.util.stream.Stream;
8685

@@ -271,60 +270,67 @@ public void testDeleteIndexStore() throws Exception {
271270
ensureGreen("test");
272271
}
273272

274-
// NORELEASE This test need to be adapted for replicated closed indices
275-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/33888")
276273
public void testPendingTasks() throws Exception {
277-
IndicesService indicesService = getIndicesService();
278-
IndexService test = createIndex("test");
274+
final IndexService indexService = createIndex("test");
275+
final Index index = indexService.index();
276+
final IndexSettings indexSettings = indexService.getIndexSettings();
279277

280-
assertTrue(test.hasShard(0));
281-
ShardPath path = test.getShardOrNull(0).shardPath();
282-
assertTrue(test.getShardOrNull(0).routingEntry().started());
283-
ShardPath shardPath = ShardPath.loadShardPath(logger, getNodeEnvironment(), new ShardId(test.index(), 0), test.getIndexSettings());
284-
assertEquals(shardPath, path);
285-
try {
286-
indicesService.processPendingDeletes(test.index(), test.getIndexSettings(), new TimeValue(0, TimeUnit.MILLISECONDS));
287-
fail("can't get lock");
288-
} catch (ShardLockObtainFailedException ex) {
278+
final IndexShard indexShard = indexService.getShardOrNull(0);
279+
assertNotNull(indexShard);
280+
assertTrue(indexShard.routingEntry().started());
289281

290-
}
291-
assertTrue(path.exists());
282+
final ShardPath shardPath = indexShard.shardPath();
283+
assertEquals(ShardPath.loadShardPath(logger, getNodeEnvironment(), indexShard.shardId(), indexSettings), shardPath);
284+
285+
final IndicesService indicesService = getIndicesService();
286+
expectThrows(ShardLockObtainFailedException.class, () ->
287+
indicesService.processPendingDeletes(index, indexSettings, TimeValue.timeValueMillis(0)));
288+
assertTrue(shardPath.exists());
292289

293290
int numPending = 1;
294291
if (randomBoolean()) {
295-
indicesService.addPendingDelete(new ShardId(test.index(), 0), test.getIndexSettings());
292+
indicesService.addPendingDelete(indexShard.shardId(), indexSettings);
296293
} else {
297294
if (randomBoolean()) {
298295
numPending++;
299-
indicesService.addPendingDelete(new ShardId(test.index(), 0), test.getIndexSettings());
296+
indicesService.addPendingDelete(indexShard.shardId(), indexSettings);
300297
}
301-
indicesService.addPendingDelete(test.index(), test.getIndexSettings());
298+
indicesService.addPendingDelete(index, indexSettings);
302299
}
300+
303301
assertAcked(client().admin().indices().prepareClose("test"));
304-
assertTrue(path.exists());
302+
assertTrue(shardPath.exists());
303+
ensureGreen("test");
305304

306-
assertEquals(indicesService.numPendingDeletes(test.index()), numPending);
305+
assertEquals(indicesService.numPendingDeletes(index), numPending);
307306
assertTrue(indicesService.hasUncompletedPendingDeletes());
308307

309-
// shard lock released... we can now delete
310-
indicesService.processPendingDeletes(test.index(), test.getIndexSettings(), new TimeValue(0, TimeUnit.MILLISECONDS));
311-
assertEquals(indicesService.numPendingDeletes(test.index()), 0);
312-
assertFalse(indicesService.hasUncompletedPendingDeletes());
313-
assertFalse(path.exists());
308+
expectThrows(ShardLockObtainFailedException.class, () ->
309+
indicesService.processPendingDeletes(index, indexSettings, TimeValue.timeValueMillis(0)));
314310

315-
if (randomBoolean()) {
316-
indicesService.addPendingDelete(new ShardId(test.index(), 0), test.getIndexSettings());
317-
indicesService.addPendingDelete(new ShardId(test.index(), 1), test.getIndexSettings());
318-
indicesService.addPendingDelete(new ShardId("bogus", "_na_", 1), test.getIndexSettings());
319-
assertEquals(indicesService.numPendingDeletes(test.index()), 2);
311+
assertEquals(indicesService.numPendingDeletes(index), numPending);
312+
assertTrue(indicesService.hasUncompletedPendingDeletes());
313+
314+
final boolean hasBogus = randomBoolean();
315+
if (hasBogus) {
316+
indicesService.addPendingDelete(new ShardId(index, 0), indexSettings);
317+
indicesService.addPendingDelete(new ShardId(index, 1), indexSettings);
318+
indicesService.addPendingDelete(new ShardId("bogus", "_na_", 1), indexSettings);
319+
assertEquals(indicesService.numPendingDeletes(index), numPending + 2);
320320
assertTrue(indicesService.hasUncompletedPendingDeletes());
321-
// shard lock released... we can now delete
322-
indicesService.processPendingDeletes(test.index(), test.getIndexSettings(), new TimeValue(0, TimeUnit.MILLISECONDS));
323-
assertEquals(indicesService.numPendingDeletes(test.index()), 0);
324-
assertTrue(indicesService.hasUncompletedPendingDeletes()); // "bogus" index has not been removed
325321
}
326-
assertAcked(client().admin().indices().prepareOpen("test").setTimeout(TimeValue.timeValueSeconds(1)));
327322

323+
assertAcked(client().admin().indices().prepareDelete("test"));
324+
assertBusy(() -> {
325+
try {
326+
indicesService.processPendingDeletes(index, indexSettings, TimeValue.timeValueMillis(0));
327+
assertEquals(indicesService.numPendingDeletes(index), 0);
328+
} catch (final Exception e) {
329+
fail(e.getMessage());
330+
}
331+
});
332+
assertThat(indicesService.hasUncompletedPendingDeletes(), equalTo(hasBogus)); // "bogus" index has not been removed
333+
assertFalse(shardPath.exists());
328334
}
329335

330336
public void testVerifyIfIndexContentDeleted() throws Exception {

0 commit comments

Comments
 (0)