@@ -1720,18 +1720,26 @@ public void testForceMergeWithSoftDeletesRetentionAndRecoverySource() throws Exc
17201720 settings .put (IndexSettings .INDEX_SOFT_DELETES_RETENTION_OPERATIONS_SETTING .getKey (), 0 );
17211721 indexSettings .updateIndexMetaData (IndexMetaData .builder (defaultSettings .getIndexMetaData ()).settings (settings ).build ());
17221722 engine .onSettingsChanged ();
1723- // If the global checkpoint equals to the local checkpoint, the next force-merge will be a noop
1724- // because all deleted documents are expunged in the previous force-merge already. We need to flush
1725- // a new segment to make merge happen so that we can verify that all _recovery_source are pruned.
1726- if (globalCheckpoint .get () == engine .getPersistedLocalCheckpoint () && liveDocs .isEmpty () == false ) {
1727- String deleteId = randomFrom (liveDocs );
1728- engine .delete (new Engine .Delete ("test" , deleteId , newUid (deleteId ), primaryTerm .get ()));
1729- liveDocsWithSource .remove (deleteId );
1730- liveDocs .remove (deleteId );
1731- engine .flush ();
1723+ // If we already merged down to 1 segment, then the next force-merge will be a noop. We need to add an extra segment to make
1724+ // merges happen so we can verify that _recovery_source are pruned. See: https://github.com/elastic/elasticsearch/issues/41628.
1725+ final int numSegments ;
1726+ try (Engine .Searcher searcher = engine .acquireSearcher ("test" , Engine .SearcherScope .INTERNAL )) {
1727+ numSegments = searcher .getDirectoryReader ().leaves ().size ();
1728+ }
1729+ if (numSegments == 1 ) {
1730+ boolean useRecoverySource = randomBoolean () || omitSourceAllTheTime ;
1731+ ParsedDocument doc = testParsedDocument ("dummy" , null , testDocument (), B_1 , null , useRecoverySource );
1732+ engine .index (indexForDoc (doc ));
1733+ if (useRecoverySource == false ) {
1734+ liveDocsWithSource .add (doc .id ());
1735+ }
1736+ engine .syncTranslog ();
1737+ globalCheckpoint .set (engine .getPersistedLocalCheckpoint ());
1738+ engine .flush (randomBoolean (), true );
1739+ } else {
1740+ globalCheckpoint .set (engine .getPersistedLocalCheckpoint ());
1741+ engine .syncTranslog ();
17321742 }
1733- globalCheckpoint .set (engine .getPersistedLocalCheckpoint ());
1734- engine .syncTranslog ();
17351743 engine .forceMerge (true , 1 , false , false , false );
17361744 assertConsistentHistoryBetweenTranslogAndLuceneIndex (engine , mapperService );
17371745 assertThat (readAllOperationsInLucene (engine , mapperService ), hasSize (liveDocsWithSource .size ()));
0 commit comments