|
12 | 12 | import org.elasticsearch.action.DocWriteResponse; |
13 | 13 | import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; |
14 | 14 | import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; |
| 15 | +import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; |
15 | 16 | import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; |
16 | 17 | import org.elasticsearch.action.admin.indices.rollover.RolloverResponse; |
| 18 | +import org.elasticsearch.action.index.IndexResponse; |
| 19 | +import org.elasticsearch.action.support.IndicesOptions; |
| 20 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 21 | +import org.elasticsearch.client.Client; |
| 22 | +import org.elasticsearch.cluster.metadata.DataStream; |
17 | 23 | import org.elasticsearch.common.settings.Settings; |
18 | 24 | import org.elasticsearch.common.unit.ByteSizeUnit; |
| 25 | +import org.elasticsearch.index.Index; |
| 26 | +import org.elasticsearch.plugins.Plugin; |
| 27 | +import org.elasticsearch.rest.RestStatus; |
| 28 | +import org.elasticsearch.search.SearchHit; |
19 | 29 | import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; |
20 | 30 | import org.elasticsearch.snapshots.RestoreInfo; |
21 | 31 | import org.elasticsearch.snapshots.SnapshotInProgressException; |
22 | 32 | import org.elasticsearch.snapshots.SnapshotInfo; |
23 | 33 | import org.elasticsearch.snapshots.SnapshotRestoreException; |
24 | 34 | import org.elasticsearch.snapshots.SnapshotState; |
| 35 | +import org.elasticsearch.snapshots.mockstore.MockRepository; |
25 | 36 | import org.elasticsearch.xpack.core.action.CreateDataStreamAction; |
26 | 37 | import org.elasticsearch.xpack.core.action.DeleteDataStreamAction; |
27 | 38 | import org.elasticsearch.xpack.core.action.GetDataStreamAction; |
28 | | -import org.elasticsearch.action.index.IndexResponse; |
29 | | -import org.elasticsearch.action.support.IndicesOptions; |
30 | | -import org.elasticsearch.action.support.master.AcknowledgedResponse; |
31 | | -import org.elasticsearch.client.Client; |
32 | | -import org.elasticsearch.cluster.metadata.DataStream; |
33 | | -import org.elasticsearch.plugins.Plugin; |
34 | | -import org.elasticsearch.rest.RestStatus; |
35 | | -import org.elasticsearch.search.SearchHit; |
36 | | -import org.elasticsearch.snapshots.mockstore.MockRepository; |
37 | 39 | import org.elasticsearch.xpack.datastreams.DataStreamsPlugin; |
38 | 40 | import org.hamcrest.Matchers; |
39 | 41 | import org.junit.After; |
|
45 | 47 | import java.util.List; |
46 | 48 | import java.util.Map; |
47 | 49 | import java.util.concurrent.ExecutionException; |
| 50 | +import java.util.stream.Collectors; |
48 | 51 |
|
49 | 52 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
50 | 53 | import static org.hamcrest.Matchers.contains; |
51 | 54 | import static org.hamcrest.Matchers.containsString; |
52 | 55 | import static org.hamcrest.Matchers.empty; |
53 | 56 | import static org.hamcrest.Matchers.equalTo; |
54 | 57 | import static org.hamcrest.Matchers.hasItems; |
| 58 | +import static org.hamcrest.Matchers.hasSize; |
55 | 59 | import static org.hamcrest.Matchers.is; |
56 | 60 | import static org.hamcrest.Matchers.not; |
57 | 61 | import static org.hamcrest.Matchers.nullValue; |
@@ -145,6 +149,109 @@ public void testSnapshotAndRestore() throws Exception { |
145 | 149 | assertEquals(DS_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); |
146 | 150 | } |
147 | 151 |
|
| 152 | + public void testSnapshotAndRestoreAllDataStreamsInPlace() throws Exception { |
| 153 | + CreateSnapshotResponse createSnapshotResponse = client.admin() |
| 154 | + .cluster() |
| 155 | + .prepareCreateSnapshot(REPO, SNAPSHOT) |
| 156 | + .setWaitForCompletion(true) |
| 157 | + .setIndices("ds") |
| 158 | + .setIncludeGlobalState(false) |
| 159 | + .get(); |
| 160 | + |
| 161 | + RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); |
| 162 | + assertEquals(RestStatus.OK, status); |
| 163 | + |
| 164 | + assertEquals(Collections.singletonList(DS_BACKING_INDEX_NAME), getSnapshot(REPO, SNAPSHOT).indices()); |
| 165 | + |
| 166 | + // Close all indices: |
| 167 | + CloseIndexRequest closeIndexRequest = new CloseIndexRequest("*"); |
| 168 | + closeIndexRequest.indicesOptions(IndicesOptions.strictExpandHidden()); |
| 169 | + assertAcked(client.admin().indices().close(closeIndexRequest).actionGet()); |
| 170 | + |
| 171 | + RestoreSnapshotResponse restoreSnapshotResponse = client.admin() |
| 172 | + .cluster() |
| 173 | + .prepareRestoreSnapshot(REPO, SNAPSHOT) |
| 174 | + .setWaitForCompletion(true) |
| 175 | + .setIndices("ds") |
| 176 | + .get(); |
| 177 | + assertEquals(1, restoreSnapshotResponse.getRestoreInfo().successfulShards()); |
| 178 | + |
| 179 | + assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS_BACKING_INDEX_NAME, id).get().getSourceAsMap()); |
| 180 | + SearchHit[] hits = client.prepareSearch("ds").get().getHits().getHits(); |
| 181 | + assertEquals(1, hits.length); |
| 182 | + assertEquals(DOCUMENT_SOURCE, hits[0].getSourceAsMap()); |
| 183 | + |
| 184 | + GetDataStreamAction.Request getDataSteamRequest = new GetDataStreamAction.Request(new String[] { "*" }); |
| 185 | + GetDataStreamAction.Response ds = client.execute(GetDataStreamAction.INSTANCE, getDataSteamRequest).get(); |
| 186 | + assertThat( |
| 187 | + ds.getDataStreams().stream().map(e -> e.getDataStream().getName()).collect(Collectors.toList()), |
| 188 | + contains(equalTo("ds"), equalTo("other-ds")) |
| 189 | + ); |
| 190 | + List<Index> backingIndices = ds.getDataStreams().get(0).getDataStream().getIndices(); |
| 191 | + assertThat(backingIndices.stream().map(Index::getName).collect(Collectors.toList()), contains(DS_BACKING_INDEX_NAME)); |
| 192 | + backingIndices = ds.getDataStreams().get(1).getDataStream().getIndices(); |
| 193 | + String expectedBackingIndexName = DataStream.getDefaultBackingIndexName("other-ds", 1); |
| 194 | + assertThat(backingIndices.stream().map(Index::getName).collect(Collectors.toList()), contains(expectedBackingIndexName)); |
| 195 | + } |
| 196 | + |
| 197 | + public void testSnapshotAndRestoreInPlace() throws Exception { |
| 198 | + CreateSnapshotResponse createSnapshotResponse = client.admin() |
| 199 | + .cluster() |
| 200 | + .prepareCreateSnapshot(REPO, SNAPSHOT) |
| 201 | + .setWaitForCompletion(true) |
| 202 | + .setIndices("ds") |
| 203 | + .setIncludeGlobalState(false) |
| 204 | + .get(); |
| 205 | + |
| 206 | + RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); |
| 207 | + assertEquals(RestStatus.OK, status); |
| 208 | + |
| 209 | + assertEquals(Collections.singletonList(DS_BACKING_INDEX_NAME), getSnapshot(REPO, SNAPSHOT).indices()); |
| 210 | + |
| 211 | + // A rollover after taking snapshot. The new backing index should be a standalone index after restoring |
| 212 | + // and not part of the data stream: |
| 213 | + RolloverRequest rolloverRequest = new RolloverRequest("ds", null); |
| 214 | + RolloverResponse rolloverResponse = client.admin().indices().rolloverIndex(rolloverRequest).actionGet(); |
| 215 | + assertThat(rolloverResponse.isRolledOver(), is(true)); |
| 216 | + assertThat(rolloverResponse.getNewIndex(), equalTo(DataStream.getDefaultBackingIndexName("ds", 2))); |
| 217 | + |
| 218 | + // Close all backing indices of ds data stream: |
| 219 | + CloseIndexRequest closeIndexRequest = new CloseIndexRequest(".ds-ds-*"); |
| 220 | + closeIndexRequest.indicesOptions(IndicesOptions.strictExpandHidden()); |
| 221 | + assertAcked(client.admin().indices().close(closeIndexRequest).actionGet()); |
| 222 | + |
| 223 | + RestoreSnapshotResponse restoreSnapshotResponse = client.admin() |
| 224 | + .cluster() |
| 225 | + .prepareRestoreSnapshot(REPO, SNAPSHOT) |
| 226 | + .setWaitForCompletion(true) |
| 227 | + .setIndices("ds") |
| 228 | + .get(); |
| 229 | + assertEquals(1, restoreSnapshotResponse.getRestoreInfo().successfulShards()); |
| 230 | + |
| 231 | + assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS_BACKING_INDEX_NAME, id).get().getSourceAsMap()); |
| 232 | + SearchHit[] hits = client.prepareSearch("ds").get().getHits().getHits(); |
| 233 | + assertEquals(1, hits.length); |
| 234 | + assertEquals(DOCUMENT_SOURCE, hits[0].getSourceAsMap()); |
| 235 | + |
| 236 | + GetDataStreamAction.Request getDataSteamRequest = new GetDataStreamAction.Request(new String[] { "ds" }); |
| 237 | + GetDataStreamAction.Response ds = client.execute(GetDataStreamAction.INSTANCE, getDataSteamRequest).actionGet(); |
| 238 | + assertThat( |
| 239 | + ds.getDataStreams().stream().map(e -> e.getDataStream().getName()).collect(Collectors.toList()), |
| 240 | + contains(equalTo("ds")) |
| 241 | + ); |
| 242 | + List<Index> backingIndices = ds.getDataStreams().get(0).getDataStream().getIndices(); |
| 243 | + assertThat(ds.getDataStreams().get(0).getDataStream().getIndices(), hasSize(1)); |
| 244 | + assertThat(backingIndices.stream().map(Index::getName).collect(Collectors.toList()), contains(equalTo(DS_BACKING_INDEX_NAME))); |
| 245 | + |
| 246 | + // The backing index created as part of rollover should still exist (but just not part of the data stream) |
| 247 | + assertThat(indexExists(DataStream.getDefaultBackingIndexName("ds", 2)), is(true)); |
| 248 | + // An additional rollover should create a new backing index (3th generation) and leave .ds-ds-...-2 index as is: |
| 249 | + rolloverRequest = new RolloverRequest("ds", null); |
| 250 | + rolloverResponse = client.admin().indices().rolloverIndex(rolloverRequest).actionGet(); |
| 251 | + assertThat(rolloverResponse.isRolledOver(), is(true)); |
| 252 | + assertThat(rolloverResponse.getNewIndex(), equalTo(DataStream.getDefaultBackingIndexName("ds", 3))); |
| 253 | + } |
| 254 | + |
148 | 255 | public void testSnapshotAndRestoreAll() throws Exception { |
149 | 256 | CreateSnapshotResponse createSnapshotResponse = client.admin() |
150 | 257 | .cluster() |
|
0 commit comments