|
22 | 22 | import org.elasticsearch.common.settings.IndexScopedSettings; |
23 | 23 | import org.elasticsearch.common.settings.Settings; |
24 | 24 | import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 25 | +import org.elasticsearch.core.Strings; |
25 | 26 | import org.elasticsearch.core.TimeValue; |
26 | 27 | import org.elasticsearch.env.Environment; |
27 | 28 | import org.elasticsearch.health.node.selection.HealthNodeTaskExecutor; |
@@ -1718,6 +1719,76 @@ public void testInvalidNonDataStreamTemplateWithDataStreamOptions() throws Excep |
1718 | 1719 | ); |
1719 | 1720 | } |
1720 | 1721 |
|
| 1722 | + public void testSystemDataStreamsIgnoredByValidateIndexTemplateV2() throws Exception { |
| 1723 | + /* |
| 1724 | + * This test makes sure that system data streams (which do not have named templates) do not appear in the list of data streams |
| 1725 | + * without named templates when validateIndexTemplateV2 fails due to another non-system data stream not having a named template. |
| 1726 | + */ |
| 1727 | + MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService(); |
| 1728 | + final String dataStreamTemplateName = "data_stream_template"; |
| 1729 | + final String indexTemplateName = "index_template"; |
| 1730 | + final String systemDataStreamName = "system_ds"; |
| 1731 | + final String ordinaryDataStreamName = "my_ds"; |
| 1732 | + final String ordinaryDataStreamIndexPattern = "my_ds*"; |
| 1733 | + ComposableIndexTemplate highPriorityDataStreamTemplate = ComposableIndexTemplate.builder() |
| 1734 | + .indexPatterns(List.of(ordinaryDataStreamIndexPattern)) |
| 1735 | + .priority(275L) |
| 1736 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(randomBoolean(), randomBoolean())) |
| 1737 | + .build(); |
| 1738 | + ComposableIndexTemplate highPriorityIndexTemplate = ComposableIndexTemplate.builder() |
| 1739 | + .indexPatterns(List.of(ordinaryDataStreamIndexPattern)) |
| 1740 | + .priority(200L) |
| 1741 | + .build(); |
| 1742 | + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) |
| 1743 | + .dataStreams( |
| 1744 | + Map.of( |
| 1745 | + systemDataStreamName, |
| 1746 | + DataStreamTestHelper.randomInstance(systemDataStreamName, System::currentTimeMillis, randomBoolean(), true), |
| 1747 | + ordinaryDataStreamName, |
| 1748 | + DataStreamTestHelper.randomInstance(ordinaryDataStreamName, System::currentTimeMillis, randomBoolean(), false) |
| 1749 | + ), |
| 1750 | + Map.of() |
| 1751 | + ) |
| 1752 | + .indexTemplates(Map.of(dataStreamTemplateName, highPriorityDataStreamTemplate, indexTemplateName, highPriorityIndexTemplate)) |
| 1753 | + .build(); |
| 1754 | + ComposableIndexTemplate lowPriorityDataStreamTemplate = ComposableIndexTemplate.builder() |
| 1755 | + .indexPatterns(List.of(ordinaryDataStreamIndexPattern)) |
| 1756 | + .priority(1L) |
| 1757 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(randomBoolean(), randomBoolean())) |
| 1758 | + .build(); |
| 1759 | + /* |
| 1760 | + * 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 |
| 1761 | + * the data stream matches the index (non data-stream) template instead. We expect an error, but that the error only mentions the |
| 1762 | + * non-system data stream. |
| 1763 | + */ |
| 1764 | + Exception exception = expectThrows( |
| 1765 | + Exception.class, |
| 1766 | + () -> metadataIndexTemplateService.validateIndexTemplateV2(project, dataStreamTemplateName, lowPriorityDataStreamTemplate) |
| 1767 | + ); |
| 1768 | + assertThat( |
| 1769 | + exception.getMessage(), |
| 1770 | + containsString( |
| 1771 | + Strings.format( |
| 1772 | + "composable template [%s] with index patterns [%s], priority [1] would cause data streams [%s] to no longer " |
| 1773 | + + "match a data stream template", |
| 1774 | + dataStreamTemplateName, |
| 1775 | + ordinaryDataStreamIndexPattern, |
| 1776 | + ordinaryDataStreamName |
| 1777 | + ) |
| 1778 | + ) |
| 1779 | + ); |
| 1780 | + ComposableIndexTemplate mediumPriorityDataStreamTemplate = ComposableIndexTemplate.builder() |
| 1781 | + .indexPatterns(List.of(ordinaryDataStreamIndexPattern)) |
| 1782 | + .priority(201L) |
| 1783 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(randomBoolean(), randomBoolean())) |
| 1784 | + .build(); |
| 1785 | + /* |
| 1786 | + * We have now corrected the problem -- the priority of the new template is lower than the old data stream template but still higher |
| 1787 | + * than the non-data-stream index template. So we expect no validation errors. |
| 1788 | + */ |
| 1789 | + metadataIndexTemplateService.validateIndexTemplateV2(project, dataStreamTemplateName, mediumPriorityDataStreamTemplate); |
| 1790 | + } |
| 1791 | + |
1721 | 1792 | private ProjectMetadata addComponentTemplate( |
1722 | 1793 | MetadataIndexTemplateService service, |
1723 | 1794 | ProjectMetadata project, |
|
0 commit comments