Skip to content

Commit 4edd732

Browse files
committed
Optimize index template registry
1 parent 6263f44 commit 4edd732

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

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

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,39 @@ public void clusterChanged(ClusterChangedEvent event) {
246246
if (event.localNodeMaster() == false && localNodeVersionAfterMaster == false) {
247247
return;
248248
}
249+
boolean settingsChanged = settingsChanged(event);
249250
for (ProjectMetadata project : event.state().metadata().projects().values()) {
251+
final ProjectMetadata previousProject = event.previousState().metadata().projects().get(project.id());
252+
if (settingsChanged == false && projectChanged(previousProject, project) == false) {
253+
continue;
254+
}
250255
addIngestPipelinesIfMissing(project);
251256
addTemplatesIfMissing(project);
252257
addIndexLifecyclePoliciesIfMissing(project);
253258
}
254259
}
255260

261+
private boolean projectChanged(ProjectMetadata previousProject, ProjectMetadata newProject) {
262+
if (previousProject == null) {
263+
return true;
264+
}
265+
return newProject.templates().isEmpty()
266+
|| newProject.templates() != previousProject.templates()
267+
|| newProject.templatesV2().isEmpty()
268+
|| newProject.templatesV2() != previousProject.templatesV2()
269+
|| newProject.componentTemplates().isEmpty()
270+
|| newProject.componentTemplates() != previousProject.componentTemplates()
271+
|| newProject.custom(IndexLifecycleMetadata.TYPE) == null
272+
|| newProject.custom(IndexLifecycleMetadata.TYPE) != previousProject.custom(IndexLifecycleMetadata.TYPE)
273+
|| newProject.custom(IngestMetadata.TYPE) == null
274+
|| newProject.custom(IngestMetadata.TYPE) != previousProject.custom(IngestMetadata.TYPE);
275+
}
276+
277+
private boolean settingsChanged(ClusterChangedEvent event) {
278+
return event.state().metadata().persistentSettings() != event.previousState().metadata().persistentSettings()
279+
|| event.state().metadata().transientSettings() != event.previousState().metadata().transientSettings();
280+
}
281+
256282
/**
257283
* A method that can be overridden to add additional conditions to be satisfied
258284
* before installing the template registry components.
@@ -325,18 +351,19 @@ private void addComponentTemplatesIfMissing(ProjectMetadata project) {
325351
final Map<String, ComponentTemplate> indexTemplates = getComponentTemplateConfigs();
326352
for (Map.Entry<String, ComponentTemplate> newTemplate : indexTemplates.entrySet()) {
327353
final String templateName = newTemplate.getKey();
354+
if (templateDependenciesSatisfied(project, newTemplate.getValue()) == false) {
355+
logger.trace(
356+
"not adding index template [{}] for [{}] because its required dependencies do not exist",
357+
templateName,
358+
getOrigin()
359+
);
360+
continue;
361+
}
328362
final AtomicBoolean creationCheck = templateCreationsInProgress.computeIfAbsent(project.id(), key -> new ConcurrentHashMap<>())
329363
.computeIfAbsent(templateName, key -> new AtomicBoolean(false));
330364
if (creationCheck.compareAndSet(false, true)) {
331365
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)) {
366+
if (Objects.isNull(currentTemplate)) {
340367
logger.debug("adding component template [{}] for [{}], because it doesn't exist", templateName, getOrigin());
341368
putComponentTemplate(project.id(), templateName, newTemplate.getValue(), creationCheck);
342369
} else if (Objects.isNull(currentTemplate.version()) || newTemplate.getValue().version() > currentTemplate.version()) {
@@ -399,22 +426,23 @@ private void addComposableTemplatesIfMissing(ProjectMetadata project) {
399426
final Map<String, ComposableIndexTemplate> indexTemplates = getComposableTemplateConfigs();
400427
for (Map.Entry<String, ComposableIndexTemplate> newTemplate : indexTemplates.entrySet()) {
401428
final String templateName = newTemplate.getKey();
429+
boolean componentTemplatesAvailable = componentTemplatesInstalled(project, newTemplate.getValue());
430+
if (componentTemplatesAvailable == false) {
431+
if (logger.isTraceEnabled()) {
432+
logger.trace(
433+
"not adding composable template [{}] for [{}] because its required component templates do not exist or do not "
434+
+ "have the right version",
435+
templateName,
436+
getOrigin()
437+
);
438+
}
439+
continue;
440+
}
402441
final AtomicBoolean creationCheck = templateCreationsInProgress.computeIfAbsent(project.id(), key -> new ConcurrentHashMap<>())
403442
.computeIfAbsent(templateName, key -> new AtomicBoolean(false));
404443
if (creationCheck.compareAndSet(false, true)) {
405444
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)) {
445+
if (Objects.isNull(currentTemplate)) {
418446
logger.debug("adding composable template [{}] for [{}], because it doesn't exist", templateName, getOrigin());
419447
putComposableTemplate(project, templateName, newTemplate.getValue(), creationCheck);
420448
} else if (Objects.isNull(currentTemplate.version()) || newTemplate.getValue().version() > currentTemplate.version()) {
@@ -679,19 +707,20 @@ protected static Map<String, ComposableIndexTemplate> parseComposableTemplates(I
679707

680708
private void addIngestPipelinesIfMissing(ProjectMetadata project) {
681709
for (IngestPipelineConfig requiredPipeline : getIngestPipelines()) {
710+
List<String> pipelineDependencies = requiredPipeline.getPipelineDependencies();
711+
if (pipelineDependencies != null && pipelineDependenciesExist(project, pipelineDependencies) == false) {
712+
logger.trace(
713+
"not adding ingest pipeline [{}] for [{}] because its dependencies do not exist",
714+
requiredPipeline.getId(),
715+
getOrigin()
716+
);
717+
continue;
718+
}
682719
final AtomicBoolean creationCheck = pipelineCreationsInProgress.computeIfAbsent(project.id(), key -> new ConcurrentHashMap<>())
683720
.computeIfAbsent(requiredPipeline.getId(), key -> new AtomicBoolean(false));
684721

685722
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 {
723+
{
695724
PipelineConfiguration existingPipeline = findInstalledPipeline(project, requiredPipeline.getId());
696725
if (existingPipeline != null) {
697726
Integer existingPipelineVersion = existingPipeline.getVersion();

0 commit comments

Comments
 (0)