use of org.opensearch.index.seqno.RetentionLeases in project OpenSearch by opensearch-project.
the class DedicatedClusterSnapshotRestoreIT method testRetentionLeasesClearedOnRestore.
public void testRetentionLeasesClearedOnRestore() throws Exception {
final String repoName = "test-repo-retention-leases";
createRepository(repoName, "fs");
final String indexName = "index-retention-leases";
final int shardCount = randomIntBetween(1, 5);
assertAcked(client().admin().indices().prepareCreate(indexName).setSettings(indexSettingsNoReplicas(shardCount)));
final ShardId shardId = new ShardId(resolveIndex(indexName), randomIntBetween(0, shardCount - 1));
final int snapshotDocCount = iterations(10, 1000);
logger.debug("--> indexing {} docs into {}", snapshotDocCount, indexName);
IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[snapshotDocCount];
for (int i = 0; i < snapshotDocCount; i++) {
indexRequestBuilders[i] = client().prepareIndex(indexName).setSource("field", "value");
}
indexRandom(true, indexRequestBuilders);
assertDocCount(indexName, snapshotDocCount);
final String leaseId = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT);
logger.debug("--> adding retention lease with id {} to {}", leaseId, shardId);
client().execute(RetentionLeaseActions.Add.INSTANCE, new RetentionLeaseActions.AddRequest(shardId, leaseId, RETAIN_ALL, "test")).actionGet();
final ShardStats shardStats = Arrays.stream(client().admin().indices().prepareStats(indexName).get().getShards()).filter(s -> s.getShardRouting().shardId().equals(shardId)).findFirst().get();
final RetentionLeases retentionLeases = shardStats.getRetentionLeaseStats().retentionLeases();
assertTrue(shardStats + ": " + retentionLeases, retentionLeases.contains(leaseId));
final String snapshotName = "snapshot-retention-leases";
createSnapshot(repoName, snapshotName, Collections.singletonList(indexName));
if (randomBoolean()) {
final int extraDocCount = iterations(10, 1000);
logger.debug("--> indexing {} extra docs into {}", extraDocCount, indexName);
indexRequestBuilders = new IndexRequestBuilder[extraDocCount];
for (int i = 0; i < extraDocCount; i++) {
indexRequestBuilders[i] = client().prepareIndex(indexName).setSource("field", "value");
}
indexRandom(true, indexRequestBuilders);
}
// Wait for green so the close does not fail in the edge case of coinciding with a shard recovery that hasn't fully synced yet
ensureGreen();
logger.debug("--> close index {}", indexName);
assertAcked(client().admin().indices().prepareClose(indexName));
logger.debug("--> restore index {} from snapshot", indexName);
RestoreSnapshotResponse restoreResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapshotName).setWaitForCompletion(true).get();
assertThat(restoreResponse.getRestoreInfo().successfulShards(), equalTo(shardCount));
assertThat(restoreResponse.getRestoreInfo().failedShards(), equalTo(0));
ensureGreen();
assertDocCount(indexName, snapshotDocCount);
final RetentionLeases restoredRetentionLeases = Arrays.stream(client().admin().indices().prepareStats(indexName).get().getShards()).filter(s -> s.getShardRouting().shardId().equals(shardId)).findFirst().get().getRetentionLeaseStats().retentionLeases();
assertFalse(restoredRetentionLeases.toString() + " has no " + leaseId, restoredRetentionLeases.contains(leaseId));
}
Aggregations