|
19 | 19 |
|
20 | 20 | package org.elasticsearch.cluster.metadata;
|
21 | 21 |
|
| 22 | +import org.elasticsearch.action.admin.indices.datastream.DeleteDataStreamRequestTests; |
22 | 23 | import org.elasticsearch.cluster.ClusterName;
|
23 | 24 | import org.elasticsearch.cluster.ClusterState;
|
24 | 25 | import org.elasticsearch.cluster.SnapshotsInProgress;
|
25 | 26 | import org.elasticsearch.cluster.block.ClusterBlocks;
|
26 | 27 | import org.elasticsearch.cluster.routing.RoutingTable;
|
27 | 28 | import org.elasticsearch.cluster.routing.allocation.AllocationService;
|
28 | 29 | import org.elasticsearch.common.collect.ImmutableOpenMap;
|
| 30 | +import org.elasticsearch.common.collect.Tuple; |
29 | 31 | import org.elasticsearch.common.settings.Settings;
|
30 | 32 | import org.elasticsearch.index.Index;
|
31 | 33 | import org.elasticsearch.index.IndexNotFoundException;
|
|
36 | 38 | import org.elasticsearch.snapshots.SnapshotInfoTests;
|
37 | 39 | import org.elasticsearch.test.ESTestCase;
|
38 | 40 | import org.elasticsearch.test.VersionUtils;
|
| 41 | +import org.hamcrest.core.IsNull; |
| 42 | +import org.junit.Before; |
| 43 | + |
| 44 | +import java.util.List; |
| 45 | +import java.util.Locale; |
| 46 | +import java.util.Set; |
39 | 47 |
|
40 | 48 | import static java.util.Collections.singleton;
|
41 | 49 | import static java.util.Collections.singletonList;
|
| 50 | +import static org.hamcrest.CoreMatchers.containsString; |
| 51 | +import static org.hamcrest.CoreMatchers.equalTo; |
42 | 52 | import static org.mockito.Matchers.any;
|
43 | 53 | import static org.mockito.Mockito.mock;
|
44 | 54 | import static org.mockito.Mockito.verify;
|
45 | 55 | import static org.mockito.Mockito.when;
|
46 | 56 |
|
47 | 57 |
|
48 | 58 | public class MetadataDeleteIndexServiceTests extends ESTestCase {
|
49 |
| - private final AllocationService allocationService = mock(AllocationService.class); |
50 |
| - private final MetadataDeleteIndexService service = new MetadataDeleteIndexService(Settings.EMPTY, null, allocationService); |
| 59 | + private AllocationService allocationService; |
| 60 | + private MetadataDeleteIndexService service; |
| 61 | + |
| 62 | + @Override |
| 63 | + @Before |
| 64 | + public void setUp() throws Exception { |
| 65 | + super.setUp(); |
| 66 | + allocationService = mock(AllocationService.class); |
| 67 | + when(allocationService.reroute(any(ClusterState.class), any(String.class))) |
| 68 | + .thenAnswer(mockInvocation -> mockInvocation.getArguments()[0]); |
| 69 | + service = new MetadataDeleteIndexService(Settings.EMPTY, null, allocationService); |
| 70 | + } |
51 | 71 |
|
52 | 72 | public void testDeleteMissing() {
|
53 | 73 | Index index = new Index("missing", "doesn't matter");
|
@@ -92,6 +112,35 @@ public void testDeleteUnassigned() {
|
92 | 112 | verify(allocationService).reroute(any(ClusterState.class), any(String.class));
|
93 | 113 | }
|
94 | 114 |
|
| 115 | + public void testDeleteBackingIndexForDataStream() { |
| 116 | + int numBackingIndices = randomIntBetween(2, 5); |
| 117 | + String dataStreamName = randomAlphaOfLength(6).toLowerCase(Locale.ROOT); |
| 118 | + ClusterState before = DeleteDataStreamRequestTests.getClusterStateWithDataStreams( |
| 119 | + List.of(new Tuple<>(dataStreamName, numBackingIndices)), List.of()); |
| 120 | + |
| 121 | + int numIndexToDelete = randomIntBetween(1, numBackingIndices - 1); |
| 122 | + |
| 123 | + Index indexToDelete = before.metadata().index(DataStream.getBackingIndexName(dataStreamName, numIndexToDelete)).getIndex(); |
| 124 | + ClusterState after = service.deleteIndices(before, Set.of(indexToDelete)); |
| 125 | + |
| 126 | + assertThat(after.metadata().getIndices().get(indexToDelete.getName()), IsNull.nullValue()); |
| 127 | + assertThat(after.metadata().getIndices().size(), equalTo(numBackingIndices - 1)); |
| 128 | + assertThat(after.metadata().getIndices().get(DataStream.getBackingIndexName(dataStreamName, numIndexToDelete)), IsNull.nullValue()); |
| 129 | + } |
| 130 | + |
| 131 | + public void testDeleteCurrentWriteIndexForDataStream() { |
| 132 | + int numBackingIndices = randomIntBetween(1, 5); |
| 133 | + String dataStreamName = randomAlphaOfLength(6).toLowerCase(Locale.ROOT); |
| 134 | + ClusterState before = DeleteDataStreamRequestTests.getClusterStateWithDataStreams( |
| 135 | + List.of(new Tuple<>(dataStreamName, numBackingIndices)), List.of()); |
| 136 | + |
| 137 | + Index indexToDelete = before.metadata().index(DataStream.getBackingIndexName(dataStreamName, numBackingIndices)).getIndex(); |
| 138 | + Exception e = expectThrows(IllegalArgumentException.class, () -> service.deleteIndices(before, Set.of(indexToDelete))); |
| 139 | + |
| 140 | + assertThat(e.getMessage(), containsString("index [" + indexToDelete.getName() + "] is the write index for data stream [" + |
| 141 | + dataStreamName + "] and cannot be deleted")); |
| 142 | + } |
| 143 | + |
95 | 144 | private ClusterState clusterState(String index) {
|
96 | 145 | IndexMetadata indexMetadata = IndexMetadata.builder(index)
|
97 | 146 | .settings(Settings.builder().put("index.version.created", VersionUtils.randomVersion(random())))
|
|
0 commit comments