Skip to content

Commit 8e66df9

Browse files
committed
Move testRetentionLeasesClearedOnRestore (#45896)
1 parent aee92d5 commit 8e66df9

File tree

2 files changed

+81
-62
lines changed

2 files changed

+81
-62
lines changed

server/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStats;
3131
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus;
3232
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse;
33+
import org.elasticsearch.action.admin.indices.stats.ShardStats;
3334
import org.elasticsearch.action.index.IndexRequestBuilder;
3435
import org.elasticsearch.action.support.ActiveShardCount;
3536
import org.elasticsearch.action.support.master.AcknowledgedResponse;
@@ -41,6 +42,7 @@
4142
import org.elasticsearch.cluster.NamedDiff;
4243
import org.elasticsearch.cluster.SnapshotsInProgress;
4344
import org.elasticsearch.cluster.health.ClusterHealthStatus;
45+
import org.elasticsearch.cluster.metadata.IndexMetaData;
4446
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
4547
import org.elasticsearch.cluster.metadata.MetaData;
4648
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider;
@@ -62,6 +64,9 @@
6264
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
6365
import org.elasticsearch.common.xcontent.XContentParser;
6466
import org.elasticsearch.env.Environment;
67+
import org.elasticsearch.index.seqno.RetentionLeaseActions;
68+
import org.elasticsearch.index.seqno.RetentionLeases;
69+
import org.elasticsearch.index.shard.ShardId;
6570
import org.elasticsearch.indices.recovery.RecoveryState;
6671
import org.elasticsearch.node.Node;
6772
import org.elasticsearch.plugins.Plugin;
@@ -95,12 +100,15 @@
95100
import java.util.Collections;
96101
import java.util.EnumSet;
97102
import java.util.List;
103+
import java.util.Locale;
98104
import java.util.concurrent.CountDownLatch;
99105
import java.util.concurrent.TimeUnit;
100106
import java.util.concurrent.atomic.AtomicReference;
101107
import java.util.function.Consumer;
102108

109+
import static org.elasticsearch.index.seqno.RetentionLeaseActions.RETAIN_ALL;
103110
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
111+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
104112
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
105113
import static org.hamcrest.Matchers.allOf;
106114
import static org.hamcrest.Matchers.containsString;
@@ -1229,6 +1237,79 @@ public void testDataNodeRestartWithBusyMasterDuringSnapshot() throws Exception {
12291237
}, 60L, TimeUnit.SECONDS);
12301238
}
12311239

