Java Code Examples for org.elasticsearch.cluster.block.ClusterBlocks#Builder

The following examples show how to use org.elasticsearch.cluster.block.ClusterBlocks#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClusterStateUpdaters.java    From crate with Apache License 2.0 6 votes vote down vote up
static ClusterState recoverClusterBlocks(final ClusterState state) {
    final ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(state.blocks());

    if (MetaData.SETTING_READ_ONLY_SETTING.get(state.metaData().settings())) {
        blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK);
    }

    if (MetaData.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.get(state.metaData().settings())) {
        blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_ALLOW_DELETE_BLOCK);
    }

    for (final IndexMetaData indexMetaData : state.metaData()) {
        blocks.addBlocks(indexMetaData);
    }

    return ClusterState.builder(state).blocks(blocks).build();
}
 
Example 2
Source File: SwapRelationsOperation.java    From crate with Apache License 2.0 6 votes vote down vote up
private void removeOccurrences(ClusterState state,
                               ClusterBlocks.Builder blocksBuilder,
                               RoutingTable.Builder routingBuilder,
                               MetaData.Builder updatedMetaData,
                               RelationName name) {
    String aliasOrIndexName = name.indexNameOrAlias();
    String templateName = PartitionName.templateName(name.schema(), name.name());
    for (Index index : indexNameResolver.concreteIndices(
        state, IndicesOptions.LENIENT_EXPAND_OPEN, aliasOrIndexName)) {

        String indexName = index.getName();
        routingBuilder.remove(indexName);
        updatedMetaData.remove(indexName);
        blocksBuilder.removeIndexBlocks(indexName);
    }
    updatedMetaData.removeTemplate(templateName);
}
 
Example 3
Source File: TribeService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void addNewIndex(ClusterState tribeState, ClusterBlocks.Builder blocks, MetaData.Builder metaData, RoutingTable.Builder routingTable, IndexMetaData tribeIndex) {
    Settings tribeSettings = Settings.builder().put(tribeIndex.getSettings()).put(TRIBE_NAME, tribeName).build();
    metaData.put(IndexMetaData.builder(tribeIndex).settings(tribeSettings));
    routingTable.add(tribeState.routingTable().index(tribeIndex.getIndex()));
    if (Regex.simpleMatch(blockIndicesMetadata, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_METADATA_BLOCK);
    }
    if (Regex.simpleMatch(blockIndicesRead, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_READ_BLOCK);
    }
    if (Regex.simpleMatch(blockIndicesWrite, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_WRITE_BLOCK);
    }
}
 
Example 4
Source File: MetaDataUpdateSettingsService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the cluster block only iff the setting exists in the given settings
 */
public static void maybeUpdateClusterBlock(String[] actualIndices, ClusterBlocks.Builder blocks, ClusterBlock block, Setting<Boolean> setting, Settings openSettings) {
    if (setting.exists(openSettings)) {
        final boolean updateBlock = setting.get(openSettings);
        for (String index : actualIndices) {
            if (updateBlock) {
                blocks.addIndexBlock(index, block);
            } else {
                blocks.removeIndexBlock(index, block);
            }
        }
    }
}
 
Example 5
Source File: SwapAndDropIndexExecutor.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterState execute(ClusterState currentState, SwapAndDropIndexRequest request) throws Exception {
    final MetaData metaData = currentState.getMetaData();
    final ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    final MetaData.Builder mdBuilder = MetaData.builder(metaData);
    final RoutingTable.Builder routingBuilder = RoutingTable.builder(currentState.routingTable());

    String sourceIndexName = request.source();
    String targetIndexName = request.target();

    mdBuilder.remove(sourceIndexName);
    mdBuilder.remove(targetIndexName);
    routingBuilder.remove(sourceIndexName);
    routingBuilder.remove(targetIndexName);
    blocksBuilder.removeIndexBlocks(sourceIndexName);
    blocksBuilder.removeIndexBlocks(targetIndexName);

    IndexMetaData sourceIndex = metaData.index(sourceIndexName);
    if (sourceIndex == null) {
        throw new IllegalArgumentException("Source index must exist: " + sourceIndexName);
    }

    IndexMetaData newIndexMetaData = IndexMetaData.builder(sourceIndex).index(targetIndexName).build();
    mdBuilder.put(newIndexMetaData, true);
    routingBuilder.addAsFromCloseToOpen(newIndexMetaData);
    blocksBuilder.addBlocks(newIndexMetaData);

    return allocationService.reroute(
        ClusterState.builder(currentState)
            .metaData(mdBuilder)
            .routingTable(routingBuilder.build())
            .blocks(blocksBuilder)
            .build(),
        "swap and drop index"
    );
}
 
