|
14 | 14 | import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
|
15 | 15 | import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
|
16 | 16 | import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
|
| 17 | +import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; |
| 18 | +import org.elasticsearch.action.admin.indices.mapping.put.TransportPutMappingAction; |
17 | 19 | import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
|
18 | 20 | import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
|
19 | 21 | import org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest;
|
|
24 | 26 | import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
25 | 27 | import org.elasticsearch.action.bulk.BulkResponse;
|
26 | 28 | import org.elasticsearch.action.bulk.IndexDocFailureStoreStatus;
|
| 29 | +import org.elasticsearch.action.datastreams.CreateDataStreamAction; |
27 | 30 | import org.elasticsearch.action.get.GetRequest;
|
28 | 31 | import org.elasticsearch.action.index.IndexRequest;
|
29 | 32 | import org.elasticsearch.action.search.SearchRequest;
|
|
34 | 37 | import org.elasticsearch.cluster.metadata.Template;
|
35 | 38 | import org.elasticsearch.common.Strings;
|
36 | 39 | import org.elasticsearch.common.compress.CompressedXContent;
|
| 40 | +import org.elasticsearch.common.settings.Setting; |
37 | 41 | import org.elasticsearch.common.settings.Settings;
|
38 | 42 | import org.elasticsearch.common.time.DateFormatter;
|
39 | 43 | import org.elasticsearch.common.time.FormatNames;
|
|
58 | 62 | import java.time.temporal.ChronoUnit;
|
59 | 63 | import java.util.Collection;
|
60 | 64 | import java.util.List;
|
| 65 | +import java.util.Map; |
61 | 66 | import java.util.concurrent.CountDownLatch;
|
62 | 67 |
|
63 | 68 | import static org.elasticsearch.test.MapMatcher.assertMap;
|
64 | 69 | import static org.elasticsearch.test.MapMatcher.matchesMap;
|
65 | 70 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
66 | 71 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
|
| 72 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
67 | 73 | import static org.hamcrest.Matchers.containsString;
|
68 | 74 | import static org.hamcrest.Matchers.equalTo;
|
69 | 75 | import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
@@ -320,16 +326,12 @@ public void testTsdbTemplatesNoKeywordFieldType() throws Exception {
|
320 | 326 | ComposableIndexTemplate.builder()
|
321 | 327 | .indexPatterns(List.of("k8s*"))
|
322 | 328 | .template(
|
323 |
| - new Template( |
324 |
| - Settings.builder().put("index.mode", "time_series").put("index.routing_path", "metricset").build(), |
325 |
| - new CompressedXContent(mappingTemplate), |
326 |
| - null |
327 |
| - ) |
| 329 | + new Template(Settings.builder().put("index.mode", "time_series").build(), new CompressedXContent(mappingTemplate), null) |
328 | 330 | )
|
329 | 331 | .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false))
|
330 | 332 | .build()
|
331 | 333 | );
|
332 |
| - client().execute(TransportPutComposableIndexTemplateAction.TYPE, request).actionGet(); |
| 334 | + assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, request)); |
333 | 335 | }
|
334 | 336 |
|
335 | 337 | public void testInvalidTsdbTemplatesMissingSettings() throws Exception {
|
@@ -621,6 +623,191 @@ public void testReindexing() throws Exception {
|
621 | 623 | );
|
622 | 624 | }
|
623 | 625 |
|
| 626 | + public void testAddDimensionToMapping() throws Exception { |
| 627 | + String dataStreamName = "my-ds"; |
| 628 | + var putTemplateRequest = new TransportPutComposableIndexTemplateAction.Request("id"); |
| 629 | + putTemplateRequest.indexTemplate( |
| 630 | + ComposableIndexTemplate.builder() |
| 631 | + .indexPatterns(List.of(dataStreamName)) |
| 632 | + .template( |
| 633 | + new Template( |
| 634 | + Settings.builder().put("index.mode", "time_series").build(), |
| 635 | + new CompressedXContent(MAPPING_TEMPLATE), |
| 636 | + null |
| 637 | + ) |
| 638 | + ) |
| 639 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) |
| 640 | + .build() |
| 641 | + ); |
| 642 | + assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, putTemplateRequest)); |
| 643 | + |
| 644 | + // create data stream |
| 645 | + CreateDataStreamAction.Request createDsRequest = new CreateDataStreamAction.Request( |
| 646 | + TEST_REQUEST_TIMEOUT, |
| 647 | + TEST_REQUEST_TIMEOUT, |
| 648 | + "my-ds" |
| 649 | + ); |
| 650 | + assertAcked(client().execute(CreateDataStreamAction.INSTANCE, createDsRequest)); |
| 651 | + |
| 652 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), equalTo(List.of("metricset"))); |
| 653 | + |
| 654 | + // put mapping with k8s.pod.uid as another time series dimension |
| 655 | + var putMappingRequest = new PutMappingRequest(dataStreamName).source(""" |
| 656 | + { |
| 657 | + "properties": { |
| 658 | + "k8s.pod.name": { |
| 659 | + "type": "keyword", |
| 660 | + "time_series_dimension": true |
| 661 | + } |
| 662 | + } |
| 663 | + } |
| 664 | + """, XContentType.JSON); |
| 665 | + assertAcked(client().execute(TransportPutMappingAction.TYPE, putMappingRequest).actionGet()); |
| 666 | + |
| 667 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), containsInAnyOrder("metricset", "k8s.pod.name")); |
| 668 | + |
| 669 | + indexWithPodNames(dataStreamName, Instant.now(), Map.of(), "dog", "cat"); |
| 670 | + } |
| 671 | + |
| 672 | + public void testDynamicStringDimensions() throws Exception { |
| 673 | + String dataStreamName = "my-ds"; |
| 674 | + var putTemplateRequest = new TransportPutComposableIndexTemplateAction.Request("id"); |
| 675 | + putTemplateRequest.indexTemplate( |
| 676 | + ComposableIndexTemplate.builder() |
| 677 | + .indexPatterns(List.of(dataStreamName)) |
| 678 | + .template(new Template(Settings.builder().put("index.mode", "time_series").build(), new CompressedXContent(""" |
| 679 | + { |
| 680 | + "_doc": { |
| 681 | + "dynamic_templates": [ |
| 682 | + { |
| 683 | + "labels": { |
| 684 | + "match_mapping_type": "string", |
| 685 | + "mapping": { |
| 686 | + "type": "keyword", |
| 687 | + "time_series_dimension": true |
| 688 | + } |
| 689 | + } |
| 690 | + } |
| 691 | + ], |
| 692 | + "properties": { |
| 693 | + "@timestamp": { |
| 694 | + "type": "date" |
| 695 | + }, |
| 696 | + "metricset": { |
| 697 | + "type": "keyword", |
| 698 | + "time_series_dimension": true |
| 699 | + } |
| 700 | + } |
| 701 | + } |
| 702 | + }"""), null)) |
| 703 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) |
| 704 | + .build() |
| 705 | + ); |
| 706 | + assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, putTemplateRequest)); |
| 707 | + |
| 708 | + CreateDataStreamAction.Request createDsRequest = new CreateDataStreamAction.Request( |
| 709 | + TEST_REQUEST_TIMEOUT, |
| 710 | + TEST_REQUEST_TIMEOUT, |
| 711 | + "my-ds" |
| 712 | + ); |
| 713 | + assertAcked(client().execute(CreateDataStreamAction.INSTANCE, createDsRequest)); |
| 714 | + |
| 715 | + // doesn't populate index.dimensions because the "labels" dynamic template doesn't have a path_math |
| 716 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 717 | + |
| 718 | + // index doc |
| 719 | + BulkResponse bulkResponse = client().prepareBulk() |
| 720 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 721 | + .add( |
| 722 | + client().prepareIndex(dataStreamName) |
| 723 | + .setOpType(DocWriteRequest.OpType.CREATE) |
| 724 | + .setSource(DOC.replace("$time", formatInstant(Instant.now())), XContentType.JSON) |
| 725 | + ) |
| 726 | + .get(); |
| 727 | + assertThat(bulkResponse.hasFailures(), is(false)); |
| 728 | + |
| 729 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 730 | + } |
| 731 | + |
| 732 | + public void testDynamicDimensions() throws Exception { |
| 733 | + String dataStreamName = "my-ds"; |
| 734 | + var putTemplateRequest = new TransportPutComposableIndexTemplateAction.Request("id"); |
| 735 | + putTemplateRequest.indexTemplate( |
| 736 | + ComposableIndexTemplate.builder() |
| 737 | + .indexPatterns(List.of(dataStreamName)) |
| 738 | + .template(new Template(Settings.builder().put("index.mode", "time_series").build(), new CompressedXContent(""" |
| 739 | +
|
| 740 | + { |
| 741 | + "_doc": { |
| 742 | + "dynamic_templates": [ |
| 743 | + { |
| 744 | + "label": { |
| 745 | + "mapping": { |
| 746 | + "type": "keyword", |
| 747 | + "time_series_dimension": true |
| 748 | + } |
| 749 | + } |
| 750 | + } |
| 751 | + ], |
| 752 | + "properties": { |
| 753 | + "@timestamp": { |
| 754 | + "type": "date" |
| 755 | + }, |
| 756 | + "metricset": { |
| 757 | + "type": "keyword", |
| 758 | + "time_series_dimension": true |
| 759 | + } |
| 760 | + } |
| 761 | + } |
| 762 | + }"""), null)) |
| 763 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) |
| 764 | + .build() |
| 765 | + ); |
| 766 | + assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, putTemplateRequest)); |
| 767 | + |
| 768 | + CreateDataStreamAction.Request createDsRequest = new CreateDataStreamAction.Request( |
| 769 | + TEST_REQUEST_TIMEOUT, |
| 770 | + TEST_REQUEST_TIMEOUT, |
| 771 | + "my-ds" |
| 772 | + ); |
| 773 | + assertAcked(client().execute(CreateDataStreamAction.INSTANCE, createDsRequest)); |
| 774 | + |
| 775 | + // doesn't populate index.dimensions because the "label" dynamic template doesn't have a path_math |
| 776 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 777 | + |
| 778 | + // index doc |
| 779 | + indexWithPodNames(dataStreamName, Instant.now(), Map.of("k8s.pod.name", "label"), "dog", "cat"); |
| 780 | + |
| 781 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 782 | + } |
| 783 | + |
| 784 | + private void indexWithPodNames(String dataStreamName, Instant timestamp, Map<String, String> dynamicTemplates, String... podNames) { |
| 785 | + // index doc |
| 786 | + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); |
| 787 | + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 788 | + for (String podName : podNames) { |
| 789 | + bulkRequestBuilder.add( |
| 790 | + client().prepareIndex(dataStreamName) |
| 791 | + .setOpType(DocWriteRequest.OpType.CREATE) |
| 792 | + .setSource(DOC.replace("$time", formatInstant(timestamp)).replace("dog", podName), XContentType.JSON) |
| 793 | + .request() |
| 794 | + .setDynamicTemplates(dynamicTemplates) |
| 795 | + ); |
| 796 | + } |
| 797 | + |
| 798 | + BulkResponse bulkResponse = bulkRequestBuilder.get(); |
| 799 | + assertThat(bulkResponse.hasFailures(), is(false)); |
| 800 | + } |
| 801 | + |
| 802 | + private <T> T getSetting(String dataStreamName, Setting<T> setting) { |
| 803 | + GetIndexResponse getIndexResponse = safeGet( |
| 804 | + indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(dataStreamName)) |
| 805 | + ); |
| 806 | + assertThat(getIndexResponse.getIndices().length, equalTo(1)); |
| 807 | + Settings settings = getIndexResponse.getSettings().get(getIndexResponse.getIndices()[0]); |
| 808 | + return setting.get(settings); |
| 809 | + } |
| 810 | + |
624 | 811 | static String formatInstant(Instant instant) {
|
625 | 812 | return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(instant);
|
626 | 813 | }
|
|
0 commit comments