Skip to content

Commit

Permalink
[7.12] Templates match indices with date math expressions (#71563)
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann committed Apr 12, 2021
1 parent 6e53f72 commit 01dc431
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public Index concreteWriteIndex(ClusterState state, IndicesOptions options, Stri
*/
public boolean hasIndexAbstraction(String indexAbstraction, ClusterState state) {
Context context = new Context(state, IndicesOptions.lenientExpandOpen(), false, false, true, isSystemIndexAccessAllowed());
String resolvedAliasOrIndex = dateMathExpressionResolver.resolveExpression(indexAbstraction, context);
String resolvedAliasOrIndex = DateMathExpressionResolver.resolveExpression(indexAbstraction, context);
return state.metadata().getIndicesLookup().containsKey(resolvedAliasOrIndex);
}

Expand All @@ -438,7 +438,15 @@ public boolean hasIndexAbstraction(String indexAbstraction, ClusterState state)
public String resolveDateMathExpression(String dateExpression) {
// The data math expression resolver doesn't rely on cluster state or indices options, because
// it just resolves the date math to an actual date.
return dateMathExpressionResolver.resolveExpression(dateExpression, new Context(null, null, isSystemIndexAccessAllowed()));
return DateMathExpressionResolver.resolveExpression(dateExpression, new Context(null, null, isSystemIndexAccessAllowed()));
}

/**
* @param time instant to consider when parsing the expression
* @return If the specified string is data math expression then this method returns the resolved expression.
*/
public String resolveDateMathExpression(String dateExpression, long time) {
return DateMathExpressionResolver.resolveExpression(dateExpression, new Context(null, null, time, isSystemIndexAccessAllowed()));
}

/**
Expand Down Expand Up @@ -1081,7 +1089,7 @@ public List<String> resolve(final Context context, List<String> expressions) {
}

@SuppressWarnings("fallthrough")
String resolveExpression(String expression, final Context context) {
static String resolveExpression(String expression, final Context context) {
if (expression.startsWith(EXPRESSION_LEFT_BOUND) == false || expression.endsWith(EXPRESSION_RIGHT_BOUND) == false) {
return expression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,9 @@ static ClusterState innerPutTemplate(final ClusterState currentState, PutRequest
*
*/
public static List<IndexTemplateMetadata> findV1Templates(Metadata metadata, String indexName, @Nullable Boolean isHidden) {
final Predicate<String> patternMatchPredicate = pattern -> Regex.simpleMatch(pattern, indexName);
final String resolvedIndexName = IndexNameExpressionResolver.DateMathExpressionResolver
.resolveExpression(indexName, new IndexNameExpressionResolver.Context(null, null, false));
final Predicate<String> patternMatchPredicate = pattern -> Regex.simpleMatch(pattern, resolvedIndexName);
final List<IndexTemplateMetadata> matchedTemplates = new ArrayList<>();
for (ObjectCursor<IndexTemplateMetadata> cursor : metadata.templates().values()) {
final IndexTemplateMetadata template = cursor.value;
Expand All @@ -822,8 +824,9 @@ public static List<IndexTemplateMetadata> findV1Templates(Metadata metadata, Str
// this is complex but if the index is not hidden in the create request but is hidden as the result of template application,
// then we need to exclude global templates
if (isHidden == null) {
final Optional<IndexTemplateMetadata> templateWithHiddenSetting = matchedTemplates.stream()
.filter(template -> IndexMetadata.INDEX_HIDDEN_SETTING.exists(template.settings())).findFirst();
final Optional<IndexTemplateMetadata>
templateWithHiddenSetting =
matchedTemplates.stream().filter(template -> IndexMetadata.INDEX_HIDDEN_SETTING.exists(template.settings())).findFirst();
if (templateWithHiddenSetting.isPresent()) {
final boolean templatedIsHidden = IndexMetadata.INDEX_HIDDEN_SETTING.get(templateWithHiddenSetting.get().settings());
if (templatedIsHidden) {
Expand All @@ -849,7 +852,9 @@ public static List<IndexTemplateMetadata> findV1Templates(Metadata metadata, Str
*/
@Nullable
public static String findV2Template(Metadata metadata, String indexName, boolean isHidden) {
final Predicate<String> patternMatchPredicate = pattern -> Regex.simpleMatch(pattern, indexName);
final String resolvedIndexName = IndexNameExpressionResolver.DateMathExpressionResolver
.resolveExpression(indexName, new IndexNameExpressionResolver.Context(null, null, false));
final Predicate<String> patternMatchPredicate = pattern -> Regex.simpleMatch(pattern, resolvedIndexName);
final Map<ComposableIndexTemplate, String> matchedTemplates = new HashMap<>();
for (Map.Entry<String, ComposableIndexTemplate> entry : metadata.templatesV2().entrySet()) {
final String name = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ public void testFindTemplatesWithHiddenIndices() throws Exception {
.map(IndexTemplateMetadata::name).collect(Collectors.toList()), contains("sneaky-hidden"));
}

public void testFindTemplatesWithDateMathIndex() throws Exception {
client().admin().indices().prepareDeleteTemplate("*").get(); // Delete all existing templates
putTemplateDetail(new PutRequest("testFindTemplatesWithDateMathIndex", "foo-1").patterns(singletonList("test-*")).order(1));
final ClusterState state = client().admin().cluster().prepareState().get().getState();

assertThat(MetadataIndexTemplateService.findV1Templates(state.metadata(), "<test-{now/d}>", false).stream()
.map(IndexTemplateMetadata::name).collect(Collectors.toList()), containsInAnyOrder("foo-1"));
}

public void testPutGlobalTemplateWithIndexHiddenSetting() throws Exception {
List<Throwable> errors = putTemplateDetail(new PutRequest("testPutGlobalTemplateWithIndexHiddenSetting", "sneaky-hidden")
.patterns(singletonList("*")).settings(Settings.builder().put("index.hidden", true).build()));
Expand Down Expand Up @@ -720,6 +729,30 @@ public void testFindV2TemplatesForHiddenIndex() throws Exception {
assertThat(result, equalTo("my-template"));
}

public void testFindV2TemplatesForDateMathIndex() throws Exception {
String indexName = "<index-{now/d}>";
final MetadataIndexTemplateService service = getMetadataIndexTemplateService();
ClusterState state = ClusterState.EMPTY_STATE;
assertNull(MetadataIndexTemplateService.findV2Template(state.metadata(), indexName, true));

ComponentTemplate ct = ComponentTemplateTests.randomInstance();
state = service.addComponentTemplate(state, true, "ct", ct);
ComposableIndexTemplate it = new ComposableIndexTemplate(
org.elasticsearch.common.collect.List.of("index-*"), null,
org.elasticsearch.common.collect.List.of("ct"), 0L, 1L, null, null, null
);
state = service.addIndexTemplateV2(state, true, "my-template", it);
ComposableIndexTemplate it2 = new ComposableIndexTemplate(
org.elasticsearch.common.collect.List.of("*"), null,
org.elasticsearch.common.collect.List.of("ct"), 10L, 2L, null, null, null
);
state = service.addIndexTemplateV2(state, true, "my-template2", it2);

String result = MetadataIndexTemplateService.findV2Template(state.metadata(), indexName, true);

assertThat(result, equalTo("my-template"));
}

public void testFindV2InvalidGlobalTemplate() {
Template templateWithHiddenSetting = new Template(builder().put(IndexMetadata.SETTING_INDEX_HIDDEN, true).build(), null, null);
try {
Expand Down

0 comments on commit 01dc431

Please sign in to comment.