21
21
22
22
import org .apache .lucene .search .MatchAllDocsQuery ;
23
23
import org .apache .lucene .search .TopDocs ;
24
+ import org .elasticsearch .action .support .ActiveShardCount ;
24
25
import org .elasticsearch .cluster .metadata .IndexMetaData ;
25
26
import org .elasticsearch .common .Strings ;
26
27
import org .elasticsearch .common .compress .CompressedXContent ;
42
43
import org .elasticsearch .threadpool .ThreadPool ;
43
44
44
45
import java .io .IOException ;
46
+ import java .nio .file .Path ;
45
47
import java .util .Collection ;
46
48
import java .util .Collections ;
49
+ import java .util .Map ;
47
50
import java .util .concurrent .CountDownLatch ;
48
51
import java .util .concurrent .atomic .AtomicInteger ;
49
52
import java .util .concurrent .atomic .AtomicReference ;
50
53
54
+ import static org .elasticsearch .index .shard .IndexShardTestCase .getEngine ;
51
55
import static org .elasticsearch .test .InternalSettingsPlugin .TRANSLOG_RETENTION_CHECK_INTERVAL_SETTING ;
52
56
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
53
57
import static org .hamcrest .core .IsEqual .equalTo ;
@@ -370,7 +374,7 @@ public void testAsyncTranslogTrimActuallyWorks() throws Exception {
370
374
.build ();
371
375
IndexService indexService = createIndex ("test" , settings );
372
376
ensureGreen ("test" );
373
- assertTrue (indexService .getRefreshTask ().mustReschedule ());
377
+ assertTrue (indexService .getTrimTranslogTask ().mustReschedule ());
374
378
client ().prepareIndex ("test" , "test" , "1" ).setSource ("{\" foo\" : \" bar\" }" , XContentType .JSON ).get ();
375
379
client ().admin ().indices ().prepareFlush ("test" ).get ();
376
380
client ().admin ().indices ().prepareUpdateSettings ("test" )
@@ -382,6 +386,48 @@ public void testAsyncTranslogTrimActuallyWorks() throws Exception {
382
386
assertBusy (() -> assertThat (IndexShardTestCase .getTranslog (shard ).totalOperations (), equalTo (0 )));
383
387
}
384
388
389
+ public void testAsyncTranslogTrimTaskOnClosedIndex () throws Exception {
390
+ final String indexName = "test" ;
391
+ IndexService indexService = createIndex (indexName , Settings .builder ()
392
+ .put (TRANSLOG_RETENTION_CHECK_INTERVAL_SETTING .getKey (), "100ms" )
393
+ .build ());
394
+
395
+ Translog translog = IndexShardTestCase .getTranslog (indexService .getShard (0 ));
396
+ final Path translogPath = translog .getConfig ().getTranslogPath ();
397
+ final String translogUuid = translog .getTranslogUUID ();
398
+
399
+ final int numDocs = scaledRandomIntBetween (10 , 100 );
400
+ for (int i = 0 ; i < numDocs ; i ++) {
401
+ client ().prepareIndex ().setIndex (indexName ).setId (String .valueOf (i )).setSource ("{\" foo\" : \" bar\" }" , XContentType .JSON ).get ();
402
+ if (randomBoolean ()) {
403
+ client ().admin ().indices ().prepareFlush (indexName ).get ();
404
+ }
405
+ }
406
+ assertThat (translog .totalOperations (), equalTo (numDocs ));
407
+ assertThat (translog .stats ().estimatedNumberOfOperations (), equalTo (numDocs ));
408
+ assertAcked (client ().admin ().indices ().prepareClose ("test" ).setWaitForActiveShards (ActiveShardCount .DEFAULT ));
409
+
410
+ indexService = getInstanceFromNode (IndicesService .class ).indexServiceSafe (indexService .index ());
411
+ assertTrue (indexService .getTrimTranslogTask ().mustReschedule ());
412
+
413
+ final long lastCommitedTranslogGeneration ;
414
+ try (Engine .IndexCommitRef indexCommitRef = getEngine (indexService .getShard (0 )).acquireLastIndexCommit (false )) {
415
+ Map <String , String > lastCommittedUserData = indexCommitRef .getIndexCommit ().getUserData ();
416
+ lastCommitedTranslogGeneration = Long .parseLong (lastCommittedUserData .get (Translog .TRANSLOG_GENERATION_KEY ));
417
+ }
418
+ assertBusy (() -> {
419
+ long minTranslogGen = Translog .readMinTranslogGeneration (translogPath , translogUuid );
420
+ assertThat (minTranslogGen , equalTo (lastCommitedTranslogGeneration ));
421
+ });
422
+
423
+ assertAcked (client ().admin ().indices ().prepareOpen ("test" ).setWaitForActiveShards (ActiveShardCount .DEFAULT ));
424
+
425
+ indexService = getInstanceFromNode (IndicesService .class ).indexServiceSafe (indexService .index ());
426
+ translog = IndexShardTestCase .getTranslog (indexService .getShard (0 ));
427
+ assertThat (translog .totalOperations (), equalTo (0 ));
428
+ assertThat (translog .stats ().estimatedNumberOfOperations (), equalTo (0 ));
429
+ }
430
+
385
431
public void testIllegalFsyncInterval () {
386
432
Settings settings = Settings .builder ()
387
433
.put (IndexSettings .INDEX_TRANSLOG_SYNC_INTERVAL_SETTING .getKey (), "0ms" ) // disable
0 commit comments