|
23 | 23 | import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
|
24 | 24 | import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequestBuilder;
|
25 | 25 | import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
|
| 26 | +import org.elasticsearch.action.admin.indices.stats.ShardStats; |
26 | 27 | import org.elasticsearch.action.index.IndexRequestBuilder;
|
27 | 28 | import org.elasticsearch.client.Client;
|
| 29 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
28 | 30 | import org.elasticsearch.common.blobstore.BlobContainer;
|
| 31 | +import org.elasticsearch.common.settings.Settings; |
| 32 | +import org.elasticsearch.index.IndexSettings; |
| 33 | +import org.elasticsearch.index.seqno.RetentionLeaseActions; |
| 34 | +import org.elasticsearch.index.seqno.RetentionLeases; |
| 35 | +import org.elasticsearch.index.shard.ShardId; |
29 | 36 | import org.elasticsearch.repositories.IndexId;
|
30 | 37 | import org.elasticsearch.repositories.RepositoriesService;
|
31 | 38 | import org.elasticsearch.repositories.Repository;
|
|
43 | 50 | import java.util.concurrent.CountDownLatch;
|
44 | 51 | import java.util.concurrent.ExecutionException;
|
45 | 52 |
|
| 53 | +import static org.elasticsearch.index.seqno.RetentionLeaseActions.RETAIN_ALL; |
46 | 54 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
47 | 55 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
48 | 56 | import static org.hamcrest.Matchers.equalTo;
|
@@ -87,8 +95,8 @@ public void testSnapshotAndRestore() throws Exception {
|
87 | 95 | int[] docCounts = new int[indexCount];
|
88 | 96 | String[] indexNames = generateRandomNames(indexCount);
|
89 | 97 | for (int i = 0; i < indexCount; i++) {
|
90 |
| - logger.info("--> create random index {} with {} records", indexNames[i], docCounts[i]); |
91 | 98 | docCounts[i] = iterations(10, 1000);
|
| 99 | + logger.info("--> create random index {} with {} records", indexNames[i], docCounts[i]); |
92 | 100 | addRandomDocuments(indexNames[i], docCounts[i]);
|
93 | 101 | assertHitCount(client().prepareSearch(indexNames[i]).setSize(0).get(), docCounts[i]);
|
94 | 102 | }
|
@@ -267,6 +275,59 @@ public void testIndicesDeletedFromRepository() throws Exception {
|
267 | 275 | }
|
268 | 276 | }
|
269 | 277 |
|
| 278 | + public void testRetentionLeasesClearedOnRestore() throws Exception { |
| 279 | + final String repoName = randomAsciiName(); |
| 280 | + logger.info("--> creating repository {}", repoName); |
| 281 | + createAndCheckTestRepository(repoName); |
| 282 | + |
| 283 | + final String indexName = randomAsciiName(); |
| 284 | + final int shardCount = randomIntBetween(1, 5); |
| 285 | + assertAcked(client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder() |
| 286 | + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, shardCount) |
| 287 | + .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true)).get()); |
| 288 | + final ShardId shardId = new ShardId(resolveIndex(indexName), randomIntBetween(0, shardCount - 1)); |
| 289 | + |
| 290 | + final int snapshotDocCount = iterations(10, 1000); |
| 291 | + logger.info("--> indexing {} docs into {}", snapshotDocCount, indexName); |
| 292 | + addRandomDocuments(indexName, snapshotDocCount); |
| 293 | + assertHitCount(client().prepareSearch(indexName).setSize(0).get(), snapshotDocCount); |
| 294 | + |
| 295 | + final String leaseId = randomAsciiName(); |
| 296 | + logger.info("--> adding retention lease with id {} to {}", leaseId, shardId); |
| 297 | + client().execute(RetentionLeaseActions.Add.INSTANCE, new RetentionLeaseActions.AddRequest( |
| 298 | + shardId, leaseId, RETAIN_ALL, "test")).actionGet(); |
| 299 | + |
| 300 | + final ShardStats shardStats = Arrays.stream(client().admin().indices().prepareStats(indexName).get().getShards()) |
| 301 | + .filter(s -> s.getShardRouting().shardId().equals(shardId)).findFirst().get(); |
| 302 | + final RetentionLeases retentionLeases = shardStats.getRetentionLeaseStats().retentionLeases(); |
| 303 | + assertTrue(shardStats + ": " + retentionLeases, retentionLeases.contains(leaseId)); |
| 304 | + |
| 305 | + final String snapshotName = randomAsciiName(); |
| 306 | + logger.info("--> create snapshot {}:{}", repoName, snapshotName); |
| 307 | + assertSuccessfulSnapshot(client().admin().cluster().prepareCreateSnapshot(repoName, snapshotName) |
| 308 | + .setWaitForCompletion(true).setIndices(indexName)); |
| 309 | + |
| 310 | + if (randomBoolean()) { |
| 311 | + final int extraDocCount = iterations(10, 1000); |
| 312 | + logger.info("--> indexing {} extra docs into {}", extraDocCount, indexName); |
| 313 | + addRandomDocuments(indexName, extraDocCount); |
| 314 | + } |
| 315 | + |
| 316 | + logger.info("--> close index {}", indexName); |
| 317 | + assertAcked(client().admin().indices().prepareClose(indexName)); |
| 318 | + |
| 319 | + logger.info("--> restore index {} from snapshot", indexName); |
| 320 | + assertSuccessfulRestore(client().admin().cluster().prepareRestoreSnapshot(repoName, snapshotName).setWaitForCompletion(true)); |
| 321 | + |
| 322 | + ensureGreen(); |
| 323 | + assertHitCount(client().prepareSearch(indexName).setSize(0).get(), snapshotDocCount); |
| 324 | + |
| 325 | + final RetentionLeases restoredRetentionLeases = Arrays.stream(client().admin().indices().prepareStats(indexName).get() |
| 326 | + .getShards()).filter(s -> s.getShardRouting().shardId().equals(shardId)).findFirst().get() |
| 327 | + .getRetentionLeaseStats().retentionLeases(); |
| 328 | + assertFalse(restoredRetentionLeases.toString() + " has no " + leaseId, restoredRetentionLeases.contains(leaseId)); |
| 329 | + } |
| 330 | + |
270 | 331 | protected void addRandomDocuments(String name, int numDocs) throws ExecutionException, InterruptedException {
|
271 | 332 | IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[numDocs];
|
272 | 333 | for (int i = 0; i < numDocs; i++) {
|
|
0 commit comments