|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.oldrepos; |
| 10 | + |
| 11 | +import org.apache.http.HttpHost; |
| 12 | +import org.elasticsearch.Version; |
| 13 | +import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; |
| 14 | +import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest; |
| 15 | +import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus; |
| 16 | +import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest; |
| 17 | +import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse; |
| 18 | +import org.elasticsearch.client.Node; |
| 19 | +import org.elasticsearch.client.Request; |
| 20 | +import org.elasticsearch.client.RequestOptions; |
| 21 | +import org.elasticsearch.client.RestClient; |
| 22 | +import org.elasticsearch.client.RestHighLevelClient; |
| 23 | +import org.elasticsearch.cluster.SnapshotsInProgress; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.common.util.set.Sets; |
| 26 | +import org.elasticsearch.snapshots.SnapshotInfo; |
| 27 | +import org.elasticsearch.snapshots.SnapshotState; |
| 28 | +import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; |
| 29 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +import static org.hamcrest.Matchers.greaterThan; |
| 38 | +import static org.hamcrest.Matchers.hasSize; |
| 39 | + |
| 40 | +public class OldRepositoryAccessIT extends ESRestTestCase { |
| 41 | + @Override |
| 42 | + protected Map<String, List<Map<?, ?>>> wipeSnapshots() { |
| 43 | + return Collections.emptyMap(); |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings("removal") |
| 47 | + public void testOldRepoAccess() throws IOException { |
| 48 | + String repoLocation = System.getProperty("tests.repo.location"); |
| 49 | + Version oldVersion = Version.fromString(System.getProperty("tests.es.version")); |
| 50 | + |
| 51 | + int oldEsPort = Integer.parseInt(System.getProperty("tests.es.port")); |
| 52 | + try ( |
| 53 | + RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(adminClient().getNodes().toArray(new Node[0]))); |
| 54 | + RestClient oldEs = RestClient.builder(new HttpHost("127.0.0.1", oldEsPort)).build() |
| 55 | + ) { |
| 56 | + try { |
| 57 | + Request createIndex = new Request("PUT", "/test"); |
| 58 | + int numberOfShards = randomIntBetween(1, 3); |
| 59 | + createIndex.setJsonEntity("{\"settings\":{\"number_of_shards\": " + numberOfShards + "}}"); |
| 60 | + oldEs.performRequest(createIndex); |
| 61 | + |
| 62 | + for (int i = 0; i < 5; i++) { |
| 63 | + Request doc = new Request("PUT", "/test/doc/testdoc" + i); |
| 64 | + doc.addParameter("refresh", "true"); |
| 65 | + doc.setJsonEntity("{\"test\":\"test" + i + "\", \"val\":" + i + "}"); |
| 66 | + oldEs.performRequest(doc); |
| 67 | + } |
| 68 | + |
| 69 | + // register repo on old ES and take snapshot |
| 70 | + Request createRepoRequest = new Request("PUT", "/_snapshot/testrepo"); |
| 71 | + createRepoRequest.setJsonEntity("{\"type\":\"fs\",\"settings\":{\"location\":\"" + repoLocation + "\"}}"); |
| 72 | + oldEs.performRequest(createRepoRequest); |
| 73 | + |
| 74 | + Request createSnapshotRequest = new Request("PUT", "/_snapshot/testrepo/snap1"); |
| 75 | + createSnapshotRequest.addParameter("wait_for_completion", "true"); |
| 76 | + createSnapshotRequest.setJsonEntity("{\"indices\":\"test\"}"); |
| 77 | + oldEs.performRequest(createSnapshotRequest); |
| 78 | + |
| 79 | + // register repo on new ES |
| 80 | + ElasticsearchAssertions.assertAcked( |
| 81 | + client.snapshot() |
| 82 | + .createRepository( |
| 83 | + new PutRepositoryRequest("testrepo").type("fs") |
| 84 | + .settings(Settings.builder().put("location", repoLocation).build()), |
| 85 | + RequestOptions.DEFAULT |
| 86 | + ) |
| 87 | + ); |
| 88 | + |
| 89 | + // list snapshots on new ES |
| 90 | + List<SnapshotInfo> snapshotInfos = client.snapshot() |
| 91 | + .get(new GetSnapshotsRequest("testrepo").snapshots(new String[] { "_all" }), RequestOptions.DEFAULT) |
| 92 | + .getSnapshots(); |
| 93 | + assertThat(snapshotInfos, hasSize(1)); |
| 94 | + SnapshotInfo snapshotInfo = snapshotInfos.get(0); |
| 95 | + assertEquals("snap1", snapshotInfo.snapshotId().getName()); |
| 96 | + assertEquals("testrepo", snapshotInfo.repository()); |
| 97 | + assertEquals(Arrays.asList("test"), snapshotInfo.indices()); |
| 98 | + assertEquals(SnapshotState.SUCCESS, snapshotInfo.state()); |
| 99 | + assertEquals(numberOfShards, snapshotInfo.successfulShards()); |
| 100 | + assertEquals(numberOfShards, snapshotInfo.totalShards()); |
| 101 | + assertEquals(0, snapshotInfo.failedShards()); |
| 102 | + assertEquals(oldVersion, snapshotInfo.version()); |
| 103 | + |
| 104 | + // list specific snapshot on new ES |
| 105 | + snapshotInfos = client.snapshot() |
| 106 | + .get(new GetSnapshotsRequest("testrepo").snapshots(new String[] { "snap1" }), RequestOptions.DEFAULT) |
| 107 | + .getSnapshots(); |
| 108 | + assertThat(snapshotInfos, hasSize(1)); |
| 109 | + snapshotInfo = snapshotInfos.get(0); |
| 110 | + assertEquals("snap1", snapshotInfo.snapshotId().getName()); |
| 111 | + assertEquals("testrepo", snapshotInfo.repository()); |
| 112 | + assertEquals(Arrays.asList("test"), snapshotInfo.indices()); |
| 113 | + assertEquals(SnapshotState.SUCCESS, snapshotInfo.state()); |
| 114 | + assertEquals(numberOfShards, snapshotInfo.successfulShards()); |
| 115 | + assertEquals(numberOfShards, snapshotInfo.totalShards()); |
| 116 | + assertEquals(0, snapshotInfo.failedShards()); |
| 117 | + assertEquals(oldVersion, snapshotInfo.version()); |
| 118 | + |
| 119 | + // list advanced snapshot info on new ES |
| 120 | + SnapshotsStatusResponse snapshotsStatusResponse = client.snapshot() |
| 121 | + .status(new SnapshotsStatusRequest("testrepo").snapshots(new String[] { "snap1" }), RequestOptions.DEFAULT); |
| 122 | + assertThat(snapshotsStatusResponse.getSnapshots(), hasSize(1)); |
| 123 | + SnapshotStatus snapshotStatus = snapshotsStatusResponse.getSnapshots().get(0); |
| 124 | + assertEquals("snap1", snapshotStatus.getSnapshot().getSnapshotId().getName()); |
| 125 | + assertEquals("testrepo", snapshotStatus.getSnapshot().getRepository()); |
| 126 | + assertEquals(Sets.newHashSet("test"), snapshotStatus.getIndices().keySet()); |
| 127 | + assertEquals(SnapshotsInProgress.State.SUCCESS, snapshotStatus.getState()); |
| 128 | + assertEquals(numberOfShards, snapshotStatus.getShardsStats().getDoneShards()); |
| 129 | + assertEquals(numberOfShards, snapshotStatus.getShardsStats().getTotalShards()); |
| 130 | + assertEquals(0, snapshotStatus.getShardsStats().getFailedShards()); |
| 131 | + assertThat(snapshotStatus.getStats().getTotalSize(), greaterThan(0L)); |
| 132 | + assertThat(snapshotStatus.getStats().getTotalFileCount(), greaterThan(0)); |
| 133 | + } finally { |
| 134 | + oldEs.performRequest(new Request("DELETE", "/test")); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments