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/ServerWriter.java |   22 ++++++++++++++++++----
 1 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/server/ServerWriter.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/server/ServerWriter.java
index fce6760..19f4b31 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/server/ServerWriter.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/server/ServerWriter.java
@@ -24,11 +24,13 @@
 import org.forgerock.opendj.ldap.DN;
 import org.opends.server.api.DirectoryThread;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
+import org.opends.server.replication.common.CSN;
 import org.opends.server.replication.common.ServerStatus;
 import org.opends.server.replication.protocol.ReplicaOfflineMsg;
 import org.opends.server.replication.protocol.Session;
 import org.opends.server.replication.protocol.UpdateMsg;
 import org.opends.server.replication.service.DSRSShutdownSync;
+import org.opends.server.replication.service.DSRSShutdownSync.ForwardingPeer;
 
 import static org.opends.messages.ReplicationMessages.*;
 import static org.opends.server.replication.common.ServerStatus.*;
@@ -121,10 +123,14 @@
            * as sent (issue #1080).
            */
           handler.releasePermitInSendWindow();
+          if (handler instanceof ReplicationServerHandler && updateMsg.contributesToDomainState())
+          {
+            ((ReplicationServerHandler) handler).updateDiscardedState(updateMsg.getCSN());
+          }
           if (updateMsg instanceof ReplicaOfflineMsg && !handler.isDataServer())
           {
             dsrsShutdownSync.replicaOfflineMsgNotForwarded(
-                replicationServerDomain.getBaseDN(), handler.getServerId());
+                replicationServerDomain.getBaseDN(), handler.offlineForwarder());
           }
         }
         else
@@ -134,6 +140,14 @@
           {
             forwardReplicaOfflineMsg((ReplicaOfflineMsg) updateMsg);
           }
+          else if (handler instanceof ReplicationServerHandler && updateMsg.contributesToDomainState())
+          {
+            // take() already advanced the handler state, while publish() may only enqueue.
+            // A shutdown must wait until the session publisher has written the change.
+            final CSN csn = updateMsg.getCSN();
+            session.publish(updateMsg,
+                () -> ((ReplicationServerHandler) handler).updatePublishedState(csn));
+          }
           else
           {
             // Publish the update to the remote server using a protocol version it supports
@@ -216,12 +230,12 @@
   private void forwardReplicaOfflineMsg(final ReplicaOfflineMsg msg) throws IOException
   {
     final DN baseDN = replicationServerDomain.getBaseDN();
-    final int serverId = handler.getServerId();
+    final ForwardingPeer peer = handler.offlineForwarder();
     final boolean accepted = session.publish(msg,
-        () -> dsrsShutdownSync.replicaOfflineMsgForwarded(baseDN, msg.getCSN(), serverId));
+        () -> dsrsShutdownSync.replicaOfflineMsgForwarded(baseDN, msg.getCSN(), peer));
     if (!accepted)
     {
-      dsrsShutdownSync.replicaOfflineMsgNotForwarded(baseDN, serverId);
+      dsrsShutdownSync.replicaOfflineMsgNotForwarded(baseDN, peer);
     }
   }
 

--
Gitblit v1.10.0