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

The following examples show how to use org.elasticsearch.cluster.ClusterState#nodes() . 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: NodesFaultDetection.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * make sure that nodes in clusterState are pinged. Any pinging to nodes which are not
 * part of the cluster will be stopped
 */
public void updateNodesAndPing(ClusterState clusterState) {
    // remove any nodes we don't need, this will cause their FD to stop
    for (DiscoveryNode monitoredNode : nodesFD.keySet()) {
        if (!clusterState.nodes().nodeExists(monitoredNode.id())) {
            nodesFD.remove(monitoredNode);
        }
    }
    // add any missing nodes

    for (DiscoveryNode node : clusterState.nodes()) {
        if (node.equals(localNode)) {
            // no need to monitor the local node
            continue;
        }
        if (!nodesFD.containsKey(node)) {
            NodeFD fd = new NodeFD(node);
            // it's OK to overwrite an existing nodeFD - it will just stop and the new one will pick things up.
            nodesFD.put(node, fd);
            // we use schedule with a 0 time value to run the pinger on the pool as it will run on later
            threadPool.schedule(TimeValue.timeValueMillis(0), ThreadPool.Names.SAME, fd);
        }
    }
}
 
Example 2
Source File: AllocationService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public RoutingAllocation.Result reroute(ClusterState clusterState, AllocationCommands commands, boolean explain) {
    RoutingNodes routingNodes = getMutableRoutingNodes(clusterState);
    // we don't shuffle the unassigned shards here, to try and get as close as possible to
    // a consistent result of the effect the commands have on the routing
    // this allows systems to dry run the commands, see the resulting cluster state, and act on it
    RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, clusterState.nodes(), clusterInfoService.getClusterInfo(), currentNanoTime());
    // don't short circuit deciders, we want a full explanation
    allocation.debugDecision(true);
    // we ignore disable allocation, because commands are explicit
    allocation.ignoreDisable(true);
    RoutingExplanations explanations = commands.execute(allocation, explain);
    // we revert the ignore disable flag, since when rerouting, we want the original setting to take place
    allocation.ignoreDisable(false);
    // the assumption is that commands will move / act on shards (or fail through exceptions)
    // so, there will always be shard "movements", so no need to check on reroute
    reroute(allocation);
    RoutingTable routingTable = new RoutingTable.Builder().updateNodes(routingNodes).build().validateRaiseException(clusterState.metaData());
    RoutingAllocation.Result result = new RoutingAllocation.Result(true, routingTable, explanations);
    logClusterHealthStateChange(
            new ClusterStateHealth(clusterState),
            new ClusterStateHealth(clusterState.getMetaData(), routingTable),
            "reroute commands"
    );
    return result;
}
 
Example 3
Source File: NodeIndexDeletedAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void nodeIndexDeleted(final ClusterState clusterState, final String index, final Settings indexSettings, final String nodeId) {
    final DiscoveryNodes nodes = clusterState.nodes();
    transportService.sendRequest(clusterState.nodes().masterNode(),
            INDEX_DELETED_ACTION_NAME, new NodeIndexDeletedMessage(index, nodeId), EmptyTransportResponseHandler.INSTANCE_SAME);
    if (nodes.localNode().isDataNode() == false) {
        logger.trace("[{}] not acking store deletion (not a data node)", index);
        return;
    }
    threadPool.generic().execute(new AbstractRunnable() {
        @Override
        public void onFailure(Throwable t) {
            logger.warn("[{}] failed to ack index store deleted for index", t, index);
        }

        @Override
        protected void doRun() throws Exception {
            lockIndexAndAck(index, nodes, nodeId, clusterState, indexSettings);
        }
    });
}
 
