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

The following examples show how to use org.elasticsearch.cluster.ClusterState#metaData() . 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: GatewayMetaState.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static Set<String> getRelevantIndicesOnDataOnlyNode(ClusterState state, ClusterState previousState, ImmutableSet<String> previouslyWrittenIndices) {
    RoutingNode newRoutingNode = state.getRoutingNodes().node(state.nodes().localNodeId());
    if (newRoutingNode == null) {
        throw new IllegalStateException("cluster state does not contain this node - cannot write index meta state");
    }
    Set<String> indices = new HashSet<>();
    for (ShardRouting routing : newRoutingNode) {
        indices.add(routing.index());
    }
    // we have to check the meta data also: closed indices will not appear in the routing table, but we must still write the state if we have it written on disk previously
    for (IndexMetaData indexMetaData : state.metaData()) {
        boolean isOrWasClosed = indexMetaData.getState().equals(IndexMetaData.State.CLOSE);
        // if the index is open we might still have to write the state if it just transitioned from closed to open
        // so we have to check for that as well.
        IndexMetaData previousMetaData = previousState.metaData().getIndices().get(indexMetaData.getIndex());
        if (previousMetaData != null) {
            isOrWasClosed = isOrWasClosed || previousMetaData.getState().equals(IndexMetaData.State.CLOSE);
        }
        if (previouslyWrittenIndices.contains(indexMetaData.getIndex()) && isOrWasClosed) {
            indices.add(indexMetaData.getIndex());
        }
    }
    return indices;
}
 
Example 2
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 3
Source File: GatewayMetaState.java    From crate with Apache License 2.0 6 votes vote down vote up
private Map<Index, Long> writeIndicesMetadata(AtomicClusterStateWriter writer, ClusterState newState, ClusterState previousState)
        throws WriteStateException {
    Map<Index, Long> previouslyWrittenIndices = previousManifest.getIndexGenerations();
    Set<Index> relevantIndices = getRelevantIndices(newState, previousState, previouslyWrittenIndices.keySet());

    Map<Index, Long> newIndices = new HashMap<>();

    MetaData previousMetaData = incrementalWrite ? previousState.metaData() : null;
    Iterable<IndexMetaDataAction> actions = resolveIndexMetaDataActions(previouslyWrittenIndices, relevantIndices, previousMetaData,
            newState.metaData());

    for (IndexMetaDataAction action : actions) {
        long generation = action.execute(writer);
        newIndices.put(action.getIndex(), generation);
    }

    return newIndices;
}
 
Example 4
Source File: GatewayMetaState.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Set<Index> getRelevantIndicesOnDataOnlyNode(ClusterState state, ClusterState previousState, Set<Index>
        previouslyWrittenIndices) {
    RoutingNode newRoutingNode = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
    if (newRoutingNode == null) {
        throw new IllegalStateException("cluster state does not contain this node - cannot write index meta state");
    }
    Set<Index> indices = new HashSet<>();
    for (ShardRouting routing : newRoutingNode) {
        indices.add(routing.index());
    }
    // we have to check the meta data also: closed indices will not appear in the routing table, but we must still write the state if
    // we have it written on disk previously
    for (IndexMetaData indexMetaData : state.metaData()) {
        boolean isOrWasClosed = indexMetaData.getState().equals(IndexMetaData.State.CLOSE);
        // if the index is open we might still have to write the state if it just transitioned from closed to open
        // so we have to check for that as well.
        IndexMetaData previousMetaData = previousState.metaData().index(indexMetaData.getIndex());
        if (previousMetaData != null) {
            isOrWasClosed = isOrWasClosed || previousMetaData.getState().equals(IndexMetaData.State.CLOSE);
        }
        if (previouslyWrittenIndices.contains(indexMetaData.getIndex()) && isOrWasClosed) {
            indices.add(indexMetaData.getIndex());
        }
    }
    return indices;
}
 
Example 5
Source File: UnassignedInfo.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the next (closest) delay expiration of an delayed shard in nanoseconds based on current time.
 * Returns 0 if delay is negative.
 * Returns -1 if no delayed shard is found.
 */
