Skip to content

Commit

Permalink
Optimize IngestService#resolvePipelinesFromIndexTemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed Oct 22, 2024
1 parent 0782efa commit d69d5a7
Showing 1 changed file with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,42 @@ static ClusterState innerPutTemplate(
return ClusterState.builder(currentState).metadata(builder).build();
}

/**
* A private, local alternative to elements.stream().anyMatch(predicate) for micro-optimization reasons.
*/
private static <T> boolean anyMatch(final List<T> elements, final Predicate<T> predicate) {
for (T e : elements) {
if (predicate.test(e)) {
return true;
}
}
return false;
}

/**
* A private, local alternative to elements.stream().noneMatch(predicate) for micro-optimization reasons.
*/
private static <T> boolean noneMatch(final List<T> elements, final Predicate<T> predicate) {
for (T e : elements) {
if (predicate.test(e)) {
return false;
}
}
return true;
}

/**
* A private, local alternative to elements.stream().filter(predicate).findFirst() for micro-optimization reasons.
*/
private static <T> Optional<T> findFirst(final List<T> elements, final Predicate<T> predicate) {
for (T e : elements) {
if (predicate.test(e)) {
return Optional.of(e);
}
}
return Optional.empty();
}

/**
* Finds index templates whose index pattern matched with the given index name. In the case of
* hidden indices, a template with a match all pattern or global template will not be returned.
Expand All @@ -1219,15 +1255,14 @@ public static List<IndexTemplateMetadata> findV1Templates(Metadata metadata, Str
final List<IndexTemplateMetadata> matchedTemplates = new ArrayList<>();
for (IndexTemplateMetadata template : metadata.templates().values()) {
if (isHidden == null || isHidden == Boolean.FALSE) {
final boolean matched = template.patterns().stream().anyMatch(patternMatchPredicate);
if (matched) {
if (anyMatch(template.patterns(), patternMatchPredicate)) {
matchedTemplates.add(template);
}
} else {
assert isHidden == Boolean.TRUE;
final boolean isNotMatchAllTemplate = template.patterns().stream().noneMatch(Regex::isMatchAllPattern);
final boolean isNotMatchAllTemplate = noneMatch(template.patterns(), Regex::isMatchAllPattern);
if (isNotMatchAllTemplate) {
if (template.patterns().stream().anyMatch(patternMatchPredicate)) {
if (anyMatch(template.patterns(), patternMatchPredicate)) {
matchedTemplates.add(template);
}
}
Expand All @@ -1238,19 +1273,21 @@ 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 = findFirst(
matchedTemplates,
template -> IndexMetadata.INDEX_HIDDEN_SETTING.exists(template.settings())
);
if (templateWithHiddenSetting.isPresent()) {
final boolean templatedIsHidden = IndexMetadata.INDEX_HIDDEN_SETTING.get(templateWithHiddenSetting.get().settings());
if (templatedIsHidden) {
// remove the global templates
matchedTemplates.removeIf(current -> current.patterns().stream().anyMatch(Regex::isMatchAllPattern));
matchedTemplates.removeIf(current -> anyMatch(current.patterns(), Regex::isMatchAllPattern));
}
// validate that hidden didn't change
final Optional<IndexTemplateMetadata> templateWithHiddenSettingPostRemoval = matchedTemplates.stream()
.filter(template -> IndexMetadata.INDEX_HIDDEN_SETTING.exists(template.settings()))
.findFirst();
final Optional<IndexTemplateMetadata> templateWithHiddenSettingPostRemoval = findFirst(
matchedTemplates,
template -> IndexMetadata.INDEX_HIDDEN_SETTING.exists(template.settings())
);
if (templateWithHiddenSettingPostRemoval.isEmpty()
|| templateWithHiddenSetting.get() != templateWithHiddenSettingPostRemoval.get()) {
throw new IllegalStateException(
Expand Down Expand Up @@ -1313,14 +1350,13 @@ static List<Tuple<String, ComposableIndexTemplate>> findV2CandidateTemplates(Met
* built with a template that none of its indices match.
*/
if (isHidden == false || template.getDataStreamTemplate() != null) {
final boolean matched = template.indexPatterns().stream().anyMatch(patternMatchPredicate);
if (matched) {
if (anyMatch(template.indexPatterns(), patternMatchPredicate)) {
candidates.add(Tuple.tuple(name, template));
}
} else {
final boolean isNotMatchAllTemplate = template.indexPatterns().stream().noneMatch(Regex::isMatchAllPattern);
final boolean isNotMatchAllTemplate = noneMatch(template.indexPatterns(), Regex::isMatchAllPattern);
if (isNotMatchAllTemplate) {
if (template.indexPatterns().stream().anyMatch(patternMatchPredicate)) {
if (anyMatch(template.indexPatterns(), patternMatchPredicate)) {
candidates.add(Tuple.tuple(name, template));
}
}
Expand All @@ -1334,7 +1370,7 @@ static List<Tuple<String, ComposableIndexTemplate>> findV2CandidateTemplates(Met
// Checks if a global template specifies the `index.hidden` setting. This check is important because a global
// template shouldn't specify the `index.hidden` setting, we leave it up to the caller to handle this situation.
private static boolean isGlobalAndHasIndexHiddenSetting(Metadata metadata, ComposableIndexTemplate template, String templateName) {
return template.indexPatterns().stream().anyMatch(Regex::isMatchAllPattern)
return anyMatch(template.indexPatterns(), Regex::isMatchAllPattern)
&& IndexMetadata.INDEX_HIDDEN_SETTING.exists(resolveSettings(metadata, templateName));
}

Expand Down

0 comments on commit d69d5a7

Please sign in to comment.