Example 6
Source File: TribeService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private void removeIndex(ClusterBlocks.Builder blocks, MetaData.Builder metaData, RoutingTable.Builder routingTable, IndexMetaData index) {
    metaData.remove(index.getIndex());
    routingTable.remove(index.getIndex());
    blocks.removeIndexBlocks(index.getIndex());
}
 
Example 7
Source File: ClusterState.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Builder blocks(ClusterBlocks.Builder blocksBuilder) {
    return blocks(blocksBuilder.build());
}
 
Example 8
Source File: ClusterState.java    From crate with Apache License 2.0 4 votes vote down vote up
public Builder blocks(ClusterBlocks.Builder blocksBuilder) {
    return blocks(blocksBuilder.build());
}
 
Example 9
Source File: SwapRelationsOperation.java    From crate with Apache License 2.0 4 votes vote down vote up
private void addSourceIndicesRenamedToTargetName(ClusterState state,
                                                 MetaData metaData,
                                                 MetaData.Builder updatedMetaData,
                                                 ClusterBlocks.Builder blocksBuilder,
                                                 RoutingTable.Builder routingBuilder,
                                                 RelationName source,
                                                 RelationName target,
                                                 Consumer<String> onProcessedIndex) {
    String sourceTemplateName = PartitionName.templateName(source.schema(), source.name());
    IndexTemplateMetaData sourceTemplate = metaData.templates().get(sourceTemplateName);

    for (Index sourceIndex : indexNameResolver.concreteIndices(
        state, IndicesOptions.LENIENT_EXPAND_OPEN, source.indexNameOrAlias())) {

        String sourceIndexName = sourceIndex.getName();
        IndexMetaData sourceMd = metaData.getIndexSafe(sourceIndex);
        IndexMetaData targetMd;
        if (sourceTemplate == null) {
            targetMd = IndexMetaData.builder(sourceMd)
                .removeAllAliases()
                .index(target.indexNameOrAlias())
                .build();
            onProcessedIndex.accept(target.indexNameOrAlias());
        } else {
            PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
            String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
            targetMd = IndexMetaData.builder(sourceMd)
                .removeAllAliases()
                .putAlias(AliasMetaData.builder(target.indexNameOrAlias()).build())
                .index(targetIndexName)
                .build();
            onProcessedIndex.accept(targetIndexName);
        }
        updatedMetaData.put(targetMd, true);
        blocksBuilder.addBlocks(targetMd);
        routingBuilder.addAsFromCloseToOpen(targetMd);
    }
    if (sourceTemplate != null) {
        IndexTemplateMetaData.Builder templateBuilder = Templates.copyWithNewName(sourceTemplate, target);
        updatedMetaData.put(templateBuilder);
    }
}
 