1240+
public void testRetentionLeasesClearedOnRestore() throws Exception {
1241+
final String repoName = "test-repo-retention-leases";
1242+
assertAcked(client().admin().cluster().preparePutRepository(repoName)
1243+
.setType("fs")
1244+
.setSettings(Settings.builder()
1245+
.put("location", randomRepoPath())
1246+
.put("compress", randomBoolean())));
1247+
1248+
final String indexName = "index-retention-leases";
1249+
final int shardCount = randomIntBetween(1, 5);
1250+
assertAcked(client().admin().indices().prepareCreate(indexName)
1251+
.setSettings(Settings.builder()
1252+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, shardCount)
1253+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0))
1254+
.get());
1255+
final ShardId shardId = new ShardId(resolveIndex(indexName), randomIntBetween(0, shardCount - 1));
1256+
1257+
final int snapshotDocCount = iterations(10, 1000);
1258+
logger.debug("--> indexing {} docs into {}", snapshotDocCount, indexName);
1259+
IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[snapshotDocCount];
1260+
for (int i = 0; i < snapshotDocCount; i++) {
1261+
indexRequestBuilders[i] = client().prepareIndex(indexName, "_doc").setSource("field", "value");
1262+
}
1263+
indexRandom(true, indexRequestBuilders);
1264+
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), snapshotDocCount);
1265+
1266+
final String leaseId = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT);
1267+
logger.debug("--> adding retention lease with id {} to {}", leaseId, shardId);
1268+
client().execute(RetentionLeaseActions.Add.INSTANCE, new RetentionLeaseActions.AddRequest(
1269+
shardId, leaseId, RETAIN_ALL, "test")).actionGet();
1270+
1271+
final ShardStats shardStats = Arrays.stream(client().admin().indices().prepareStats(indexName).get().getShards())
1272+
.filter(s -> s.getShardRouting().shardId().equals(shardId)).findFirst().get();
1273+
final RetentionLeases retentionLeases = shardStats.getRetentionLeaseStats().retentionLeases();
1274+
assertTrue(shardStats + ": " + retentionLeases, retentionLeases.contains(leaseId));
1275+
1276+
final String snapshotName = "snapshot-retention-leases";
1277+
logger.debug("--> create snapshot {}:{}", repoName, snapshotName);
1278+
CreateSnapshotResponse createResponse = client().admin().cluster().prepareCreateSnapshot(repoName, snapshotName)
1279+
.setWaitForCompletion(true).setIndices(indexName).get();
1280+
assertThat(createResponse.getSnapshotInfo().successfulShards(), equalTo(shardCount));
1281+
assertThat(createResponse.getSnapshotInfo().failedShards(), equalTo(0));
1282+
1283+
if (randomBoolean()) {
1284+
final int extraDocCount = iterations(10, 1000);
1285+
logger.debug("--> indexing {} extra docs into {}", extraDocCount, indexName);
1286+
indexRequestBuilders = new IndexRequestBuilder[extraDocCount];
1287+
for (int i = 0; i < extraDocCount; i++) {
1288+
indexRequestBuilders[i] = client().prepareIndex(indexName, "_doc").setSource("field", "value");
1289+
}
1290+
indexRandom(true, indexRequestBuilders);
1291+
}
1292+
1293+
// 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
1294+
ensureGreen();
1295+
logger.debug("--> close index {}", indexName);
1296+
assertAcked(client().admin().indices().prepareClose(indexName));
1297+
1298+
logger.debug("--> restore index {} from snapshot", indexName);
1299+
RestoreSnapshotResponse restoreResponse = client().admin().cluster().prepareRestoreSnapshot(repoName, snapshotName)
1300+
.setWaitForCompletion(true).get();
1301+
assertThat(restoreResponse.getRestoreInfo().successfulShards(), equalTo(shardCount));
1302+
assertThat(restoreResponse.getRestoreInfo().failedShards(), equalTo(0));
1303+
1304+
ensureGreen();
1305+
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), snapshotDocCount);
1306+
1307+
final RetentionLeases restoredRetentionLeases = Arrays.stream(client().admin().indices().prepareStats(indexName).get()
1308+
.getShards()).filter(s -> s.getShardRouting().shardId().equals(shardId)).findFirst().get()
1309+
.getRetentionLeaseStats().retentionLeases();
1310+
assertFalse(restoredRetentionLeases.toString() + " has no " + leaseId, restoredRetentionLeases.contains(leaseId));
1311+
}
1312+
12321313
private long calculateTotalFilesSize(List<Path> files) {
12331314
return files.stream().mapToLong(f -> {
12341315
try {
@@ -1239,7 +1320,6 @@ private long calculateTotalFilesSize(List<Path> files) {
12391320
}).sum();
12401321
}
12411322

1242-
12431323
private List<Path> scanSnapshotFolder(Path repoPath) throws IOException {
12441324
List<Path> files = new ArrayList<>();
12451325
Files.walkFileTree(repoPath, new SimpleFileVisitor<Path>(){

test/framework/src/main/java/org/elasticsearch/repositories/blobstore/ESBlobStoreRepositoryIntegTestCase.java

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@
2323
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
2424
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequestBuilder;
2525
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
26-
import org.elasticsearch.action.admin.indices.stats.ShardStats;
2726
import org.elasticsearch.action.index.IndexRequestBuilder;
2827
import org.elasticsearch.client.Client;
29-
import org.elasticsearch.cluster.metadata.IndexMetaData;
3028
import org.elasticsearch.common.blobstore.BlobContainer;
31-
import org.elasticsearch.common.settings.Settings;
32-
import org.elasticsearch.index.seqno.RetentionLeaseActions;
33-
import org.elasticsearch.index.seqno.RetentionLeases;
34-
import org.elasticsearch.index.shard.ShardId;
3529
import org.elasticsearch.repositories.IndexId;
3630
import org.elasticsearch.repositories.RepositoriesService;
3731
import org.elasticsearch.repositories.Repository;
@@ -49,7 +43,6 @@
4943
import java.util.concurrent.CountDownLatch;
5044
import java.util.concurrent.ExecutionException;
5145

52-
import static org.elasticsearch.index.seqno.RetentionLeaseActions.RETAIN_ALL;
5346
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
5447
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
5548
import static org.hamcrest.Matchers.equalTo;
@@ -278,60 +271,6 @@ public void testIndicesDeletedFromRepository() throws Exception {
278271
}
279272
}
280273

281-
public void testRetentionLeasesClearedOnRestore() throws Exception {
282-
final String repoName = randomAsciiName();
283-
logger.info("--> creating repository {}", repoName);
284-
createAndCheckTestRepository(repoName);
285-
286-
final String indexName = randomAsciiName();
287-
final int shardCount = randomIntBetween(1, 5);
288-
assertAcked(client().admin().indices().prepareCreate(indexName).setSettings(
289-
Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, shardCount)).get());
290-
final ShardId shardId = new ShardId(resolveIndex(indexName), randomIntBetween(0, shardCount - 1));
291-
292-
final int snapshotDocCount = iterations(10, 1000);
293-
logger.info("--> indexing {} docs into {}", snapshotDocCount, indexName);
294-
addRandomDocuments(indexName, snapshotDocCount);
295-
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), snapshotDocCount);
296-
297-
final String leaseId = randomAsciiName();
298-
logger.info("--> adding retention lease with id {} to {}", leaseId, shardId);
299-
client().execute(RetentionLeaseActions.Add.INSTANCE, new RetentionLeaseActions.AddRequest(
300-
shardId, leaseId, RETAIN_ALL, "test")).actionGet();
301-
302-
final ShardStats shardStats = Arrays.stream(client().admin().indices().prepareStats(indexName).get().getShards())
303-
.filter(s -> s.getShardRouting().shardId().equals(shardId)).findFirst().get();
304-
final RetentionLeases retentionLeases = shardStats.getRetentionLeaseStats().retentionLeases();
305-
assertTrue(shardStats + ": " + retentionLeases, retentionLeases.contains(leaseId));
306-
307-
final String snapshotName = randomAsciiName();
308-
logger.info("--> create snapshot {}:{}", repoName, snapshotName);
309-
assertSuccessfulSnapshot(client().admin().cluster().prepareCreateSnapshot(repoName, snapshotName)
310-
.setWaitForCompletion(true).setIndices(indexName));
311-
312-
if (randomBoolean()) {
313-
final int extraDocCount = iterations(10, 1000);
314-
logger.info("--> indexing {} extra docs into {}", extraDocCount, indexName);
315-
addRandomDocuments(indexName, extraDocCount);
316-
}
317-
318-
// 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
319-
ensureGreen();
320-
logger.info("--> close index {}", indexName);
321-
assertAcked(client().admin().indices().prepareClose(indexName));
322-
323-
logger.info("--> restore index {} from snapshot", indexName);
324-
assertSuccessfulRestore(client().admin().cluster().prepareRestoreSnapshot(repoName, snapshotName).setWaitForCompletion(true));
325-
326-
ensureGreen();
327-
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), snapshotDocCount);
328-
329-
final RetentionLeases restoredRetentionLeases = Arrays.stream(client().admin().indices().prepareStats(indexName).get()
330-
.getShards()).filter(s -> s.getShardRouting().shardId().equals(shardId)).findFirst().get()
331-
.getRetentionLeaseStats().retentionLeases();
332-
assertFalse(restoredRetentionLeases.toString() + " has no " + leaseId, restoredRetentionLeases.contains(leaseId));
333-
}
334-
335274
protected void addRandomDocuments(String name, int numDocs) throws ExecutionException, InterruptedException {
336275
IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[numDocs];
337276
for (int i = 0; i < numDocs; i++) {

0 commit comments

Comments
 (0)