From 52694d4c03f9b09b2e52f389701255f3c78dfd9b Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Mon, 30 Nov 2026 08:18:21 +0000
Subject: [PATCH] Bound replication server discovery during failover (#1346)

---
 opendj-server-legacy/src/main/java/org/opends/server/replication/protocol/Session.java |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/protocol/Session.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/protocol/Session.java
index 0fa9c14..98b2fca 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/protocol/Session.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/protocol/Session.java
@@ -897,6 +897,39 @@
   public ReplicationMsg receive() throws IOException,
       DataFormatException, NotSupportedOldVersionPDUException
   {
+    return receive(false);
+  }
+
+  /**
+   * Receives the first message of a new connection, distinguishing a disconnect before
+   * any frame bytes arrive from a frame interrupted during transmission.
+   *
+   * @return the first replication message
+   * @throws EarlyPeerDisconnectException if the stream ends or the socket fails before the first byte
+   * @throws IOException if another I/O error occurs
+   * @throws DataFormatException if the frame is malformed
+   * @throws NotSupportedOldVersionPDUException if the message uses an unsupported old protocol
+   */
+  public ReplicationMsg receiveFirstMessage() throws IOException,
+      DataFormatException, NotSupportedOldVersionPDUException
+  {
+    return receive(true);
+  }
+
+  /** The peer disconnected before sending any bytes of its first replication frame. */
+  public static final class EarlyPeerDisconnectException extends IOException
+  {
+    private static final long serialVersionUID = 1L;
+
+    private EarlyPeerDisconnectException(IOException cause)
+    {
+      super(cause.getMessage(), cause);
+    }
+  }
+
+  private ReplicationMsg receive(boolean firstMessage)
+      throws IOException, DataFormatException, NotSupportedOldVersionPDUException
+  {
     try
     {
       /*
@@ -906,13 +939,13 @@
       lastReceiveTime = System.currentTimeMillis();
 
       // Read the first 8 bytes containing the packet length.
-      read(rcvLengthBuf);
+      read(rcvLengthBuf, firstMessage);
       final int totalLength = Integer.parseInt(new String(rcvLengthBuf), 16);
 
       try
       {
         final byte[] buffer = new byte[totalLength];
-        read(buffer);
+        read(buffer, false);
 
         /*
          * We do not want the heartbeat to close the session when we are
@@ -933,19 +966,30 @@
     }
   }
 
-  private void read(byte[] buffer) throws IOException
+  private void read(byte[] buffer, boolean firstFrameHeader) throws IOException
   {
     final int totalLength = buffer.length;
     int length = 0;
     while (length < totalLength)
     {
-      final int read = input.read(buffer, length, totalLength - length);
-      if (read == -1)
+      try
       {
-        lastReceiveTime = 0;
-        throw new EOFException("no more data");
+        final int read = input.read(buffer, length, totalLength - length);
+        if (read == -1)
+        {
+          lastReceiveTime = 0;
+          throw new EOFException("no more data");
+        }
+        length += read;
       }
-      length += read;
+      catch (EOFException | SocketException e)
+      {
+        if (firstFrameHeader && length == 0)
+        {
+          throw new EarlyPeerDisconnectException(e);
+        }
+        throw e;
+      }
     }
   }
 

--
Gitblit v1.10.0