Example 10
Source File: CloseTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterState execute(ClusterState currentState, OpenCloseTableOrPartitionRequest request) throws Exception {
    Context context = prepare(currentState, request);

    Set<IndexMetaData> indicesToClose = context.indicesMetaData();
    IndexTemplateMetaData templateMetaData = context.templateMetaData();

    if (indicesToClose.isEmpty() && templateMetaData == null) {
        return currentState;
    }

    // Check if index closing conflicts with any running restores
    RestoreService.checkIndexClosing(currentState, indicesToClose);
    // Check if index closing conflicts with any running snapshots
    SnapshotsService.checkIndexClosing(currentState, indicesToClose);

    MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder()
        .blocks(currentState.blocks());
    for (IndexMetaData openIndexMetadata : indicesToClose) {
        final String indexName = openIndexMetadata.getIndex().getName();
        mdBuilder.put(IndexMetaData.builder(openIndexMetadata).state(IndexMetaData.State.CLOSE));
        blocksBuilder.addIndexBlock(indexName, INDEX_CLOSED_BLOCK);
    }

    // mark closed at possible partitioned table template
    if (templateMetaData != null) {
        mdBuilder.put(updateOpenCloseOnPartitionTemplate(templateMetaData, false));
    }

    // The MetaData will always be overridden (and not merged!) when applying it on a cluster state builder.
    // So we must re-build the state with the latest modifications before we pass this state to possible modifiers.
    // Otherwise they would operate on the old MetaData and would just ignore any modifications.
    ClusterState updatedState = ClusterState.builder(currentState).metaData(mdBuilder).blocks(blocksBuilder).build();

    // call possible registered modifiers
    if (context.partitionName() != null) {
        updatedState = ddlClusterStateService.onCloseTablePartition(updatedState, context.partitionName());
    } else {
        updatedState = ddlClusterStateService.onCloseTable(updatedState, request.tableIdent());
    }


    RoutingTable.Builder rtBuilder = RoutingTable.builder(currentState.routingTable());
    for (IndexMetaData index : indicesToClose) {
        rtBuilder.remove(index.getIndex().getName());
    }

    //no explicit wait for other nodes needed as we use AckedClusterStateUpdateTask
    return allocationService.reroute(
        ClusterState.builder(updatedState).routingTable(rtBuilder.build()).build(),
        "indices closed " + indicesToClose);
}
 
Example 11
Source File: OpenTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterState execute(ClusterState currentState, OpenCloseTableOrPartitionRequest request) throws Exception {
    Context context = prepare(currentState, request);
    Set<IndexMetaData> indicesToOpen = context.indicesMetaData();
    IndexTemplateMetaData templateMetaData = context.templateMetaData();

    if (indicesToOpen.isEmpty() && templateMetaData == null) {
        return currentState;
    }

    MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder()
        .blocks(currentState.blocks());
    final Version minIndexCompatibilityVersion = currentState.getNodes().getMaxNodeVersion()
        .minimumIndexCompatibilityVersion();
    for (IndexMetaData closedMetaData : indicesToOpen) {
        final String indexName = closedMetaData.getIndex().getName();
        IndexMetaData indexMetaData = IndexMetaData.builder(closedMetaData).state(IndexMetaData.State.OPEN).build();
        // The index might be closed because we couldn't import it due to old incompatible version
        // We need to check that this index can be upgraded to the current version
        indexMetaData = metaDataIndexUpgradeService.upgradeIndexMetaData(indexMetaData, minIndexCompatibilityVersion);
        try {
            indicesService.verifyIndexMetadata(indexMetaData, indexMetaData);
        } catch (Exception e) {
            throw new ElasticsearchException("Failed to verify index " + indexMetaData.getIndex(), e);
        }

        mdBuilder.put(indexMetaData, true);
        blocksBuilder.removeIndexBlock(indexName, INDEX_CLOSED_BLOCK);
    }

    // remove closed flag at possible partitioned table template
    if (templateMetaData != null) {
        mdBuilder.put(updateOpenCloseOnPartitionTemplate(templateMetaData, true));
    }

    // The MetaData will always be overridden (and not merged!) when applying it on a cluster state builder.
    // So we must re-build the state with the latest modifications before we pass this state to possible modifiers.
    // Otherwise they would operate on the old MetaData and would just ignore any modifications.
    ClusterState updatedState = ClusterState.builder(currentState).metaData(mdBuilder).blocks(blocksBuilder).build();

    // call possible registered modifiers
    if (context.partitionName() != null) {
        updatedState = ddlClusterStateService.onOpenTablePartition(updatedState, context.partitionName());
    } else {
        updatedState = ddlClusterStateService.onOpenTable(updatedState, request.tableIdent());
    }

    RoutingTable.Builder rtBuilder = RoutingTable.builder(updatedState.routingTable());
    for (IndexMetaData index : indicesToOpen) {
        rtBuilder.addAsFromCloseToOpen(updatedState.metaData().getIndexSafe(index.getIndex()));
    }

    //no explicit wait for other nodes needed as we use AckedClusterStateUpdateTask
    return allocationService.reroute(
        ClusterState.builder(updatedState).routingTable(rtBuilder.build()).build(),
        "indices opened " + indicesToOpen);
}
 
Example 12
Source File: RenameTableClusterStateExecutor.java    From crate with Apache License 2.0 4 votes vote down vote up
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"
    );
}