From 2863452733f1815ce5557ff01cfe148b49e182b0 Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Mon, 30 Nov 2026 16:00:33 +0000
Subject: [PATCH] [#1208] Retry the discovery responder before declaring replication isolation (#1349)

---
 opendj-server-legacy/src/main/java/org/opends/server/replication/service/ReplicationBroker.java |   37 +++++++++++++++++++++++++++----------
 1 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/service/ReplicationBroker.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/service/ReplicationBroker.java
index 176c02d..5662bbd 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/service/ReplicationBroker.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/service/ReplicationBroker.java
@@ -49,6 +49,7 @@
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 
 import net.jcip.annotations.GuardedBy;
@@ -192,7 +193,7 @@
   }
 
   /**
-   * A stop of the broker, from the moment {@link #stop()} begins until {@link #start()} clears it.
+   * A registered stop of the broker, kept until {@link #start()} clears it.
    * <p>
    * What a publish gives up for the stop is marked on the stop, and not on the broker: a publish
    * which found the stop past its deadline may mark it only after start() cleared it, and then the
@@ -266,9 +267,9 @@
   /** A backend whose state could not be restored must never reconnect through this broker. */
   private volatile boolean permanentlyStopped;
   /**
-   * The stop in progress: set by {@link #stop()} before anything else it does, and cleared to
-   * {@code null} by {@link #start()}. While it is set, a publish which finds no window credit by
-   * its deadline gives its message up instead of waiting on.
+   * The stop in progress, registered under {@link #startStopLock} and cleared to {@code null}
+   * by {@link #start()}. While it is set, a publish which finds no window credit by its deadline
+   * gives its message up instead of waiting on.
    * <p>
    * stop() publishes the replica offline message before it sets {@link #shutdown}, the flag which
    * ends the wait for credit, and that message needs credit too, and the lock of the pending
@@ -282,6 +283,8 @@
    */
   private volatile StopInProgress stopping;
   private final Object startStopLock = new Object();
+  /** Stop requests waiting for the lock are visible to the reconnect that holds it. */
+  private final AtomicInteger pendingStopRequests = new AtomicInteger();
   private volatile ReplicationDomainCfg config;
   /** String reported under CSN=monitor when there is no connected RS. */
   static final HostPort NO_CONNECTED_SERVER = new HostPort(null, 0);
@@ -1158,7 +1161,7 @@
         }
         computeNewTopology(toRSInfos(rsInfos));
 
-        final boolean canRetry = previousRS != null && stopping == null && !permanentlyStopped;
+        final boolean canRetry = previousRS != null && canRetryFailover();
         final boolean hasReplies = !rsInfos.isEmpty();
         String electedURL = null;
         if (hasReplies)
@@ -1242,7 +1245,7 @@
       discovery.close();
     }
     ConnectedRS electedRS = performPhaseOneHandshake(electedURL, true, canRetry);
-    if (electedRS.rsInfo == null && canRetry && stopping == null && !permanentlyStopped)
+    if (electedRS.rsInfo == null && canRetry && canRetryFailover())
     {
       electedRS = retryElectedConnection(discovery, rsInfos, electedURL, previousRS, electedRS);
     }
@@ -1288,7 +1291,7 @@
     final String timedOutRS = electedFormerRS && responder == null ? discovery.timedOutServer(previousRS) : null;
     discovery.close();
 
-    if (stopping != null || permanentlyStopped)
+    if (!canRetryFailover())
     {
       return failure;
     }
@@ -1304,6 +1307,11 @@
     return failure;
   }
 
+  private boolean canRetryFailover()
+  {
+    return pendingStopRequests.get() == 0 && stopping == null && !permanentlyStopped;
+  }
+
   /** Opens the selected RS session using the fresh state returned by its start reply. */
   private void finishElectedConnection(ConnectedRS electedRS, Map<Integer, ReplicationServerInfo> rsInfos)
   {
@@ -1333,7 +1341,12 @@
 
   private ConnectedRS connectToDiscoveryResponder(ReplicationServerInfo responder)
   {
-    final ConnectedRS replacement = performPhaseOneHandshake(responder.getServerURL(), true, false);
+    final String url = responder.getServerURL();
+    ConnectedRS replacement = performPhaseOneHandshake(url, true, true);
+    if (replacement == ConnectedRS.START_REPLY_TIMED_OUT && canRetryFailover())
+    {
+      replacement = performPhaseOneHandshake(url, true, false);
+    }
     // The generation may have changed since discovery.
     if (replacement.rsInfo != null && !hasCompatibleGeneration(replacement.rsInfo))
     {
@@ -3556,8 +3569,12 @@
     }
     final StopInProgress stop;
     final boolean newStop;
-    synchronized (startStopLock) {
-      if (shutdown) {
+    pendingStopRequests.incrementAndGet();
+    synchronized (startStopLock)
+    {
+      pendingStopRequests.decrementAndGet();
+      if (shutdown)
+      {
         return;
       }
       // A stop which overlaps one in progress keeps its deadlines and what it gave up.

--
Gitblit v1.10.0