use of org.opensearch.indices.IndexTemplateMissingException in project OpenSearch by opensearch-project.
the class ExceptionSerializationTests method testIndexTemplateMissingException.
public void testIndexTemplateMissingException() throws IOException {
IndexTemplateMissingException ex = serialize(new IndexTemplateMissingException("name"));
assertEquals("index_template [name] missing", ex.getMessage());
assertEquals("name", ex.name());
ex = serialize(new IndexTemplateMissingException((String) null));
assertEquals("index_template [null] missing", ex.getMessage());
assertNull(ex.name());
}
use of org.opensearch.indices.IndexTemplateMissingException in project OpenSearch by opensearch-project.
the class MetadataIndexTemplateServiceTests method testRemoveIndexTemplateV2.
public void testRemoveIndexTemplateV2() throws Exception {
ComposableIndexTemplate template = ComposableIndexTemplateTests.randomInstance();
final MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService();
IndexTemplateMissingException e = expectThrows(IndexTemplateMissingException.class, () -> MetadataIndexTemplateService.innerRemoveIndexTemplateV2(ClusterState.EMPTY_STATE, "foo"));
assertThat(e.getMessage(), equalTo("index_template [foo] missing"));
final ClusterState state = metadataIndexTemplateService.addIndexTemplateV2(ClusterState.EMPTY_STATE, false, "foo", template);
assertNotNull(state.metadata().templatesV2().get("foo"));
assertTemplatesEqual(state.metadata().templatesV2().get("foo"), template);
ClusterState updatedState = MetadataIndexTemplateService.innerRemoveIndexTemplateV2(state, "foo");
assertNull(updatedState.metadata().templatesV2().get("foo"));
}
use of org.opensearch.indices.IndexTemplateMissingException in project OpenSearch by opensearch-project.
the class MetadataIndexTemplateService method removeComponentTemplate.
/**
* Remove the given component template from the cluster state. The component template name
* supports simple regex wildcards for removing multiple component templates at a time.
*/
public void removeComponentTemplate(final String name, final TimeValue masterTimeout, final ActionListener<AcknowledgedResponse> listener) {
validateNotInUse(clusterService.state().metadata(), name);
clusterService.submitStateUpdateTask("remove-component-template [" + name + "]", new ClusterStateUpdateTask(Priority.URGENT) {
@Override
public TimeValue timeout() {
return masterTimeout;
}
@Override
public void onFailure(String source, Exception e) {
listener.onFailure(e);
}
@Override
public ClusterState execute(ClusterState currentState) {
Set<String> templateNames = new HashSet<>();
for (String templateName : currentState.metadata().componentTemplates().keySet()) {
if (Regex.simpleMatch(name, templateName)) {
templateNames.add(templateName);
}
}
if (templateNames.isEmpty()) {
// fail with index missing...
if (Regex.isMatchAllPattern(name)) {
return currentState;
}
// TODO: perhaps introduce a ComponentTemplateMissingException?
throw new IndexTemplateMissingException(name);
}
Metadata.Builder metadata = Metadata.builder(currentState.metadata());
for (String templateName : templateNames) {
logger.info("removing component template [{}]", templateName);
metadata.removeComponentTemplate(templateName);
}
return ClusterState.builder(currentState).metadata(metadata).build();
}
@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
listener.onResponse(new AcknowledgedResponse(true));
}
});
}
use of org.opensearch.indices.IndexTemplateMissingException in project OpenSearch by opensearch-project.
the class TemplateUpgradeService method upgradeTemplates.
void upgradeTemplates(Map<String, BytesReference> changes, Set<String> deletions) {
final AtomicBoolean anyUpgradeFailed = new AtomicBoolean(false);
if (threadPool.getThreadContext().isSystemContext() == false) {
throw new IllegalStateException("template updates from the template upgrade service should always happen in a system context");
}
for (Map.Entry<String, BytesReference> change : changes.entrySet()) {
PutIndexTemplateRequest request = new PutIndexTemplateRequest(change.getKey()).source(change.getValue(), XContentType.JSON);
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
client.admin().indices().putTemplate(request, new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) {
if (response.isAcknowledged() == false) {
anyUpgradeFailed.set(true);
logger.warn("Error updating template [{}], request was not acknowledged", change.getKey());
}
tryFinishUpgrade(anyUpgradeFailed);
}
@Override
public void onFailure(Exception e) {
anyUpgradeFailed.set(true);
logger.warn(new ParameterizedMessage("Error updating template [{}]", change.getKey()), e);
tryFinishUpgrade(anyUpgradeFailed);
}
});
}
for (String template : deletions) {
DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest(template);
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
client.admin().indices().deleteTemplate(request, new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) {
if (response.isAcknowledged() == false) {
anyUpgradeFailed.set(true);
logger.warn("Error deleting template [{}], request was not acknowledged", template);
}
tryFinishUpgrade(anyUpgradeFailed);
}
@Override
public void onFailure(Exception e) {
anyUpgradeFailed.set(true);
if (e instanceof IndexTemplateMissingException == false) {
// we might attempt to delete the same template from different nodes - so that's ok if template doesn't exist
// otherwise we need to warn
logger.warn(new ParameterizedMessage("Error deleting template [{}]", template), e);
}
tryFinishUpgrade(anyUpgradeFailed);
}
});
}
}
Aggregations