Java Code Examples for org.elasticsearch.cluster.metadata.MetaData#getIndexSafe()

The following examples show how to use org.elasticsearch.cluster.metadata.MetaData#getIndexSafe() . 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: DiskThresholdDecider.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the expected shard size for the given shard or the default value provided if not enough information are available
 * to estimate the shards size.
 */
public static long getExpectedShardSize(ShardRouting shard,
                                        long defaultValue,
                                        ClusterInfo clusterInfo,
                                        MetaData metaData,
                                        RoutingTable routingTable) {
    final IndexMetaData indexMetaData = metaData.getIndexSafe(shard.index());
    if (indexMetaData.getResizeSourceIndex() != null && shard.active() == false &&
        shard.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
        // in the shrink index case we sum up the source index shards since we basically make a copy of the shard in
        // the worst case
        long targetShardSize = 0;
        final Index mergeSourceIndex = indexMetaData.getResizeSourceIndex();
        final IndexMetaData sourceIndexMeta = metaData.index(mergeSourceIndex);
        if (sourceIndexMeta != null) {
            final Set<ShardId> shardIds = IndexMetaData.selectRecoverFromShards(shard.id(),
                sourceIndexMeta, indexMetaData.getNumberOfShards());
            for (IndexShardRoutingTable shardRoutingTable : routingTable.index(mergeSourceIndex.getName())) {
                if (shardIds.contains(shardRoutingTable.shardId())) {
                    targetShardSize += clusterInfo.getShardSize(shardRoutingTable.primaryShard(), 0);
                }
            }
        }
        return targetShardSize == 0 ? defaultValue : targetShardSize;
    } else {
        return clusterInfo.getShardSize(shard, defaultValue);
    }
}
 
Example 2
Source File: IndexMetaDataUpdater.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the current {@link MetaData} based on the changes of this RoutingChangesObserver. Specifically
 * we update {@link IndexMetaData#getInSyncAllocationIds()} and {@link IndexMetaData#primaryTerm(int)} based on
 * the changes made during this allocation.
 *
 * @param oldMetaData {@link MetaData} object from before the routing nodes was changed.
 * @param newRoutingTable {@link RoutingTable} object after routing changes were applied.
 * @return adapted {@link MetaData}, potentially the original one if no change was needed.
 */
public MetaData applyChanges(MetaData oldMetaData, RoutingTable newRoutingTable) {
    Map<Index, List<Map.Entry<ShardId, Updates>>> changesGroupedByIndex =
        shardChanges.entrySet().stream().collect(Collectors.groupingBy(e -> e.getKey().getIndex()));

    MetaData.Builder metaDataBuilder = null;
    for (Map.Entry<Index, List<Map.Entry<ShardId, Updates>>> indexChanges : changesGroupedByIndex.entrySet()) {
        Index index = indexChanges.getKey();
        final IndexMetaData oldIndexMetaData = oldMetaData.getIndexSafe(index);
        IndexMetaData.Builder indexMetaDataBuilder = null;
        for (Map.Entry<ShardId, Updates> shardEntry : indexChanges.getValue()) {
            ShardId shardId = shardEntry.getKey();
            Updates updates = shardEntry.getValue();
            indexMetaDataBuilder = updateInSyncAllocations(newRoutingTable, oldIndexMetaData, indexMetaDataBuilder, shardId, updates);
            indexMetaDataBuilder = updatePrimaryTerm(oldIndexMetaData, indexMetaDataBuilder, shardId, updates);
        }

        if (indexMetaDataBuilder != null) {
            if (metaDataBuilder == null) {
                metaDataBuilder = MetaData.builder(oldMetaData);
            }
            metaDataBuilder.put(indexMetaDataBuilder);
        }
    }

    if (metaDataBuilder != null) {
        return metaDataBuilder.build();
    } else {
        return oldMetaData;
    }
}
 
Example 3
Source File: GatewayMetaState.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Returns list of {@link IndexMetaDataAction} for each relevant index.
 * For each relevant index there are 3 options:
 * <ol>
 * <li>
 * {@link KeepPreviousGeneration} - index metadata is already stored to disk and index metadata version is not changed, no
 * action is required.
 * </li>
 * <li>
 * {@link WriteNewIndexMetaData} - there is no index metadata on disk and index metadata for this index should be written.
 * </li>
 * <li>
 * {@link WriteChangedIndexMetaData} - index metadata is already on disk, but index metadata version has changed. Updated
 * index metadata should be written to disk.
 * </li>
 * </ol>
 *
 * @param previouslyWrittenIndices A list of indices for which the state was already written before
 * @param relevantIndices          The list of indices for which state should potentially be written
 * @param previousMetaData         The last meta data we know of
 * @param newMetaData              The new metadata
 * @return list of {@link IndexMetaDataAction} for each relevant index.
 */
public static List<IndexMetaDataAction> resolveIndexMetaDataActions(Map<Index, Long> previouslyWrittenIndices,
                                                                    Set<Index> relevantIndices,
                                                                    MetaData previousMetaData,
                                                                    MetaData newMetaData) {
    List<IndexMetaDataAction> actions = new ArrayList<>();
    for (Index index : relevantIndices) {
        IndexMetaData newIndexMetaData = newMetaData.getIndexSafe(index);
        IndexMetaData previousIndexMetaData = previousMetaData == null ? null : previousMetaData.index(index);

        if (previouslyWrittenIndices.containsKey(index) == false || previousIndexMetaData == null) {
            actions.add(new WriteNewIndexMetaData(newIndexMetaData));
        } else if (previousIndexMetaData.getVersion() != newIndexMetaData.getVersion()) {
            actions.add(new WriteChangedIndexMetaData(previousIndexMetaData, newIndexMetaData));
        } else {
            actions.add(new KeepPreviousGeneration(index, previouslyWrittenIndices.get(index)));
        }
    }
    return actions;
}
 
Example 4
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 5
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"
    );
}