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

The following examples show how to use org.elasticsearch.cluster.ClusterState#routingTable() . 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: 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 2
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 3
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 4
Source File: ESIntegTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that all shards are allocated on nodes matching the given node pattern.
 */
public Set<String> assertAllShardsOnNodes(String index, String... pattern) {
    Set<String> nodes = new HashSet<>();
    ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
    for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
        for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
            for (ShardRouting shardRouting : indexShardRoutingTable) {
                if (shardRouting.currentNodeId() != null && index.equals(shardRouting.getIndexName())) {
                    String name = clusterState.nodes().get(shardRouting.currentNodeId()).getName();
                    nodes.add(name);
                    assertThat("Allocated on new node: " + name, Regex.simpleMatch(pattern, name), is(true));
                }
            }
        }
    }
    return nodes;
}
 
Example 5
Source File: TransportUpgradeAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Finds all indices that have not all primaries available
 */
private Set<String> indicesWithMissingPrimaries(ClusterState clusterState, String[] concreteIndices) {
    Set<String> indices = newHashSet();
    RoutingTable routingTable = clusterState.routingTable();
    for (String index : concreteIndices) {
        IndexRoutingTable indexRoutingTable = routingTable.index(index);
        if (indexRoutingTable.allPrimaryShardsActive() == false) {
            indices.add(index);
        }
    }
    return indices;
}
 
Example 6
Source File: TransportIndicesShardStoresAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void masterOperation(IndicesShardStoresRequest request, ClusterState state, ActionListener<IndicesShardStoresResponse> listener) {
    final RoutingTable routingTables = state.routingTable();
    final RoutingNodes routingNodes = state.getRoutingNodes();
    final String[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request);
    final Set<ShardId> shardIdsToFetch = new HashSet<>();

    logger.trace("using cluster state version [{}] to determine shards", state.version());
    // collect relevant shard ids of the requested indices for fetching store infos
    for (String index : concreteIndices) {
        IndexRoutingTable indexShardRoutingTables = routingTables.index(index);
        if (indexShardRoutingTables == null) {
            continue;
        }
        for (IndexShardRoutingTable routing : indexShardRoutingTables) {
            ClusterShardHealth shardHealth = new ClusterShardHealth(routing.shardId().id(), routing);
            if (request.shardStatuses().contains(shardHealth.getStatus())) {
                shardIdsToFetch.add(routing.shardId());
            }
        }
    }

    // async fetch store infos from all the nodes
    // NOTE: instead of fetching shard store info one by one from every node (nShards * nNodes requests)
    // we could fetch all shard store info from every node once (nNodes requests)
    // we have to implement a TransportNodesAction instead of using TransportNodesListGatewayStartedShards
    // for fetching shard stores info, that operates on a list of shards instead of a single shard
    new AsyncShardStoresInfoFetches(state.nodes(), routingNodes, state.metaData(), shardIdsToFetch, listener).start();
}
 
Example 7
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 8
Source File: TransportUpgradeAction.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Finds all indices that have not all primaries available
 */
private Set<String> indicesWithMissingPrimaries(ClusterState clusterState, String[] concreteIndices) {
    Set<String> indices = new HashSet<>();
    RoutingTable routingTable = clusterState.routingTable();
    for (String index : concreteIndices) {
        IndexRoutingTable indexRoutingTable = routingTable.index(index);
        if (indexRoutingTable.allPrimaryShardsActive() == false) {
            indices.add(index);
        }
    }
    return indices;
}