From 8672f11245231e8d7eecc2054653a3e3b133f1a3 Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Tue, 01 Dec 2026 12:44:58 +0000
Subject: [PATCH] [#1208] Use the active writer's replication server for combined scale checkpoints (#1351)

---
 .github/scripts/replication/disturb-scale.sh |  181 ++++++++++++++++++++++++++++++++++-----------
 1 files changed, 137 insertions(+), 44 deletions(-)

diff --git a/.github/scripts/replication/disturb-scale.sh b/.github/scripts/replication/disturb-scale.sh
index ab1ef7d..8fcc72f 100644
--- a/.github/scripts/replication/disturb-scale.sh
+++ b/.github/scripts/replication/disturb-scale.sh
@@ -40,12 +40,13 @@
 
 # The administration port is published on loopback for scale rows. A native client
 # avoids starting another JVM for every checkpoint and binds only to that loopback port.
-scale_monitor_search() { # <node>
+scale_monitor_search() { # <node> [connected-to <monitor DN>]
   local mapping port
   mapping=$(docker port "$1" 4444/tcp) || return 1
   case $mapping in 127.0.0.1:*) port=${mapping#127.0.0.1:} ;; *) echo 'admin port is not loopback-only' >&2; return 1 ;; esac
   case $port in ''|*[!0-9]*) echo 'invalid admin port' >&2; return 1 ;; esac
-  printf '%s' "$ROOT_PASSWORD" | python3 -I "$HERE/scale-monitor.py" "$port" "$ROLLING_END_MS"
+  shift
+  printf '%s' "$ROOT_PASSWORD" | python3 -I "$HERE/scale-monitor.py" "$port" "$ROLLING_END_MS" "$@"
 }
 
 scale_checkpoint_query() { # <node> <output> <error> <role>
@@ -270,24 +271,24 @@
 
 # During a combined-node checkpoint, request every pause before waiting for
 # the departing node and the other writers to finish their segments. Keep the
-# observer's clients running while the others' high-water marks become finite.
+# selected writer's clients running while the others' high-water marks become finite.
 # The other writers resume after the stop, and the departing writer after its join.
-scale_hold_writers() { # <observer DS> <departing DS> <down|up>
-  local observer=$1 departing=$2 direction=$3 node
+scale_hold_writers() { # <writer DS> <departing DS> <down|up>
+  local writer=$1 departing=$2 direction=$3 node
+  # On a handoff, keep the old writer running until the new one has active
+  # clients. Other parked workers can stay parked throughout.
+  if [ -n "$SCALE_WRITER" ]; then
+    rolling_resume "$writer" || return 1
+    scale_wait "$writer resumes before the handoff" rolling_clients_running "$writer" || return 1
+  fi
   SCALE_HELD_WRITERS=
-  rolling_request_pause "$departing" || return 1
   for node in $DS_NODES; do
-    [ "$node" != "$observer" ] && [ "$node" != "$departing" ] || continue
-    # The removed node is absent during scale-in, but is a writer again during scale-out.
+    [ "$node" != "$writer" ] && [ "$node" != "$departing" ] || continue
     [ "$direction" != down ] || [ "$node" != "$REMOVED" ] || continue
-    rolling_request_pause "$node" || return 1
     SCALE_HELD_WRITERS="$SCALE_HELD_WRITERS $node"
   done
-  rolling_pause "$departing" || return 1
-  for node in $SCALE_HELD_WRITERS; do rolling_pause "$node" || return 1; done
-  scale_wait "$observer has active load clients" rolling_clients_running "$observer" || return 1
-  mkdir -p "$OUT/scale-progress" || return 1
-  scale_wait "$observer accepts a checkpoint write" scale_probe "$observer" "$departing" "checkpoint-$direction" || return 1
+  for node in "$departing" $SCALE_HELD_WRITERS; do rolling_request_pause "$node" || return 1; done
+  for node in "$departing" $SCALE_HELD_WRITERS; do rolling_pause "$node" || return 1; done
 }
 
 scale_release_writers() {
@@ -297,6 +298,98 @@
   [ -z "$failed" ]
 }
 
+scale_writer_connected() { # <writer> <expected endpoint>
+  local endpoint
+  endpoint=$(scale_monitor_search "$1" connected-to "$MONITOR_DOMAIN") && [ "$endpoint" = "$2" ]
+}
+
+# End this wait when the checkpoint is ready or the writer changes route.
+# Clearing SCALE_OBSERVER tells the caller to prepare a new checkpoint before stopping.
+scale_routed_checkpoint() { # <departing> <down|up> <writer> <endpoint>
+  local departing=$1 key= node
+  if ! scale_writer_connected "$3" "$4"; then
+    SCALE_OBSERVER=
+    return 0
+  fi
+  scale_caught_up "$SCALE_OBSERVER" "$departing" || return 1
+  # Docker reads a log position from each node. Recheck the route after these
+  # reads finish, just before the stop.
+  if [ "$departing" = "$REMOVED" ]; then
+    dns_window_open "$departing" || return 200
+    window_open "dns:$departing" 180 || return 200
+  else
+    key="-$2-$departing"
+    window_open_key "${key#-}" "dns:$REMOVED" "dns:$departing" || return 200
+  fi
+  scale_writer_connected "$3" "$4" && return 0
+  SCALE_OBSERVER=
+  # The stop did not happen. Discard these pending starts so the next attempt
+  # captures a new window without excusing errors from the intervening catch-up.
+  for node in $ALL_NODES; do
+    rm -f "$OUT/logs/$node.window$key" || return 200
+    if [ "$departing" = "$REMOVED" ]; then rm -f "$OUT/logs/$node.dns-window" || return 200; fi
+  done
+  return 0
+}
+
+# A combined DS may send changes through a remote RS. Keep a writer whose serving
+# RS survives this stop and compare that RS against the departing changelog.
+scale_prepare_routed_writer() { # <departing> <down|up> <surviving node=endpoint>...
+  local departing=$1 direction=$2 writer observer= pair endpoint probe="checkpoint-$2"
+  local -a candidates=("$SCALE_WRITER")
+  shift 2
+  for pair in "$@"; do
+    [ "${pair%%=*}" = "$SCALE_WRITER" ] || candidates+=("${pair%%=*}")
+  done
+  for writer in "${candidates[@]}"; do
+    [ -n "$writer" ] || continue
+    endpoint=$(scale_monitor_search "$writer" connected-to "$MONITOR_DOMAIN") || continue
+    for pair in "$@"; do
+      if [ "${pair#*=}" = "$endpoint" ]; then observer=${pair%%=*}; break 2; fi
+    done
+  done
+  if [ -z "$observer" ]; then
+    SCALE_OBSERVER=
+    printf 'no workload DS connects to a surviving RS before stopping %s (eligible: %s)\n' \
+      "$departing" "$*" >"$OUT/scale-checkpoint/check.txt"
+    return 1
+  fi
+  scale_checkpoint_init "$observer" "$departing" || return 200
+  printf 'writer=%s observer=%s endpoint=%s departing=%s\n' "$writer" "$observer" "$endpoint" "$departing" \
+    >>"$SCALE_CHECKPOINT_DIR/history.txt" || return 200
+  if [ -n "$SCALE_WRITER" ]; then probe="$probe-$SCALE_CHECKPOINT_NUMBER"; fi
+  if [ "$writer" != "$SCALE_WRITER" ]; then
+    scale_hold_writers "$writer" "$departing" "$direction" || return 200
+  fi
+  scale_wait "$writer has active load clients" rolling_clients_running "$writer" || return 200
+  scale_wait "$writer accepts a checkpoint write" scale_probe "$writer" "$departing" "$probe" || return 200
+  SCALE_WRITER=$writer
+  scale_writer_connected "$writer" "$endpoint" || return 1
+  SCALE_OBSERVER=$observer
+  SCALE_ENDPOINT=$endpoint
+}
+
+scale_combined_checkpoint() { # <departing> <down|up>
+  local departing=$1 direction=$2 node address SCALE_OBSERVER= SCALE_ENDPOINT=
+  local -a survivors=()
+  SCALE_WRITER=
+  mkdir -p "$OUT/scale-checkpoint" "$OUT/scale-progress" || return 1
+  for node in $RS_NODES; do
+    [ "$node" != "$departing" ] || continue
+    [ "$direction" = up ] || [ "$node" != "$REMOVED" ] || continue
+    address=$(docker inspect -f "{{(index .NetworkSettings.Networks \"$NETWORK\").IPAddress}}" "$node") || return 1
+    case $address in ''|*[!0-9.]*) return 1 ;; esac
+    survivors+=("$node=$address:8989")
+  done
+  while :; do
+    scale_wait "a writer confirms a write through a surviving RS" \
+      scale_prepare_routed_writer "$departing" "$direction" "${survivors[@]}" || return 1
+    scale_wait "$SCALE_OBSERVER reaches the checkpoint of $departing" \
+      scale_routed_checkpoint "$departing" "$direction" "$SCALE_WRITER" "$SCALE_ENDPOINT" || return 1
+    [ -z "$SCALE_OBSERVER" ] || return 0
+  done
+}
+
 # A restart removes the old container and with it its bootstrap timings. Keep the
 # Docker timestamp on each line so events from different processes can be correlated.
 scale_capture_bootstrap() { # <node> <generation>
@@ -347,33 +440,33 @@
 }
 
 scale_roll() { # <node> <peer list>
-  local node=$1 peers=$2 role=directory from observer= writer= candidate direction=up
+  local node=$1 peers=$2 role=directory from observer= writer= candidate direction=up SCALE_WRITER=
   [ "$peers" != "$SHORT_PEERS" ] || direction=down
-  for candidate in $RS_NODES; do
-    [ "$candidate" != "$REMOVED" ] && [ "$candidate" != "$node" ] || continue
-    observer=$candidate; break
-  done
-  [ -n "$observer" ] || { fail "no replication server remains while rolling $node"; return 1; }
-  for candidate in $DS_NODES; do
-    [ "$candidate" != "$REMOVED" ] && [ "$candidate" != "$node" ] || continue
-    writer=$candidate; break
-  done
-  [ -n "$writer" ] || { fail "no directory server remains while rolling $node"; return 1; }
-  [ "$TOPOLOGY" != combined ] || role=combined
-  case " $RS_ONLY " in *" $node "*) role=replication ;; esac
   SCALE_HELD_WRITERS=
   if [ "$TOPOLOGY" = combined ]; then
-    scale_hold_writers "$observer" "$node" "$direction" || { scale_release_writers || :; return 1; }
-  elif is_directory_node "$node"; then
-    rolling_pause "$node" || return 1
+    role=combined
+    scale_combined_checkpoint "$node" "$direction" || { scale_release_writers || :; return 1; }
+    writer=$SCALE_WRITER
+  else
+    for candidate in $RS_NODES; do
+      [ "$candidate" != "$REMOVED" ] && [ "$candidate" != "$node" ] || continue
+      observer=$candidate; break
+    done
+    [ -n "$observer" ] || { fail "no replication server remains while rolling $node"; return 1; }
+    for candidate in $DS_NODES; do
+      [ "$candidate" != "$REMOVED" ] && [ "$candidate" != "$node" ] || continue
+      writer=$candidate; break
+    done
+    [ -n "$writer" ] || { fail "no directory server remains while rolling $node"; return 1; }
+    case " $RS_ONLY " in *" $node "*) role=replication ;; esac
+    if is_directory_node "$node"; then rolling_pause "$node" || return 1; fi
+    case " $RS_NODES " in
+      *" $node "*)
+        scale_checkpoint_wait "$observer reaches a checkpoint of $node" "$observer" "$node" || return 1
+        window_open_key "$direction-$node" "dns:$REMOVED" "dns:$node" || return 1 ;;
+      *) window_open_key "$direction-$node" "dns:$REMOVED" || return 1 ;;
+    esac
   fi
