Search in sources :

Example 31 with RetentionLeases

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));
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) Matchers.containsString(org.hamcrest.Matchers.containsString) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) RetentionLeases(org.opensearch.index.seqno.RetentionLeases)

Aggregations

RetentionLeases (org.opensearch.index.seqno.RetentionLeases)31 ArrayList (java.util.ArrayList)16 IndexSettings (org.opensearch.index.IndexSettings)16 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)13 Settings (org.opensearch.common.settings.Settings)13 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)12 RetentionLease (org.opensearch.index.seqno.RetentionLease)12 IndexShard (org.opensearch.index.shard.IndexShard)12 List (java.util.List)11 ActionListener (org.opensearch.action.ActionListener)11 IOException (java.io.IOException)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 IndexCommit (org.apache.lucene.index.IndexCommit)9 ShardRouting (org.opensearch.cluster.routing.ShardRouting)9 GatedCloseable (org.opensearch.common.concurrent.GatedCloseable)8 ReplicationTracker (org.opensearch.index.seqno.ReplicationTracker)8 Translog (org.opensearch.index.translog.Translog)8 Arrays (java.util.Arrays)7