Skip to content

Conversation

houssain-barouni
Copy link
Collaborator

@houssain-barouni houssain-barouni commented Aug 26, 2025

Description

Skip redundant index template settings PUTs for Elasticsearch/OpenSearch.
Adds parallel execution of index settings updates to cut overhead.

Problem

Every startup overwrote template settings (priority, shards, replicas) even when unchanged. This wasted requests, bumped ES/OS cluster.

Solution

  • Compare desired vs current template priority + serialized settings.
  • Only send PUT if something changed; otherwise log debug skip.
  • Run settings updates in parallel (virtual threads).

Effects

  • No unnecessary template PUTs on steady restarts.
  • Faster startup with many templates.

Checklist

Related issues

closes #37240

@houssain-barouni houssain-barouni marked this pull request as ready for review August 26, 2025 17:15
@houssain-barouni houssain-barouni requested review from EuroLew and a team August 26, 2025 17:15
@houssain-barouni houssain-barouni force-pushed the hb-update-settings branch 4 times, most recently from 834d109 to 3aeb344 Compare August 27, 2025 10:37
Copy link
Contributor

@EuroLew EuroLew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM would just like th logic to be easier to understand

@@ -483,33 +491,46 @@ utils.new SchemaSettingsAppender(templateFile)
}
}

private PutIndexTemplateRequest updateTemplateSettings(
private Optional<PutIndexTemplateRequest> buildTemplateSettingsUpdateRequestIfChanged(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 A bit hard to understand I would prefer it be split up

private Optional<PutIndexTemplateRequest> buildTemplateSettingsUpdateRequestIfChanged(
    final IndexTemplateDescriptor templateDescriptor,
    final IndexConfiguration configuredSettings) {

  try (final var templateFile = getResourceAsStream(templateDescriptor.getMappingsClasspathFilename())) {
    final var currentTemplate = getIndexTemplateState(templateDescriptor);
    final var configuredPriority = convertValue(configuredSettings.getTemplatePriority(), Long::valueOf);
    
    final var newSettings = utils.new SchemaSettingsAppender(templateFile)
        .withNumberOfShards(configuredSettings.getNumberOfShards())
        .withNumberOfReplicas(configuredSettings.getNumberOfReplicas());

    if (areTemplateSettingsEqualToConfigured(currentTemplate, configuredPriority, newSettings)) {
      LOG.debug("Index template settings for [{}] are already up to date", templateDescriptor.getTemplateName());
      return Optional.empty();
    }

    return Optional.of(buildPutIndexTemplateRequest(templateDescriptor, currentTemplate, newSettings, configuredPriority));

  } catch (final IOException e) {
    throw new SearchEngineException(
        "Failed to load file " + templateDescriptor.getMappingsClasspathFilename() + " from classpath.", e);
  }
}

private boolean areTemplateSettingsEqualToConfigured(IndexTemplateState currentTemplate, Long configuredPriority, SchemaSettingsAppender newSettings) {
  return Objects.equals(configuredPriority, currentTemplate.priority())
      && newSettings.equalsSettings(serializeAsMap(currentTemplate.template().settings()));
}

private PutIndexTemplateRequest buildPutIndexTemplateRequest(
    IndexTemplateDescriptor templateDescriptor,
    IndexTemplateState currentTemplate, 
    SchemaSettingsAppender settingsAppender,
    Long priority) {
  
  final var updatedSettings = deserializeJson(IndexTemplateMapping._DESERIALIZER, settingsAppender.build()).settings();
  
  LOG.debug("Applying to index template [{}]: settings={}, priority={}", 
      templateDescriptor.getTemplateName(), updatedSettings, priority);

  return new PutIndexTemplateRequest.Builder()
      .name(templateDescriptor.getTemplateName())
      .indexPatterns(templateDescriptor.getIndexPattern())
      .template(t -> t
          .settings(updatedSettings)
          .mappings(currentTemplate.template().mappings())
          .aliases(currentTemplate.template().aliases()))
      .priority(priority)
      .build();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also do the same for OS

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint, I tired to make it clearer now
I did not add though

private PutIndexTemplateRequest buildPutIndexTemplateRequest(
    IndexTemplateDescriptor templateDescriptor,
    IndexTemplateState currentTemplate, 
    SchemaSettingsAppender settingsAppender,
    Long priority) {

I feel it is very specific and it is not reusable, I think it is clear enough now

@houssain-barouni houssain-barouni added this pull request to the merge queue Aug 27, 2025
Merged via the queue into main with commit 9f965f8 Aug 27, 2025
142 of 144 checks passed
@houssain-barouni houssain-barouni deleted the hb-update-settings branch August 27, 2025 14:30
@backport-action
Copy link
Collaborator

Successfully created backport PR for release-8.8.0-alpha8:

houssain-barouni added a commit that referenced this pull request Aug 27, 2025
…nly when they change (#37298)

# Description
Backport of #37217 to `release-8.8.0-alpha8`.

relates to #37240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary index template settings updates cause redundant PUT requests and startup overhead
3 participants