Search in sources :

Example 1 with IndexTemplateMissingException

use of org.elasticsearch.indices.IndexTemplateMissingException in project crate by crate.

the class DropTableTask method execute.

@Override
public void execute(final BatchConsumer consumer, Row parameters) {
    if (tableInfo.isPartitioned()) {
        String templateName = PartitionName.templateName(tableInfo.ident().schema(), tableInfo.ident().name());
        deleteTemplateAction.execute(new DeleteIndexTemplateRequest(templateName), new ActionListener<DeleteIndexTemplateResponse>() {

            @Override
            public void onResponse(DeleteIndexTemplateResponse response) {
                if (!response.isAcknowledged()) {
                    warnNotAcknowledged();
                }
                if (!tableInfo.partitions().isEmpty()) {
                    deleteESIndex(tableInfo.ident().indexName(), consumer);
                } else {
                    consumer.accept(RowsBatchIterator.newInstance(ROW_ONE), null);
                }
            }

            @Override
            public void onFailure(Throwable e) {
                e = ExceptionsHelper.unwrapCause(e);
                if (e instanceof IndexTemplateMissingException && !tableInfo.partitions().isEmpty()) {
                    logger.warn(e.getMessage());
                    deleteESIndex(tableInfo.ident().indexName(), consumer);
                } else {
                    consumer.accept(null, e);
                }
            }
        });
    } else {
        deleteESIndex(tableInfo.ident().indexName(), consumer);
    }
}
Also used : IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)

Example 2 with IndexTemplateMissingException

use of org.elasticsearch.indices.IndexTemplateMissingException in project crate by crate.

the class RenameTableClusterStateExecutor method execute.

public ClusterState execute(ClusterState currentState, RenameTableRequest request) throws Exception {
    RelationName source = request.sourceTableIdent();
    RelationName target = request.targetTableIdent();
    boolean isPartitioned = request.isPartitioned();
    Metadata currentMetadata = currentState.getMetadata();
    Metadata.Builder newMetadata = Metadata.builder(currentMetadata);
    if (isPartitioned) {
        IndexTemplateMetadata indexTemplateMetadata = DDLClusterStateHelpers.templateMetadata(currentMetadata, source);
        if (indexTemplateMetadata == null) {
            throw new IndexTemplateMissingException("Template for partitioned table is missing");
        }
        renameTemplate(newMetadata, indexTemplateMetadata, target);
    }
    RoutingTable.Builder newRoutingTable = RoutingTable.builder(currentState.routingTable());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    logger.info("renaming table '{}' to '{}'", source.fqn(), target.fqn());
    try {
        Index[] sourceIndices = indexNameExpressionResolver.concreteIndices(currentState, STRICT_INDICES_OPTIONS, source.indexNameOrAlias());
        for (Index sourceIndex : sourceIndices) {
            IndexMetadata sourceIndexMetadata = currentMetadata.getIndexSafe(sourceIndex);
            String sourceIndexName = sourceIndex.getName();
            newMetadata.remove(sourceIndexName);
            newRoutingTable.remove(sourceIndexName);
            blocksBuilder.removeIndexBlocks(sourceIndexName);
            IndexMetadata targetMd;
            if (isPartitioned) {
                PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
                String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
                targetMd = IndexMetadata.builder(sourceIndexMetadata).removeAllAliases().putAlias(AliasMetadata.builder(target.indexNameOrAlias()).build()).index(targetIndexName).build();
            } else {
                targetMd = IndexMetadata.builder(sourceIndexMetadata).index(target.indexNameOrAlias()).build();
            }
            newMetadata.put(targetMd, true);
            newRoutingTable.addAsFromCloseToOpen(targetMd);
            blocksBuilder.addBlocks(targetMd);
        }
    } catch (IndexNotFoundException e) {
        if (isPartitioned == false) {
            throw e;
        }
    // empty partition case, no indices, just a template exists
    }
    ClusterState clusterStateAfterRename = ClusterState.builder(currentState).metadata(newMetadata).routingTable(newRoutingTable.build()).blocks(blocksBuilder).build();
    return allocationService.reroute(ddlClusterStateService.onRenameTable(clusterStateAfterRename, source, target, request.isPartitioned()), "rename-table");
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) Index(org.elasticsearch.index.Index) PartitionName(io.crate.metadata.PartitionName) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RelationName(io.crate.metadata.RelationName) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata)

Example 3 with IndexTemplateMissingException

use of org.elasticsearch.indices.IndexTemplateMissingException in project elasticsearch by elastic.

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.elasticsearch.indices.IndexTemplateMissingException)

Example 4 with IndexTemplateMissingException

use of org.elasticsearch.indices.IndexTemplateMissingException in project crate by crate.

the class TemplateUpgradeService method upgradeTemplates.

void upgradeTemplates(Map<String, BytesReference> changes, Set<String> deletions) {
    final AtomicBoolean anyUpgradeFailed = new AtomicBoolean(false);
    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.elasticsearch.common.bytes.BytesReference) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) IOException(java.io.IOException) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Aggregations

IndexTemplateMissingException (org.elasticsearch.indices.IndexTemplateMissingException)4 DeleteIndexTemplateRequest (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)2 PartitionName (io.crate.metadata.PartitionName)1 RelationName (io.crate.metadata.RelationName)1 IOException (java.io.IOException)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 DeleteIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse)1 PutIndexTemplateRequest (org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest)1 AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 ClusterBlocks (org.elasticsearch.cluster.block.ClusterBlocks)1 AliasMetadata (org.elasticsearch.cluster.metadata.AliasMetadata)1 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)1 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)1 Metadata (org.elasticsearch.cluster.metadata.Metadata)1 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)1