mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
91 mins ago f82d22de94de20cb44775530e934eb8c26368484
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
#!/usr/bin/env bash
#
# The contents of this file are subject to the terms of the Common Development and
# Distribution License (the License). You may not use this file except in compliance with the
# License.
#
# You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
# specific language governing permission and limitations under the License.
#
# When distributing Covered Software, include this CDDL Header Notice in each file and include
# the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
# Header, with the fields enclosed by brackets [] replaced by your own identifying
# information: "Portions copyright [year] [name of copyright owner]".
#
# Copyright 2026 3A Systems, LLC.
 
set -e -o pipefail
HERE=$(cd "$(dirname "$0")" && pwd)
OUT=$(mktemp -d)
trap 'rm -rf "$OUT"' EXIT
if [ ! -f "$HERE/disturb-scale.sh" ]; then
  echo 'scale disturbance is missing' >&2
  exit 1
fi
 
run_case() (
  local topology=$1 mode=$2 new_id=${3:-0} target n mappings
  OUT="$OUT/$topology-$mode-$new_id"
  mkdir -p "$OUT/logs" "$OUT/scale-checkpoint"
  if [ "$topology" = combined ]; then
    DS_NODES='d0 d1 d2 d3'; RS_ONLY=; RS_NODES=$DS_NODES; target=d3
  else
    DS_NODES='d0 d1 d2 d3'; RS_ONLY='r0 r1 r2'; RS_NODES=$RS_ONLY; target=r2
  fi
  ALL_NODES="$DS_NODES${RS_ONLY:+ $RS_ONLY}"
  PEERS=$(echo "$ALL_NODES" | tr ' ' ',')
  TOPOLOGY=$topology SCALE_VOLUME=$mode DISTURB_TIMEOUT=900 ROLLING_END=$((SECONDS + 900))
  NETWORK=fixture MONITOR_DOMAIN='cn=dc_example_dc_com,cn=Replication,cn=monitor'
  BASE_DN=dc=example,dc=com ROOT_PASSWORD=password
  . "$HERE/disturb-scale.sh"
  record() { printf '%s\n' "$*" >>"$OUT/calls"; }
  docker() {
    record "docker $*"
    if [ "$1" = stop ] && [ "${*: -1}" = "${FAIL_STOP:-}" ]; then return 1; fi
    case $1 in
      cp)
        if [ "$3" = - ]; then
          [ "${FAIL_CHANGELOG_COPY:-0}" = 0 ] || return 1
          [ "$2" != "${FAIL_CHANGELOG_SOURCE:-none}:/opt/opendj/data/changelogDb" ] || return 1
          printf 'old changelog\n'
        else
          printf 'old log\n' >"$3"
        fi
        ;;
      logs) echo 'old container log' ;;
      inspect)
        case ${*: -1} in
          d0) echo 172.18.0.9 ;;
          d1) echo 172.18.0.10 ;;
          d2) echo 172.18.0.11 ;;
          d3) echo 172.18.0.12 ;;
          r0) echo 172.18.0.18 ;;
          r1) echo 172.18.0.19 ;;
          r2) echo 172.18.0.20 ;;
          *) return 1 ;;
        esac ;;
      exec)
        if [ "${3:-}" = sh ]; then
          [ "${FAIL_OFFLINE_SNAPSHOT:-0}" = 0 ] || return 1
          printf 'domains.state\n1: dc=example,dc=com\n1.dom/1.server/offline.state 0000000000000001000100000001\n'
        else cat >"$OUT/probe.ldif"; [ "${FAIL_PROBE:-0}" = 0 ]; fi ;;
    esac
  }
  start_server() { record "start $1 $2 $PEERS"; [ "$#" -le 2 ] || record "start-options $1 ${*:3}"; }
  admin_search() { echo 'scale checkpoint must not start an admin-search JVM' >&2; return 1; }
  scale_monitor_search() {
    if [ "$2" = connected-to ]; then
      printf '%s:8989\n' "$(docker inspect "$1")"
    else
      record "search $1"; echo 'baseline'
    fi
  }
  scale_caught_up() { record "wait scale_caught_up $*"; [ "${FAIL_CHECKPOINT:-0}" != 1 ]; }
  window_open() { record "window $*"; }; window_close() { :; }
  window_open_key() { record "key-open $1"; shift; record "window $*"; }
  window_close_key() { record "window-close $1"; }
  dns_window_open() { record "dns-open $*"; }; dns_window_close() { record dns-close; }
  errors_lines() { echo 10; }
  scale_record_certificate_recoveries() { record "certificate-proof $*"; }
  rolling_request_pause() { record "request-pause $1"; }
  rolling_pause() { record "pause $1"; [ "$1" != "${FAIL_PAUSE:-}" ]; }
  rolling_resume() { record "resume $1"; }
  record_rejoined_id() { REJOINED_WITH_NEW_ID=$new_id; record "identity $1"; }
  scale_backlog() { record "fill $REMOVED"; }
  wait_until() {
    case $3 in scale_prepare_routed_writer|scale_routed_checkpoint) "${@:3}"; return $? ;; esac
    record "wait ${*:3}"
    [ "${FAIL_CHECKPOINT:-0}" != 1 ] || [ "${3:-}" != scale_caught_up ] || return 1
    [ "${FAIL_RETURN_HISTORY:-0}" != 1 ] || [ "${3:-} ${4:-} ${5:-}" != 'scale_caught_up r2 r0' ] || return 1
    [ "${FAIL_JOIN:-0}" != 1 ] || [ "${3:-}" != bootstrap_ended ] || return 1
    [ "${FAIL_RS:-0}" != 1 ] || [ "${3:-} ${4:-} ${5:-}" != 'scale_caught_up r1 r0' ] || return 1
    [ "${FAIL_PAIR_PROBE:-0}" != 1 ] || { [ "${3:-}" != scale_probe ] && [ "${3:-}" != scale_probe_batch ]; } || return 1
    [ "${FAIL_ACTIVE:-0}" != 1 ] || [ "${3:-}" != rolling_clients_running ] || return 1
    if [ "${3:-}" = record_rejoined_id ]; then record_rejoined_id "$4"; fi
    if [ "${3:-} ${4:-} ${5:-} ${6:-}" = 'scale_probe d3 d3 rejoined' ]; then
      FAIL_PROBE=${FAIL_REJOINED_WRITE:-0} scale_probe "$4" "$5" "$6" || return 1
      record 'rejoined write confirmed'
    fi
    return 0
  }
  date() { echo 400; }
  sleep() { record "sleep $*"; }
  fail() { echo "$*" >&2; return 1; }
  # d0 writes through the departing RS. d1 starts on d0, switches to d3
  # while log windows open, then switches to the departing RS during catch-up.
  if [ "$topology-$mode-$new_id" = combined-kept-0 ]; then (
    scale_monitor_search() {
      [ "$2" = connected-to ] && [ "$3" = "$MONITOR_DOMAIN" ] || return 1
      case $1 in
        d0) echo 172.18.0.11:8989 ;;
        d1)
          if [ -e "$OUT/blip" ]; then
            rm "$OUT/blip"; touch "$OUT/blipped"; echo 172.18.0.12:8989
          elif [ -e "$OUT/switched" ]; then echo 172.18.0.11:8989
          elif [ -e "$OUT/opened" ]; then echo 172.18.0.12:8989
          else echo 172.18.0.9:8989; fi ;;
        d3) echo 172.18.0.10:8989 ;;
        *) return 1 ;;
      esac
    }
    rolling_clients_running() { record "active $1"; }
    scale_probe() {
      record "write $*"
      [ "${FAIL_MOVED_WRITE:-0}" = 0 ] || [ ! -e "$OUT/opened" ]
    }
    scale_caught_up() {
      record "checkpoint $*"
      if [ "$1" = d3 ]; then touch "$OUT/switched"; fi
    }
    window_open_key() {
      local node
      for node in $ALL_NODES; do
        [ ! -e "$OUT/logs/$node.window-$1" ] || return 1
        touch "$OUT/logs/$node.window-$1"
      done
      if [ "${ROUTE_BLIP:-0}" = 1 ] && [ ! -e "$OUT/blipped" ]; then
        touch "$OUT/blip"
      else
        touch "$OUT/opened"
      fi
    }
    wait_until() {
      shift 2
      local try rc
      for try in 1 2 3 4; do
        "$@" && return 0; rc=$?
        [ "$rc" != 200 ] || return 1
      done
      return 1
    }
    started_since() { :; }; bootstrap_ended() { :; }
    if (FAIL_MOVED_WRITE=1; scale_roll d2 "$FULL_PEERS"); then
      echo 'accepted an unconfirmed write after the same DS changed serving RS' >&2; exit 1
    fi
    if grep -q '^docker stop ' "$OUT/calls"; then echo 'stopped after a failed route probe' >&2; exit 1; fi
    rm -f "$OUT/opened" "$OUT/switched"
    : >"$OUT/calls"
    ROUTE_BLIP=1 scale_roll d2 "$FULL_PEERS" || { echo 'could not roll d2 with a surviving route' >&2; exit 1; }
    awk '
      /^active d1$/ && !first { first=NR }
      /^checkpoint d0 d2$/ { observed=NR }
      /^write d1 d2 checkpoint-up/ { probes++; confirmed=NR }
      /^checkpoint d3 d2$/ { moved=NR }
      /^active d3$/ && !replacement { replacement=NR }
      /^resume d0$/ && !stopped { exit 1 }
      /^request-pause d1$/ { held=NR }
      /^checkpoint d1 d2$/ { checked=NR }
      /^docker stop -t 120 d2$/ { stopped=NR }
      /^write d3 d2 up$/ { written=NR }
      END { if (!(probes == 3 && first && first < observed && observed < confirmed && confirmed < moved &&
                  moved < replacement && replacement < held && held < checked && checked < stopped &&
                  stopped < written)) exit 1 }
    ' "$OUT/calls" || { echo 'checkpoint did not follow the active writer to a surviving RS' >&2; exit 1; }
    : >"$OUT/calls"
    # d3 is absent on scale-in. d0 and d1 both use d2, leaving no safe route.
    if scale_roll d2 "$SHORT_PEERS"; then echo 'accepted only departing routes' >&2; exit 1; fi
    if grep -Eq '^(request-pause|docker stop|checkpoint) ' "$OUT/calls"; then
      echo 'changed the topology before finding a surviving route' >&2; exit 1
    fi
  ); fi
  : >"$OUT/calls"
  # No server is stopped if the surviving RS has not reached the departing RS's checkpoint.
  if (FAIL_CHECKPOINT=1; disturb) >/dev/null 2>&1; then
    echo 'failed checkpoint was accepted' >&2; exit 1
  fi
  if grep '^docker stop' "$OUT/calls" >/dev/null; then
    echo 'stopped a node without a checkpoint' >&2; exit 1
  fi
  : >"$OUT/calls"
  if [ "$topology" = rs534 ] && [ "$mode" = empty ]; then
    if (FAIL_RETURN_HISTORY=1; disturb) >/dev/null 2>&1; then
      echo 'incomplete returned RS history was accepted' >&2; exit 1
    fi
    if grep -Fx 'key-open up-r0' "$OUT/calls" >/dev/null; then
      echo 'writers exposed to the returned RS before its history caught up' >&2; exit 1
    fi
    : >"$OUT/calls"
    rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
  fi
  # A failed stop must abort the roll rather than recreate that still-running node.
  if (FAIL_STOP=d0; disturb) >/dev/null 2>&1; then
    echo 'failed node stop was accepted' >&2; exit 1
  fi
  if grep '^start d0 ' "$OUT/calls" >/dev/null; then
    echo 'recreated a node whose stop failed' >&2; exit 1
  fi
  : >"$OUT/calls"
  rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
  if [ "$topology" = rs534 ] && [ "$mode" = empty ]; then
    if (FAIL_CHANGELOG_COPY=1; disturb) >/dev/null 2>&1; then
      echo 'failed original changelog snapshot was accepted' >&2; exit 1
    fi
    if grep -Fx 'docker rm r2' "$OUT/calls" >/dev/null; then
      echo 'removed RS container after its changelog snapshot failed' >&2; exit 1
    fi
    : >"$OUT/calls"
    rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
    if (FAIL_CHANGELOG_SOURCE=r1; disturb) >/dev/null 2>&1; then
      echo 'failed survivor changelog snapshot was accepted' >&2; exit 1
    fi
    if grep -Fx 'docker rm r1' "$OUT/calls" >/dev/null; then
      echo 'removed surviving RS container after its changelog snapshot failed' >&2; exit 1
    fi
    : >"$OUT/calls"
    rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
    if (FAIL_OFFLINE_SNAPSHOT=1; disturb) >/dev/null 2>&1; then
      echo 'failed offline-state snapshot was accepted' >&2; exit 1
    fi
    if grep '^fill ' "$OUT/calls" >/dev/null; then
      echo 'continued after a failed offline-state snapshot' >&2; exit 1
    fi
    : >"$OUT/calls"
    rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
  fi
  if [ "$topology" = rs534 ]; then
    # Do not stop either DS of a pair without a responding replication server.
    if (FAIL_RS=1; disturb) >/dev/null 2>&1; then echo 'unavailable RS accepted' >&2; exit 1; fi
    if grep -E '^docker stop .* (d[01]|r0)$' "$OUT/calls" >/dev/null; then echo 'group stopped without an RS' >&2; exit 1; fi
    : >"$OUT/calls"
    rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
    # A failed RS stop cannot be followed by any restart in the affected group.
    if (FAIL_STOP=r0; disturb) >/dev/null 2>&1; then echo 'failed RS stop accepted' >&2; exit 1; fi
    if grep -E '^start (d[01]|r0) ' "$OUT/calls" >/dev/null; then echo 'group restarted after failed RS stop' >&2; exit 1; fi
    : >"$OUT/calls"
    rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
    # A failed surviving-DS write cannot be followed by a pair restart.
    if (FAIL_PAIR_PROBE=1; disturb) >/dev/null 2>&1; then echo 'failed pair write accepted' >&2; exit 1; fi
    if grep -E '^start d[01] ' "$OUT/calls" >/dev/null; then echo 'pair restarted after failed write' >&2; exit 1; fi
    : >"$OUT/calls"
    rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
  fi
  if [ "$topology" = combined ]; then
    # Do not remove the node if the observer has no running load clients.
    if (FAIL_ACTIVE=1; disturb) >/dev/null 2>&1; then echo 'inactive observer accepted' >&2; exit 1; fi
    if grep '^docker stop' "$OUT/calls" >/dev/null; then echo 'stopped node without an active writer' >&2; exit 1; fi
    grep -Fx 'resume d1' "$OUT/calls" >/dev/null || { echo 'first held writer not resumed on failure' >&2; exit 1; }
    grep -Fx 'resume d2' "$OUT/calls" >/dev/null || { echo 'second held writer not resumed on failure' >&2; exit 1; }
    : >"$OUT/calls"
    # A failed drain also releases the surviving writers whose pauses were already requested.
    if (FAIL_PAUSE="$target"; disturb) >/dev/null 2>&1; then
      echo 'failed combined pause accepted' >&2; exit 1
    fi
    if grep '^docker stop' "$OUT/calls" >/dev/null; then echo 'stopped a node after a failed pause' >&2; exit 1; fi
    for n in d1 d2; do
      grep -Fx "resume $n" "$OUT/calls" >/dev/null || { echo "$n not resumed after a failed pause" >&2; exit 1; }
    done
    : >"$OUT/calls"
  fi
  # A failed join must prevent membership assertions and backlog creation.
  if (FAIL_JOIN=1; disturb) >/dev/null 2>&1; then
    echo 'failed join was accepted' >&2; exit 1
  fi
  if grep -E '^wait peer_removed |^fill ' "$OUT/calls" >/dev/null; then
    echo 'proceeded after a failed join' >&2; exit 1
  fi
  : >"$OUT/calls"
  rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
  if [ "$topology" = combined ] && { [ "$mode" = empty ] || [ "$new_id" = 1 ]; }; then
    if (FAIL_REJOINED_WRITE=1; disturb) >/dev/null 2>&1; then
      echo 'failed write from the rebuilt DS was accepted' >&2; exit 1
    fi
    if sed -n '/^start d3 /,$p' "$OUT/calls" | grep -Fx 'request-pause d3' >/dev/null; then
      echo 'rebuilt writer was held before its first write' >&2; exit 1
    fi
    : >"$OUT/calls"
    rm -f "$OUT/logs/$target.offline" "$OUT/logs/$target.previous"
  fi
  disturb
  # A recreated container loses its log: retain both rolling generations as well as
  # the final logs that a successful run will otherwise remove during cleanup.
  for n in $SURVIVORS; do
    [ -s "$OUT/bootstrap/down-$n.log" ] && [ -s "$OUT/bootstrap/up-$n.log" ] || { echo "missing saved bootstrap logs for $n" >&2; exit 1; }
  done
  [ -s "$OUT/bootstrap/removed-$target.log" ] || { echo 'missing removed node bootstrap log' >&2; exit 1; }
  collect_scale_bootstrap
  for n in $ALL_NODES; do [ -s "$OUT/bootstrap/final-$n.log" ] || { echo "missing final bootstrap log for $n" >&2; exit 1; }; done
  for phase in down return up; do
    for n in $RS_NODES; do
      [ "$phase" != down ] || [ "$n" != "$target" ] || continue
      [ -s "$OUT/scale-checkpoint/offline-$phase-$n.txt" ] || { echo "missing $phase offline state of $n" >&2; exit 1; }
      grep -q '^capture-start-ms [0-9]' "$OUT/scale-checkpoint/offline-$phase-$n.txt" || { echo 'missing snapshot capture time' >&2; exit 1; }
    done
  done
  if [ "$topology" = rs534 ] && [ "$mode" = empty ]; then
    for phase in down return up; do
      for n in r0 r1; do
        [ -s "$OUT/scale-checkpoint/offline-$phase-$n.txt" ] || { echo "missing $phase offline state of $n" >&2; exit 1; }
      done
    done
    # Exercise the real filter with the allowances passed by disturb() for the return.
    (
      . "$HERE/assert-converged.sh"
      local allowances warning recovery unexpected
      allowances=$(sed -n 's/^window \(.* scale-cert-return\)$/\1/p' "$OUT/calls")
      warning='[t] category=SYNC severity=WARNING msgID=312 msg=Replication server RS(31897) could not connect to replication server r2:8989 for domain "cn=admin data": SSLHandshakeException((bad_certificate) PKIX path validation failed)'
      recovery='[t] category=SYNC severity=NOTICE msgID=313 msg=Replication server RS(31897) connected to replication server r2:8989 for domain "cn=admin data", which it had reported it had no replication session with'
      since_mark() { printf '%s\n' "$warning" "$recovery"; }
      for n in r0 r1; do
        printf '0\n' >"$OUT/logs/$n.mark"
        printf '0 2 %s\n' "$allowances" >"$OUT/logs/$n.windows"
        unexpected=$(replication_errors "$n")
        if [ -n "$unexpected" ] || ! grep -Fxq "$warning" "$OUT/logs/$n.expected"; then
          echo "$n: recovered RS312 rejected by the scale return window" >&2
          exit 1
        fi
      done
    )
    [ -s "$OUT/scale-checkpoint/offline-return-r2.txt" ] || { echo 'missing return offline state of r2' >&2; exit 1; }
    [ -s "$OUT/scale-checkpoint/removed-r2-changelog.tar" ] || { echo 'missing original removed RS changelog' >&2; exit 1; }
    awk '
      $0 == "docker stop -t 120 r2" { stopped=NR }
      $0 == "docker cp r2:/opt/opendj/data/changelogDb -" { saved=NR }
      $0 == "docker rm r2" { removed=NR }
      END { if (!stopped || !saved || !removed || !(stopped < saved && saved < removed)) exit 1 }
    ' "$OUT/calls" || { echo 'removed RS changelog was not saved before container removal' >&2; exit 1; }
    for n in r0 r1; do
      [ -s "$OUT/scale-checkpoint/down-$n-changelog.tar" ] || { echo "missing $n changelog before restart" >&2; exit 1; }
      awk -v n="$n" '
        $0 == "docker stop -t 120 " n && !stopped { stopped=NR }
        $0 == "docker cp " n ":/opt/opendj/data/changelogDb -" && !saved { saved=NR }
        $0 == "docker rm " n && !removed { removed=NR }
        END { if (!stopped || !saved || !removed || !(stopped < saved && saved < removed)) exit 1 }
      ' "$OUT/calls" || { echo "$n changelog was not saved before restart" >&2; exit 1; }
    done
  fi
  # Every restarted directory server completes its join before measured load resumes.
  awk '
    $1 == "start" { started[$2]=1; joined[$2]=0 }
    $1 == "wait" && $2 == "bootstrap_ended" { joined[$3]=1 }
    $1 == "resume" && started[$2] && !joined[$2] { exit 1 }
    END { for (node in started) if (!joined[node]) exit 1 }
  ' "$OUT/calls" || { echo 'restarted writer resumed before bootstrap completed' >&2; exit 1; }
  if [ "$topology" = combined ]; then
    # Each checkpoint drains all present writers except its observer. Request their
    # pauses before waiting on any one of them so the segment ends can overlap.
    awk -v nodes="$DS_NODES" -v target="$target" '
      BEGIN { count=split(nodes, writers, " ") }
      $1 == "start" && $2 == target { returned=1 }
      $1 == "request-pause" { if (waiting) exit 1; requested[$2]=1 }
      $1 == "pause" { if (!requested[$2]) exit 1; waiting=1; drained[$2]=1 }
      $1 == "wait" && $2 == "rolling_clients_running" {
        for (i=1; i<=count; i++) {
          node=writers[i]
          needed=node != $3 && (node != target || !removed || returned)
          if (needed != (node in drained) || needed != (node in requested)) exit 1
        }
        observed=1
      }
      $1 == "wait" && $2 == "scale_caught_up" {
        if (!observed) exit 1
        checked=1
      }
      $1 == "docker" && $2 == "stop" {
        if (!checked || !drained[$NF]) exit 1
        if ($NF == target) removed=1
        for (node in requested) delete requested[node]
        for (node in drained) delete drained[node]
        waiting=observed=checked=0
      }
    ' "$OUT/calls" || { echo 'combined pauses were not requested together before draining checkpoint writers' >&2; exit 1; }
    # A combined node must serve again before another is stopped.
    awk -v target="$target" '
      $1 == "docker" && $2 == "stop" && $NF != target { if (down) exit 1; down=$NF }
      $1 == "wait" && $2 == "started_since" && $3 == down { down="" }
      END { if (down) exit 1 }
    ' "$OUT/calls" || { echo 'more than one combined survivor stopped at a time' >&2; exit 1; }
  else
    # Finish a whole group before resuming either writer or starting the next group.
    awk '
      $1 == "start" && $2 == "r0" { joined=0 }
      $1 == "wait" && $2 == "bootstrap_ended" && $3 ~ /^(d0|d1|r0)$/ { joined++ }
      $1 == "resume" && $2 ~ /^d[01]$/ && joined != 3 { exit 1 }
      $1 == "docker" && $2 == "stop" && $NF ~ /^(d2|d3|r1)$/ && joined != 3 { exit 1 }
    ' "$OUT/calls" || { echo 'next rolling group or load resumed before the first group joined' >&2; exit 1; }
    # The first pair and one RS overlap; the other RS must remain up until the first serves.
    awk '
      $1 == "docker" && $2 == "stop" && $NF == "d0" && !first {first=NR}
      $1 == "docker" && $2 == "stop" && $NF == "d1" && !second {second=NR}
      $1 == "docker" && $2 == "stop" && $NF == "r0" && !rs {rs=NR; rsdown=1}
      $1 == "wait" && $2 == "started_since" && $3 == "r0" {rsdown=0}
      $1 == "docker" && $2 == "stop" && $NF == "r1" && rsdown {exit 1}
      $1 == "start" && $2 == "d0" && first && !started {started=NR}
      END {if (!first || !second || !rs || !started || !(first < started && second < started && rs < started)) exit 1}
    ' "$OUT/calls" || { echo 'DS and RS transitions did not overlap safely' >&2; exit 1; }
    grep -Fx 'request-pause d0' "$OUT/calls" >/dev/null || { echo 'first pair pause not requested' >&2; exit 1; }
    grep -Fx 'request-pause d1' "$OUT/calls" >/dev/null || { echo 'second pair pause not requested' >&2; exit 1; }
    awk '
      $0 == "request-pause d0" && !a {a=NR}
      $0 == "request-pause d1" && !b {b=NR}
      $0 == "pause d0" && !c {c=NR}
      END {if (!a || !b || !c || !(a < c && b < c)) exit 1}
    ' "$OUT/calls" || { echo 'pair pauses were not requested together' >&2; exit 1; }
    grep -Fx 'wait scale_probe_batch d2 down d0 d1 r0' "$OUT/calls" >/dev/null || { echo 'group write used no surviving DS' >&2; exit 1; }
    awk '
      $0 == "key-open down-r0" && !opened {opened=NR}
      $0 == "wait scale_caught_up r1 r0" && !checked {checked=NR}
      $0 == "docker stop -t 120 d0" && !stopped {stopped=NR}
      END {if (!opened || !checked || !stopped || !(opened < checked && checked < stopped)) exit 1}
    ' "$OUT/calls" || { echo 'RS checkpoint was not taken after preparing log windows' >&2; exit 1; }
    awk '
      $0 == "key-open down-d0" && !a {a=NR}
      $0 == "key-open down-d1" && !b {b=NR}
      $1 == "docker" && $2 == "stop" && $NF == "d0" && !c {c=NR}
      $1 == "docker" && $2 == "stop" && $NF == "d1" && !d {d=NR}
      $0 == "wait scale_probe_batch d2 down d0 d1 r0" && !p {p=NR}
      END {if (!a || !b || !c || !d || !p || !(a < c && b < c && c < p && d < p)) exit 1}
    ' "$OUT/calls" || { echo 'pair windows or write probe occurred in the wrong order' >&2; exit 1; }
  fi
  expected_windows=4
  [ "$(grep -c '^sleep 10$' "$OUT/calls")" -eq "$expected_windows" ] || { echo 'missing handshake grace after a transition' >&2; exit 1; }
  expected_checkpoints=$((1 + 2 * ($(echo "$RS_NODES" | wc -w) - 1)))
  if [ "$topology" = rs534 ] && [ "$mode" = empty ]; then
    expected_checkpoints=$((expected_checkpoints + 1))
    grep -Fx 'start-options r2 -e REPLICATION_RECOVER_FROM_RS=1' "$OUT/calls" >/dev/null || { echo 'empty RS returned without recovery-only bootstrap' >&2; exit 1; }
    awk '
      $0 == "wait bootstrap_ended r2" { joined=NR }
      $0 == "wait scale_caught_up r2 r0" { copied=NR }
      $0 == "key-open up-r0" { exposed=NR }
      END { if (!joined || !copied || !exposed || !(joined < copied && copied < exposed)) exit 1 }
    ' "$OUT/calls" || { echo 'returned RS was exposed before retained history catch-up' >&2; exit 1; }
  fi
  if [ "$topology" != rs534 ] || [ "$mode" != empty ]; then
    if grep -q 'REPLICATION_RECOVER_FROM_RS' "$OUT/calls"; then echo 'recovery-only bootstrap changed another scale variant' >&2; exit 1; fi
  fi
  [ "$(grep -c '^wait scale_caught_up ' "$OUT/calls")" -eq "$expected_checkpoints" ] || { echo 'a rolling RS lacked a checkpoint' >&2; exit 1; }
  grep -F 'wait scale_probe' "$OUT/calls" >/dev/null || { echo 'no confirmed write required while nodes are stopped' >&2; exit 1; }
  grep -Fx "dns-open $target" "$OUT/calls" >/dev/null || { echo 'absence DNS window not opened' >&2; exit 1; }
  grep -Fx 'dns-close' "$OUT/calls" >/dev/null || { echo 'absence DNS window not closed' >&2; exit 1; }
  grep -Fx "fill $target" "$OUT/calls" >/dev/null || { echo "backlog was not generated" >&2; exit 1; }
  awk -v target="$target" -v survivor="${RS_NODES%% *}" '
    $1 == "docker" && $2 == "stop" && $NF == target { stopped = 1 }
    $1 == "search" && $2 == survivor { if (!stopped) exit 1; baseline = 1 }
    END { if (!baseline) exit 1 }' "$OUT/calls" || { echo 'backlog baseline was not captured after stop' >&2; exit 1; }
  [ -s "$OUT/scale-checkpoint/absent-start.ldif" ] || { echo 'backlog baseline missing' >&2; exit 1; }
  awk -v target="$target" -v full="$FULL_PEERS" '
    $1 == "start" && $2 == target { returned = 1 }
    $1 == "start" && $2 != target && $4 == full && !returned { exit 1 }
    END { if (!returned) exit 1 }' "$OUT/calls" || { echo 'survivor rolled before the node returned' >&2; exit 1; }
  [ "$(grep -c '^wait peer_removed ' "$OUT/calls")" -eq "$(($(echo "$ALL_NODES" | wc -w) - 1))" ]
  [ "$(grep -c '^wait peer_present ' "$OUT/calls")" -eq "$(($(echo "$ALL_NODES" | wc -w) - 1))" ] || { echo 'return not checked on every survivor' >&2; exit 1; }
  for n in $ALL_NODES; do
    [ "$n" = "$target" ] && continue
    grep -Fx "window-close down-$n" "$OUT/calls" >/dev/null || { echo "down window for $n left open" >&2; exit 1; }
    grep -Fx "window-close up-$n" "$OUT/calls" >/dev/null || { echo "up window for $n left open" >&2; exit 1; }
    [ "$(grep -c "^start $n " "$OUT/calls")" -eq 2 ] || { echo "$n was not rolled twice" >&2; exit 1; }
  done
  if [ "$mode" = empty ]; then
    grep -Fx "docker volume rm vol-$target" "$OUT/calls" >/dev/null
    [ -s "$OUT/logs/$target.previous" ]
    grep -F "cert:d0:$target" "$OUT/calls" >/dev/null || { echo 'missing exact certificate window' >&2; exit 1; }
    grep -E '^window .* scale-cert-return$' "$OUT/calls" >/dev/null || { echo 'missing scale certificate window' >&2; exit 1; }
    mappings='172.18.0.12 d0=172.18.0.9 d1=172.18.0.10 d2=172.18.0.11'
    if [ "$topology" = rs534 ]; then
      mappings="172.18.0.20 ${mappings#* } d3=172.18.0.12 r0=172.18.0.18 r1=172.18.0.19"
    fi
    grep -Fx "certificate-proof $mappings" "$OUT/calls" >/dev/null || { echo 'wrong certificate proof addresses' >&2; exit 1; }
    awk '
      /^wait peer_present / { checked=NR }
      /^certificate-proof / { proof=NR }
      END { if (!checked || !proof || checked >= proof) exit 1 }
    ' "$OUT/calls" || { echo 'certificate recovery checked before scale-out membership settled' >&2; exit 1; }
  else
    if grep -E 'docker volume rm|scale-cert-return|certificate-proof' "$OUT/calls" >/dev/null; then
      echo 'kept volume was treated as a rebuild' >&2; exit 1
    fi
  fi
  if [ "$topology" = combined ]; then
    grep -Fx "identity $target" "$OUT/calls" >/dev/null
    grep -Fx "window dns:$target dns:d0" "$OUT/calls" >/dev/null || { echo 'rolling combined node lacks its DNS allowance' >&2; exit 1; }
    grep -Fx "pause $target" "$OUT/calls" >/dev/null
    grep -Fx "resume $target" "$OUT/calls" >/dev/null
    grep -Fx 'wait rolling_clients_running d0' "$OUT/calls" >/dev/null || { echo 'checkpoint writer was not checked' >&2; exit 1; }
    grep -Fx 'wait scale_probe d0 d3 checkpoint-down' "$OUT/calls" >/dev/null || { echo 'checkpoint write was not confirmed' >&2; exit 1; }
    awk '
      $0 == "request-pause d1" && !a {a=NR}
      $0 == "request-pause d2" && !b {b=NR}
      $0 == "docker stop -t 120 d3" && !c {c=NR}
      $0 == "resume d1" && !d {d=NR}
      $0 == "resume d2" && !e {e=NR}
      END {if (!a || !b || !c || !d || !e || !(a < c && b < c && c < d && c < e)) exit 1}
    ' "$OUT/calls" || { echo 'other writers were not held until the node stopped' >&2; exit 1; }
    awk '
      $1 == "start" && $2 == "d3" && !returned {returned=NR}
      returned && $0 == "request-pause d3" && !held {held=NR}
      held && $0 == "docker stop -t 120 d0" && !stopped {stopped=NR}
      stopped && $0 == "resume d3" && !released {released=NR}
      END {if (!returned || !held || !stopped || !released || !(returned < held && held < stopped && stopped < released)) exit 1}
    ' "$OUT/calls" || { echo 'returned writer was not held during scale-out checkpoint' >&2; exit 1; }
    if [ "$mode" = empty ] || [ "$new_id" = 1 ]; then
      awk '
        /^start d3 / { returned=1 }
        /^identity d3$/ { identity=NR }
        identity && /^resume d3$/ && !resumed { resumed=NR }
        /^rejoined write confirmed$/ { written=NR }
        returned && /^request-pause d3$/ && !held { held=NR }
        END { if (!(identity && identity < resumed && resumed < written && written < held)) exit 1 }
      ' "$OUT/calls" || { echo 'rebuilt DS did not write after recording its ID and before being held' >&2; exit 1; }
      grep -Fx 'dn: uid=scale-rejoined-d3,ou=People,dc=example,dc=com' "$OUT/probe.ldif" >/dev/null
    elif grep -Fxq 'rejoined write confirmed' "$OUT/calls"; then
      echo 'kept replica with the same ID ran a rejoin write probe' >&2; exit 1
    fi
  fi
  [ ! -e "$OUT/logs/$target.offline" ]
  # A failed observer query identifies the node and status in the checkpoint diagnostic.
  (
    . "$HERE/disturb-scale.sh"
    scale_monitor_search() { [ "$1" != observer ] || return 124; echo baseline; }
    scale_checkpoint_init observer departing
    if scale_caught_up observer departing; then echo 'failed checkpoint query accepted' >&2; exit 1; fi
    grep -F 'observer' "$OUT/scale-checkpoint/check.txt" >/dev/null || { echo 'checkpoint diagnostic omitted observer' >&2; exit 1; }
    grep -F '124' "$OUT/scale-checkpoint/check.txt" >/dev/null || { echo 'checkpoint diagnostic omitted failure status' >&2; exit 1; }
  )
  backlog_ldif "scale-backlog-$target" "$BASE_DN" 1 3 >"$OUT/fill.ldif"
  [ "$(grep -c '^changetype: modify$' "$OUT/fill.ldif")" -eq 3 ] || { echo 'wrong number of fill writes' >&2; exit 1; }
  grep -Fx 'description: scale-3' "$OUT/fill.ldif" >/dev/null || { echo 'fill values are not distinct' >&2; exit 1; }
  scale_probe d0 d1 down || { echo 'confirmed probe was rejected' >&2; exit 1; }
  grep -Fx 'dn: uid=scale-down-d1,ou=People,dc=example,dc=com' "$OUT/probe.ldif" >/dev/null
  if (FAIL_PROBE=1; scale_probe d0 d1 up); then echo 'failed LDAP write accepted' >&2; exit 1; fi
  if [ "$topology" = rs534 ]; then
    scale_probe_batch d2 down d0 d1 r0 || { echo 'confirmed batch probe was rejected' >&2; exit 1; }
    [ "$(grep -c '^dn: uid=scale-down-' "$OUT/probe.ldif")" -eq 3 ] || { echo 'batch lacks a write per stopped node' >&2; exit 1; }
    grep -Fx 'dn: uid=scale-down-r0,ou=People,dc=example,dc=com' "$OUT/probe.ldif" >/dev/null || { echo 'batch lacks the RS write' >&2; exit 1; }
    if (FAIL_PROBE=1; scale_probe_batch d2 up d0 d1 r0); then echo 'failed batch accepted' >&2; exit 1; fi
  fi
)
check_checkpoint_budget() ( # <load end> <caught up at> <expected result> <expected time> [route change at]
  local caught_up_at=$2 switch_at=${5:-} result=0
  OUT="$OUT/budget-$1-$2-$switch_at"
  mkdir -p "$OUT"
  TOPOLOGY=combined SCALE_VOLUME=kept
  . "$HERE/lib.sh"
  . "$HERE/disturb-scale.sh"
  # Use the real wait loop with a deterministic clock. Writer preparation takes 80 s.
  unset SECONDS
  SECONDS=0 ROLLING_END=$1 DISTURB_TIMEOUT=900
  sleep() { SECONDS=$((SECONDS + $1)); }
  rolling_request_pause() { :; }
  rolling_pause() { [ "$1" != "$REMOVED" ] || sleep 80; }
  rolling_clients_running() { :; }
  scale_probe() { :; }
  docker() {
    local node=${*: -1}
    [ "$1" = inspect ] && echo "172.18.0.1${node##*-c}"
  }
  scale_monitor_search() {
    if [ -n "$switch_at" ] && [ "$SECONDS" -ge "$switch_at" ]; then
      echo '172.18.0.11:8989'
    else
      echo '172.18.0.10:8989'
    fi
  }
  scale_caught_up() { [ "$SECONDS" -ge "$caught_up_at" ]; }
  dns_window_open() { :; }
  window_open() { :; }
  scale_combined_checkpoint "$REMOVED" down >"$OUT/result" 2>&1 || result=$?
  if [ "$result" != "$3" ] || [ "$SECONDS" != "$4" ]; then
    cat "$OUT/result" >&2
    echo "checkpoint with load end $1 and catch-up at $2 returned $result at $SECONDS, expected $3 at $4" >&2
    exit 1
  fi
)
check_checkpoint_budget 2580 940 0 940
check_checkpoint_budget 880 940 1 880
check_checkpoint_budget 2580 1000 1 980
# Switching RS at 900 starts a fresh checkpoint, still bounded by the same load end.
check_checkpoint_budget 2580 1700 0 1700 900
check_checkpoint_budget 1500 1700 1 1500 900
 
