Java Code Examples for org.elasticsearch.cluster.ClusterState#getMetaData()

The following examples show how to use org.elasticsearch.cluster.ClusterState#getMetaData() . 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: InternalClusterInfoService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
static void buildShardLevelInfo(ESLogger logger, ShardStats[] stats, HashMap<String, Long> newShardSizes, HashMap<ShardRouting, String> newShardRoutingToDataPath, ClusterState state) {
    MetaData meta = state.getMetaData();
    for (ShardStats s : stats) {
        IndexMetaData indexMeta = meta.index(s.getShardRouting().index());
        Settings indexSettings = indexMeta == null ? null : indexMeta.getSettings();
        newShardRoutingToDataPath.put(s.getShardRouting(), s.getDataPath());
        long size = s.getStats().getStore().sizeInBytes();
        String sid = ClusterInfo.shardIdentifierFromRouting(s.getShardRouting());
        if (logger.isTraceEnabled()) {
            logger.trace("shard: {} size: {}", sid, size);
        }
        if (indexSettings != null && IndexMetaData.isIndexUsingShadowReplicas(indexSettings)) {
            // Shards on a shared filesystem should be considered of size 0
            if (logger.isTraceEnabled()) {
                logger.trace("shard: {} is using shadow replicas and will be treated as size 0", sid);
            }
            size = 0;
        }
        newShardSizes.put(sid, size);
    }
}
 
Example 2
Source File: DefaultTemplateService.java    From crate with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static ClusterState addDefaultTemplate(ClusterState currentState) throws IOException {
    MetaData currentMetaData = currentState.getMetaData();
    ImmutableOpenMap<String, IndexTemplateMetaData> currentTemplates = currentMetaData.getTemplates();
    ImmutableOpenMap<String, IndexTemplateMetaData> newTemplates = createCopyWithDefaultTemplateAdded(currentTemplates);
    MetaData.Builder mdBuilder = MetaData.builder(currentMetaData).templates(newTemplates);
    return ClusterState.builder(currentState).metaData(mdBuilder).build();
}
 
Example 3
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 4
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"
    );
}