|
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 |
|
| 68 | +import static org.elasticsearch.datastreams.DataStreamIndexSettingsProvider.INDEX_DIMENSIONS_TSID_OPTIMIZATION_FEATURE_FLAG; |
63 | 69 | import static org.elasticsearch.test.MapMatcher.assertMap;
|
64 | 70 | import static org.elasticsearch.test.MapMatcher.matchesMap;
|
65 | 71 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
66 | 72 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
|
| 73 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
67 | 74 | import static org.hamcrest.Matchers.containsString;
|
| 75 | +import static org.hamcrest.Matchers.empty; |
68 | 76 | import static org.hamcrest.Matchers.equalTo;
|
69 | 77 | import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
70 | 78 | import static org.hamcrest.Matchers.hasSize;
|
@@ -321,15 +329,18 @@ public void testTsdbTemplatesNoKeywordFieldType() throws Exception {
|
321 | 329 | .indexPatterns(List.of("k8s*"))
|
322 | 330 | .template(
|
323 | 331 | new Template(
|
324 |
| - Settings.builder().put("index.mode", "time_series").put("index.routing_path", "metricset").build(), |
| 332 | + Settings.builder() |
| 333 | + .put("index.mode", "time_series") |
| 334 | + .put("index.routing_path", randomBoolean() ? "metricset" : null) |
| 335 | + .build(), |
325 | 336 | new CompressedXContent(mappingTemplate),
|
326 | 337 | null
|
327 | 338 | )
|
328 | 339 | )
|
329 | 340 | .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false))
|
330 | 341 | .build()
|
331 | 342 | );
|
332 |
| - client().execute(TransportPutComposableIndexTemplateAction.TYPE, request).actionGet(); |
| 343 | + assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, request)); |
333 | 344 | }
|
334 | 345 |
|
335 | 346 | public void testInvalidTsdbTemplatesMissingSettings() throws Exception {
|
@@ -619,6 +630,207 @@ public void testReindexing() throws Exception {
|
619 | 630 | getIndexResponse.getSetting(index2, IndexMetadata.INDEX_ROUTING_PATH.getKey()),
|
620 | 631 | equalTo(getIndexResponse.getSetting(index1, IndexMetadata.INDEX_ROUTING_PATH.getKey()))
|
621 | 632 | );
|
| 633 | + assertThat( |
| 634 | + getIndexResponse.getSetting(index2, IndexMetadata.INDEX_DIMENSIONS.getKey()), |
| 635 | + equalTo(getIndexResponse.getSetting(index1, IndexMetadata.INDEX_DIMENSIONS.getKey())) |
| 636 | + ); |
| 637 | + } |
| 638 | + |
| 639 | + public void testAddDimensionToMapping() throws Exception { |
| 640 | + String dataStreamName = "my-ds"; |
| 641 | + var putTemplateRequest = new TransportPutComposableIndexTemplateAction.Request("id"); |
| 642 | + putTemplateRequest.indexTemplate( |
| 643 | + ComposableIndexTemplate.builder() |
| 644 | + .indexPatterns(List.of(dataStreamName)) |
| 645 | + .template( |
| 646 | + new Template( |
| 647 | + Settings.builder().put("index.mode", "time_series").build(), |
| 648 | + new CompressedXContent(MAPPING_TEMPLATE), |
| 649 | + null |
| 650 | + ) |
| 651 | + ) |
| 652 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) |
| 653 | + .build() |
| 654 | + ); |
| 655 | + assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, putTemplateRequest)); |
| 656 | + |
| 657 | + // create data stream |
| 658 | + CreateDataStreamAction.Request createDsRequest = new CreateDataStreamAction.Request( |
| 659 | + TEST_REQUEST_TIMEOUT, |
| 660 | + TEST_REQUEST_TIMEOUT, |
| 661 | + "my-ds" |
| 662 | + ); |
| 663 | + assertAcked(client().execute(CreateDataStreamAction.INSTANCE, createDsRequest)); |
| 664 | + if (INDEX_DIMENSIONS_TSID_OPTIMIZATION_FEATURE_FLAG) { |
| 665 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), equalTo(List.of("metricset"))); |
| 666 | + } else { |
| 667 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), empty()); |
| 668 | + } |
| 669 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 670 | + |
| 671 | + // put mapping with k8s.pod.uid as another time series dimension |
| 672 | + var putMappingRequest = new PutMappingRequest(dataStreamName).source(""" |
| 673 | + { |
| 674 | + "properties": { |
| 675 | + "k8s.pod.name": { |
| 676 | + "type": "keyword", |
| 677 | + "time_series_dimension": true |
| 678 | + } |
| 679 | + } |
| 680 | + } |
| 681 | + """, XContentType.JSON); |
| 682 | + assertAcked(client().execute(TransportPutMappingAction.TYPE, putMappingRequest).actionGet()); |
| 683 | + if (INDEX_DIMENSIONS_TSID_OPTIMIZATION_FEATURE_FLAG) { |
| 684 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), containsInAnyOrder("metricset", "k8s.pod.name")); |
| 685 | + } else { |
| 686 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), empty()); |
| 687 | + } |
| 688 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 689 | + |
| 690 | + indexWithPodNames(dataStreamName, Instant.now(), Map.of(), "dog", "cat"); |
| 691 | + } |
| 692 | + |
| 693 | + public void testDynamicStringDimensions() throws Exception { |
| 694 | + String dataStreamName = "my-ds"; |
| 695 | + var putTemplateRequest = new TransportPutComposableIndexTemplateAction.Request("id"); |
| 696 | + putTemplateRequest.indexTemplate( |
| 697 | + ComposableIndexTemplate.builder() |
| 698 | + .indexPatterns(List.of(dataStreamName)) |
| 699 | + .template(new Template(Settings.builder().put("index.mode", "time_series").build(), new CompressedXContent(""" |
| 700 | + { |
| 701 | + "_doc": { |
| 702 | + "dynamic_templates": [ |
| 703 | + { |
| 704 | + "labels": { |
| 705 | + "match_mapping_type": "string", |
| 706 | + "mapping": { |
| 707 | + "type": "keyword", |
| 708 | + "time_series_dimension": true |
| 709 | + } |
| 710 | + } |
| 711 | + } |
| 712 | + ], |
| 713 | + "properties": { |
| 714 | + "@timestamp": { |
| 715 | + "type": "date" |
| 716 | + }, |
| 717 | + "metricset": { |
| 718 | + "type": "keyword", |
| 719 | + "time_series_dimension": true |
| 720 | + } |
| 721 | + } |
| 722 | + } |
| 723 | + }"""), null)) |
| 724 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) |
| 725 | + .build() |
| 726 | + ); |
| 727 | + assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, putTemplateRequest)); |
| 728 | + |
| 729 | + CreateDataStreamAction.Request createDsRequest = new CreateDataStreamAction.Request( |
| 730 | + TEST_REQUEST_TIMEOUT, |
| 731 | + TEST_REQUEST_TIMEOUT, |
| 732 | + "my-ds" |
| 733 | + ); |
| 734 | + assertAcked(client().execute(CreateDataStreamAction.INSTANCE, createDsRequest)); |
| 735 | + |
| 736 | + // doesn't populate index.dimensions custom metadata because the "labels" dynamic template doesn't have a path_math |
| 737 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), empty()); |
| 738 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 739 | + |
| 740 | + // index doc |
| 741 | + BulkResponse bulkResponse = client().prepareBulk() |
| 742 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 743 | + .add( |
| 744 | + client().prepareIndex(dataStreamName) |
| 745 | + .setOpType(DocWriteRequest.OpType.CREATE) |
| 746 | + .setSource(DOC.replace("$time", formatInstant(Instant.now())), XContentType.JSON) |
| 747 | + ) |
| 748 | + .get(); |
| 749 | + assertThat(bulkResponse.hasFailures(), is(false)); |
| 750 | + |
| 751 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), empty()); |
| 752 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 753 | + } |
| 754 | + |
| 755 | + public void testDynamicDimensions() throws Exception { |
| 756 | + String dataStreamName = "my-ds"; |
| 757 | + var putTemplateRequest = new TransportPutComposableIndexTemplateAction.Request("id"); |
| 758 | + putTemplateRequest.indexTemplate( |
| 759 | + ComposableIndexTemplate.builder() |
| 760 | + .indexPatterns(List.of(dataStreamName)) |
| 761 | + .template(new Template(Settings.builder().put("index.mode", "time_series").build(), new CompressedXContent(""" |
| 762 | +
|
| 763 | + { |
| 764 | + "_doc": { |
| 765 | + "dynamic_templates": [ |
| 766 | + { |
| 767 | + "label": { |
| 768 | + "mapping": { |
| 769 | + "type": "keyword", |
| 770 | + "time_series_dimension": true |
| 771 | + } |
| 772 | + } |
| 773 | + } |
| 774 | + ], |
| 775 | + "properties": { |
| 776 | + "@timestamp": { |
| 777 | + "type": "date" |
| 778 | + }, |
| 779 | + "metricset": { |
| 780 | + "type": "keyword", |
| 781 | + "time_series_dimension": true |
| 782 | + } |
| 783 | + } |
| 784 | + } |
| 785 | + }"""), null)) |
| 786 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) |
| 787 | + .build() |
| 788 | + ); |
| 789 | + assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, putTemplateRequest)); |
| 790 | + |
| 791 | + CreateDataStreamAction.Request createDsRequest = new CreateDataStreamAction.Request( |
| 792 | + TEST_REQUEST_TIMEOUT, |
| 793 | + TEST_REQUEST_TIMEOUT, |
| 794 | + "my-ds" |
| 795 | + ); |
| 796 | + assertAcked(client().execute(CreateDataStreamAction.INSTANCE, createDsRequest)); |
| 797 | + |
| 798 | + // doesn't populate index.dimensions because the "label" dynamic template doesn't have a path_math |
| 799 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), empty()); |
| 800 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 801 | + |
| 802 | + // index doc |
| 803 | + indexWithPodNames(dataStreamName, Instant.now(), Map.of("k8s.pod.name", "label"), "dog", "cat"); |
| 804 | + |
| 805 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_DIMENSIONS), empty()); |
| 806 | + assertThat(getSetting(dataStreamName, IndexMetadata.INDEX_ROUTING_PATH), equalTo(List.of("metricset"))); |
| 807 | + } |
| 808 | + |
| 809 | + private void indexWithPodNames(String dataStreamName, Instant timestamp, Map<String, String> dynamicTemplates, String... podNames) { |
| 810 | + // index doc |
| 811 | + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); |
| 812 | + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 813 | + for (String podName : podNames) { |
| 814 | + bulkRequestBuilder.add( |
| 815 | + client().prepareIndex(dataStreamName) |
| 816 | + .setOpType(DocWriteRequest.OpType.CREATE) |
| 817 | + .setSource(DOC.replace("$time", formatInstant(timestamp)).replace("dog", podName), XContentType.JSON) |
| 818 | + .request() |
| 819 | + .setDynamicTemplates(dynamicTemplates) |
| 820 | + ); |
| 821 | + } |
| 822 | + |
| 823 | + BulkResponse bulkResponse = bulkRequestBuilder.get(); |
| 824 | + assertThat(bulkResponse.hasFailures(), is(false)); |
| 825 | + } |
| 826 | + |
| 827 | + private <T> T getSetting(String dataStreamName, Setting<T> setting) { |
| 828 | + GetIndexResponse getIndexResponse = safeGet( |
| 829 | + indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(dataStreamName)) |
| 830 | + ); |
| 831 | + assertThat(getIndexResponse.getIndices().length, equalTo(1)); |
| 832 | + Settings settings = getIndexResponse.getSettings().get(getIndexResponse.getIndices()[0]); |
| 833 | + return setting.get(settings); |
622 | 834 | }
|
623 | 835 |
|
624 | 836 | static String formatInstant(Instant instant) {
|
|
0 commit comments