use of io.zulia.client.command.base.MultiIndexRoutableCommand in project zuliasearch by zuliaio.
the class ZuliaPool method execute.
public <R extends Result> R execute(BaseCommand<R> command) throws Exception {
int tries = 0;
while (true) {
ZuliaConnection zuliaConnection;
Node selectedNode = null;
try {
if (routingEnabled && (indexRouting != null)) {
if (command instanceof ShardRoutableCommand) {
ShardRoutableCommand rc = (ShardRoutableCommand) command;
selectedNode = indexRouting.getNode(rc.getIndexName(), rc.getUniqueId());
} else if (command instanceof SingleIndexRoutableCommand) {
SingleIndexRoutableCommand sirc = (SingleIndexRoutableCommand) command;
selectedNode = indexRouting.getRandomNode(sirc.getIndexName());
} else if (command instanceof MultiIndexRoutableCommand) {
MultiIndexRoutableCommand mirc = (MultiIndexRoutableCommand) command;
selectedNode = indexRouting.getRandomNode(mirc.getIndexNames());
}
}
if (selectedNode == null) {
// stop array index out bounds on updates without locking
List<Node> tempList = nodes;
if (tempList.isEmpty()) {
throw new IOException("There are no active nodes");
}
int randomNodeIndex = (int) (Math.random() * tempList.size());
selectedNode = tempList.get(randomNodeIndex);
}
String nodeKey = getNodeKey(selectedNode);
if (command instanceof RESTCommand) {
int restPort = selectedNode.getRestPort();
if (selectedNode.getRestPort() == 0) {
Node fullSelectedNode = nodeKeyToNode.get(nodeKey);
if (fullSelectedNode != null) {
restPort = fullSelectedNode.getRestPort();
} else {
LOG.warning("Failed to find rest port for <" + nodeKey + "> using default");
restPort = ZuliaConstants.DEFAULT_REST_SERVICE_PORT;
}
}
int finalRestPort = restPort;
final String finalServer = selectedNode.getServerAddress();
ZuliaRESTClient restClient = zuliaRestPoolMap.computeIfAbsent(nodeKey, s -> new ZuliaRESTClient(finalServer, finalRestPort));
return ((RESTCommand<R>) command).execute(restClient);
} else {
GrpcCommand<R> grpcCommand = (GrpcCommand<R>) command;
final Node finalSelectedNode = selectedNode;
GenericObjectPool<ZuliaConnection> nodePool = zuliaConnectionPoolMap.computeIfAbsent(nodeKey, (String key) -> {
//
GenericObjectPoolConfig<ZuliaConnection> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxTotal(maxConnections);
return new GenericObjectPool<>(new ZuliaConnectionFactory(finalSelectedNode, compressedConnection), poolConfig);
});
boolean valid = true;
zuliaConnection = nodePool.borrowObject();
R r;
try {
r = grpcCommand.executeTimed(zuliaConnection);
return r;
} catch (StatusRuntimeException e) {
if (!Status.INVALID_ARGUMENT.equals(e.getStatus())) {
valid = false;
}
Metadata trailers = e.getTrailers();
if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
String errorMessage = trailers.get(MetaKeys.ERROR_KEY);
if (!Status.INVALID_ARGUMENT.equals(e.getStatus())) {
throw new Exception(grpcCommand.getClass().getSimpleName() + ": " + errorMessage);
} else {
throw new IllegalArgumentException(grpcCommand.getClass().getSimpleName() + ":" + errorMessage);
}
} else {
throw e;
}
} finally {
if (valid) {
nodePool.returnObject(zuliaConnection);
} else {
nodePool.invalidateObject(zuliaConnection);
}
}
}
} catch (Exception e) {
System.err.println(e.getClass().getSimpleName() + ":" + e.getMessage());
if (tries >= retries) {
throw e;
}
tries++;
}
}
}
Aggregations