Search in sources :

Example 1 with MasterNotDiscoveredException

use of org.elasticsearch.discovery.MasterNotDiscoveredException in project elasticsearch by elastic.

the class LocalAllocateDangledIndices method allocateDangled.

public void allocateDangled(Collection<IndexMetaData> indices, final Listener listener) {
    ClusterState clusterState = clusterService.state();
    DiscoveryNode masterNode = clusterState.nodes().getMasterNode();
    if (masterNode == null) {
        listener.onFailure(new MasterNotDiscoveredException("no master to send allocate dangled request"));
        return;
    }
    AllocateDangledRequest request = new AllocateDangledRequest(clusterService.localNode(), indices.toArray(new IndexMetaData[indices.size()]));
    transportService.sendRequest(masterNode, ACTION_NAME, request, new TransportResponseHandler<AllocateDangledResponse>() {

        @Override
        public AllocateDangledResponse newInstance() {
            return new AllocateDangledResponse();
        }

        @Override
        public void handleResponse(AllocateDangledResponse response) {
            listener.onResponse(response);
        }

        @Override
        public void handleException(TransportException exp) {
            listener.onFailure(exp);
        }

        @Override
        public String executor() {
            return ThreadPool.Names.SAME;
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) TransportException(org.elasticsearch.transport.TransportException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 2 with MasterNotDiscoveredException

use of org.elasticsearch.discovery.MasterNotDiscoveredException in project MSEC by Tencent.

the class ESHelper method waitForClusterReady.

public void waitForClusterReady(final TransportClient client, ArrayList<String> ips, final ClusterHealthStatus status) throws IOException {
    Logger logger = Logger.getLogger(ESHelper.class);
    int timeout = 60;
    int node_num = 0;
    long begin = System.currentTimeMillis() / 1000L;
    long end = begin;
    Set<String> done_ips = new HashSet<>();
    try {
        logger.info("waiting for cluster state: " + status.name());
        ClusterHealthResponse healthResponse = null;
        while (true) {
            try {
                healthResponse = client.admin().cluster().prepareHealth().setWaitForStatus(status).setTimeout(TimeValue.timeValueSeconds(5)).execute().actionGet();
            } catch (NoNodeAvailableException | MasterNotDiscoveredException ex) {
                end = System.currentTimeMillis() / 1000L;
                if (end - begin >= timeout)
                    throw new IOException("Server start timeout");
                logger.info("server still starting/discovering, retry...");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                }
                continue;
            }
            if (healthResponse != null && healthResponse.isTimedOut()) {
                end = System.currentTimeMillis() / 1000L;
                if (//timeout
                end - begin >= timeout)
                    throw new IOException("cluster not ready, current state is " + healthResponse.getStatus().name());
                continue;
            } else {
                logger.info("cluster state ok");
                int new_node_num = healthResponse.getNumberOfNodes();
                if (new_node_num > node_num) {
                    node_num = new_node_num;
                    NodesInfoResponse nodeInfos = client.admin().cluster().prepareNodesInfo().all().get();
                    for (NodeInfo node : nodeInfos.getNodes()) {
                        if (!done_ips.contains(node.getHostname()) && ips.contains(node.getHostname())) {
                            updateStatus(node.getHostname(), "Done.");
                            done_ips.add(node.getHostname());
                        }
                    }
                    if (done_ips.size() == ips.size())
                        break;
                    end = System.currentTimeMillis() / 1000L;
                    if (//timeout
                    end - begin >= timeout)
                        break;
                }
            }
        }
    } catch (final ElasticsearchTimeoutException e) {
        throw new IOException("ES API timeout");
    }
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) IOException(java.io.IOException) Logger(org.apache.log4j.Logger) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo)

Aggregations

MasterNotDiscoveredException (org.elasticsearch.discovery.MasterNotDiscoveredException)2 IOException (java.io.IOException)1 Logger (org.apache.log4j.Logger)1 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)1 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)1 NodeInfo (org.elasticsearch.action.admin.cluster.node.info.NodeInfo)1 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)1 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 TransportException (org.elasticsearch.transport.TransportException)1