Search in sources :

Example 1 with IndexTemplateMissingException

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());
}
Also used : IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException)

Example 2 with IndexTemplateMissingException

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"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException)

Example 3 with IndexTemplateMissingException

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));
        }
    });
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) TimeValue(org.opensearch.common.unit.TimeValue) ValidationException(org.opensearch.common.ValidationException) UncheckedIOException(java.io.UncheckedIOException) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) IOException(java.io.IOException) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException)

Example 4 with IndexTemplateMissingException

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);
            }
        });
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.opensearch.action.admin.indices.template.put.PutIndexTemplateRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Aggregations

IndexTemplateMissingException (org.opensearch.indices.IndexTemplateMissingException)4 IOException (java.io.IOException)2 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)2 ClusterState (org.opensearch.cluster.ClusterState)2 UncheckedIOException (java.io.UncheckedIOException)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)1 PutIndexTemplateRequest (org.opensearch.action.admin.indices.template.put.PutIndexTemplateRequest)1 ClusterStateUpdateTask (org.opensearch.cluster.ClusterStateUpdateTask)1 ValidationException (org.opensearch.common.ValidationException)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)1 TimeValue (org.opensearch.common.unit.TimeValue)1