for topology in rs534 combined; do
  for mode in kept empty; do run_case "$topology" "$mode"; done
done
run_case combined kept 1
# The fast monitor connects only to a numeric port published on IPv4 loopback.
(
  OUT="$OUT/loopback"; mkdir -p "$OUT/scale-checkpoint"
  TOPOLOGY=combined SCALE_VOLUME=kept DS_NODES='d0 d1 d2' RS_NODES=$DS_NODES RS_ONLY=
  ALL_NODES=$DS_NODES PEERS=d0,d1,d2 ROOT_PASSWORD=secret ROLLING_END_MS=2000000000000
  . "$HERE/disturb-scale.sh"
  docker() { [ "$1" = port ] || return 1; printf '%s\n' "$MAPPING"; }
  python3() { printf '%s\n' "$*" >"$OUT/arguments"; cat >/dev/null; printf 'snapshot\n'; }
  MAPPING='127.0.0.1:12345'
  [ "$(scale_monitor_search d0)" = snapshot ] || { echo 'loopback monitor rejected' >&2; exit 1; }
  grep -F '12345 2000000000000' "$OUT/arguments" >/dev/null || { echo 'port or deadline not passed' >&2; exit 1; }
  if grep -F 'secret' "$OUT/arguments" >/dev/null; then echo 'password appeared in process arguments' >&2; exit 1; fi
  for MAPPING in '0.0.0.0:12345' '127.0.0.1:not-a-port' '127.0.0.1:12345 extra'; do
    if scale_monitor_search d0 >/dev/null 2>&1; then echo "unsafe port mapping accepted: $MAPPING" >&2; exit 1; fi
  done
)
# Repeated comparisons must use one immutable reference, and retain every result.
(
  OUT="$OUT/checkpoint-history"; mkdir -p "$OUT/scale-checkpoint"
  TOPOLOGY=combined SCALE_VOLUME=kept DS_NODES='d0 d1 d2' RS_NODES=$DS_NODES RS_ONLY=
  ALL_NODES=$DS_NODES PEERS=d0,d1,d2 BASE_DN=dc=example,dc=com
  DISTURB_TIMEOUT=900 ROLLING_END=$((SECONDS + 900))
  . "$HERE/disturb-scale.sh"
  echo '1 d0' >"$OUT/server-ids.txt"
  scale_monitor_search() {
    echo "$1" >>"$OUT/queries"
    local csn=000001a1a1e836e400010000000a
    if [ "$1" = observer ] && [ ! -f "$OUT/first-observer" ]; then
      csn=000001a1a1e836e4000100000009; touch "$OUT/first-observer"
    fi
    printf 'dn: cn=one,cn=monitor\ndomain-name: %s\nreplicationServer-database: 1\nlast-change: %s\n\n' "$BASE_DN" "$csn"
  }
  scale_checkpoint_init observer departing
  if scale_caught_up observer departing; then echo 'older position accepted' >&2; exit 1; fi
  scale_caught_up observer departing || { echo 'caught-up position rejected' >&2; exit 1; }
  [ "$(grep -c '^departing$' "$OUT/queries")" -eq 2 ] || { echo 'reference was resampled before catch-up or not refreshed after it' >&2; exit 1; }
  [ "$(grep -c '^observer$' "$OUT/queries")" -eq 3 ] || { echo 'missing observer retry or fresh confirmation' >&2; exit 1; }
  grep -F 'needs' "$SCALE_CHECKPOINT_DIR/attempt-0001.compare" >/dev/null || { echo 'first comparison was not retained' >&2; exit 1; }
  [ -f "$SCALE_CHECKPOINT_DIR/attempt-0002.compare" ] || { echo 'second comparison was not retained' >&2; exit 1; }
  [ "$(grep -c 'observer' "$SCALE_CHECKPOINT_DIR/history.txt")" -ge 2 ] || { echo 'query history missing' >&2; exit 1; }
  scale_monitor_search() { echo 'timeout=query' >&2; return 124; }
  if scale_caught_up observer departing; then echo 'timed-out query accepted' >&2; exit 1; fi
  grep -F 'timeout=query' "$SCALE_CHECKPOINT_DIR/history.txt" >/dev/null || { echo 'query timeout not distinguished' >&2; exit 1; }
  scale_monitor_search() { echo 'timeout=load' >&2; return 124; }
  if scale_caught_up observer departing; then echo 'reported load deadline accepted' >&2; exit 1; fi
  grep -F 'timeout=load' "$SCALE_CHECKPOINT_DIR/history.txt" >/dev/null || { echo 'reported load deadline not distinguished' >&2; exit 1; }
  scale_monitor_search() { return 124; }
  ROLLING_END=$SECONDS
  if scale_caught_up observer departing; then echo 'load deadline accepted' >&2; exit 1; fi
  grep -F 'timeout=load' "$SCALE_CHECKPOINT_DIR/history.txt" >/dev/null || { echo 'load deadline not distinguished' >&2; exit 1; }
)
# A new origin can be absent on both RSs when a checkpoint first samples them.
(
  OUT="$OUT/checkpoint-new-origin"; mkdir -p "$OUT/scale-checkpoint"
  TOPOLOGY=combined SCALE_VOLUME=empty DS_NODES='d0 d1 d2' RS_NODES=$DS_NODES RS_ONLY=
  ALL_NODES=$DS_NODES PEERS=d0,d1,d2 BASE_DN=dc=example,dc=com
  DISTURB_TIMEOUT=900 ROLLING_END=$((SECONDS + 900))
  . "$HERE/disturb-scale.sh"
  printf '1 d0\n9 d2\n' >"$OUT/server-ids.txt"
  scale_monitor_search() {
    printf 'dn: cn=one,cn=monitor\ndomain-name: %s\nreplicationServer-database: 1\nlast-change: 000001a1a1e836e400010000000a\n\n' "$BASE_DN"
    if [ -f "$OUT/$1-new-origin" ]; then
      printf 'dn: cn=nine,cn=monitor\ndomain-name: %s\nreplicationServer-database: 9\nlast-change: 000001a1a1e836e4000900000001\n\n' "$BASE_DN"
    fi
  }
  scale_checkpoint_init observer departing
  if scale_caught_up observer departing; then echo 'missing new origin was accepted' >&2; exit 1; fi
  touch "$OUT/departing-new-origin"
  if scale_caught_up observer departing; then echo 'observer without new origin was accepted' >&2; exit 1; fi
  grep -F 'DS(9): needs' "$SCALE_CHECKPOINT_DIR/attempt-0002.compare" >/dev/null || { echo 'departing node was not resampled for the new origin' >&2; exit 1; }
  touch "$OUT/observer-new-origin"
  scale_caught_up observer departing || { echo 'checkpoint did not recover when both new positions appeared' >&2; exit 1; }
)
# A caught-up old checkpoint cannot authorize stopping a server whose current position advanced.
(
  OUT="$OUT/checkpoint-freshness"; mkdir -p "$OUT/scale-checkpoint"
  TOPOLOGY=combined SCALE_VOLUME=kept DS_NODES='d0 d1 d2' RS_NODES=$DS_NODES RS_ONLY=
  ALL_NODES=$DS_NODES PEERS=d0,d1,d2 BASE_DN=dc=example,dc=com
  DISTURB_TIMEOUT=900 ROLLING_END=$((SECONDS + 900))
  . "$HERE/disturb-scale.sh"
  echo '1 d0' >"$OUT/server-ids.txt"
  scale_monitor_search() {
    local csn=000001a1a1e836e400010000000a
    if [ "$1" = departing ]; then
      if [ -f "$OUT/reference-read" ]; then csn=000001a1a1e836e400010000000b; else touch "$OUT/reference-read"; fi
    elif [ ! -f "$OUT/observer-read" ]; then csn=000001a1a1e836e4000100000009; touch "$OUT/observer-read"
    elif [ -f "$OUT/observer-caught-up" ]; then csn=000001a1a1e836e400010000000b
    fi
    printf 'dn: cn=one,cn=monitor\ndomain-name: %s\nreplicationServer-database: 1\nlast-change: %s\n\n' "$BASE_DN" "$csn"
  }
  scale_checkpoint_init observer departing
  if scale_caught_up observer departing; then echo 'lagging observer accepted' >&2; exit 1; fi
  if scale_caught_up observer departing; then echo 'stale checkpoint accepted' >&2; exit 1; fi
  touch "$OUT/observer-caught-up"
  scale_caught_up observer departing || { echo 'fresh checkpoint rejected' >&2; exit 1; }
  # If the first two searches already match, the reference is fresh and must not be queried twice.
  scale_monitor_search() {
    echo "$1" >>"$OUT/fast-queries"
    printf 'dn: cn=one,cn=monitor\ndomain-name: %s\nreplicationServer-database: 1\nlast-change: 000001a1a1e836e400010000000b\n\n' "$BASE_DN"
  }
  scale_checkpoint_init observer departing
  scale_caught_up observer departing || { echo 'fresh fast checkpoint rejected' >&2; exit 1; }
  [ "$(wc -l <"$OUT/fast-queries")" -eq 2 ] || { echo 'fresh checkpoint made redundant searches' >&2; exit 1; }
)
# All group stops start before any returns, and any failed stop fails the group.
(
  OUT="$OUT/parallel-stops"; mkdir -p "$OUT"
  TOPOLOGY=rs534 SCALE_VOLUME=kept DS_NODES='d0 d1 d2' RS_ONLY='r0 r1 r2' RS_NODES=$RS_ONLY
  ALL_NODES="$DS_NODES $RS_ONLY" PEERS=d0,d1,d2,r0,r1,r2
  . "$HERE/disturb-scale.sh"
  docker() {
    printf '%s\n' "$4" >>"$OUT/started"
    local end=$((SECONDS + 5))
    while [ "$(wc -l <"$OUT/started")" -lt 3 ]; do [ "$SECONDS" -lt "$end" ] || return 1; sleep 0.05; done
    [ "$4" != "${FAIL_NODE:-}" ]
  }
  scale_stop_group d0 d1 r0 || { echo 'group stops were not concurrent' >&2; exit 1; }
  : >"$OUT/started"
  if (FAIL_NODE=d1; scale_stop_group d0 d1 r0); then echo 'failed group stop was accepted' >&2; exit 1; fi
  [ "$(wc -l <"$OUT/started")" -eq 3 ] || { echo 'failed stop abandoned another operation' >&2; exit 1; }
)
# Exercise the real fill pipeline: all 50,001 records reach one client, and a failed
# client must not produce a successful backlog report.
(
  OUT="$OUT/fill-test"; mkdir -p "$OUT/scale-progress" "$OUT/scale-checkpoint" "$OUT/load"
  TOPOLOGY=combined SCALE_VOLUME=kept DS_NODES='d0 d1 d2 d3' RS_NODES=$DS_NODES RS_ONLY=
  ALL_NODES=$DS_NODES PEERS=d0,d1,d2,d3 NETWORK=fixture IMAGE=fixture
  BASE_DN=dc=example,dc=com ROOT_PASSWORD=password CLIENT_JAVA_ARGS=-Xmx128m
  DISTURB_TIMEOUT=900 ROLLING_END=$((SECONDS + 900)) ROLLING_END_MS=2000000000000
  . "$HERE/disturb-scale.sh"
  scale_wait() { shift; "$@"; }
  scale_wait_fence() { shift; "$@"; }
  admin_search() { echo writer; }
  ldaps_search() { printf '%s\n' "$2" >"$OUT/writer-dn"; echo writer; }
  scale_fence() { [ "${FAIL_FENCE:-0}" = 0 ]; }
  scale_coverage() { [ "${FAIL_COVERAGE:-0}" = 0 ]; }
  rolling_kill_active() { :; }
  docker() {
    case $1 in
      exec) cat >/dev/null ;;
      run) cat >"$OUT/fill.ldif"; [ "${FAIL_FILL:-0}" = 0 ] ;;
      *) return 1 ;;
    esac
  }
  scale_backlog
  grep -Fx 'uid=scale-backlog-d3,ou=People,dc=example,dc=com' "$OUT/writer-dn" >/dev/null || { echo 'final fill entry was not read' >&2; exit 1; }
  [ "$(grep -c '^changetype: modify$' "$OUT/fill.ldif")" -eq 50000 ]
  grep -Fx 'description: scale-50001' "$OUT/fill.ldif" >/dev/null
  grep -Fx '50001 changes while the server was absent' "$OUT/scale-checkpoint/backlog.txt" >/dev/null
  rm "$OUT/scale-checkpoint/backlog.txt"
  if (FAIL_FILL=1; scale_backlog); then echo 'failed fill was accepted' >&2; exit 1; fi
  [ ! -e "$OUT/scale-checkpoint/backlog.txt" ] || { echo 'failed fill wrote a success report' >&2; exit 1; }
  if (FAIL_FENCE=1; scale_backlog); then echo 'lagging RS fence was accepted' >&2; exit 1; fi
  [ ! -e "$OUT/scale-checkpoint/backlog.txt" ] || { echo 'lagging RS wrote a success report' >&2; exit 1; }
  if (FAIL_COVERAGE=1; scale_backlog); then echo 'incomplete RS changelog was accepted' >&2; exit 1; fi
  [ ! -e "$OUT/scale-checkpoint/backlog.txt" ] || { echo 'missing changes wrote a success report' >&2; exit 1; }
)
# The fence receives the remaining load time; other waits retain their individual cap.
(
  OUT="$OUT/fence-wait"; mkdir -p "$OUT"
  TOPOLOGY=combined SCALE_VOLUME=kept DS_NODES='d0 d1 d2' RS_NODES=$DS_NODES RS_ONLY=
  ALL_NODES=$DS_NODES PEERS=d0,d1,d2 DISTURB_TIMEOUT=900 ROLLING_END=$((SECONDS + 1800))
  . "$HERE/disturb-scale.sh"
  wait_until() { printf '%s\n' "$1" >"$OUT/timeout"; }
  scale_wait_fence 'fence' true
  [ "$(cat "$OUT/timeout")" -gt 900 ] || { echo 'fence still capped at 900 seconds' >&2; exit 1; }
  scale_wait 'other wait' true
  [ "$(cat "$OUT/timeout")" -eq 900 ] || { echo 'unrelated wait cap changed' >&2; exit 1; }
  fail() { return 1; }
  ROLLING_END=$SECONDS
  if scale_wait_fence 'expired fence' true; then echo 'expired fence was accepted' >&2; exit 1; fi
)
# Each fence attempt retains its query and comparison, including failures and both CSNs.
(
  OUT="$OUT/fence-history"; mkdir -p "$OUT/scale-checkpoint"
  TOPOLOGY=combined SCALE_VOLUME=kept DS_NODES='d0 d1 d2' RS_NODES=$DS_NODES RS_ONLY=
  ALL_NODES=$DS_NODES PEERS=d0,d1,d2 BASE_DN=dc=example,dc=com ROLLING_END=$((SECONDS + 1800))
  . "$HERE/disturb-scale.sh"
  cat >"$OUT/scale-checkpoint/writer.ldif" <<'LDIF'
dn: cn=writer
description: scale-50001
ds-sync-hist: description:000000000000000100010000000a:repl:scale-50001
LDIF
  scale_monitor_search() {
    if [ "${QUERY_FAIL:-0}" = 1 ]; then echo 'timeout=query' >&2; return 124; fi
    printf 'dn: cn=changelog\ndomain-name: dc=example,dc=com\nreplicationServer-database: 1\nlast-change: %s\n' "$CURRENT"
  }
  CURRENT=0000000000000001000100000009
  if scale_fence d1; then echo 'lagging fence passed' >&2; exit 1; fi
  CURRENT=000000000000000100010000000a
  scale_fence d1 || { echo 'caught-up fence failed' >&2; exit 1; }
  if (QUERY_FAIL=1; scale_fence d1); then echo 'failed query passed' >&2; exit 1; fi
  history="$OUT/scale-checkpoint/fence-d1/history.txt"
  grep -F '0000000000000001000100000009' "$history" >/dev/null || { echo 'lagging CSN missing from history' >&2; exit 1; }
  grep -F '000000000000000100010000000a' "$history" >/dev/null || { echo 'target CSN missing from history' >&2; exit 1; }
  grep -F 'timeout=query' "$history" >/dev/null || { echo 'query error missing from history' >&2; exit 1; }
  [ "$(grep -c 'phase=start' "$history")" -eq 3 ] && [ "$(grep -c 'phase=end' "$history")" -eq 3 ] || { echo 'fence attempt history is incomplete' >&2; exit 1; }
  grep -F 'query_exit=124 compare_exit=not-run' "$history" >/dev/null || { echo 'fence failure status missing' >&2; exit 1; }
  [ -s "$OUT/scale-checkpoint/fence-d1/attempt-0001.ldif" ] && [ -s "$OUT/scale-checkpoint/fence-d1/attempt-0002.ldif" ] || { echo 'fence snapshots overwritten' >&2; exit 1; }
)
echo 'scale lifecycle cases passed'