Example 4
Source File: TransportClearScrollAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private Async(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener, ClusterState clusterState) {
    int expectedOps = 0;
    this.nodes = clusterState.nodes();
    if (request.getScrollIds().size() == 1 && "_all".equals(request.getScrollIds().get(0))) {
        expectedOps = nodes.size();
    } else {
        for (String parsedScrollId : request.getScrollIds()) {
            ScrollIdForNode[] context = parseScrollId(parsedScrollId).getContext();
            expectedOps += context.length;
            this.contexts.add(context);
        }
    }

    this.request = request;
    this.listener = listener;
    this.expHolder = new AtomicReference<>();
    this.expectedOps = new CountDown(expectedOps);
}
 
Example 5
Source File: TransportCreatePartitionsAction.java    From crate with Apache License 2.0 6 votes vote down vote up
private Settings createIndexSettings(ClusterState currentState, List<IndexTemplateMetaData> templates) {
    Settings.Builder indexSettingsBuilder = Settings.builder();
    // apply templates, here, in reverse order, since first ones are better matching
    for (int i = templates.size() - 1; i >= 0; i--) {
        indexSettingsBuilder.put(templates.get(i).settings());
    }
    if (indexSettingsBuilder.get(IndexMetaData.SETTING_VERSION_CREATED) == null) {
        DiscoveryNodes nodes = currentState.nodes();
        final Version createdVersion = Version.min(Version.CURRENT, nodes.getSmallestNonClientNodeVersion());
        indexSettingsBuilder.put(IndexMetaData.SETTING_VERSION_CREATED, createdVersion);
    }
    if (indexSettingsBuilder.get(IndexMetaData.SETTING_CREATION_DATE) == null) {
        indexSettingsBuilder.put(IndexMetaData.SETTING_CREATION_DATE, new DateTime(DateTimeZone.UTC).getMillis());
    }
    indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID());

    return indexSettingsBuilder.build();
}
 
Example 6
Source File: DiscoveryNodeFilterer.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
/**
 * Find nodes that are elibile to be used by us.  For example, Ultrawarm
 *  introduces warm nodes into the ES cluster. Currently, we distribute
 *  model partitions to all data nodes in the cluster randomly, which
 *  could cause a model performance downgrade issue once warm nodes
 *  are throttled due to resource limitations. The PR excludes warm nodes
 *  to place model partitions.
 * @return an array of eligible data nodes
 */
public DiscoveryNode[] getEligibleDataNodes() {
    ClusterState state = this.clusterService.state();
    final List<DiscoveryNode> eligibleNodes = new ArrayList<>();
    for (DiscoveryNode node : state.nodes()) {
        if (eligibleNodeFilter.test(node)) {
            eligibleNodes.add(node);
        }
    }
    return eligibleNodes.toArray(new DiscoveryNode[0]);
}
 
Example 7
Source File: NodeMappingRefreshAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void nodeMappingRefresh(final ClusterState state, final NodeMappingRefreshRequest request) {
    final DiscoveryNodes nodes = state.nodes();
    if (nodes.masterNode() == null) {
        logger.warn("can't send mapping refresh for [{}][{}], no master known.", request.index(), Strings.arrayToCommaDelimitedString(request.types()));
        return;
    }
    transportService.sendRequest(nodes.masterNode(), ACTION_NAME, request, EmptyTransportResponseHandler.INSTANCE_SAME);
}
 
Example 8
Source File: TransportSingleShardAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private AsyncSingleAction(Request request, ActionListener<Response> listener) {
    this.listener = listener;

    ClusterState clusterState = clusterService.state();
    if (logger.isTraceEnabled()) {
        logger.trace("executing [{}] based on cluster state version [{}]", request, clusterState.version());
    }
    nodes = clusterState.nodes();
    ClusterBlockException blockException = checkGlobalBlock(clusterState);
    if (blockException != null) {
        throw blockException;
    }

    String concreteSingleIndex;
    if (resolveIndex(request)) {
        concreteSingleIndex = indexNameExpressionResolver.concreteSingleIndex(clusterState, request);
    } else {
        concreteSingleIndex = request.index();
    }
    this.internalRequest = new InternalRequest(request, concreteSingleIndex);
    resolveRequest(clusterState, internalRequest);

    blockException = checkRequestBlock(clusterState, internalRequest);
    if (blockException != null) {
        throw blockException;
    }

    this.shardIt = shards(clusterState, internalRequest);
}
 