public static long findNextDelayedAllocation(long currentNanoTime, ClusterState state) {
    MetaData metaData = state.metaData();
    RoutingTable routingTable = state.routingTable();
    long nextDelayNanos = Long.MAX_VALUE;
    for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.UNASSIGNED)) {
        UnassignedInfo unassignedInfo = shard.unassignedInfo();
        if (unassignedInfo.isDelayed()) {
            Settings indexSettings = metaData.index(shard.index()).getSettings();
            // calculate next time to schedule
            final long newComputedLeftDelayNanos = unassignedInfo.getRemainingDelay(currentNanoTime, indexSettings);
            if (newComputedLeftDelayNanos < nextDelayNanos) {
                nextDelayNanos = newComputedLeftDelayNanos;
            }
        }
    }
    return nextDelayNanos == Long.MAX_VALUE ? -1L : nextDelayNanos;
}
 
Example 6
Source File: MasterService.java    From crate with Apache License 2.0 6 votes vote down vote up
private ClusterState patchVersions(ClusterState previousClusterState, ClusterTasksResult<?> executionResult) {
    ClusterState newClusterState = executionResult.resultingState;

    if (previousClusterState != newClusterState) {
        // only the master controls the version numbers
        Builder builder = incrementVersion(newClusterState);
        if (previousClusterState.routingTable() != newClusterState.routingTable()) {
            builder.routingTable(RoutingTable.builder(newClusterState.routingTable())
                .version(newClusterState.routingTable().version() + 1).build());
        }
        if (previousClusterState.metaData() != newClusterState.metaData()) {
            builder.metaData(MetaData.builder(newClusterState.metaData()).version(newClusterState.metaData().version() + 1));
        }

        newClusterState = builder.build();
    }

    return newClusterState;
}
 
Example 7
Source File: PrimaryTermsTests.java    From crate with Apache License 2.0 6 votes vote down vote up
private void applyRerouteResult(ClusterState newClusterState) {
    ClusterState previousClusterState = this.clusterState;
    ClusterState.Builder builder = ClusterState.builder(newClusterState).incrementVersion();
    if (previousClusterState.routingTable() != newClusterState.routingTable()) {
        builder.routingTable(RoutingTable.builder(newClusterState.routingTable()).version(newClusterState.routingTable().version() + 1)
                                 .build());
    }
    if (previousClusterState.metaData() != newClusterState.metaData()) {
        builder.metaData(MetaData.builder(newClusterState.metaData()).version(newClusterState.metaData().version() + 1));
    }
    this.clusterState = builder.build();
    final ClusterStateHealth clusterHealth = new ClusterStateHealth(clusterState);
    logger.info("applied reroute. active shards: p [{}], t [{}], init shards: [{}], relocating: [{}]",
                clusterHealth.getActivePrimaryShards(), clusterHealth.getActiveShards(),
                clusterHealth.getInitializingShards(), clusterHealth.getRelocatingShards());
}
 
Example 8
Source File: GatewayMetaState.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Set<String> getRelevantIndicesForMasterEligibleNode(ClusterState state) {
    Set<String> relevantIndices;
    relevantIndices = new HashSet<>();
    // we have to iterate over the metadata to make sure we also capture closed indices
    for (IndexMetaData indexMetaData : state.metaData()) {
        relevantIndices.add(indexMetaData.getIndex());
    }
    return relevantIndices;
}
 
