21
21
22
22
import org .elasticsearch .cluster .routing .AllocationId ;
23
23
import org .elasticsearch .common .settings .Settings ;
24
+ import org .elasticsearch .common .unit .TimeValue ;
25
+ import org .elasticsearch .index .IndexSettings ;
24
26
import org .elasticsearch .index .shard .ShardId ;
25
27
import org .elasticsearch .test .IndexSettingsModule ;
26
28
27
29
import java .util .Collection ;
28
30
import java .util .Collections ;
29
31
import java .util .HashMap ;
30
32
import java .util .Map ;
33
+ import java .util .concurrent .atomic .AtomicLong ;
34
+ import java .util .function .LongSupplier ;
31
35
32
36
import static org .elasticsearch .index .seqno .SequenceNumbers .UNASSIGNED_SEQ_NO ;
33
37
import static org .hamcrest .Matchers .equalTo ;
34
38
import static org .hamcrest .Matchers .hasItem ;
35
39
import static org .hamcrest .Matchers .hasSize ;
40
+ import static org .hamcrest .Matchers .lessThanOrEqualTo ;
36
41
37
42
public class ReplicationTrackerRetentionLeaseTests extends ReplicationTrackerTestCase {
38
43
@@ -43,7 +48,8 @@ public void testAddOrUpdateRetentionLease() {
43
48
id .getId (),
44
49
IndexSettingsModule .newIndexSettings ("test" , Settings .EMPTY ),
45
50
UNASSIGNED_SEQ_NO ,
46
- value -> {});
51
+ value -> {},
52
+ () -> 0L );
47
53
replicationTracker .updateFromMaster (
48
54
randomNonNegativeLong (),
49
55
Collections .singleton (id .getId ()),
@@ -55,19 +61,73 @@ public void testAddOrUpdateRetentionLease() {
55
61
for (int i = 0 ; i < length ; i ++) {
56
62
minimumRetainingSequenceNumbers [i ] = randomLongBetween (SequenceNumbers .NO_OPS_PERFORMED , Long .MAX_VALUE );
57
63
replicationTracker .addOrUpdateRetentionLease (Integer .toString (i ), minimumRetainingSequenceNumbers [i ], "test-" + i );
58
- assertRetentionLeases (replicationTracker , i + 1 , minimumRetainingSequenceNumbers );
64
+ assertRetentionLeases (replicationTracker , i + 1 , minimumRetainingSequenceNumbers , () -> 0L );
59
65
}
60
66
61
67
for (int i = 0 ; i < length ; i ++) {
62
68
minimumRetainingSequenceNumbers [i ] = randomLongBetween (minimumRetainingSequenceNumbers [i ], Long .MAX_VALUE );
63
69
replicationTracker .addOrUpdateRetentionLease (Integer .toString (i ), minimumRetainingSequenceNumbers [i ], "test-" + i );
64
- assertRetentionLeases (replicationTracker , length , minimumRetainingSequenceNumbers );
70
+ assertRetentionLeases (replicationTracker , length , minimumRetainingSequenceNumbers , () -> 0L );
65
71
}
66
72
67
73
}
68
74
75
+ public void testExpiration () {
76
+ final AllocationId id = AllocationId .newInitializing ();
77
+ final AtomicLong currentTimeMillis = new AtomicLong (randomLongBetween (0 , 1024 ));
78
+ final long retentionLeaseMillis = randomLongBetween (1 , TimeValue .timeValueHours (12 ).millis ());
79
+ final Settings settings = Settings
80
+ .builder ()
81
+ .put (IndexSettings .INDEX_SOFT_DELETES_RETENTION_LEASE_SETTING .getKey (), TimeValue .timeValueMillis (retentionLeaseMillis ))
82
+ .build ();
83
+ final ReplicationTracker replicationTracker = new ReplicationTracker (
84
+ new ShardId ("test" , "_na" , 0 ),
85
+ id .getId (),
86
+ IndexSettingsModule .newIndexSettings ("test" , settings ),
87
+ UNASSIGNED_SEQ_NO ,
88
+ value -> {},
89
+ currentTimeMillis ::get );
90
+ replicationTracker .updateFromMaster (
91
+ randomNonNegativeLong (),
92
+ Collections .singleton (id .getId ()),
93
+ routingTable (Collections .emptySet (), id ),
94
+ Collections .emptySet ());
95
+ replicationTracker .activatePrimaryMode (SequenceNumbers .NO_OPS_PERFORMED );
96
+ final long [] retainingSequenceNumbers = new long [1 ];
97
+ retainingSequenceNumbers [0 ] = randomLongBetween (SequenceNumbers .NO_OPS_PERFORMED , Long .MAX_VALUE );
98
+ replicationTracker .addOrUpdateRetentionLease ("0" , retainingSequenceNumbers [0 ], "test-0" );
99
+
100
+ {
101
+ final Collection <RetentionLease > retentionLeases = replicationTracker .getRetentionLeases ();
102
+ assertThat (retentionLeases , hasSize (1 ));
103
+ final RetentionLease retentionLease = retentionLeases .iterator ().next ();
104
+ assertThat (retentionLease .timestamp (), equalTo (currentTimeMillis .get ()));
105
+ assertRetentionLeases (replicationTracker , 1 , retainingSequenceNumbers , currentTimeMillis ::get );
106
+ }
107
+
108
+ // renew the lease
109
+ currentTimeMillis .set (currentTimeMillis .get () + randomLongBetween (0 , 1024 ));
110
+ retainingSequenceNumbers [0 ] = randomLongBetween (retainingSequenceNumbers [0 ], Long .MAX_VALUE );
111
+ replicationTracker .addOrUpdateRetentionLease ("0" , retainingSequenceNumbers [0 ], "test-0" );
112
+
113
+ {
114
+ final Collection <RetentionLease > retentionLeases = replicationTracker .getRetentionLeases ();
115
+ assertThat (retentionLeases , hasSize (1 ));
116
+ final RetentionLease retentionLease = retentionLeases .iterator ().next ();
117
+ assertThat (retentionLease .timestamp (), equalTo (currentTimeMillis .get ()));
118
+ assertRetentionLeases (replicationTracker , 1 , retainingSequenceNumbers , currentTimeMillis ::get );
119
+ }
120
+
121
+ // now force the lease to expire
122
+ currentTimeMillis .set (currentTimeMillis .get () + randomLongBetween (retentionLeaseMillis , Long .MAX_VALUE - currentTimeMillis .get ()));
123
+ assertRetentionLeases (replicationTracker , 0 , retainingSequenceNumbers , currentTimeMillis ::get );
124
+ }
125
+
69
126
private void assertRetentionLeases (
70
- final ReplicationTracker replicationTracker , final int size , final long [] minimumRetainingSequenceNumbers ) {
127
+ final ReplicationTracker replicationTracker ,
128
+ final int size ,
129
+ final long [] minimumRetainingSequenceNumbers ,
130
+ final LongSupplier currentTimeMillisSupplier ) {
71
131
final Collection <RetentionLease > retentionLeases = replicationTracker .getRetentionLeases ();
72
132
final Map <String , RetentionLease > idToRetentionLease = new HashMap <>();
73
133
for (final RetentionLease retentionLease : retentionLeases ) {
@@ -79,6 +139,9 @@ private void assertRetentionLeases(
79
139
assertThat (idToRetentionLease .keySet (), hasItem (Integer .toString (i )));
80
140
final RetentionLease retentionLease = idToRetentionLease .get (Integer .toString (i ));
81
141
assertThat (retentionLease .retainingSequenceNumber (), equalTo (minimumRetainingSequenceNumbers [i ]));
142
+ assertThat (
143
+ currentTimeMillisSupplier .getAsLong () - retentionLease .timestamp (),
144
+ lessThanOrEqualTo (replicationTracker .indexSettings ().getRetentionLeaseMillis ()));
82
145
assertThat (retentionLease .source (), equalTo ("test-" + i ));
83
146
}
84
147
}
0 commit comments