Example 9
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 10
Source File: AddVotingConfigExclusionsRequest.java    From crate with Apache License 2.0 5 votes vote down vote up
Set<VotingConfigExclusion> resolveVotingConfigExclusions(ClusterState currentState) {
    final DiscoveryNodes allNodes = currentState.nodes();
    final Set<VotingConfigExclusion> resolvedNodes = Arrays.stream(allNodes.resolveNodes(nodeDescriptions))
            .map(allNodes::get).filter(DiscoveryNode::isMasterNode).map(VotingConfigExclusion::new).collect(Collectors.toSet());

    if (resolvedNodes.isEmpty()) {
        throw new IllegalArgumentException("add voting config exclusions request for " + Arrays.asList(nodeDescriptions)
            + " matched no master-eligible nodes");
    }

    resolvedNodes.removeIf(n -> currentState.getVotingConfigExclusions().contains(n));
    return resolvedNodes;
}
 
Example 11
Source File: DefaultTemplateService.java    From crate with Apache License 2.0 5 votes vote down vote up
void createIfNotExists(ClusterState state) {
    DiscoveryNodes nodes = state.nodes();
    if (Objects.equals(nodes.getMasterNodeId(), nodes.getLocalNodeId()) == false) {
        return;
    }
    if (state.getMetaData().getTemplates().containsKey(TEMPLATE_NAME) == false) {
        createDefaultTemplate();
    }
}
 
Example 12
Source File: TransportMasterNodeAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
protected void doStart() {
    final ClusterState clusterState = observer.observedState();
    final DiscoveryNodes nodes = clusterState.nodes();
    if (nodes.localNodeMaster() || localExecute(request)) {
        // check for block, if blocked, retry, else, execute locally
        final ClusterBlockException blockException = checkBlock(request, clusterState);
        if (blockException != null) {
            if (!blockException.retryable()) {
                listener.onFailure(blockException);
            } else {
                logger.trace("can't execute due to a cluster block, retrying", blockException);
                retry(blockException, retryableOrNoBlockPredicate);
            }
        } else {
            final ActionListener<Response> delegate = new ActionListener<Response>() {
                @Override
                public void onResponse(Response response) {
                    listener.onResponse(response);
                }

                @Override
                public void onFailure(Throwable t) {
                    if (t instanceof NotMasterException) {
                        logger.debug("master could not publish cluster state or stepped down before publishing action [{}], scheduling a retry", t, actionName);
                        retry(t, masterNodeChangedPredicate);
                    } else {
                        listener.onFailure(t);
                    }
                }
            };
            taskManager.registerChildTask(task, nodes.getLocalNodeId());
            threadPool.executor(executor).execute(new ActionRunnable(delegate) {
                @Override
                protected void doRun() throws Exception {
                    masterOperation(task, request, clusterService.state(), delegate);
                }
            });
        }
    } else {
        if (nodes.masterNode() == null) {
            logger.debug("no known master node, scheduling a retry");
            retry(null, masterNodeChangedPredicate);
        } else {
            taskManager.registerChildTask(task, nodes.masterNode().getId());
            transportService.sendRequest(nodes.masterNode(), actionName, request, new ActionListenerResponseHandler<Response>(listener) {
                @Override
                public Response newInstance() {
                    return newResponse();
                }

                @Override
                public void handleException(final TransportException exp) {
                    Throwable cause = exp.unwrapCause();
                    if (cause instanceof ConnectTransportException) {
                        // we want to retry here a bit to see if a new master is elected
                        logger.debug("connection exception while trying to forward request with action name [{}] to master node [{}], scheduling a retry. Error: [{}]",
                                actionName, nodes.masterNode(), exp.getDetailedMessage());
                        retry(cause, masterNodeChangedPredicate);
                    } else {
                        listener.onFailure(exp);
                    }
                }
            });
        }
    }
}