|
116 | 116 | import java.util.function.Consumer;
|
117 | 117 | import java.util.function.Predicate;
|
118 | 118 | import java.util.stream.Collectors;
|
| 119 | +import java.util.stream.IntStream; |
119 | 120 | import java.util.stream.Stream;
|
120 | 121 |
|
121 | 122 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
|
@@ -3704,6 +3705,48 @@ public void testAbortedSnapshotDuringInitDoesNotStart() throws Exception {
|
3704 | 3705 | }
|
3705 | 3706 | }
|
3706 | 3707 |
|
| 3708 | + public void testRestoreIncreasesPrimaryTerms() { |
| 3709 | + final String indexName = randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT); |
| 3710 | + createIndex(indexName, Settings.builder() |
| 3711 | + .put(SETTING_NUMBER_OF_SHARDS, 2) |
| 3712 | + .put(SETTING_NUMBER_OF_REPLICAS, 0) |
| 3713 | + .build()); |
| 3714 | + ensureGreen(indexName); |
| 3715 | + |
| 3716 | + if (randomBoolean()) { |
| 3717 | + // open and close the index to increase the primary terms |
| 3718 | + for (int i = 0; i < randomInt(3); i++) { |
| 3719 | + assertAcked(client().admin().indices().prepareClose(indexName)); |
| 3720 | + assertAcked(client().admin().indices().prepareOpen(indexName)); |
| 3721 | + } |
| 3722 | + } |
| 3723 | + |
| 3724 | + final IndexMetaData indexMetaData = client().admin().cluster().prepareState().clear().setIndices(indexName) |
| 3725 | + .setMetaData(true).get().getState().metaData().index(indexName); |
| 3726 | + final int numPrimaries = getNumShards(indexName).numPrimaries; |
| 3727 | + final Map<Integer, Long> primaryTerms = IntStream.range(0, numPrimaries) |
| 3728 | + .boxed().collect(Collectors.toMap(shardId -> shardId, indexMetaData::primaryTerm)); |
| 3729 | + |
| 3730 | + assertAcked(client().admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(randomRepoSettings())); |
| 3731 | + final CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap") |
| 3732 | + .setWaitForCompletion(true).setIndices(indexName).get(); |
| 3733 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(numPrimaries)); |
| 3734 | + assertThat(createSnapshotResponse.getSnapshotInfo().failedShards(), equalTo(0)); |
| 3735 | + |
| 3736 | + assertAcked(client().admin().indices().prepareClose(indexName)); |
| 3737 | + |
| 3738 | + final RestoreSnapshotResponse restoreSnapshotResponse = client().admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap") |
| 3739 | + .setWaitForCompletion(true).get(); |
| 3740 | + assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), equalTo(numPrimaries)); |
| 3741 | + assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0)); |
| 3742 | + |
| 3743 | + final IndexMetaData restoredIndexMetaData = client().admin().cluster().prepareState().clear().setIndices(indexName) |
| 3744 | + .setMetaData(true).get().getState().metaData().index(indexName); |
| 3745 | + for (int shardId = 0; shardId < numPrimaries; shardId++) { |
| 3746 | + assertThat(restoredIndexMetaData.primaryTerm(shardId), equalTo(primaryTerms.get(shardId) + 1)); |
| 3747 | + } |
| 3748 | + } |
| 3749 | + |
3707 | 3750 | private RepositoryData getRepositoryData(Repository repository) throws InterruptedException {
|
3708 | 3751 | ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class, internalCluster().getMasterName());
|
3709 | 3752 | final SetOnce<RepositoryData> repositoryData = new SetOnce<>();
|
|
0 commit comments