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/service/DSRSShutdownSync.java |   79 +++++++++++++++++++++++++++------------
 1 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/service/DSRSShutdownSync.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/service/DSRSShutdownSync.java
index 361af54..e5866f6 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/service/DSRSShutdownSync.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/service/DSRSShutdownSync.java
@@ -45,6 +45,37 @@
 public class DSRSShutdownSync
 {
   /**
+   * Identifies one connection to a peer RS without retaining the handler or session.
+   * Connections with the same server ID have distinct tokens.
+   */
+  public static final class ForwardingPeer
+  {
+    private final int serverId;
+
+    /**
+     * Creates the token for one connection.
+     *
+     * @param serverId the peer's server ID, for diagnostics
+     */
+    public ForwardingPeer(int serverId)
+    {
+      this.serverId = serverId;
+    }
+
+    /** Returns the peer's server ID. */
+    public int getServerId()
+    {
+      return serverId;
+    }
+
+    @Override
+    public String toString()
+    {
+      return "RS(" + serverId + ")";
+    }
+  }
+
+  /**
    * How long a ReplicaOfflineMsg may hold back the shutdown of the collocated
    * RS, in milliseconds, counted from the moment the message was announced.
    */
@@ -196,11 +227,11 @@
    *          the domain for which the message has been sent
    * @param offlineCSN
    *          the CSN of the message which is being queued
-   * @param replicationServerIds
-   *          the server ids of the replication servers the message is being queued for
+   * @param peers
+   *          the peer connections the message is being queued for
    */
   public void replicaOfflineMsgDispatched(
-      DN baseDN, CSN offlineCSN, Collection<Integer> replicationServerIds)
+      DN baseDN, CSN offlineCSN, Collection<ForwardingPeer> peers)
   {
     final ConcurrentMap<Integer, PendingOfflineMsg> msgs = replicaOfflineMsgs.get(baseDN);
     if (msgs == null)
@@ -215,7 +246,7 @@
      * nothing about the announcement the shutdown is waiting for.
      */
     if (pending != null && pending.csn.equals(offlineCSN)
-        && pending.awaitForwardsFrom(replicationServerIds))
+        && pending.awaitForwardsFrom(peers))
     {
       // queued for nobody: there is nothing to wait for
       msgs.remove(serverId, pending);
@@ -230,10 +261,10 @@
    *          the domain for which the message has been sent
    * @param forwardedCSN
    *          the CSN of the forwarded message
-   * @param replicationServerId
-   *          the server id of the replication server the message has been forwarded to
+   * @param peer
+   *          the peer connection through which the message was forwarded
    */
-  public void replicaOfflineMsgForwarded(DN baseDN, CSN forwardedCSN, int replicationServerId)
+  public void replicaOfflineMsgForwarded(DN baseDN, CSN forwardedCSN, ForwardingPeer peer)
   {
     final ConcurrentMap<Integer, PendingOfflineMsg> msgs = replicaOfflineMsgs.get(baseDN);
     if (msgs != null)
@@ -248,7 +279,7 @@
        * not consume its grace period.
        */
       if (pending != null && pending.csn.isOlderThanOrEqualTo(forwardedCSN)
-          && pending.forwardedBy(replicationServerId))
+          && pending.forwardedBy(peer))
       {
         msgs.remove(serverId, pending);
       }
@@ -265,10 +296,10 @@
    *
    * @param baseDN
    *          the domain the replication server is connected to
-   * @param replicationServerId
-   *          the server id of the replication server which will not forward the message
+   * @param peer
+   *          the peer connection which will not forward the message
    */
-  public void replicaOfflineMsgNotForwarded(DN baseDN, int replicationServerId)
+  public void replicaOfflineMsgNotForwarded(DN baseDN, ForwardingPeer peer)
   {
     final ConcurrentMap<Integer, PendingOfflineMsg> msgs = replicaOfflineMsgs.get(baseDN);
     if (msgs != null)
@@ -276,7 +307,7 @@
       for (Entry<Integer, PendingOfflineMsg> entry : msgs.entrySet())
       {
         final PendingOfflineMsg pending = entry.getValue();
-        if (pending.giveUpOn(replicationServerId))
+        if (pending.giveUpOn(peer))
         {
           msgs.remove(entry.getKey(), pending);
         }
@@ -448,7 +479,7 @@
      * The replication servers the message was queued for and which have not forwarded it yet,
      * null as long as it has not been queued for anybody.
      */
-    private volatile Set<Integer> awaitedForwarders;
+    private volatile Set<ForwardingPeer> awaitedForwarders;
 
     private PendingOfflineMsg(CSN csn, long sentTime, PendingOfflineMsg displaced)
     {
@@ -461,10 +492,10 @@
      * Records the replication servers the message is being queued for, and returns whether there
      * is none of them, i.e. nothing left to wait for.
      */
-    private boolean awaitForwardsFrom(Collection<Integer> replicationServerIds)
+    private boolean awaitForwardsFrom(Collection<ForwardingPeer> peers)
     {
-      final Set<Integer> awaited = ConcurrentHashMap.newKeySet();
-      awaited.addAll(replicationServerIds);
+      final Set<ForwardingPeer> awaited = ConcurrentHashMap.newKeySet();
+      awaited.addAll(peers);
       awaitedForwarders = awaited;
       return awaited.isEmpty();
     }
@@ -473,9 +504,9 @@
      * Records the forward of one replication server, and returns whether nothing is left to wait
      * for.
      */
-    private boolean forwardedBy(int replicationServerId)
+    private boolean forwardedBy(ForwardingPeer peer)
     {
-      final Set<Integer> awaited = awaitedForwarders;
+      final Set<ForwardingPeer> awaited = awaitedForwarders;
       if (awaited == null)
       {
         /*
@@ -485,7 +516,7 @@
          */
         return true;
       }
-      awaited.remove(replicationServerId);
+      awaited.remove(peer);
       return awaited.isEmpty();
     }
 
@@ -494,10 +525,10 @@
      * wait for. Unlike a forward, this releases nothing while no recipient is known: a peer going
      * away says nothing about a message it was never given.
      */
-    private boolean giveUpOn(int replicationServerId)
+    private boolean giveUpOn(ForwardingPeer peer)
     {
-      final Set<Integer> awaited = awaitedForwarders;
-      return awaited != null && awaited.remove(replicationServerId) && awaited.isEmpty();
+      final Set<ForwardingPeer> awaited = awaitedForwarders;
+      return awaited != null && awaited.remove(peer) && awaited.isEmpty();
     }
 
     /**
@@ -506,14 +537,14 @@
      */
     private boolean isFullyForwarded()
     {
-      final Set<Integer> awaited = awaitedForwarders;
+      final Set<ForwardingPeer> awaited = awaitedForwarders;
       return awaited != null && awaited.isEmpty();
     }
 
     @Override
     public String toString()
     {
-      final Set<Integer> awaited = awaitedForwarders;
+      final Set<ForwardingPeer> awaited = awaitedForwarders;
       return "PendingOfflineMsg(" + csn
           + (awaited != null ? ", awaiting the forward of " + awaited : "") + ")";
     }

--
Gitblit v1.10.0