[NO ISSUE][NET] Ensure Handshake Connections List is Thread-Safe

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
- Ensure the list of connections that completed their asynchronous
  SSL handshake is accessed in thread-safe manner.

Change-Id: Ibfd4fc569b59f39d730ef04f5d69b23d91713ed7
Reviewed-on: https://asterix-gerrit.ics.uci.edu/3523
Reviewed-by: Murtadha Hubail <mhubail@apache.org>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/hyracks-fullstack/hyracks/hyracks-net/src/main/java/org/apache/hyracks/net/protocols/tcp/TCPEndpoint.java b/hyracks-fullstack/hyracks/hyracks-net/src/main/java/org/apache/hyracks/net/protocols/tcp/TCPEndpoint.java
index faec871..b75e3c6 100644
--- a/hyracks-fullstack/hyracks/hyracks-net/src/main/java/org/apache/hyracks/net/protocols/tcp/TCPEndpoint.java
+++ b/hyracks-fullstack/hyracks/hyracks-net/src/main/java/org/apache/hyracks/net/protocols/tcp/TCPEndpoint.java
@@ -176,11 +176,13 @@
                         }
                         workingIncomingConnections.clear();
                     }
-                    if (!handshakeCompletedConnections.isEmpty()) {
-                        for (final PendingHandshakeConnection conn : handshakeCompletedConnections) {
-                            handshakeCompleted(conn);
+                    synchronized (handshakeCompletedConnections) {
+                        if (!handshakeCompletedConnections.isEmpty()) {
+                            for (final PendingHandshakeConnection conn : handshakeCompletedConnections) {
+                                handshakeCompleted(conn);
+                            }
+                            handshakeCompletedConnections.clear();
                         }
-                        handshakeCompletedConnections.clear();
                     }
                     if (n > 0) {
                         Iterator<SelectionKey> i = selector.selectedKeys().iterator();
@@ -297,8 +299,7 @@
             if (socketChannel.requiresHandshake()) {
                 asyncHandshake(conn);
             } else {
-                conn.handshakeSuccess = true;
-                handshakeCompletedConnections.add(conn);
+                handshakeCompleted(true, conn);
             }
         }
 
@@ -308,8 +309,7 @@
             if (socketChannel.requiresHandshake()) {
                 asyncHandshake(conn);
             } else {
-                conn.handshakeSuccess = true;
-                handshakeCompletedConnections.add(conn);
+                handshakeCompleted(true, conn);
             }
         }
 
@@ -319,11 +319,17 @@
         }
 
         private void handleHandshakeCompletion(Boolean handshakeSuccess, PendingHandshakeConnection conn) {
-            conn.handshakeSuccess = handshakeSuccess;
-            handshakeCompletedConnections.add(conn);
+            handshakeCompleted(handshakeSuccess, conn);
             selector.wakeup();
         }
 
+        private void handshakeCompleted(Boolean handshakeSuccess, PendingHandshakeConnection conn) {
+            conn.handshakeSuccess = handshakeSuccess;
+            synchronized (handshakeCompletedConnections) {
+                handshakeCompletedConnections.add(conn);
+            }
+        }
+
         private void connectionEstablished(TCPConnection connection) {
             synchronized (connectionListener) {
                 connectionListener.connectionEstablished(connection);