@@ -229,22 +229,28 @@ public void testCheckUnreferencedCommits() throws Exception {
229
229
lastCheckpoint = randomLongBetween (lastCheckpoint , lastMaxSeqNo );
230
230
commitList .add (mockIndexCommit (lastCheckpoint , lastMaxSeqNo , translogUUID , lastTranslogGen ));
231
231
}
232
- IndexCommit safeCommit = randomFrom ( commitList );
233
- globalCheckpoint .set (Long .parseLong (safeCommit .getUserData ().get (SequenceNumbers .MAX_SEQ_NO )));
232
+ int safeCommitIndex = randomIntBetween ( 0 , commitList . size () - 1 );
233
+ globalCheckpoint .set (Long .parseLong (commitList . get ( safeCommitIndex ) .getUserData ().get (SequenceNumbers .MAX_SEQ_NO )));
234
234
commitList .forEach (this ::resetDeletion );
235
235
indexPolicy .onCommit (commitList );
236
- if (safeCommit == commitList .get (commitList .size () - 1 )) {
236
+
237
+ if (safeCommitIndex == commitList .size () - 1 ) {
237
238
// Safe commit is the last commit - no need to clean up
238
239
assertThat (translogPolicy .getMinTranslogGenerationForRecovery (), equalTo (lastTranslogGen ));
239
240
assertThat (translogPolicy .getTranslogGenerationOfLastCommit (), equalTo (lastTranslogGen ));
240
241
assertThat (indexPolicy .hasUnreferencedCommits (), equalTo (false ));
241
242
} else {
242
- // Advanced but not enough
243
- globalCheckpoint .set (randomLongBetween (globalCheckpoint .get (), lastMaxSeqNo - 1 ));
244
- assertThat (indexPolicy .hasUnreferencedCommits (), equalTo (false ));
245
- // Advanced enough
243
+ // Advanced but not enough for any commit after the safe commit becomes safe
244
+ IndexCommit nextSafeCommit = commitList .get (safeCommitIndex + 1 );
245
+ globalCheckpoint .set (randomLongBetween (globalCheckpoint .get (),
246
+ Long .parseLong (nextSafeCommit .getUserData ().get (SequenceNumbers .MAX_SEQ_NO )) - 1 ));
247
+ assertFalse (indexPolicy .hasUnreferencedCommits ());
248
+ // Advanced enough for some index commit becomes safe
249
+ globalCheckpoint .set (randomLongBetween (
250
+ Long .parseLong (nextSafeCommit .getUserData ().get (SequenceNumbers .MAX_SEQ_NO )), lastMaxSeqNo ));
251
+ assertTrue (indexPolicy .hasUnreferencedCommits ());
252
+ // Advanced enough for the last commit becomes safe
246
253
globalCheckpoint .set (randomLongBetween (lastMaxSeqNo , Long .MAX_VALUE ));
247
- assertThat (indexPolicy .hasUnreferencedCommits (), equalTo (true ));
248
254
commitList .forEach (this ::resetDeletion );
249
255
indexPolicy .onCommit (commitList );
250
256
// Safe commit is the last commit - no need to clean up
0 commit comments