Search in sources :

Example 1 with SingleIndexRoutableCommand

use of io.zulia.client.command.base.SingleIndexRoutableCommand 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++;
        }
    }
}
Also used : ZuliaRESTClient(io.zulia.client.rest.ZuliaRESTClient) Node(io.zulia.message.ZuliaBase.Node) GrpcCommand(io.zulia.client.command.base.GrpcCommand) Metadata(io.grpc.Metadata) RESTCommand(io.zulia.client.command.base.RESTCommand) IOException(java.io.IOException) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) MultiIndexRoutableCommand(io.zulia.client.command.base.MultiIndexRoutableCommand) ShardRoutableCommand(io.zulia.client.command.base.ShardRoutableCommand) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) StatusRuntimeException(io.grpc.StatusRuntimeException) SingleIndexRoutableCommand(io.zulia.client.command.base.SingleIndexRoutableCommand)

Aggregations

Metadata (io.grpc.Metadata)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 GrpcCommand (io.zulia.client.command.base.GrpcCommand)1 MultiIndexRoutableCommand (io.zulia.client.command.base.MultiIndexRoutableCommand)1 RESTCommand (io.zulia.client.command.base.RESTCommand)1 ShardRoutableCommand (io.zulia.client.command.base.ShardRoutableCommand)1 SingleIndexRoutableCommand (io.zulia.client.command.base.SingleIndexRoutableCommand)1 ZuliaRESTClient (io.zulia.client.rest.ZuliaRESTClient)1 Node (io.zulia.message.ZuliaBase.Node)1 IOException (java.io.IOException)1 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1