如何强制Zookeeper服务器等待客户端读取响应?

时间:2019-01-11 08:26:59

标签: java apache-zookeeper

我有一个由Kubernetes上运行的几个节点创建的Zookeeper集群。我还有一个“管理器”窗格,该窗格应获取Zookeeper节点元数据。我通过向动物园管理员节点发送一个CardAction plButton = new CardAction() { Title = "Connect to Employee", Value = "Connect", Type = ActionTypes.PostBack }; 字来实现这一目标。发送请求后,我应该能够读取带有元数据的响应,但是不幸的是,并非一直如此。存在某种竞争条件,因为有时执行读取响应时不会产生stat异常,有时会在读取数据的过程中引发异常(已接收数据,但仍会引发异常)。第三种选择是-没有接收到任何数据并引发异常。

我认为Zookeeper节点在读取响应之前/之中关闭了一个连接。有什么方法可以迫使Zookeeper等待,直到完全收到响应?

IOException

Zookeeper服务器日志来自抛出客户端异常的情况

private void getMetadataFromPod(Pod pod, SSLSocketFactory factory) {
int port = 2181;
    try {
        String host = getPodIP();
        SSLSocket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        SSLSocket socket = (SSLSocket) factory.createSocket();

        if (socket == null) {
            log.error("Could not create socket for getting Zookeeper data");
            return;
        }
        try {
            socket.connect(new InetSocketAddress(host, port), 10_000);
        } catch (ConnectException | SocketTimeoutException e) {
            log.error("Could not connect " + e.getMessage());
        }
        try {
            log.debug("Starting handshake with {}", socket.getRemoteSocketAddress());
            try {
                socket.startHandshake();
                out = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(
                                        socket.getOutputStream(), StandardCharsets.UTF_8)));
                out.println("stat");
                out.flush();

                in = new BufferedReader(
                        new InputStreamReader(
                                socket.getInputStream(), StandardCharsets.UTF_8));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    log.debug(inputLine);
                }
            } catch (SSLHandshakeException e) {
                log.error("Error while performing TLS handshake with pod {} in namespace {}",
                        pod.getMetadata().getName(), pod.getMetadata().getNamespace(), e);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            log.debug("Closing resources");
            in.close();
            out.close();
            socket.close();
        }
    } catch (IOException e) {
        log.debug("Error while getting Zookeeper metadata: " + e.getMessage());
    }
}

从“经理”客户端登录

2019-01-11 10:55:33,907 INFO Accepted socket connection from /127.0.0.1:46698 (org.apache.zookeeper.server.NIOServerCnxnFactory) [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:21813]
2019-01-11 10:55:33,908 INFO Processing stat command from /127.0.0.1:46698 (org.apache.zookeeper.server.NIOServerCnxn) [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:21813]
2019-01-11 10:55:33,909 INFO Stat command output (org.apache.zookeeper.server.NIOServerCnxn) [Thread-811]
2019-01-11 10:55:33,910 INFO Closed socket connection for client /127.0.0.1:46698 (no session established for client) (org.apache.zookeeper.server.NIOServerCnxn) [Thread-811]

0 个答案:

没有答案