|
26 | 26 | import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
|
27 | 27 | import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
|
28 | 28 | import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
|
| 29 | +import org.elasticsearch.action.admin.indices.recovery.RecoveryRequest; |
29 | 30 | import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse;
|
30 | 31 | import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
|
31 | 32 | import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
|
32 | 33 | import org.elasticsearch.action.index.IndexRequestBuilder;
|
33 | 34 | import org.elasticsearch.action.search.SearchResponse;
|
| 35 | +import org.elasticsearch.action.support.WriteRequest.RefreshPolicy; |
34 | 36 | import org.elasticsearch.cluster.action.shard.ShardStateAction;
|
35 | 37 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
36 | 38 | import org.elasticsearch.cluster.routing.RecoverySource;
|
@@ -786,4 +788,55 @@ public void sendRequest(Transport.Connection connection, long requestId, String
|
786 | 788 | assertHitCount(client().prepareSearch(indexName).get(), numDocs);
|
787 | 789 | }
|
788 | 790 | }
|
| 791 | + |
| 792 | + @TestLogging("org.elasticsearch.indices.recovery:TRACE") |
| 793 | + public void testHistoryRetention() throws Exception { |
| 794 | + internalCluster().startNodes(3); |
| 795 | + |
| 796 | + final String indexName = "test"; |
| 797 | + client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder() |
| 798 | + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) |
| 799 | + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 2)).get(); |
| 800 | + ensureGreen(indexName); |
| 801 | + |
| 802 | + // Perform some replicated operations so the replica isn't simply empty, because ops-based recovery isn't better in that case |
| 803 | + final List<IndexRequestBuilder> requests = new ArrayList<>(); |
| 804 | + final int replicatedDocCount = scaledRandomIntBetween(25, 250); |
| 805 | + while (requests.size() < replicatedDocCount) { |
| 806 | + requests.add(client().prepareIndex(indexName, "_doc").setSource("{}", XContentType.JSON)); |
| 807 | + } |
| 808 | + indexRandom(true, requests); |
| 809 | + if (randomBoolean()) { |
| 810 | + flush(indexName); |
| 811 | + } |
| 812 | + |
| 813 | + internalCluster().stopRandomNode(s -> true); |
| 814 | + internalCluster().stopRandomNode(s -> true); |
| 815 | + |
| 816 | + final long desyncNanoTime = System.nanoTime(); |
| 817 | + while (System.nanoTime() <= desyncNanoTime) { |
| 818 | + // time passes |
| 819 | + } |
| 820 | + |
| 821 | + final int numNewDocs = scaledRandomIntBetween(25, 250); |
| 822 | + for (int i = 0; i < numNewDocs; i++) { |
| 823 | + client().prepareIndex(indexName, "_doc").setSource("{}", XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE).get(); |
| 824 | + } |
| 825 | + // Flush twice to update the safe commit's local checkpoint |
| 826 | + assertThat(client().admin().indices().prepareFlush(indexName).setForce(true).execute().get().getFailedShards(), equalTo(0)); |
| 827 | + assertThat(client().admin().indices().prepareFlush(indexName).setForce(true).execute().get().getFailedShards(), equalTo(0)); |
| 828 | + |
| 829 | + assertAcked(client().admin().indices().prepareUpdateSettings(indexName) |
| 830 | + .setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1))); |
| 831 | + internalCluster().startNode(); |
| 832 | + ensureGreen(indexName); |
| 833 | + |
| 834 | + final RecoveryResponse recoveryResponse = client().admin().indices().recoveries(new RecoveryRequest(indexName)).get(); |
| 835 | + final List<RecoveryState> recoveryStates = recoveryResponse.shardRecoveryStates().get(indexName); |
| 836 | + recoveryStates.removeIf(r -> r.getTimer().getStartNanoTime() <= desyncNanoTime); |
| 837 | + |
| 838 | + assertThat(recoveryStates, hasSize(1)); |
| 839 | + assertThat(recoveryStates.get(0).getIndex().totalFileCount(), is(0)); |
| 840 | + assertThat(recoveryStates.get(0).getTranslog().recoveredOperations(), greaterThan(0)); |
| 841 | + } |
789 | 842 | }
|
0 commit comments