2424import org .apache .lucene .search .Query ;
2525import org .elasticsearch .common .lease .Releasable ;
2626import org .elasticsearch .index .seqno .RetentionLease ;
27- import org .elasticsearch .index .seqno .SequenceNumbers ;
2827import org .elasticsearch .test .ESTestCase ;
2928
3029import java .util .ArrayList ;
3130import java .util .Arrays ;
3231import java .util .Collection ;
32+ import java .util .Collections ;
3333import java .util .HashSet ;
3434import java .util .List ;
3535import java .util .Set ;
3636import java .util .concurrent .atomic .AtomicLong ;
3737import java .util .function .Supplier ;
3838
39+ import static org .elasticsearch .index .seqno .SequenceNumbers .NO_OPS_PERFORMED ;
40+ import static org .hamcrest .Matchers .contains ;
41+ import static org .hamcrest .Matchers .empty ;
3942import static org .hamcrest .Matchers .equalTo ;
4043import static org .hamcrest .Matchers .instanceOf ;
4144
@@ -46,7 +49,7 @@ public class SoftDeletesPolicyTests extends ESTestCase {
4649 */
4750 public void testSoftDeletesRetentionLock () {
4851 long retainedOps = between (0 , 10000 );
49- AtomicLong globalCheckpoint = new AtomicLong (SequenceNumbers . NO_OPS_PERFORMED );
52+ AtomicLong globalCheckpoint = new AtomicLong (NO_OPS_PERFORMED );
5053 final AtomicLong [] retainingSequenceNumbers = new AtomicLong [randomIntBetween (0 , 8 )];
5154 for (int i = 0 ; i < retainingSequenceNumbers .length ; i ++) {
5255 retainingSequenceNumbers [i ] = new AtomicLong ();
@@ -116,4 +119,23 @@ public void testSoftDeletesRetentionLock() {
116119 assertThat (policy .getMinRetainedSeqNo (), equalTo (minRetainedSeqNo ));
117120 }
118121
122+ public void testAlwaysFetchLatestRetentionLeases () {
123+ final AtomicLong globalCheckpoint = new AtomicLong (NO_OPS_PERFORMED );
124+ final Collection <RetentionLease > leases = new ArrayList <>();
125+ final int numLeases = randomIntBetween (0 , 10 );
126+ for (int i = 0 ; i < numLeases ; i ++) {
127+ leases .add (new RetentionLease (Integer .toString (i ), randomLongBetween (0 , 1000 ), randomNonNegativeLong (), "test" ));
128+ }
129+ final Supplier <Collection <RetentionLease >> leasesSupplier = () -> Collections .unmodifiableCollection (new ArrayList <>(leases ));
130+ final SoftDeletesPolicy policy =
131+ new SoftDeletesPolicy (globalCheckpoint ::get , randomIntBetween (1 , 1000 ), randomIntBetween (0 , 1000 ), leasesSupplier );
132+ if (randomBoolean ()) {
133+ policy .acquireRetentionLock ();
134+ }
135+ if (numLeases == 0 ) {
136+ assertThat (policy .getRetentionPolicy ().v2 (), empty ());
137+ } else {
138+ assertThat (policy .getRetentionPolicy ().v2 (), contains (leases .toArray (new RetentionLease [0 ])));
139+ }
140+ }
119141}
0 commit comments