From 400fb2694641b05e53217d3c742a6e49e3a254c6 Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Sun, 29 Nov 2026 07:16:47 +0000
Subject: [PATCH] [#1208] Preserve replication history during scaling, recovery and shutdown (#1282)

---
 opendj-server-legacy/src/main/java/org/opends/server/replication/server/ReplicationServer.java |   30 ++++++++++++++++++++++++++++--
 1 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/server/ReplicationServer.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/server/ReplicationServer.java
index 4aa9a57..5dc7de1 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/server/ReplicationServer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/server/ReplicationServer.java
@@ -13,7 +13,7 @@
  *
  * Copyright 2006-2010 Sun Microsystems, Inc.
  * Portions Copyright 2011-2016 ForgeRock AS.
- * Portions Copyrighted 2026 3A Systems, LLC.
+ * Portions Copyright 2026 3A Systems, LLC.
  */
 package org.opends.server.replication.server;
 
@@ -718,7 +718,7 @@
         throw new ConnectException("Connection to " + remoteServerAddress
             + " is a TCP self-connect, no replication server is listening");
       }
-      session = replSessionSecurity.createClientSession(socket, timeoutMS);
+      session = replSessionSecurity.createClientSession(socket, timeoutMS, "RS", getServerId(), baseDN);
 
       ReplicationServerHandler rsHandler = new ReplicationServerHandler(
           session, config.getQueueSize(), this, config.getWindowSize());
@@ -1694,6 +1694,22 @@
       listenThread.interrupt();
     }
 
+    // Remote DSs may still write while this RS stops. End their readers first, then
+    // drain those accepted updates while the peer writers and their sessions remain up.
+    // Unlike a collocated DS, they need not send a ReplicaOfflineMsg at this shutdown.
+    for (ReplicationServerDomain domain : getReplicationServerDomains())
+    {
+      domain.stopDirectoryServers();
+    }
+    try
+    {
+      awaitDirectUpdatesForwarded(TimeUnit.SECONDS.toNanos(30));
+    }
+    catch (InterruptedException e)
+    {
+      Thread.currentThread().interrupt();
+    }
+
     for (ReplicationServerDomain domain : getReplicationServerDomains())
     {
       domain.shutdown();
@@ -1714,6 +1730,16 @@
     allInstances.remove(this);
   }
 
+  /** Waits for all domains within one direct-update forwarding budget. */
+  void awaitDirectUpdatesForwarded(long timeoutNanos) throws InterruptedException
+  {
+    final long deadline = System.nanoTime() + timeoutNanos;
+    for (ReplicationServerDomain domain : getReplicationServerDomains())
+    {
+      domain.awaitDirectUpdatesForwarded(deadline);
+    }
+  }
+
   /**
    * Waits for the ReplicaOfflineMsg of every domain which has a replication server to forward it
    * to. With no such server connected there is nobody to forward the message to, and waiting

--
Gitblit v1.10.0