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

Valery Kharseko
20 hours ago 400fb2694641b05e53217d3c742a6e49e3a254c6
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
#
# 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.
 
# Bound Docker commands in the scale scenario too: a blocked daemon or a large online
# changelog copy must not make a predicate run past the load deadline.
docker() {
  if [ "${SCALE_DEADLINE_ACTIVE:-0}" = 1 ]; then
    python3 -I "$HERE/scale-deadline.py" "$ROLLING_END_MS" "$(command -v docker)" "$@"
  else
    command docker "$@"
  fi
}
 
# Each DS gets naturally ending segments. A rolling replacement waits for both clients
# before stopping it; other DS keep writing. Three surnames per DS keep each indexed
# shard below the entry limit while allowing short segments for an hour-long load.
ROLLING_SEGMENT=${ROLLING_SEGMENT:-50}
# Docker's finish time includes JVM startup and connection setup, unlike --maxDuration.
# A successful run cannot claim the requested duration if a client exited after it.
finished_before_deadline() { # <Docker UTC timestamp> <deadline in epoch milliseconds>
  python3 -I - "$1" "$2" <<'PYTIME'
from datetime import datetime, timezone
import re
import sys
match = re.fullmatch(r"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?Z", sys.argv[1])
try:
    if not match:
        raise ValueError("invalid Docker finish time")
    seconds = int(datetime.strptime(match[1], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc).timestamp())
    nanoseconds = seconds * 10**9 + int((match[2] or "").ljust(9, "0"))
    sys.exit(0 if nanoseconds <= int(sys.argv[2]) * 10**6 else 1)
except ValueError:
    sys.exit(1)
PYTIME
}
 
# Keep recent LDAP rejections and replication warnings close to a failed client.
# The complete logs are still collected by the failed-run handler.
rolling_collect_error_context() { # <directory server>
  docker exec "$1" /bin/sh -c '
    logs=/opt/opendj/data/logs
    echo "Recent nonzero LDAP results:"
    tail -n 5000 "$logs"/access* 2>/dev/null | grep -E "(MODIFY|ADD|DELETE) RES .*result=[1-9][0-9]*" | tail -n 30 || :
    echo "Recent replication warnings and errors:"
    tail -n 500 "$logs/errors" 2>/dev/null | grep -E "category=SYNC severity=(WARNING|ERROR)" | tail -n 30 || :
  ' >"$OUT/load/$1.recent-errors.log" 2>&1 || :
  echo "::group::Recent server errors for $1"
  cat "$OUT/load/$1.recent-errors.log" || :
  echo "::endgroup::"
}
 
