11
11
import org .elasticsearch .snapshots .SnapshotId ;
12
12
import org .elasticsearch .snapshots .SnapshotInfo ;
13
13
import org .elasticsearch .snapshots .SnapshotShardFailure ;
14
+ import org .elasticsearch .snapshots .SnapshotState ;
14
15
import org .elasticsearch .test .ESTestCase ;
15
16
import org .elasticsearch .xpack .core .slm .SnapshotLifecyclePolicy ;
16
17
import org .elasticsearch .xpack .core .slm .SnapshotRetentionConfiguration ;
@@ -103,13 +104,21 @@ public void testMaximum() {
103
104
}
104
105
105
106
public void testFailuresDeletedIfExpired () {
107
+ assertUnsuccessfulDeletedIfExpired (true );
108
+ }
109
+
110
+ public void testPartialsDeletedIfExpired () {
111
+ assertUnsuccessfulDeletedIfExpired (false );
112
+ }
113
+
114
+ private void assertUnsuccessfulDeletedIfExpired (boolean failure ) {
106
115
SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration (
107
116
() -> TimeValue .timeValueDays (1 ).millis () + 1 ,
108
117
TimeValue .timeValueDays (1 ), null , null );
109
- SnapshotInfo oldInfo = makeFailureInfo ( 0 );
118
+ SnapshotInfo oldInfo = makeFailureOrPartial ( 0 , failure );
110
119
assertThat (conf .getSnapshotDeletionPredicate (Collections .singletonList (oldInfo )).test (oldInfo ), equalTo (true ));
111
120
112
- SnapshotInfo newInfo = makeFailureInfo ( 1 );
121
+ SnapshotInfo newInfo = makeFailureOrPartial ( 1 , failure );
113
122
assertThat (conf .getSnapshotDeletionPredicate (Collections .singletonList (newInfo )).test (newInfo ), equalTo (false ));
114
123
115
124
List <SnapshotInfo > infos = new ArrayList <>();
@@ -120,10 +129,18 @@ public void testFailuresDeletedIfExpired() {
120
129
}
121
130
122
131
public void testFailuresDeletedIfNoExpiryAndMoreRecentSuccessExists () {
132
+ assertUnsuccessfulDeletedIfNoExpiryAndMoreRecentSuccessExists (true );
133
+ }
134
+
135
+ public void testPartialsDeletedIfNoExpiryAndMoreRecentSuccessExists () {
136
+ assertUnsuccessfulDeletedIfNoExpiryAndMoreRecentSuccessExists (false );
137
+ }
138
+
139
+ private void assertUnsuccessfulDeletedIfNoExpiryAndMoreRecentSuccessExists (boolean failure ) {
123
140
SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration (() -> 1 , null , 2 , 5 );
124
141
SnapshotInfo s1 = makeInfo (1 );
125
142
SnapshotInfo s2 = makeInfo (2 );
126
- SnapshotInfo s3 = makeFailureInfo ( 3 );
143
+ SnapshotInfo s3 = makeFailureOrPartial ( 3 , failure );
127
144
SnapshotInfo s4 = makeInfo (4 );
128
145
129
146
List <SnapshotInfo > infos = Arrays .asList (s1 , s2 , s3 , s4 );
@@ -134,12 +151,20 @@ public void testFailuresDeletedIfNoExpiryAndMoreRecentSuccessExists() {
134
151
}
135
152
136
153
public void testFailuresKeptIfNoExpiryAndNoMoreRecentSuccess () {
154
+ assertUnsuccessfulKeptIfNoExpiryAndNoMoreRecentSuccess (true );
155
+ }
156
+
157
+ public void testPartialsKeptIfNoExpiryAndNoMoreRecentSuccess () {
158
+ assertUnsuccessfulKeptIfNoExpiryAndNoMoreRecentSuccess (false );
159
+ }
160
+
161
+ private void assertUnsuccessfulKeptIfNoExpiryAndNoMoreRecentSuccess (boolean failure ) {
137
162
// Also tests that failures are not counted towards the maximum
138
163
SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration (() -> 1 , null , 2 , 3 );
139
164
SnapshotInfo s1 = makeInfo (1 );
140
165
SnapshotInfo s2 = makeInfo (2 );
141
166
SnapshotInfo s3 = makeInfo (3 );
142
- SnapshotInfo s4 = makeFailureInfo ( 4 );
167
+ SnapshotInfo s4 = makeFailureOrPartial ( 4 , failure );
143
168
144
169
List <SnapshotInfo > infos = Arrays .asList (s1 , s2 , s3 , s4 );
145
170
assertThat (conf .getSnapshotDeletionPredicate (infos ).test (s1 ), equalTo (false ));
@@ -149,11 +174,19 @@ public void testFailuresKeptIfNoExpiryAndNoMoreRecentSuccess() {
149
174
}
150
175
151
176
public void testFailuresNotCountedTowardsMaximum () {
177
+ assertUnsuccessfulNotCountedTowardsMaximum (true );
178
+ }
179
+
180
+ public void testPartialsNotCountedTowardsMaximum () {
181
+ assertUnsuccessfulNotCountedTowardsMaximum (false );
182
+ }
183
+
184
+ private void assertUnsuccessfulNotCountedTowardsMaximum (boolean failure ) {
152
185
SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration (() -> 1 , TimeValue .timeValueDays (1 ), 2 , 2 );
153
186
SnapshotInfo s1 = makeInfo (1 );
154
- SnapshotInfo s2 = makeFailureInfo ( 2 );
155
- SnapshotInfo s3 = makeFailureInfo ( 3 );
156
- SnapshotInfo s4 = makeFailureInfo ( 4 );
187
+ SnapshotInfo s2 = makeFailureOrPartial ( 2 , failure );
188
+ SnapshotInfo s3 = makeFailureOrPartial ( 3 , failure );
189
+ SnapshotInfo s4 = makeFailureOrPartial ( 4 , failure );
157
190
SnapshotInfo s5 = makeInfo (5 );
158
191
159
192
List <SnapshotInfo > infos = Arrays .asList (s1 , s2 , s3 , s4 , s5 );
@@ -165,10 +198,18 @@ public void testFailuresNotCountedTowardsMaximum() {
165
198
}
166
199
167
200
public void testFailuresNotCountedTowardsMinimum () {
201
+ assertUnsuccessfulNotCountedTowardsMinimum (true );
202
+ }
203
+
204
+ public void testPartialsNotCountedTowardsMinimum () {
205
+ assertUnsuccessfulNotCountedTowardsMinimum (false );
206
+ }
207
+
208
+ private void assertUnsuccessfulNotCountedTowardsMinimum (boolean failure ) {
168
209
SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration (() -> TimeValue .timeValueDays (1 ).millis () + 1 ,
169
210
TimeValue .timeValueDays (1 ), 2 , null );
170
211
SnapshotInfo oldInfo = makeInfo (0 );
171
- SnapshotInfo failureInfo = makeFailureInfo ( 1 );
212
+ SnapshotInfo failureInfo = makeFailureOrPartial ( 1 , failure );
172
213
SnapshotInfo newInfo = makeInfo (2 );
173
214
174
215
List <SnapshotInfo > infos = new ArrayList <>();
@@ -186,12 +227,14 @@ public void testFailuresNotCountedTowardsMinimum() {
186
227
assertThat (conf .getSnapshotDeletionPredicate (infos ).test (oldInfo ), equalTo (true ));
187
228
}
188
229
230
+
189
231
public void testMostRecentSuccessfulTimestampIsUsed () {
232
+ boolean failureBeforePartial = randomBoolean ();
190
233
SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration (() -> 1 , null , 2 , 2 );
191
234
SnapshotInfo s1 = makeInfo (1 );
192
235
SnapshotInfo s2 = makeInfo (2 );
193
- SnapshotInfo s3 = makeFailureInfo ( 3 );
194
- SnapshotInfo s4 = makeFailureInfo ( 4 );
236
+ SnapshotInfo s3 = makeFailureOrPartial ( 3 , failureBeforePartial );
237
+ SnapshotInfo s4 = makeFailureOrPartial ( 4 , failureBeforePartial == false );
195
238
196
239
List <SnapshotInfo > infos = Arrays .asList (s1 , s2 , s3 , s4 );
197
240
assertThat (conf .getSnapshotDeletionPredicate (infos ).test (s1 ), equalTo (false ));
@@ -204,15 +247,25 @@ private SnapshotInfo makeInfo(long startTime) {
204
247
final Map <String , Object > meta = new HashMap <>();
205
248
meta .put (SnapshotLifecyclePolicy .POLICY_ID_METADATA_FIELD , REPO );
206
249
final int totalShards = between (1 ,20 );
207
- return new SnapshotInfo (new SnapshotId ("snap-" + randomAlphaOfLength (3 ), "uuid" ),
250
+ SnapshotInfo snapInfo = new SnapshotInfo (new SnapshotId ("snap-" + randomAlphaOfLength (3 ), "uuid" ),
208
251
Collections .singletonList ("foo" ),
209
252
startTime ,
210
253
null ,
211
- startTime + between (1 ,10000 ),
254
+ startTime + between (1 , 10000 ),
212
255
totalShards ,
213
256
new ArrayList <>(),
214
257
false ,
215
258
meta );
259
+ assertThat (snapInfo .state (), equalTo (SnapshotState .SUCCESS ));
260
+ return snapInfo ;
261
+ }
262
+
263
+ private SnapshotInfo makeFailureOrPartial (long startTime , boolean failure ) {
264
+ if (failure ) {
265
+ return makeFailureInfo (startTime );
266
+ } else {
267
+ return makePartialInfo (startTime );
268
+ }
216
269
}
217
270
218
271
private SnapshotInfo makeFailureInfo (long startTime ) {
@@ -225,14 +278,39 @@ private SnapshotInfo makeFailureInfo(long startTime) {
225
278
failures .add (new SnapshotShardFailure ("nodeId" , new ShardId ("index-name" , "index-uuid" , i ), "failed" ));
226
279
}
227
280
assert failureCount == failures .size ();
228
- return new SnapshotInfo (new SnapshotId ("snap-fail-" + randomAlphaOfLength (3 ), "uuid-fail" ),
281
+ SnapshotInfo snapInfo = new SnapshotInfo (new SnapshotId ("snap-fail-" + randomAlphaOfLength (3 ), "uuid-fail" ),
229
282
Collections .singletonList ("foo-fail" ),
230
283
startTime ,
231
284
"forced-failure" ,
232
- startTime + between (1 ,10000 ),
285
+ startTime + between (1 , 10000 ),
286
+ totalShards ,
287
+ failures ,
288
+ randomBoolean (),
289
+ meta );
290
+ assertThat (snapInfo .state (), equalTo (SnapshotState .FAILED ));
291
+ return snapInfo ;
292
+ }
293
+
294
+ private SnapshotInfo makePartialInfo (long startTime ) {
295
+ final Map <String , Object > meta = new HashMap <>();
296
+ meta .put (SnapshotLifecyclePolicy .POLICY_ID_METADATA_FIELD , REPO );
297
+ final int totalShards = between (2 ,20 );
298
+ final List <SnapshotShardFailure > failures = new ArrayList <>();
299
+ final int failureCount = between (1 ,totalShards - 1 );
300
+ for (int i = 0 ; i < failureCount ; i ++) {
301
+ failures .add (new SnapshotShardFailure ("nodeId" , new ShardId ("index-name" , "index-uuid" , i ), "failed" ));
302
+ }
303
+ assert failureCount == failures .size ();
304
+ SnapshotInfo snapInfo = new SnapshotInfo (new SnapshotId ("snap-fail-" + randomAlphaOfLength (3 ), "uuid-fail" ),
305
+ Collections .singletonList ("foo-fail" ),
306
+ startTime ,
307
+ null ,
308
+ startTime + between (1 , 10000 ),
233
309
totalShards ,
234
310
failures ,
235
311
randomBoolean (),
236
312
meta );
313
+ assertThat (snapInfo .state (), equalTo (SnapshotState .PARTIAL ));
314
+ return snapInfo ;
237
315
}
238
316
}
0 commit comments