|
20 | 20 | package org.elasticsearch.action.admin.indices.mapping.put;
|
21 | 21 |
|
22 | 22 | import org.elasticsearch.action.ActionRequestValidationException;
|
| 23 | +import org.elasticsearch.action.admin.indices.datastream.DeleteDataStreamRequestTests; |
| 24 | +import org.elasticsearch.cluster.ClusterState; |
| 25 | +import org.elasticsearch.cluster.metadata.AliasMetadata; |
| 26 | +import org.elasticsearch.cluster.metadata.IndexAbstraction; |
| 27 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 28 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 29 | +import org.elasticsearch.cluster.metadata.Metadata; |
23 | 30 | import org.elasticsearch.common.Strings;
|
24 | 31 | import org.elasticsearch.common.bytes.BytesReference;
|
| 32 | +import org.elasticsearch.common.collect.Tuple; |
25 | 33 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
26 | 34 | import org.elasticsearch.common.xcontent.XContentParser;
|
27 | 35 | import org.elasticsearch.common.xcontent.XContentType;
|
|
31 | 39 | import org.elasticsearch.test.ESTestCase;
|
32 | 40 |
|
33 | 41 | import java.io.IOException;
|
| 42 | +import java.util.Arrays; |
| 43 | +import java.util.List; |
| 44 | +import java.util.stream.Collectors; |
34 | 45 |
|
| 46 | +import static org.elasticsearch.common.collect.Tuple.tuple; |
35 | 47 | import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
|
| 48 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 49 | +import static org.hamcrest.Matchers.containsString; |
36 | 50 |
|
37 | 51 | public class PutMappingRequestTests extends ESTestCase {
|
38 | 52 |
|
@@ -75,6 +89,7 @@ public void testBuildFromSimplifiedDef() {
|
75 | 89 | assertEquals("mapping source must be pairs of fieldnames and properties definition.", e.getMessage());
|
76 | 90 | }
|
77 | 91 |
|
| 92 | + |
78 | 93 | public void testToXContent() throws IOException {
|
79 | 94 | PutMappingRequest request = new PutMappingRequest("foo");
|
80 | 95 | request.type("my_type");
|
@@ -138,4 +153,127 @@ private static PutMappingRequest createTestItem() throws IOException {
|
138 | 153 |
|
139 | 154 | return request;
|
140 | 155 | }
|
| 156 | + |
| 157 | + public void testResolveIndicesWithWriteIndexOnlyAndDataStreamsAndWriteAliases() { |
| 158 | + String[] dataStreamNames = {"foo", "bar", "baz"}; |
| 159 | + List<Tuple<String, Integer>> dsMetadata = org.elasticsearch.common.collect.List.of( |
| 160 | + tuple(dataStreamNames[0], randomIntBetween(1, 3)), |
| 161 | + tuple(dataStreamNames[1], randomIntBetween(1, 3)), |
| 162 | + tuple(dataStreamNames[2], randomIntBetween(1, 3))); |
| 163 | + |
| 164 | + ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, |
| 165 | + org.elasticsearch.common.collect.List.of("index1", "index2", "index3")); |
| 166 | + cs = addAliases(cs, org.elasticsearch.common.collect.List.of( |
| 167 | + tuple("alias1", org.elasticsearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), |
| 168 | + tuple("alias2", org.elasticsearch.common.collect.List.of(tuple("index2", false), tuple("index3", true))) |
| 169 | + )); |
| 170 | + PutMappingRequest request = new PutMappingRequest().indices("foo", "alias1", "alias2").writeIndexOnly(true); |
| 171 | + Index[] indices = TransportPutMappingAction.resolveIndices(cs, request, new IndexNameExpressionResolver()); |
| 172 | + List<String> indexNames = Arrays.stream(indices).map(Index::getName).collect(Collectors.toList()); |
| 173 | + IndexAbstraction expectedDs = cs.metadata().getIndicesLookup().get("foo"); |
| 174 | + // should resolve the data stream and each alias to their respective write indices |
| 175 | + assertThat(indexNames, containsInAnyOrder(expectedDs.getWriteIndex().getIndex().getName(), "index2", "index3")); |
| 176 | + } |
| 177 | + |
| 178 | + public void testResolveIndicesWithoutWriteIndexOnlyAndDataStreamsAndWriteAliases() { |
| 179 | + String[] dataStreamNames = {"foo", "bar", "baz"}; |
| 180 | + List<Tuple<String, Integer>> dsMetadata = org.elasticsearch.common.collect.List.of( |
| 181 | + tuple(dataStreamNames[0], randomIntBetween(1, 3)), |
| 182 | + tuple(dataStreamNames[1], randomIntBetween(1, 3)), |
| 183 | + tuple(dataStreamNames[2], randomIntBetween(1, 3))); |
| 184 | + |
| 185 | + ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, |
| 186 | + org.elasticsearch.common.collect.List.of("index1", "index2", "index3")); |
| 187 | + cs = addAliases(cs, org.elasticsearch.common.collect.List.of( |
| 188 | + tuple("alias1", org.elasticsearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), |
| 189 | + tuple("alias2", org.elasticsearch.common.collect.List.of(tuple("index2", false), tuple("index3", true))) |
| 190 | + )); |
| 191 | + PutMappingRequest request = new PutMappingRequest().indices("foo", "alias1", "alias2"); |
| 192 | + Index[] indices = TransportPutMappingAction.resolveIndices(cs, request, new IndexNameExpressionResolver()); |
| 193 | + List<String> indexNames = Arrays.stream(indices).map(Index::getName).collect(Collectors.toList()); |
| 194 | + IndexAbstraction expectedDs = cs.metadata().getIndicesLookup().get("foo"); |
| 195 | + List<String> expectedIndices = expectedDs.getIndices().stream().map(im -> im.getIndex().getName()).collect(Collectors.toList()); |
| 196 | + expectedIndices.addAll(org.elasticsearch.common.collect.List.of("index1", "index2", "index3")); |
| 197 | + // should resolve the data stream and each alias to _all_ their respective indices |
| 198 | + assertThat(indexNames, containsInAnyOrder(expectedIndices.toArray())); |
| 199 | + } |
| 200 | + |
| 201 | + public void testResolveIndicesWithWriteIndexOnlyAndDataStreamAndIndex() { |
| 202 | + String[] dataStreamNames = {"foo", "bar", "baz"}; |
| 203 | + List<Tuple<String, Integer>> dsMetadata = org.elasticsearch.common.collect.List.of( |
| 204 | + tuple(dataStreamNames[0], randomIntBetween(1, 3)), |
| 205 | + tuple(dataStreamNames[1], randomIntBetween(1, 3)), |
| 206 | + tuple(dataStreamNames[2], randomIntBetween(1, 3))); |
| 207 | + |
| 208 | + ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, |
| 209 | + org.elasticsearch.common.collect.List.of("index1", "index2", "index3")); |
| 210 | + cs = addAliases(cs, org.elasticsearch.common.collect.List.of( |
| 211 | + tuple("alias1", org.elasticsearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), |
| 212 | + tuple("alias2", org.elasticsearch.common.collect.List.of(tuple("index2", false), tuple("index3", true))) |
| 213 | + )); |
| 214 | + PutMappingRequest request = new PutMappingRequest().indices("foo", "index3").writeIndexOnly(true); |
| 215 | + Index[] indices = TransportPutMappingAction.resolveIndices(cs, request, new IndexNameExpressionResolver()); |
| 216 | + List<String> indexNames = Arrays.stream(indices).map(Index::getName).collect(Collectors.toList()); |
| 217 | + IndexAbstraction expectedDs = cs.metadata().getIndicesLookup().get("foo"); |
| 218 | + List<String> expectedIndices = expectedDs.getIndices().stream().map(im -> im.getIndex().getName()).collect(Collectors.toList()); |
| 219 | + expectedIndices.addAll(org.elasticsearch.common.collect.List.of("index1", "index2", "index3")); |
| 220 | + // should resolve the data stream and each alias to _all_ their respective indices |
| 221 | + assertThat(indexNames, containsInAnyOrder(expectedDs.getWriteIndex().getIndex().getName(), "index3")); |
| 222 | + } |
| 223 | + |
| 224 | + public void testResolveIndicesWithWriteIndexOnlyAndNoSingleWriteIndex() { |
| 225 | + String[] dataStreamNames = {"foo", "bar", "baz"}; |
| 226 | + List<Tuple<String, Integer>> dsMetadata = org.elasticsearch.common.collect.List.of( |
| 227 | + tuple(dataStreamNames[0], randomIntBetween(1, 3)), |
| 228 | + tuple(dataStreamNames[1], randomIntBetween(1, 3)), |
| 229 | + tuple(dataStreamNames[2], randomIntBetween(1, 3))); |
| 230 | + |
| 231 | + ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, |
| 232 | + org.elasticsearch.common.collect.List.of("index1", "index2", "index3")); |
| 233 | + final ClusterState cs2 = addAliases(cs, org.elasticsearch.common.collect.List.of( |
| 234 | + tuple("alias1", org.elasticsearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), |
| 235 | + tuple("alias2", org.elasticsearch.common.collect.List.of(tuple("index2", false), tuple("index3", true))) |
| 236 | + )); |
| 237 | + PutMappingRequest request = new PutMappingRequest().indices("*").writeIndexOnly(true); |
| 238 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 239 | + () -> TransportPutMappingAction.resolveIndices(cs2, request, new IndexNameExpressionResolver())); |
| 240 | + assertThat(e.getMessage(), containsString("The index expression [*] and options provided did not point to a single write-index")); |
| 241 | + } |
| 242 | + |
| 243 | + public void testResolveIndicesWithWriteIndexOnlyAndAliasWithoutWriteIndex() { |
| 244 | + String[] dataStreamNames = {"foo", "bar", "baz"}; |
| 245 | + List<Tuple<String, Integer>> dsMetadata = org.elasticsearch.common.collect.List.of( |
| 246 | + tuple(dataStreamNames[0], randomIntBetween(1, 3)), |
| 247 | + tuple(dataStreamNames[1], randomIntBetween(1, 3)), |
| 248 | + tuple(dataStreamNames[2], randomIntBetween(1, 3))); |
| 249 | + |
| 250 | + ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, |
| 251 | + org.elasticsearch.common.collect.List.of("index1", "index2", "index3")); |
| 252 | + final ClusterState cs2 = addAliases(cs, org.elasticsearch.common.collect.List.of( |
| 253 | + tuple("alias1", org.elasticsearch.common.collect.List.of(tuple("index1", false), tuple("index2", false))), |
| 254 | + tuple("alias2", org.elasticsearch.common.collect.List.of(tuple("index2", false), tuple("index3", false))) |
| 255 | + )); |
| 256 | + PutMappingRequest request = new PutMappingRequest().indices("alias2").writeIndexOnly(true); |
| 257 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 258 | + () -> TransportPutMappingAction.resolveIndices(cs2, request, new IndexNameExpressionResolver())); |
| 259 | + assertThat(e.getMessage(), containsString("no write index is defined for alias [alias2]")); |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Adds aliases to the supplied ClusterState instance. The aliases parameter takes of list of tuples of aliasName |
| 264 | + * to the alias's indices. The alias's indices are a tuple of index name and a flag indicating whether the alias |
| 265 | + * is a write alias for that index. See usage examples above. |
| 266 | + */ |
| 267 | + private static ClusterState addAliases(ClusterState cs, List<Tuple<String, List<Tuple<String, Boolean>>>> aliases) { |
| 268 | + Metadata.Builder builder = Metadata.builder(cs.metadata()); |
| 269 | + for (Tuple<String, List<Tuple<String, Boolean>>> alias : aliases) { |
| 270 | + for (Tuple<String, Boolean> index : alias.v2()) { |
| 271 | + IndexMetadata im = builder.get(index.v1()); |
| 272 | + AliasMetadata newAliasMd = AliasMetadata.newAliasMetadataBuilder(alias.v1()).writeIndex(index.v2()).build(); |
| 273 | + builder.put(IndexMetadata.builder(im).putAlias(newAliasMd)); |
| 274 | + } |
| 275 | + } |
| 276 | + return ClusterState.builder(cs).metadata(builder.build()).build(); |
| 277 | + } |
| 278 | + |
141 | 279 | }
|
0 commit comments