rolling_worker() { # <directory server>
  local n=$1 segment=1 name c length remaining finished failed post_segment
  # The parent reports this worker's failure; do not run run.sh's collection trap here.
  trap - ERR
  while [ ! -e "$OUT/load/stop" ]; do
    if [ -e "$OUT/load/$n.pause" ]; then
      touch "$OUT/load/$n.parked" || return 1
      while [ -e "$OUT/load/$n.pause" ] && [ ! -e "$OUT/load/stop" ]; do sleep 1; done
      rm -f "$OUT/load/$n.parked" || return 1
      continue
    fi
    length=$ROLLING_SEGMENT
    if [ -n "${ROLLING_END:-}" ]; then
      # --maxDuration starts only after the JVM and its connections are ready. Leave
      # a small launch/teardown allowance; FinishedAt still enforces the hard deadline.
      remaining=$((ROLLING_END - SECONDS - 40))
      [ "$remaining" -ge $((3 * STAT_INTERVAL)) ] || break
      [ "$length" -le "$remaining" ] || length=$remaining
    fi
    name=$n
    [ "$segment" -eq 1 ] || name=$n-$segment
    post_segment=
    [ ! -e "$OUT/load/post-start" ] || post_segment=1
    printf 'load-mod-%s load-add-%s\n' "$name" "$name" >"$OUT/load/$n.active" || return 1
    if ! start_clients "$n" "$segment" "$length"; then
      [ ! -e "$OUT/load/deadline" ] || rolling_kill_active "$n"
      return 1
    fi
    if [ -e "$OUT/load/deadline" ]; then
      rolling_kill_active "$n"
      return 1
    fi
    failed=
    for c in "load-mod-$name" "load-add-$name"; do
      docker wait "$c" >/dev/null || { echo "::error::docker wait failed for $c" >&2; return 1; }
      if [ -n "${ROLLING_END_MS:-}" ]; then
        finished=$(docker inspect -f '{{.State.FinishedAt}}' "$c") || { echo "::error::cannot inspect finish time for $c" >&2; return 1; }
        printf '%s\n' "$finished" >"$OUT/load/$c.finished" || return 1
      fi
      docker logs "$c" >"$OUT/load/$c.csv" 2>&1 || { echo "::error::cannot collect logs for $c" >&2; return 1; }
      docker inspect -f '{{.State.ExitCode}} {{.State.OOMKilled}}' "$c" >"$OUT/load/$c.exit" || { echo "::error::cannot inspect exit state for $c" >&2; return 1; }
      docker rm "$c" >/dev/null || { echo "::error::cannot remove client $c" >&2; return 1; }
      if [ -n "${ROLLING_END_MS:-}" ]; then
        finished_before_deadline "$finished" "$ROLLING_END_MS" || {
          echo "::error::$c finished after the load deadline ($finished)" >&2; failed=1; touch "$OUT/load/$n.failed" || return 1;
        }
      fi
      check_client "$c" || { failed=1; touch "$OUT/load/$n.failed" || return 1; }
    done
    if [ -n "$failed" ]; then rolling_collect_error_context "$n"; return 1; fi
    [ -z "$post_segment" ] || touch "$OUT/load/$n.post-verified" || return 1
    rm "$OUT/load/$n.active" || return 1
    segment=$((segment + 1))
  done
}
# A marker is written before Docker starts the clients; require both containers to be
# running before relying on this DS as the sole load writer during a checkpoint.
rolling_clients_running() { # <directory server>
  local node=$1 mod add extra client state
  [ ! -e "$OUT/load/$node.pause" ] && [ -f "$OUT/load/$node.active" ] || return 1
  read -r mod add extra <"$OUT/load/$node.active" || return 1
  case $mod in "load-mod-$node"|"load-mod-$node-"*) ;; *) return 1 ;; esac
  [ -z "$extra" ] && [ "$add" = "load-add-${mod#load-mod-}" ] || return 1
  for client in "$mod" "$add"; do
    state=$(docker inspect -f '{{.State.Running}}' "$client") || return 1
    [ "$state" = true ] || return 1
  done
}
rolling_worker_failed() {
  local marker
  for marker in "$OUT"/load/*.failed; do [ ! -e "$marker" ] || return 0; done
  return 1
}
rolling_parked() { [ -e "$OUT/load/$1.parked" ] || [ -e "$OUT/load/$1.done" ] || rolling_worker_failed; }
rolling_request_pause() { touch "$OUT/load/$1.pause"; }
rolling_pause() { # <directory server>
  local timeout=${DISTURB_TIMEOUT:-900} left
  if [ -n "${ROLLING_END:-}" ]; then
    left=$((ROLLING_END - SECONDS))
    [ "$left" -gt 0 ] || return 1
    [ "$left" -ge "$timeout" ] || timeout=$left
  fi
  rolling_request_pause "$1" || return 1
  wait_until "$timeout" "$1 clients finish" rolling_parked "$1" || return 1
  if rolling_worker_failed || [ -e "$OUT/load/$1.done" ]; then
    echo "::error::load worker $1 failed or stopped before it could pause" >&2
    return 1
  fi
}
rolling_resume() { rm -f "$OUT/load/$1.pause"; }
 
# At the hard limit, kill only the load clients of this run which are still active.
# A killed client makes the run fail; it is never counted as a successful segment.
rolling_kill_active() { # <directory server>
  local c clients
  [ -f "$OUT/load/$1.active" ] || return 0
  clients=$(cat "$OUT/load/$1.active") || return 1
  for c in $clients; do
    if [ "$(SCALE_DEADLINE_ACTIVE=0 docker inspect -f '{{.State.Running}}' "$c" 2>/dev/null || :)" = true ]; then
      touch "$OUT/load/cutoff" || return 1
      SCALE_DEADLINE_ACTIVE=0 docker kill "$c" >/dev/null 2>&1 || true
    fi
  done
}
 
rolling_deadline() { # <seconds>
  local n sleep_pid
  sleep "$1" & sleep_pid=$!
  trap 'kill "$sleep_pid" 2>/dev/null || true; exit 1' TERM INT
  wait "$sleep_pid" || return 1
  trap - TERM INT
  touch "$OUT/load/deadline" "$OUT/load/stop" || return 1
  for n in $DS_NODES fill; do rolling_kill_active "$n" || return 1; done
}
 
# The sampler is a subshell, so establish the same cutoff before starting it.
prepare_rolling_deadline() {
  ROLLING_END=$((SECONDS + DURATION))
  ROLLING_END_MS=$(( ${LOAD_START_MS:-$(($(date +%s) * 1000))} + DURATION * 1000 ))
  SCALE_DEADLINE_ACTIVE=1
}
 
# Every writer must complete and pass both clients of a segment launched after
# scale-out. A worker that ended earlier must wake the wait and fail it.
rolling_post_segments_complete() {
  local n
  [ ! -e "$OUT/load/deadline" ] || return 1
  for n in $DS_NODES; do
    [ ! -e "$OUT/load/$n.done" ] && [ ! -e "$OUT/load/$n.failed" ] || return 1
    [ -e "$OUT/load/$n.post-verified" ] || return 1
  done
}
rolling_post_segments_ready() {
  local n
  for n in $DS_NODES; do [ ! -e "$OUT/load/$n.done" ] || return 0; done
  rolling_post_segments_complete
}
 
# Used in place of run_load for a rolling scale change. The deadline covers the complete
# disturbance: a late return is a failed run, even if the topology later converges.
rolling_index_budget() {
  local segments per_shard
  [ "$ROLLING_SEGMENT" -gt 0 ] || return 1
  segments=$(((DURATION + ROLLING_SEGMENT - 1) / ROLLING_SEGMENT))
  per_shard=$(((segments + 2) / 3))
  # The FIFO threshold is 100; allow for concurrent in-flight adds on eight
  # connections. Each DS cycles through three indexed surnames.
  [ $((per_shard * 160)) -lt 4000 ]
}
 
rolling_run_load() {
  local n pid watchdog pids= failed= left
  rolling_index_budget || { fail "rolling segments exceed the sn index entry limit"; return 1; }
  mkdir -p "$OUT/load" || return 1
  [ -n "${ROLLING_END_MS:-}" ] || prepare_rolling_deadline
  left=$((ROLLING_END - SECONDS))
  [ "$left" -gt 0 ] || { SCALE_DEADLINE_ACTIVE=0; fail 'load deadline already passed'; return 1; }
  ( trap - ERR; rolling_deadline "$left" ) &
  watchdog=$!
  for n in $DS_NODES; do
    ( if rolling_worker "$n"; then touch "$OUT/load/$n.done"; else touch "$OUT/load/$n.failed"; exit 1; fi ) &
    pids="$pids $!"
  done
  left=$((DISTURB_AT - (SECONDS - (ROLLING_END - DURATION))))
  [ "$left" -le 0 ] || sleep "$left"
  disturb || failed=1
  if [ -z "$failed" ]; then
    touch "$OUT/load/post-start" || failed=1
    left=$((ROLLING_END - SECONDS))
    if [ "$left" -le 0 ] || ! wait_until "$left" 'post-scale segments finish on every writer' rolling_post_segments_ready; then
      echo "::error::scale change left too little of the $DURATION s limit for verified writes after it" >&2
      failed=1
    elif ! rolling_post_segments_complete; then
      echo '::error::a load worker stopped before all post-scale segments were verified' >&2
      failed=1
    fi
  fi
  if [ -n "$failed" ]; then
    # Workers stop after their current segment. Keep the deadline guard alive while
    # waiting: a client or docker wait may be stuck until the deadline kills it.
    touch "$OUT/load/stop" || true
    for pid in $pids; do wait "$pid" || true; done
    kill "$watchdog" 2>/dev/null || true
    wait "$watchdog" 2>/dev/null || true
  else
    touch "$OUT/load/stop" || failed=1
    for pid in $pids; do wait "$pid" || failed=1; done
    kill "$watchdog" 2>/dev/null || true
    wait "$watchdog" 2>/dev/null || true
  fi
  [ ! -e "$OUT/load/cutoff" ] || failed=1
  [ ! -e "$OUT/load/deadline" ] || failed=1
  SCALE_DEADLINE_ACTIVE=0
  ROLLING_ELAPSED=$((SECONDS - (ROLLING_END - DURATION)))
  [ -z "$failed" ] || fail "the rolling load or scale change failed"
}