-
Notifications
You must be signed in to change notification settings - Fork 2.3k
SPI for loading ABC templates #14659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shwetathareja
merged 12 commits into
opensearch-project:main
from
mgodwan:managed-templates-main-pr
Jul 16, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
21f882a
SPI for loading ABC templates
mgodwan d4c6472
Change template load trigger
mgodwan 1c80d7a
Address PR comments, add verification logic, refactor classes
mgodwan c10f537
Add changelog
mgodwan d75ac13
Merge branch 'main' into managed-templates-main-pr
mgodwan 2a13e48
Address PR comments
mgodwan 21541ba
Add tests, Fix spotless, Modify implementation based on PR Comments, …
mgodwan d7b35de
Fix precommit
mgodwan 3a90009
Fix node bootup
mgodwan 9713c4c
Address PR comments
mgodwan bbb1667
Apply spotless
mgodwan e839cc6
Merge branch 'main' into managed-templates-main-pr
mgodwan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...in/java/org/opensearch/cluster/applicationtemplates/ClusterStateSystemTemplateLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.applicationtemplates; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.OpenSearchCorruptionException; | ||
| import org.opensearch.action.admin.indices.template.put.PutComponentTemplateAction; | ||
| import org.opensearch.client.Client; | ||
| import org.opensearch.client.OriginSettingClient; | ||
| import org.opensearch.cluster.ClusterState; | ||
| import org.opensearch.cluster.metadata.ComponentTemplate; | ||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
| import org.opensearch.common.unit.TimeValue; | ||
| import org.opensearch.common.xcontent.json.JsonXContent; | ||
| import org.opensearch.core.xcontent.DeprecationHandler; | ||
| import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
| import org.opensearch.core.xcontent.XContentParser; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Class responsible for loading the component templates provided by a repository into the cluster state. | ||
| */ | ||
| @ExperimentalApi | ||
| public class ClusterStateSystemTemplateLoader implements SystemTemplateLoader { | ||
|
|
||
| private final Client client; | ||
|
|
||
| private final Supplier<ClusterState> clusterStateSupplier; | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(SystemTemplateLoader.class); | ||
|
|
||
| public static final String TEMPLATE_LOADER_IDENTIFIER = "system_template_loader"; | ||
| public static final String TEMPLATE_TYPE_KEY = "_type"; | ||
|
|
||
| public ClusterStateSystemTemplateLoader(Client client, Supplier<ClusterState> clusterStateSupplier) { | ||
| this.client = new OriginSettingClient(client, TEMPLATE_LOADER_IDENTIFIER); | ||
| this.clusterStateSupplier = clusterStateSupplier; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean loadTemplate(SystemTemplate template) throws IOException { | ||
| final ComponentTemplate existingTemplate = clusterStateSupplier.get() | ||
| .metadata() | ||
| .componentTemplates() | ||
| .get(template.templateMetadata().fullyQualifiedName()); | ||
|
|
||
| if (existingTemplate != null | ||
| && !SystemTemplateMetadata.COMPONENT_TEMPLATE_TYPE.equals( | ||
| Objects.toString(existingTemplate.metadata().get(TEMPLATE_TYPE_KEY)) | ||
| )) { | ||
| throw new OpenSearchCorruptionException( | ||
| "Attempting to create " + template.templateMetadata().name() + " which has already been created through some other source." | ||
| ); | ||
| } | ||
|
|
||
| if (existingTemplate != null && existingTemplate.version() >= template.templateMetadata().version()) { | ||
| logger.debug( | ||
| "Skipping putting template {} as its existing version [{}] is >= fetched version [{}]", | ||
| template.templateMetadata().fullyQualifiedName(), | ||
| existingTemplate.version(), | ||
| template.templateMetadata().version() | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| ComponentTemplate newTemplate = null; | ||
| try ( | ||
| XContentParser contentParser = JsonXContent.jsonXContent.createParser( | ||
| NamedXContentRegistry.EMPTY, | ||
| DeprecationHandler.IGNORE_DEPRECATIONS, | ||
| template.templateContent().utf8ToString() | ||
| ) | ||
| ) { | ||
| newTemplate = ComponentTemplate.parse(contentParser); | ||
| } | ||
|
|
||
| if (!Objects.equals(newTemplate.version(), template.templateMetadata().version())) { | ||
| throw new OpenSearchCorruptionException( | ||
| "Template version mismatch for " | ||
| + template.templateMetadata().name() | ||
| + ". Version in metadata: " | ||
| + template.templateMetadata().version() | ||
| + " , Version in content: " | ||
| + newTemplate.version() | ||
| ); | ||
| } | ||
|
|
||
| final PutComponentTemplateAction.Request request = new PutComponentTemplateAction.Request( | ||
| template.templateMetadata().fullyQualifiedName() | ||
| ).componentTemplate(newTemplate); | ||
|
|
||
| return client.admin() | ||
| .indices() | ||
| .execute(PutComponentTemplateAction.INSTANCE, request) | ||
| .actionGet(TimeValue.timeValueMillis(30000)) | ||
| .isAcknowledged(); | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.applicationtemplates; | ||
|
|
||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
| import org.opensearch.core.common.bytes.BytesReference; | ||
|
|
||
| /** | ||
| * Encapsulates the information and content about a system template available within a repository. | ||
| */ | ||
| @ExperimentalApi | ||
| public class SystemTemplate { | ||
|
|
||
| private final BytesReference templateContent; | ||
|
|
||
| private final SystemTemplateMetadata templateMetadata; | ||
|
|
||
| private final TemplateRepositoryMetadata repositoryMetadata; | ||
|
|
||
| public SystemTemplate(BytesReference templateContent, SystemTemplateMetadata templateInfo, TemplateRepositoryMetadata repositoryInfo) { | ||
| this.templateContent = templateContent; | ||
| this.templateMetadata = templateInfo; | ||
| this.repositoryMetadata = repositoryInfo; | ||
| } | ||
|
|
||
| public BytesReference templateContent() { | ||
| return templateContent; | ||
| } | ||
|
|
||
| public SystemTemplateMetadata templateMetadata() { | ||
| return templateMetadata; | ||
| } | ||
|
|
||
| public TemplateRepositoryMetadata repositoryMetadata() { | ||
| return repositoryMetadata; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.applicationtemplates; | ||
|
|
||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Interface to load template into the OpenSearch runtime. | ||
| */ | ||
| @ExperimentalApi | ||
| public interface SystemTemplateLoader { | ||
|
|
||
| /** | ||
| * @param template Templated to be loaded | ||
| * @throws IOException If an exceptional situation is encountered while parsing/loading the template | ||
| */ | ||
| boolean loadTemplate(SystemTemplate template) throws IOException; | ||
| } |
68 changes: 68 additions & 0 deletions
68
server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.applicationtemplates; | ||
|
|
||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
|
|
||
| /** | ||
| * Metadata information about a template available in a template repository. | ||
| */ | ||
| @ExperimentalApi | ||
| public class SystemTemplateMetadata { | ||
|
|
||
| private final long version; | ||
| private final String type; | ||
| private final String name; | ||
|
|
||
| private static final String DELIMITER = "@"; | ||
|
|
||
| public static final String COMPONENT_TEMPLATE_TYPE = "@abc_template"; | ||
|
|
||
| public SystemTemplateMetadata(long version, String type, String name) { | ||
| this.version = version; | ||
| this.type = type; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String type() { | ||
| return type; | ||
| } | ||
|
|
||
| public String name() { | ||
| return name; | ||
| } | ||
|
|
||
| public long version() { | ||
| return version; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the metadata using fully qualified name for the template | ||
| * @param fullyQualifiedName (e.g. @abc_template@logs@1) | ||
| * @return Metadata object based on name | ||
| */ | ||
| public static SystemTemplateMetadata fromComponentTemplate(String fullyQualifiedName) { | ||
mgodwan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert fullyQualifiedName.length() > 1 : "System template name must have at least one component"; | ||
| assert fullyQualifiedName.substring(1, fullyQualifiedName.indexOf(DELIMITER, 1)).equals(COMPONENT_TEMPLATE_TYPE); | ||
|
|
||
| return new SystemTemplateMetadata( | ||
| Long.parseLong(fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(DELIMITER))), | ||
| COMPONENT_TEMPLATE_TYPE, | ||
| fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf(DELIMITER)) | ||
| ); | ||
| } | ||
|
|
||
| public static SystemTemplateMetadata fromComponentTemplateInfo(String name, long version) { | ||
| return new SystemTemplateMetadata(version, COMPONENT_TEMPLATE_TYPE, name); | ||
| } | ||
|
|
||
| public final String fullyQualifiedName() { | ||
| return type + DELIMITER + name + DELIMITER + version; | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
...r/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.applicationtemplates; | ||
|
|
||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Repository interface around the templates provided by a store (e.g. code repo, remote file store, etc) | ||
| */ | ||
| @ExperimentalApi | ||
| public interface SystemTemplateRepository extends AutoCloseable { | ||
|
|
||
| /** | ||
| * @return Metadata about the repository | ||
| */ | ||
| TemplateRepositoryMetadata metadata(); | ||
|
|
||
| /** | ||
| * @return Metadata for all available templates | ||
| */ | ||
| Iterable<SystemTemplateMetadata> listTemplates() throws IOException; | ||
|
|
||
| /** | ||
| * | ||
| * @param template metadata about template to be fetched | ||
| * @return The actual template content | ||
| */ | ||
| SystemTemplate getTemplate(SystemTemplateMetadata template) throws IOException; | ||
| } |
31 changes: 31 additions & 0 deletions
31
server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplatesPlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.applicationtemplates; | ||
|
|
||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Plugin interface to expose the template maintaining logic. | ||
| */ | ||
| @ExperimentalApi | ||
| public interface SystemTemplatesPlugin { | ||
|
|
||
| /** | ||
| * @return repository implementation from which templates are to be fetched. | ||
| */ | ||
| SystemTemplateRepository loadRepository() throws IOException; | ||
|
|
||
| /** | ||
| * @param templateInfo Metadata about the template to load | ||
| * @return Implementation of TemplateLoader which determines how to make the template available at runtime. | ||
| */ | ||
| SystemTemplateLoader loaderFor(SystemTemplateMetadata templateInfo); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.