|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | 
|  | 3 | + * or more contributor license agreements. Licensed under the "Elastic License | 
|  | 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | 
|  | 5 | + * Public License v 1"; you may not use this file except in compliance with, at | 
|  | 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public | 
|  | 7 | + * License v3.0 only", or the "Server Side Public License, v 1". | 
|  | 8 | + */ | 
|  | 9 | + | 
|  | 10 | +package org.elasticsearch.upgrades; | 
|  | 11 | + | 
|  | 12 | +import com.carrotsearch.randomizedtesting.annotations.Name; | 
|  | 13 | + | 
|  | 14 | +import org.elasticsearch.client.Request; | 
|  | 15 | +import org.elasticsearch.common.Strings; | 
|  | 16 | +import org.elasticsearch.common.settings.Settings; | 
|  | 17 | +import org.elasticsearch.test.rest.ObjectPath; | 
|  | 18 | + | 
|  | 19 | +import java.io.IOException; | 
|  | 20 | +import java.util.Collection; | 
|  | 21 | + | 
|  | 22 | +import static org.elasticsearch.upgrades.SnapshotBasedRecoveryIT.indexDocs; | 
|  | 23 | +import static org.hamcrest.Matchers.empty; | 
|  | 24 | +import static org.hamcrest.Matchers.equalTo; | 
|  | 25 | +import static org.hamcrest.Matchers.not; | 
|  | 26 | + | 
|  | 27 | +public class RunningSnapshotIT extends AbstractRollingUpgradeTestCase { | 
|  | 28 | + | 
|  | 29 | +    public RunningSnapshotIT(@Name("upgradedNodes") int upgradedNodes) { | 
|  | 30 | +        super(upgradedNodes); | 
|  | 31 | +    } | 
|  | 32 | + | 
|  | 33 | +    public void testRunningSnapshotCompleteAfterUpgrade() throws Exception { | 
|  | 34 | +        final String indexName = "index"; | 
|  | 35 | +        final String repositoryName = "repo"; | 
|  | 36 | +        final String snapshotName = "snapshot"; | 
|  | 37 | +        final var nodeIds = getNodesInfo(client()).keySet(); | 
|  | 38 | + | 
|  | 39 | +        if (isOldCluster()) { | 
|  | 40 | +            registerRepository(repositoryName, "fs", randomBoolean(), Settings.builder().put("location", "backup").build()); | 
|  | 41 | +            // create an index to have one shard per node | 
|  | 42 | +            createIndex(indexName, indexSettings(3, 0).put("index.routing.allocation.total_shards_per_node", 1).build()); | 
|  | 43 | +            ensureGreen(indexName); | 
|  | 44 | +            if (randomBoolean()) { | 
|  | 45 | +                indexDocs(indexName, between(10, 50)); | 
|  | 46 | +            } | 
|  | 47 | +            flush(indexName, true); | 
|  | 48 | +            // Signal shutdown to prevent snapshot from being completed | 
|  | 49 | +            putShutdownMetadata(nodeIds); | 
|  | 50 | +            createSnapshot(repositoryName, snapshotName, false); | 
|  | 51 | +            assertRunningSnapshot(repositoryName, snapshotName); | 
|  | 52 | +        } else { | 
|  | 53 | +            if (isUpgradedCluster()) { | 
|  | 54 | +                deleteShutdownMetadata(nodeIds); | 
|  | 55 | +                assertNoShutdownMetadata(nodeIds); | 
|  | 56 | +                ensureGreen(indexName); | 
|  | 57 | +                assertBusy(() -> assertCompletedSnapshot(repositoryName, snapshotName)); | 
|  | 58 | +            } else { | 
|  | 59 | +                assertRunningSnapshot(repositoryName, snapshotName); | 
|  | 60 | +            } | 
|  | 61 | +        } | 
|  | 62 | +    } | 
|  | 63 | + | 
|  | 64 | +    private void putShutdownMetadata(Collection<String> nodeIds) throws IOException { | 
|  | 65 | +        for (String nodeId : nodeIds) { | 
|  | 66 | +            final Request putShutdownRequest = new Request("PUT", "/_nodes/" + nodeId + "/shutdown"); | 
|  | 67 | +            putShutdownRequest.setJsonEntity(""" | 
|  | 68 | +                { | 
|  | 69 | +                  "type": "remove", | 
|  | 70 | +                  "reason": "test" | 
|  | 71 | +                }"""); | 
|  | 72 | +            client().performRequest(putShutdownRequest); | 
|  | 73 | +        } | 
|  | 74 | +    } | 
|  | 75 | + | 
|  | 76 | +    private void deleteShutdownMetadata(Collection<String> nodeIds) throws IOException { | 
|  | 77 | +        for (String nodeId : nodeIds) { | 
|  | 78 | +            final Request request = new Request("DELETE", "/_nodes/" + nodeId + "/shutdown"); | 
|  | 79 | +            client().performRequest(request); | 
|  | 80 | +        } | 
|  | 81 | +    } | 
|  | 82 | + | 
|  | 83 | +    private void assertNoShutdownMetadata(Collection<String> nodeIds) throws IOException { | 
|  | 84 | +        final ObjectPath responsePath = assertOKAndCreateObjectPath( | 
|  | 85 | +            client().performRequest(new Request("GET", "/_nodes/" + Strings.collectionToCommaDelimitedString(nodeIds) + "/shutdown")) | 
|  | 86 | +        ); | 
|  | 87 | +        assertThat(responsePath.evaluate("nodes"), empty()); | 
|  | 88 | +    } | 
|  | 89 | + | 
|  | 90 | +    private void assertRunningSnapshot(String repositoryName, String snapshotName) throws IOException { | 
|  | 91 | +        final Request request = new Request("GET", "/_snapshot/" + repositoryName + "/_current"); | 
|  | 92 | +        final ObjectPath responsePath = assertOKAndCreateObjectPath(client().performRequest(request)); | 
|  | 93 | +        assertThat(responsePath.evaluate("total"), equalTo(1)); | 
|  | 94 | +        assertThat(responsePath.evaluate("snapshots.0.snapshot"), equalTo(snapshotName)); | 
|  | 95 | +    } | 
|  | 96 | + | 
|  | 97 | +    private void assertCompletedSnapshot(String repositoryName, String snapshotName) throws IOException { | 
|  | 98 | +        { | 
|  | 99 | +            final Request request = new Request("GET", "/_snapshot/" + repositoryName + "/_current"); | 
|  | 100 | +            final ObjectPath responsePath = assertOKAndCreateObjectPath(client().performRequest(request)); | 
|  | 101 | +            assertThat(responsePath.evaluate("total"), equalTo(0)); | 
|  | 102 | +        } | 
|  | 103 | + | 
|  | 104 | +        { | 
|  | 105 | +            final Request request = new Request("GET", "/_snapshot/" + repositoryName + "/" + snapshotName); | 
|  | 106 | +            final ObjectPath responsePath = assertOKAndCreateObjectPath(client().performRequest(request)); | 
|  | 107 | +            assertThat(responsePath.evaluate("total"), equalTo(1)); | 
|  | 108 | +            assertThat(responsePath.evaluate("snapshots.0.snapshot"), equalTo(snapshotName)); | 
|  | 109 | +            assertThat(responsePath.evaluate("snapshots.0.state"), not(equalTo("IN_PROGRESS"))); | 
|  | 110 | +        } | 
|  | 111 | +    } | 
|  | 112 | +} | 
0 commit comments