jhuan31 commented on a change in pull request #1301: ZOOKEEPER-3774: Close 
quorum socket asynchronously on the leader to a…
URL: https://github.com/apache/zookeeper/pull/1301#discussion_r409977118
 
 

 ##########
 File path: 
zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandler.java
 ##########
 @@ -1150,4 +1140,33 @@ public void setFirstPacket(boolean value) {
         needOpPacket = value;
     }
 
+    void closeSocket() {
+        if (sock != null && !sock.isClosed() && 
sockBeingClosed.compareAndSet(false, true)) {
+            LOG.error("Unexpected exception causing shutdown while sock is 
still open");
+
+            if (closeSocketAsync) {
+                closeSockAsync();
+            } else {
+                closeSockSync();
+            }
+        }
+    }
+
+    void closeSockAsync() {
+        final Thread closingThread = new Thread(() -> closeSockSync(), 
"CloseSocketThread(sid:" + this.sid);
+        closingThread.setDaemon(true);
+        closingThread.start();
+    }
+
+    void closeSockSync() {
+        try {
+            if (sock != null) {
+                long startTime = Time.currentElapsedTime();
+                sock.close();
+                
ServerMetrics.getMetrics().SOCKET_CLOSING_TIME.add(Time.currentElapsedTime() - 
startTime);
 
 Review comment:
   I'm hesitating... This metric is to measure the real time for closing a 
socket. If it fails, say, the socket is closed already, then the elapsed time 
is not really the time for closing the socket.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to