Search in sources :

Example 1 with CreateRequest

use of org.codelibs.elasticsearch.idxproxy.action.CreateRequest in project elasticsearch-indexing-proxy by codelibs.

the class IndexingProxyService method renewOnRemote.

private <Request extends ActionRequest, Response extends ActionResponse> void renewOnRemote(final String nodeName, final long version, final ActionListener<Response> listener, final int tryCount) {
    final List<DiscoveryNode> nodeList = new ArrayList<>();
    clusterService.state().nodes().getNodes().valuesIt().forEachRemaining(node -> {
        if (writerNodes.isEmpty() || writerNodes.contains(node.getName())) {
            nodeList.add(node);
        }
    });
    int pos = -1;
    for (int i = 0; i < nodeList.size(); i++) {
        if (nodeList.get(i).getName().equals(nodeName)) {
            pos = i;
            break;
        }
    }
    if (pos == -1) {
        if (tryCount >= writerRetryCount) {
            listener.onFailure(new ElasticsearchException("Writer nodes are not found for renew."));
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("No available write node.");
            }
            updateWriterNode(version, nodeList, (res, ex) -> renew(listener, tryCount + 1));
        }
        return;
    }
    final int nodeIdx = pos;
    transportService.sendRequest(nodeList.get(nodeIdx), IndexingProxyPlugin.ACTION_IDXPROXY_CREATE, new CreateRequest(), new TransportResponseHandler<CreateResponse>() {

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

        @Override
        public void handleResponse(final CreateResponse response) {
            if (response.isAcknowledged()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Update file_id in " + nodeName);
                }
                listener.onResponse(null);
            } else {
                throw new ElasticsearchException("Failed to update file_id in " + nodeName);
            }
        }

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

        @Override
        public String executor() {
            return ThreadPool.Names.GENERIC;
        }
    });
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) CreateRequest(org.codelibs.elasticsearch.idxproxy.action.CreateRequest) CreateResponse(org.codelibs.elasticsearch.idxproxy.action.CreateResponse) ArrayList(java.util.ArrayList) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException)

Aggregations

ArrayList (java.util.ArrayList)1 CreateRequest (org.codelibs.elasticsearch.idxproxy.action.CreateRequest)1 CreateResponse (org.codelibs.elasticsearch.idxproxy.action.CreateResponse)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 TransportException (org.elasticsearch.transport.TransportException)1