[NO ISSUE][NET] Ensure ssl socket is connected before write

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

Details:

- Ensure the SSL socket is connected before attempting a
  write/wrap operation.
- Only attempt to send goodbye message to replicas when
  the socket channel is still connected.
- Always attempt to close the ssl socket channel even when
  the close handshake fails.

Change-Id: I07fbcd76be29853c94cb133485d83034ceee9cb3
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/9825
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Michael Blow <mblow@apache.org>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-replication/src/main/java/org/apache/asterix/replication/api/PartitionReplica.java b/asterixdb/asterix-replication/src/main/java/org/apache/asterix/replication/api/PartitionReplica.java
index 3226299..6b97306 100644
--- a/asterixdb/asterix-replication/src/main/java/org/apache/asterix/replication/api/PartitionReplica.java
+++ b/asterixdb/asterix-replication/src/main/java/org/apache/asterix/replication/api/PartitionReplica.java
@@ -113,10 +113,10 @@
 
     public synchronized void close() {
         try {
-            if (sc != null) {
+            if (NetworkingUtil.isHealthy(sc)) {
                 sendGoodBye();
-                NetworkUtil.closeQuietly(sc);
             }
+            NetworkUtil.closeQuietly(sc);
         } finally {
             sc = null;
         }
diff --git a/hyracks-fullstack/hyracks/hyracks-ipc/src/main/java/org/apache/hyracks/ipc/sockets/SslSocketChannel.java b/hyracks-fullstack/hyracks/hyracks-ipc/src/main/java/org/apache/hyracks/ipc/sockets/SslSocketChannel.java
index 02a1a02..74deefe 100644
--- a/hyracks-fullstack/hyracks/hyracks-ipc/src/main/java/org/apache/hyracks/ipc/sockets/SslSocketChannel.java
+++ b/hyracks-fullstack/hyracks/hyracks-ipc/src/main/java/org/apache/hyracks/ipc/sockets/SslSocketChannel.java
@@ -20,6 +20,7 @@
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
 import java.nio.channels.SocketChannel;
 
 import javax.net.ssl.SSLEngine;
@@ -142,6 +143,9 @@
         while (src.hasRemaining()) {
             // chunk src to encrypted ssl records of pocket size
             outEncryptedData.clear();
+            if (!socketChannel.isConnected()) {
+                throw new ClosedChannelException();
+            }
             final SSLEngineResult result = engine.wrap(src, outEncryptedData);
             switch (result.getStatus()) {
                 case OK:
@@ -186,8 +190,11 @@
     public synchronized void close() throws IOException {
         if (socketChannel.isOpen()) {
             engine.closeOutbound();
-            new SslHandshake(this).handshake();
-            socketChannel.close();
+            try {
+                new SslHandshake(this).handshake();
+            } finally {
+                socketChannel.close();
+            }
         }
     }