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/plugin/LDAPReplicationDomain.java |   45 +++++++++++++++++++++++++++++++++------------
 1 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java
index 44d7bc9..b8c600c 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java
@@ -1007,8 +1007,7 @@
         {
           if (rsUpdater == this)
           {
-            rsUpdater = null;
-            broker.setRecoveryRequired(false);
+            releasePublisher();
             startTheSessionAgain = true;
           }
         }
@@ -1025,6 +1024,16 @@
       }
     }
 
+    /** Called under recoveryLock. Leaving the publisher does not prove delivery. */
+    private void releasePublisher()
+    {
+      rsUpdater = null;
+      if (pendingChanges.isRecovering())
+      {
+        broker.recoveryAborted();
+      }
+    }
+
     /**
      * Waits before the session restart a failed run asks for: one second more for each failure in a
      * row, up to ten. A session which hands its start over meanwhile ends the wait, as it is the next
@@ -1065,12 +1074,11 @@
       {
         final RecoveryStart start = nextStart;
         nextStart = null;
-        if (start == null || shutdown.get())
+        if (start == null || stopRecoveryForShutdown())
         {
           // RSUpdater thread has finished its work, let's remove it from memory
           // so another RSUpdater thread can be started if needed.
-          rsUpdater = null;
-          broker.setRecoveryRequired(false);
+          releasePublisher();
           return null;
         }
         return start;
@@ -6506,6 +6514,12 @@
   @Override
   protected void importBackend(InputStream input) throws DirectoryException
   {
+    importBackend(input, () -> { });
+  }
+
+  @Override
+  protected void importBackend(InputStream input, ImportPreparation onPrepared) throws DirectoryException
+  {
     LocalBackend<?> backend = getBackend();
 
     LDIFImportConfig importConfig = null;
@@ -6537,9 +6551,10 @@
             ? ERR_INIT_PREPARATION_INTERRUPTED.get(getBaseDN())
             : ERR_INIT_OPERATIONS_NOT_DRAINED.get(getBaseDN());
         logger.error(reason);
-        ieCtx.setExceptionIfNoneSet(new DirectoryException(ResultCode.UNAVAILABLE, reason));
+        ieCtx.failImportPreparation(new DirectoryException(ResultCode.UNAVAILABLE, reason));
         return;
       }
+      onPrepared.reportReady();
 
       if (!backend.supports(BackendOperation.LDIF_IMPORT))
       {
@@ -7444,6 +7459,12 @@
     }
   }
 
+  /** A shutdown still announcing this replica may drain the replacement within the broker stop deadline. */
+  private boolean stopRecoveryForShutdown()
+  {
+    return shutdown.get() && !pendingChanges.isPublishingOffline();
+  }
+
   /**
    * Has the changes of this replica which come after the provided CSN published on the session
    * which starts: starts the thread which publishes them, or hands the start over to the one which
@@ -7469,13 +7490,13 @@
       if (replServerMisses)
       {
         pendingChanges.setRecovering(true);
-        broker.setRecoveryRequired(true);
       }
       else if (!pendingChanges.isRecovering())
       {
         // The publisher of an earlier session ended it since it was checked.
         return;
       }
+      broker.setRecoveryRequired(true);
       if (rsUpdater == null)
       {
         rsUpdater = new RSUpdater(start);
@@ -7549,7 +7570,7 @@
     boolean holdBackLocalChanges = false;
     while (true)
     {
-      if (shutdown.get() || missingChanges.startHandedOver())
+      if (stopRecoveryForShutdown() || missingChanges.startHandedOver())
       {
         return false;
       }
@@ -7674,7 +7695,7 @@
       MissingChangesRound round = publish(fromCSN, true);
       while (round != null && round.succeeded && !round.caughtUp)
       {
-        if (shutdown.get())
+        if (stopRecoveryForShutdown())
         {
           return null;
         }
@@ -7763,7 +7784,7 @@
     {
       for (LDAPUpdateMsg msg : recent.messages)
       {
-        if (shutdown.get())
+        if (stopRecoveryForShutdown())
         {
           return null;
         }
@@ -7908,7 +7929,7 @@
 
       for (FakeOperation opToSend : opsToSend.values())
       {
-        if (shutdown.get())
+        if (stopRecoveryForShutdown())
         {
           return null;
         }
@@ -7953,7 +7974,7 @@
       CSN publishedUpTo = fromCSN;
       for (LDAPUpdateMsg msg : kept)
       {
-        if (shutdown.get())
+        if (stopRecoveryForShutdown())
         {
           return null;
         }

--
Gitblit v1.10.0