-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: mgodwan <[email protected]>
- Loading branch information
Showing
14 changed files
with
468 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
65 changes: 65 additions & 0 deletions
65
.../opensearch/cluster/service/applicationtemplates/ClusterStateComponentTemplateLoader.java
This file contains 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,65 @@ | ||
/* | ||
* 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.service.applicationtemplates; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
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.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 org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.function.Supplier; | ||
|
||
public class ClusterStateComponentTemplateLoader implements TemplateLoader { | ||
|
||
private Client client; | ||
|
||
private Supplier<ClusterState> clusterStateSupplier; | ||
|
||
private static final Logger logger = LogManager.getLogger(TemplateLoader.class); | ||
|
||
public static final String TEMPLATE_LOADER_IDENTIFIER = "system_template_loader"; | ||
|
||
public ClusterStateComponentTemplateLoader(Client client, | ||
ThreadPool threadPool, | ||
Supplier<ClusterState> clusterStateSupplier) { | ||
this.client = new OriginSettingClient(client, TEMPLATE_LOADER_IDENTIFIER); | ||
this.clusterStateSupplier = clusterStateSupplier; | ||
} | ||
|
||
@Override | ||
public void loadTemplate(SystemTemplate template) throws IOException { | ||
ComponentTemplate existingTemplate = clusterStateSupplier.get().metadata().componentTemplates().get(template.templateInfo().fullyQualifiedName()); | ||
|
||
XContentParser contentParser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, | ||
template.templateContent().utf8ToString()); | ||
ComponentTemplate newTemplate = ComponentTemplate.parse(contentParser); | ||
|
||
if (existingTemplate != null && existingTemplate.version() >= newTemplate.version()) { | ||
logger.debug("Skipping putting template {} as its existing version [{}] is >= fetched version [{}]", template.templateInfo().fullyQualifiedName(), | ||
existingTemplate.version(), | ||
newTemplate.version()); | ||
} | ||
|
||
PutComponentTemplateAction.Request request = new PutComponentTemplateAction.Request(template.templateInfo().fullyQualifiedName()) | ||
.componentTemplate(newTemplate); | ||
|
||
client.admin().indices().execute(PutComponentTemplateAction.INSTANCE, request).actionGet(TimeValue.timeValueMillis(30000)); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplate.java
This file contains 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,41 @@ | ||
/* | ||
* 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.service.applicationtemplates; | ||
|
||
import org.opensearch.core.common.bytes.BytesReference; | ||
|
||
/** | ||
* Encapsulates the information and content about a system template available within a repository. | ||
*/ | ||
public class SystemTemplate { | ||
|
||
private final BytesReference templateContent; | ||
|
||
private final SystemTemplateInfo templateInfo; | ||
|
||
private final TemplateRepositoryInfo repositoryInfo; | ||
|
||
public SystemTemplate(BytesReference templateContent, SystemTemplateInfo templateInfo, TemplateRepositoryInfo repositoryInfo) { | ||
this.templateContent = templateContent; | ||
this.templateInfo = templateInfo; | ||
this.repositoryInfo = repositoryInfo; | ||
} | ||
|
||
public BytesReference templateContent() { | ||
return templateContent; | ||
} | ||
|
||
public SystemTemplateInfo templateInfo() { | ||
return templateInfo; | ||
} | ||
|
||
public TemplateRepositoryInfo repositoryInfo() { | ||
return repositoryInfo; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplateInfo.java
This file contains 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,53 @@ | ||
/* | ||
* 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.service.applicationtemplates; | ||
|
||
/** | ||
* Metadata information about a template available in a template repository. | ||
*/ | ||
public class SystemTemplateInfo { | ||
|
||
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 SystemTemplateInfo(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; | ||
} | ||
|
||
public static SystemTemplateInfo fromComponentTemplate(String fullyQualifiedName) { | ||
return new SystemTemplateInfo(Long.parseLong(fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(DELIMITER))), COMPONENT_TEMPLATE_TYPE, fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf(DELIMITER))); | ||
} | ||
|
||
public static SystemTemplateInfo createComponentTemplateInfo(String name, long version) { | ||
return new SystemTemplateInfo(version, COMPONENT_TEMPLATE_TYPE, name); | ||
} | ||
|
||
public final String fullyQualifiedName() { | ||
return name + DELIMITER + version; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesPlugin.java
This file contains 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,28 @@ | ||
/* | ||
* 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.service.applicationtemplates; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Plugin interface to expose the template maintaining logic. | ||
*/ | ||
public interface SystemTemplatesPlugin { | ||
|
||
/** | ||
* @return repository implementation from which templates are to be fetched. | ||
*/ | ||
TemplateRepository 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. | ||
*/ | ||
TemplateLoader loaderFor(SystemTemplateInfo templateInfo); | ||
} |
Oops, something went wrong.