use of org.codelibs.elasticsearch.idxproxy.action.WriteResponse in project elasticsearch-indexing-proxy by codelibs.
the class IndexingProxyService method writeOnRemote.
private <Request extends ActionRequest, Response extends ActionResponse> void writeOnRemote(final String nodeName, final long version, final Request request, 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 writing."));
} else {
if (logger.isDebugEnabled()) {
logger.debug("No available write node.");
}
updateWriterNode(version, nodeList, (res, ex) -> write(request, listener, tryCount + 1));
}
return;
}
final int nodeIdx = pos;
transportService.sendRequest(nodeList.get(nodeIdx), IndexingProxyPlugin.ACTION_IDXPROXY_WRITE, new WriteRequest<>(request), new TransportResponseHandler<WriteResponse>() {
@Override
public WriteResponse newInstance() {
return new WriteResponse();
}
@Override
public void handleResponse(final WriteResponse response) {
if (response.isAcknowledged()) {
if (logger.isDebugEnabled()) {
logger.debug("Stored request in " + nodeName);
}
listener.onResponse(null);
} else {
throw new ElasticsearchException("Failed to store request: " + RequestUtils.getClassType(request));
}
}
@Override
public void handleException(final TransportException e) {
if (tryCount >= writerRetryCount) {
listener.onFailure(e);
} else {
final DiscoveryNode nextNode = nodeList.get((nodeIdx + 1) % nodeList.size());
if (nextNode.getName().equals(nodeName)) {
if (tryCount >= writerRetryCount) {
listener.onFailure(e);
} else {
randomWait();
write(request, listener, tryCount + 1);
}
} else {
final Map<String, Object> source = new HashMap<>();
source.put(IndexingProxyPlugin.NODE_NAME, nextNode.getName());
source.put(IndexingProxyPlugin.TIMESTAMP, new Date());
client.prepareUpdate(IndexingProxyPlugin.INDEX_NAME, IndexingProxyPlugin.TYPE_NAME, FILE_ID).setVersion(version).setDoc(source).setRefreshPolicy(RefreshPolicy.WAIT_UNTIL).execute(wrap(res -> {
write(request, listener, tryCount + 1);
}, ex -> {
if (logger.isDebugEnabled()) {
logger.debug("Failed to update file_id.", ex);
}
write(request, listener, tryCount + 1);
}));
}
}
}
@Override
public String executor() {
return ThreadPool.Names.GENERIC;
}
});
}
Aggregations