|  | 
| 23 | 23 | import org.elasticsearch.common.settings.IndexScopedSettings; | 
| 24 | 24 | import org.elasticsearch.common.settings.Settings; | 
| 25 | 25 | import org.elasticsearch.common.util.concurrent.ThreadContext; | 
|  | 26 | +import org.elasticsearch.core.Strings; | 
| 26 | 27 | import org.elasticsearch.core.TimeValue; | 
| 27 | 28 | import org.elasticsearch.env.Environment; | 
| 28 | 29 | import org.elasticsearch.health.node.selection.HealthNodeTaskExecutor; | 
| @@ -1693,6 +1694,81 @@ public void testInvalidNonDataStreamTemplateWithDataStreamOptions() throws Excep | 
| 1693 | 1694 |         ); | 
| 1694 | 1695 |     } | 
| 1695 | 1696 | 
 | 
|  | 1697 | +    public void testSystemDataStreamsIgnoredByValidateIndexTemplateV2() throws Exception { | 
|  | 1698 | +        /* | 
|  | 1699 | +         * This test makes sure that system data streams (which do not have named templates) do not appear in the list of data streams | 
|  | 1700 | +         * without named templates when validateIndexTemplateV2 fails due to another non-system data stream not having a named template. | 
|  | 1701 | +         */ | 
|  | 1702 | +        MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService(); | 
|  | 1703 | +        final String dataStreamTemplateName = "data_stream_template"; | 
|  | 1704 | +        final String indexTemplateName = "index_template"; | 
|  | 1705 | +        final String systemDataStreamName = "system_ds"; | 
|  | 1706 | +        final String ordinaryDataStreamName = "my_ds"; | 
|  | 1707 | +        final String ordinaryDataStreamIndexPattern = "my_ds*"; | 
|  | 1708 | +        ComposableIndexTemplate highPriorityDataStreamTemplate = ComposableIndexTemplate.builder() | 
|  | 1709 | +            .indexPatterns(List.of(ordinaryDataStreamIndexPattern)) | 
|  | 1710 | +            .priority(275L) | 
|  | 1711 | +            .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(randomBoolean(), randomBoolean())) | 
|  | 1712 | +            .build(); | 
|  | 1713 | +        ComposableIndexTemplate highPriorityIndexTemplate = ComposableIndexTemplate.builder() | 
|  | 1714 | +            .indexPatterns(List.of(ordinaryDataStreamIndexPattern)) | 
|  | 1715 | +            .priority(200L) | 
|  | 1716 | +            .build(); | 
|  | 1717 | +        ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) | 
|  | 1718 | +            .metadata( | 
|  | 1719 | +                Metadata.builder() | 
|  | 1720 | +                    .dataStreams( | 
|  | 1721 | +                        Map.of( | 
|  | 1722 | +                            systemDataStreamName, | 
|  | 1723 | +                            DataStreamTestHelper.randomInstance(systemDataStreamName, System::currentTimeMillis, randomBoolean(), true), | 
|  | 1724 | +                            ordinaryDataStreamName, | 
|  | 1725 | +                            DataStreamTestHelper.randomInstance(ordinaryDataStreamName, System::currentTimeMillis, randomBoolean(), false) | 
|  | 1726 | +                        ), | 
|  | 1727 | +                        Map.of() | 
|  | 1728 | +                    ) | 
|  | 1729 | +                    .indexTemplates( | 
|  | 1730 | +                        Map.of(dataStreamTemplateName, highPriorityDataStreamTemplate, indexTemplateName, highPriorityIndexTemplate) | 
|  | 1731 | +                    ) | 
|  | 1732 | +            ) | 
|  | 1733 | +            .build(); | 
|  | 1734 | +        ComposableIndexTemplate lowPriorityDataStreamTemplate = ComposableIndexTemplate.builder() | 
|  | 1735 | +            .indexPatterns(List.of(ordinaryDataStreamIndexPattern)) | 
|  | 1736 | +            .priority(1L) | 
|  | 1737 | +            .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(randomBoolean(), randomBoolean())) | 
|  | 1738 | +            .build(); | 
|  | 1739 | +        /* | 
|  | 1740 | +         * Here we attempt to change the priority of a template that matches an existing non-system data stream so that it is so low that | 
|  | 1741 | +         * the data stream matches the index (non data-stream) template instead. We expect an error, but that the error only mentions the | 
|  | 1742 | +         * non-system data stream. | 
|  | 1743 | +         */ | 
|  | 1744 | +        Exception exception = expectThrows( | 
|  | 1745 | +            Exception.class, | 
|  | 1746 | +            () -> metadataIndexTemplateService.validateIndexTemplateV2(dataStreamTemplateName, lowPriorityDataStreamTemplate, clusterState) | 
|  | 1747 | +        ); | 
|  | 1748 | +        assertThat( | 
|  | 1749 | +            exception.getMessage(), | 
|  | 1750 | +            containsString( | 
|  | 1751 | +                Strings.format( | 
|  | 1752 | +                    "composable template [%s] with index patterns [%s], priority [1] would cause data streams [%s] to no longer " | 
|  | 1753 | +                        + "match a data stream template", | 
|  | 1754 | +                    dataStreamTemplateName, | 
|  | 1755 | +                    ordinaryDataStreamIndexPattern, | 
|  | 1756 | +                    ordinaryDataStreamName | 
|  | 1757 | +                ) | 
|  | 1758 | +            ) | 
|  | 1759 | +        ); | 
|  | 1760 | +        ComposableIndexTemplate mediumPriorityDataStreamTemplate = ComposableIndexTemplate.builder() | 
|  | 1761 | +            .indexPatterns(List.of(ordinaryDataStreamIndexPattern)) | 
|  | 1762 | +            .priority(201L) | 
|  | 1763 | +            .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(randomBoolean(), randomBoolean())) | 
|  | 1764 | +            .build(); | 
|  | 1765 | +        /* | 
|  | 1766 | +         * We have now corrected the problem -- the priority of the new template is lower than the old data stream template but still higher | 
|  | 1767 | +         * than the non-data-stream index template. So we expect no validation errors. | 
|  | 1768 | +         */ | 
|  | 1769 | +        metadataIndexTemplateService.validateIndexTemplateV2(dataStreamTemplateName, mediumPriorityDataStreamTemplate, clusterState); | 
|  | 1770 | +    } | 
|  | 1771 | + | 
| 1696 | 1772 |     private ClusterState addComponentTemplate( | 
| 1697 | 1773 |         MetadataIndexTemplateService service, | 
| 1698 | 1774 |         ClusterState state, | 
|  | 
0 commit comments