Example 9
Source File: GatewayMetaState.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Updates manifest and meta data on disk.
 *
 * @param newState new {@link ClusterState}
 * @param previousState previous {@link ClusterState}
 *
 * @throws WriteStateException if exception occurs. See also {@link WriteStateException#isDirty()}.
 */
protected void updateClusterState(ClusterState newState, ClusterState previousState)
        throws WriteStateException {
    MetaData newMetaData = newState.metaData();

    final AtomicClusterStateWriter writer = new AtomicClusterStateWriter(metaStateService, previousManifest);
    long globalStateGeneration = writeGlobalState(writer, newMetaData);
    Map<Index, Long> indexGenerations = writeIndicesMetadata(writer, newState, previousState);
    Manifest manifest = new Manifest(previousManifest.getCurrentTerm(), newState.version(), globalStateGeneration, indexGenerations);
    writeManifest(writer, manifest);

    previousManifest = manifest;
    previousClusterState = newState;
}
 
Example 10
Source File: GatewayMetaState.java    From crate with Apache License 2.0 5 votes vote down vote up
private static Set<Index> getRelevantIndicesForMasterEligibleNode(ClusterState state) {
    Set<Index> relevantIndices;
    relevantIndices = new HashSet<>();
    // we have to iterate over the metadata to make sure we also capture closed indices
    for (IndexMetaData indexMetaData : state.metaData()) {
        relevantIndices.add(indexMetaData.getIndex());
    }
    return relevantIndices;
}
 
Example 11
Source File: RoutingAllocation.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link RoutingAllocation}
 *  @param deciders {@link AllocationDeciders} to used to make decisions for routing allocations
 * @param routingNodes Routing nodes in the current cluster
 * @param clusterState cluster state before rerouting
 * @param currentNanoTime the nano time to use for all delay allocation calculation (typically {@link System#nanoTime()})
 */
public RoutingAllocation(AllocationDeciders deciders, RoutingNodes routingNodes, ClusterState clusterState, ClusterInfo clusterInfo,
                         long currentNanoTime) {
    this.deciders = deciders;
    this.routingNodes = routingNodes;
    this.metaData = clusterState.metaData();
    this.routingTable = clusterState.routingTable();
    this.nodes = clusterState.nodes();
    this.customs = clusterState.customs();
    this.clusterInfo = clusterInfo;
    this.currentNanoTime = currentNanoTime;
}
 
Example 12
Source File: AbstractOpenCloseTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 5 votes vote down vote up
protected Context prepare(ClusterState currentState, OpenCloseTableOrPartitionRequest request) {
    RelationName relationName = request.tableIdent();
    String partitionIndexName = request.partitionIndexName();
    MetaData metaData = currentState.metaData();
    String indexToResolve = partitionIndexName != null ? partitionIndexName : relationName.indexNameOrAlias();
    PartitionName partitionName = partitionIndexName != null ? PartitionName.fromIndexOrTemplate(partitionIndexName) : null;
    String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(
        currentState, IndicesOptions.lenientExpandOpen(), indexToResolve);
    Set<IndexMetaData> indicesMetaData = DDLClusterStateHelpers.indexMetaDataSetFromIndexNames(metaData, concreteIndices, indexState());
    IndexTemplateMetaData indexTemplateMetaData = null;
    if (partitionIndexName == null) {
        indexTemplateMetaData = DDLClusterStateHelpers.templateMetaData(metaData, relationName);
    }
    return new Context(indicesMetaData, indexTemplateMetaData, partitionName);
}
 
Example 13
Source File: DocTableInfoBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
DocTableInfoBuilder(Functions functions,
                    RelationName ident,
                    ClusterState state,
                    IndexNameExpressionResolver indexNameExpressionResolver) {
    this.functions = functions;
    this.indexNameExpressionResolver = indexNameExpressionResolver;
    this.ident = ident;
    this.state = state;
    this.metaData = state.metaData();
}
 
Example 14
Source File: UserManagerDDLModifier.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterState onRenameTable(ClusterState currentState,
                                  RelationName sourceRelationName,
                                  RelationName targetRelationName,
                                  boolean isPartitionedTable) {
    MetaData currentMetaData = currentState.metaData();
    MetaData.Builder mdBuilder = MetaData.builder(currentMetaData);
    if (transferTablePrivileges(mdBuilder, sourceRelationName, targetRelationName)) {
        return ClusterState.builder(currentState).metaData(mdBuilder).build();
    }
    return currentState;
}
 
Example 15
Source File: UserManagerDDLModifier.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterState onSwapRelations(ClusterState currentState, RelationName source, RelationName target) {
    MetaData currentMetaData = currentState.metaData();
    UsersPrivilegesMetaData userPrivileges = currentMetaData.custom(UsersPrivilegesMetaData.TYPE);
    if (userPrivileges == null) {
        return currentState;
    }
    UsersPrivilegesMetaData updatedPrivileges = UsersPrivilegesMetaData.swapPrivileges(userPrivileges, source, target);
    return ClusterState.builder(currentState)
        .metaData(MetaData.builder(currentMetaData)
            .putCustom(UsersPrivilegesMetaData.TYPE, updatedPrivileges)
            .build())
        .build();
}
 
Example 16
Source File: UserManagerDDLModifier.java    From crate with Apache License 2.0 5 votes vote down vote up
private ClusterState dropPrivilegesForTableOrView(ClusterState currentState, RelationName relationName) {
    MetaData currentMetaData = currentState.metaData();
    MetaData.Builder mdBuilder = MetaData.builder(currentMetaData);

    if (dropPrivileges(mdBuilder, relationName) == false) {
        // if nothing is affected, don't modify the state and just return the given currentState
        return currentState;
    }

    return ClusterState.builder(currentState).metaData(mdBuilder).build();
}