-  case " $RS_NODES " in
-    *" $node "*) scale_checkpoint_wait "$observer reaches a checkpoint of $node" "$observer" "$node" || { scale_release_writers || :; return 1; } ;;
-  esac
-  case " $RS_NODES " in
-    *" $node "*) window_open_key "$direction-$node" "dns:$REMOVED" "dns:$node" || { scale_release_writers || :; return 1; } ;;
-    *) window_open_key "$direction-$node" "dns:$REMOVED" || { scale_release_writers || :; return 1; } ;;
-  esac
   from=$(date +%s) || { scale_release_writers || :; return 1; }
   docker stop -t 120 "$node" >/dev/null || { scale_release_writers || :; return 1; }
   scale_release_writers || return 1
@@ -743,18 +836,18 @@
 }
 
 disturb() {
-  local node from end address surviving_rs= REJOINED_WITH_NEW_ID=0
+  local node from end address surviving_rs= REJOINED_WITH_NEW_ID=0 SCALE_WRITER=
   local -a return_window=("dns:$REMOVED" 180) recovery=() certificate_peers=()
   from=$(date +%s) || return 1
   SCALE_HELD_WRITERS=
   if [ "$TOPOLOGY" = combined ]; then
-    scale_hold_writers "$SURVIVING_RS" "$REMOVED" down || { scale_release_writers || :; return 1; }
-  elif is_directory_node "$REMOVED"; then
-    rolling_pause "$REMOVED" || return 1
+    scale_combined_checkpoint "$REMOVED" down || { scale_release_writers || :; return 1; }
+  else
+    if is_directory_node "$REMOVED"; then rolling_pause "$REMOVED" || return 1; fi
+    scale_checkpoint_wait "$SURVIVING_RS reaches the departing RS checkpoint" "$SURVIVING_RS" "$REMOVED" || return 1
+    dns_window_open "$REMOVED" || return 1
+    window_open "dns:$REMOVED" 180 || return 1
   fi
-  scale_checkpoint_wait "$SURVIVING_RS reaches the departing RS checkpoint" "$SURVIVING_RS" "$REMOVED" || { scale_release_writers || :; return 1; }
-  dns_window_open "$REMOVED" || { scale_release_writers || :; return 1; }
-  window_open "dns:$REMOVED" 180 || { scale_release_writers || :; return 1; }
   docker stop -t 120 "$REMOVED" >/dev/null || { scale_release_writers || :; return 1; }
   scale_release_writers || return 1
   ABSENCE_START=$(date +%s) || return 1

--
Gitblit v1.10.0