2727import org .elasticsearch .xcontent .XContentBuilder ;
2828import org .elasticsearch .xpack .stateless .engine .IndexEngine ;
2929
30+ import java .util .ArrayList ;
31+ import java .util .List ;
3032import java .util .concurrent .atomic .AtomicInteger ;
3133import java .util .function .Supplier ;
3234
@@ -168,7 +170,7 @@ public void doTestDocsInLiveVersionMapRefresh(TestDocsType testDocsType, boolean
168170
169171 final int numDocs = randomIntBetween (1 , 20 );
170172 createIndexWithTermVectorMappings (1 , 1 );
171- indexTestDocuments (numDocs );
173+ List < String > indexedDocumentIds = indexTestDocuments (numDocs );
172174
173175 // If true, we test that the term vectors API will refresh appropriately depending on whether the second (not the first) version
174176 // of the updated documents is in the live version map (i.e., not yet searchable) of the indexing shard.
@@ -197,6 +199,8 @@ public void doTestDocsInLiveVersionMapRefresh(TestDocsType testDocsType, boolean
197199 // No additional refreshes are expected in the test.
198200 refresh (INDEX_NAME );
199201 refresh (INDEX_NAME );
202+ // Ensure that version map archive is empty since it is cleared asynchronously via `IndexEngine.commitSuccess()`.
203+ assertDocumentNotInLiveVersionMapArchive (indexedDocumentIds .getLast ());
200204 }
201205 }
202206
@@ -236,14 +240,7 @@ public void doTestDocsInLiveVersionMapRefresh(TestDocsType testDocsType, boolean
236240 // After the expected number of refreshes from the first docs, wait to ensure that the async
237241 // IndexEngine.commitSuccess() has been called to move out the docs from the live version map archive.
238242 // That way, the next term request should not do any other refreshes.
239- assertBusy (() -> assertTrue (findIndexShard (INDEX_NAME ).withEngine (e -> {
240- assertThat (e , instanceOf (IndexEngine .class ));
241- final var indexEngine = (IndexEngine ) e ;
242- final var archive = getArchive (indexEngine .getLiveVersionMap ());
243- assertNotNull (archive );
244- assertNull (archive .get (uid (termVectorsResponse .getId ())));
245- return true ;
246- })));
243+ assertDocumentNotInLiveVersionMapArchive (termVectorsResponse .getId ());
247244 }
248245 }
249246 }
@@ -383,11 +380,17 @@ protected void createIndexWithTermVectorMappings(int shards, int replicas) throw
383380 );
384381 }
385382
386- protected void indexTestDocuments (int numDocs ) {
383+ protected List <String > indexTestDocuments (int numDocs ) {
384+ var indexedDocumentIds = new ArrayList <String >();
385+
387386 for (int i = 0 ; i < numDocs ; i ++) {
388- final var response = prepareIndex (INDEX_NAME ).setId (String .valueOf (i )).setSource ("field" , "foo bar " + i ).get ();
387+ String id = String .valueOf (i );
388+ final var response = prepareIndex (INDEX_NAME ).setId (id ).setSource ("field" , "foo bar " + i ).get ();
389389 assertEquals (1 , response .getVersion ());
390+ indexedDocumentIds .add (id );
390391 }
392+
393+ return indexedDocumentIds ;
391394 }
392395
393396 protected void updateTestDocuments (int numDocs ) {
@@ -396,4 +399,15 @@ protected void updateTestDocuments(int numDocs) {
396399 assertEquals (2 , response .getVersion ());
397400 }
398401 }
402+
403+ private void assertDocumentNotInLiveVersionMapArchive (String documentId ) throws Exception {
404+ assertBusy (() -> assertTrue (findIndexShard (INDEX_NAME ).withEngine (e -> {
405+ assertThat (e , instanceOf (IndexEngine .class ));
406+ final var indexEngine = (IndexEngine ) e ;
407+ final var archive = getArchive (indexEngine .getLiveVersionMap ());
408+ assertNotNull (archive );
409+ assertNull (archive .get (uid (documentId )));
410+ return true ;
411+ })));
412+ }
399413}
0 commit comments