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