Skip to content

Commit abd1cc7

Browse files
committed
Optimize index template registry
1 parent 272c731 commit abd1cc7

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,31 @@ public void clusterChanged(ClusterChangedEvent event) {
247247
return;
248248
}
249249
for (ProjectMetadata project : event.state().metadata().projects().values()) {
250+
if (projectChanged(event.previousState().metadata().projects().get(project.id()), project) == false) {
251+
continue;
252+
}
250253
addIngestPipelinesIfMissing(project);
251254
addTemplatesIfMissing(project);
252255
addIndexLifecyclePoliciesIfMissing(project);
253256
}
254257
}
255258

259+
private boolean projectChanged(ProjectMetadata oldProject, ProjectMetadata newProject) {
260+
if (oldProject == null) {
261+
return true;
262+
}
263+
return newProject.templates().isEmpty()
264+
|| newProject.templates() != oldProject.templates()
265+
|| newProject.templatesV2().isEmpty()
266+
|| newProject.templatesV2() != oldProject.templatesV2()
267+
|| newProject.componentTemplates().isEmpty()
268+
|| newProject.componentTemplates() != oldProject.componentTemplates()
269+
|| newProject.custom(IndexLifecycleMetadata.TYPE) == null
270+
|| newProject.custom(IndexLifecycleMetadata.TYPE) != oldProject.custom(IndexLifecycleMetadata.TYPE)
271+
|| newProject.custom(IngestMetadata.TYPE) == null
272+
|| newProject.custom(IngestMetadata.TYPE) != oldProject.custom(IngestMetadata.TYPE);
273+
}
274+
256275
/**
257276
* A method that can be overridden to add additional conditions to be satisfied
258277
* before installing the template registry components.
@@ -325,18 +344,19 @@ private void addComponentTemplatesIfMissing(ProjectMetadata project) {
325344
final Map<String, ComponentTemplate> indexTemplates = getComponentTemplateConfigs();
326345
for (Map.Entry<String, ComponentTemplate> newTemplate : indexTemplates.entrySet()) {
327346
final String templateName = newTemplate.getKey();
347+
if (templateDependenciesSatisfied(project, newTemplate.getValue()) == false) {
348+
logger.trace(
349+
"not adding index template [{}] for [{}] because its required dependencies do not exist",
350+
templateName,
351+
getOrigin()
352+
);
353+
continue;
354+
}
328355
final AtomicBoolean creationCheck = templateCreationsInProgress.computeIfAbsent(project.id(), key -> new ConcurrentHashMap<>())
329356
.computeIfAbsent(templateName, key -> new AtomicBoolean(false));
330357
if (creationCheck.compareAndSet(false, true)) {
331358
ComponentTemplate currentTemplate = project.componentTemplates().get(templateName);
332-
if (templateDependenciesSatisfied(project, newTemplate.getValue()) == false) {
333-
creationCheck.set(false);
334-
logger.trace(
335-
"not adding index template [{}] for [{}] because its required dependencies do not exist",
336-
templateName,
337-
getOrigin()
338-
);
339-
} else if (Objects.isNull(currentTemplate)) {
359+
if (Objects.isNull(currentTemplate)) {
340360
logger.debug("adding component template [{}] for [{}], because it doesn't exist", templateName, getOrigin());
341361
putComponentTemplate(project.id(), templateName, newTemplate.getValue(), creationCheck);
342362
} else if (Objects.isNull(currentTemplate.version()) || newTemplate.getValue().version() > currentTemplate.version()) {
@@ -399,22 +419,23 @@ private void addComposableTemplatesIfMissing(ProjectMetadata project) {
399419
final Map<String, ComposableIndexTemplate> indexTemplates = getComposableTemplateConfigs();
400420
for (Map.Entry<String, ComposableIndexTemplate> newTemplate : indexTemplates.entrySet()) {
401421
final String templateName = newTemplate.getKey();
422+
boolean componentTemplatesAvailable = componentTemplatesInstalled(project, newTemplate.getValue());
423+
if (componentTemplatesAvailable == false) {
424+
if (logger.isTraceEnabled()) {
425+
logger.trace(
426+
"not adding composable template [{}] for [{}] because its required component templates do not exist or do not "
427+
+ "have the right version",
428+
templateName,
429+
getOrigin()
430+
);
431+
}
432+
continue;
433+
}
402434
final AtomicBoolean creationCheck = templateCreationsInProgress.computeIfAbsent(project.id(), key -> new ConcurrentHashMap<>())
403435
.computeIfAbsent(templateName, key -> new AtomicBoolean(false));
404436
if (creationCheck.compareAndSet(false, true)) {
405437
ComposableIndexTemplate currentTemplate = project.templatesV2().get(templateName);
406-
boolean componentTemplatesAvailable = componentTemplatesInstalled(project, newTemplate.getValue());
407-
if (componentTemplatesAvailable == false) {
408-
creationCheck.set(false);
409-
if (logger.isTraceEnabled()) {
410-
logger.trace(
411-
"not adding composable template [{}] for [{}] because its required component templates do not exist or do not "
412-
+ "have the right version",
413-
templateName,
414-
getOrigin()
415-
);
416-
}
417-
} else if (Objects.isNull(currentTemplate)) {
438+
if (Objects.isNull(currentTemplate)) {
418439
logger.debug("adding composable template [{}] for [{}], because it doesn't exist", templateName, getOrigin());
419440
putComposableTemplate(project, templateName, newTemplate.getValue(), creationCheck);
420441
} else if (Objects.isNull(currentTemplate.version()) || newTemplate.getValue().version() > currentTemplate.version()) {
@@ -679,19 +700,20 @@ protected static Map<String, ComposableIndexTemplate> parseComposableTemplates(I
679700

680701
private void addIngestPipelinesIfMissing(ProjectMetadata project) {
681702
for (IngestPipelineConfig requiredPipeline : getIngestPipelines()) {
703+
List<String> pipelineDependencies = requiredPipeline.getPipelineDependencies();
704+
if (pipelineDependencies != null && pipelineDependenciesExist(project, pipelineDependencies) == false) {
705+
logger.trace(
706+
"not adding ingest pipeline [{}] for [{}] because its dependencies do not exist",
707+
requiredPipeline.getId(),
708+
getOrigin()
709+
);
710+
continue;
711+
}
682712
final AtomicBoolean creationCheck = pipelineCreationsInProgress.computeIfAbsent(project.id(), key -> new ConcurrentHashMap<>())
683713
.computeIfAbsent(requiredPipeline.getId(), key -> new AtomicBoolean(false));
684714

685715
if (creationCheck.compareAndSet(false, true)) {
686-
List<String> pipelineDependencies = requiredPipeline.getPipelineDependencies();
687-
if (pipelineDependencies != null && pipelineDependenciesExist(project, pipelineDependencies) == false) {
688-
creationCheck.set(false);
689-
logger.trace(
690-
"not adding ingest pipeline [{}] for [{}] because its dependencies do not exist",
691-
requiredPipeline.getId(),
692-
getOrigin()
693-
);
694-
} else {
716+
{
695717
PipelineConfiguration existingPipeline = findInstalledPipeline(project, requiredPipeline.getId());
696718
if (existingPipeline != null) {
697719
Integer existingPipelineVersion = existingPipeline.getVersion();

0 commit comments

Comments
 (0)