Skip to content

Commit 0ff1afc

Browse files
author
Mark Pollack
committed
Isolate concurrent run-all invocations: port blocks and one Maven repository
Each run-all takes an exclusive 10,000-port block (IT_PORT_BASE, else the first of 20000/30000/40000/50000 it can flock, held for the run). Every Maven call of the suite goes through scripts/mvnw.sh, which pins -Dmaven.repo.local to IT_M2_REPO (default integration-testing/.cache/m2, ~/.m2/repository when CI=true); JBANG_REPO follows it.
1 parent 66e1df4 commit 0ff1afc

5 files changed

Lines changed: 91 additions & 12 deletions

File tree

‎integration-testing/RunScenario.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454
* with {@code --prepared}, which touches no shared state (no install, no git, no build). Each
5555
* scenario has its own log directory, its own {@code ${TMP}} (also {@code TMPDIR} for every
5656
* process), and takes {@code ${PORT}} from {@code IT_PORT_RANGE} (e.g. 20000-20099) when set, so
57-
* concurrent scenarios given disjoint ranges never race for a port.
57+
* concurrent scenarios given disjoint ranges never race for a port (unset: an OS-chosen ephemeral
58+
* port). Every Maven call goes through scripts/mvnw.sh, so the SDK SNAPSHOT is installed into and
59+
* resolved from {@code IT_M2_REPO} only (default: integration-testing/.cache/m2, or
60+
* ~/.m2/repository when CI=true).
5861
*/
5962
public class RunScenario {
6063

@@ -126,7 +129,7 @@ static void usage() {
126129
Options:
127130
--peer <name>=<ref> test peer SDK <name> (see peers.json) at <ref> (branch, tag or SHA)
128131
--peers-ref <ref> test every peer SDK at <ref> (a --peer entry wins)
129-
--skip-sdk-install do not run ./mvnw install first (run-all.sh installs once)
132+
--skip-sdk-install do not install the SDK first (run-all.sh installs once)
130133
--prepare <s>... only prepare the peers and run the builds of the scenarios (no install)
131134
--prepared the scenario was prepared: no install, no git, no build (parallel runs)
132135
--list list scenarios
@@ -184,11 +187,11 @@ int run(boolean skipInstall, boolean prepared, String peersRef, Map<String, Stri
184187
String acpVersion = sdkVersion();
185188
if (!skipInstall) {
186189
step("Installing the SDK from " + repo + " (" + acpVersion + ")");
187-
exec(logDir.resolve("sdk-install.log"), repo, 900, "./mvnw", "-q", "-B", "-DskipTests", "install");
190+
exec(logDir.resolve("sdk-install.log"), repo, 900, mvnw(root), "-q", "-B", "-DskipTests", "install");
188191
}
189192
vars.put("ROOT", root.toString());
190193
vars.put("REPO", repo.toString());
191-
vars.put("MVNW", repo.resolve("mvnw").toString());
194+
vars.put("MVNW", mvnw(root));
192195
vars.put("ACP_VERSION", acpVersion);
193196
vars.put("CACHE", root.resolve(".cache").toString());
194197
vars.put("LOG_DIR", logDir.toString());
@@ -485,6 +488,14 @@ String sdkVersion() throws Exception {
485488
return m.group(1).trim();
486489
}
487490

491+
/**
492+
* The Maven wrapper every build uses: scripts/mvnw.sh, the checkout's ./mvnw pinned to the
493+
* suite's local repository ({@code -Dmaven.repo.local=$IT_M2_REPO}, see scripts/m2-repo.sh).
494+
*/
495+
static String mvnw(Path root) {
496+
return root.resolve("scripts").resolve("mvnw.sh").toString();
497+
}
498+
488499
/**
489500
* A free port: the first one that binds in {@code IT_PORT_RANGE} ({@code <from>-<to>}, set by
490501
* run-all.sh so concurrent scenarios use disjoint ranges), else one the OS picks.
@@ -528,7 +539,7 @@ static int prepareAll(Path root, List<String> scenarios, String peersRef, Map<St
528539
Map<String, String> vars = new LinkedHashMap<>();
529540
vars.put("ROOT", root.toString());
530541
vars.put("REPO", r.repo.toString());
531-
vars.put("MVNW", r.repo.resolve("mvnw").toString());
542+
vars.put("MVNW", mvnw(root));
532543
vars.put("ACP_VERSION", r.sdkVersion());
533544
vars.put("CACHE", root.resolve(".cache").toString());
534545
vars.put("LOG_DIR", r.logDir.toString());

‎integration-testing/programs/smoke/run-smoke.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ ACP_VERSION="$(sed -n '/<parent>/,/<\/parent>/d; s#^ <version>\(.*\)</version
4141

4242
if [ -z "$SKIP_INSTALL" ]; then
4343
echo "Installing the SDK $ACP_VERSION from $REPO_DIR"
44-
(cd "$REPO_DIR" && timeout 900 ./mvnw -q -B -DskipTests install) > "$LOGS/sdk-install.log" 2>&1 || {
44+
(cd "$REPO_DIR" && timeout 900 "$IT_DIR/scripts/mvnw.sh" -q -B -DskipTests install) > "$LOGS/sdk-install.log" 2>&1 || {
4545
echo "SDK install failed; see $LOGS/sdk-install.log" >&2; tail -40 "$LOGS/sdk-install.log" >&2; exit 1; }
4646
fi
4747
echo "Building the smoke client against acp $ACP_VERSION"
48-
(cd "$SCRIPT_DIR" && timeout 600 "$REPO_DIR/mvnw" -q -B -Dacp.version="$ACP_VERSION" compile \
48+
(cd "$SCRIPT_DIR" && timeout 600 "$IT_DIR/scripts/mvnw.sh" -q -B -Dacp.version="$ACP_VERSION" compile \
4949
dependency:build-classpath -Dmdep.outputFile=target/classpath.txt) > "$LOGS/build.log" 2>&1 || {
5050
echo "Smoke client build failed; see $LOGS/build.log" >&2; tail -40 "$LOGS/build.log" >&2; exit 1; }
5151

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# shellcheck shell=bash
2+
# Sourced, not run. Resolves the one Maven local repository the suite uses and exports it:
3+
# IT_M2_REPO if set, used as is (made absolute);
4+
# else ~/.m2/repository when CI=true (keeps the CI cache);
5+
# else integration-testing/.cache/m2 (per checkout, git-ignored).
6+
# JBANG_REPO = IT_M2_REPO, so JBang resolves into the same repository.
7+
# Every Maven call of the suite goes through scripts/mvnw.sh, which passes
8+
# -Dmaven.repo.local="$IT_M2_REPO". See README.md, Contracts section 7.
9+
_it_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
10+
if [ -z "${IT_M2_REPO:-}" ]; then
11+
if [ "${CI:-}" = "true" ]; then
12+
IT_M2_REPO="$HOME/.m2/repository"
13+
else
14+
IT_M2_REPO="$_it_dir/.cache/m2"
15+
fi
16+
fi
17+
mkdir -p "$IT_M2_REPO"
18+
IT_M2_REPO="$(cd "$IT_M2_REPO" && pwd)"
19+
export IT_M2_REPO
20+
export JBANG_REPO="$IT_M2_REPO"
21+
unset _it_dir
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
# The Maven wrapper of this checkout, pinned to the suite's local repository (IT_M2_REPO, resolved
3+
# by m2-repo.sh). Every Maven call of the suite (SDK install, program builds) goes through this,
4+
# so a SNAPSHOT is installed into and resolved from one repository only. Runs in the caller's
5+
# directory, like ./mvnw.
6+
set -euo pipefail
7+
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
# shellcheck source=m2-repo.sh
9+
. "$here/m2-repo.sh"
10+
exec "$here/../../mvnw" -Dmaven.repo.local="$IT_M2_REPO" "$@"

‎integration-testing/scripts/run-all.sh‎

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
# directory and port range; load-* scenarios run afterwards, alone and serially. Exits non-zero
2626
# if any scenario fails; a declared expected failure (for example the Java client -> Python server
2727
# reconnect-then-load 404) does not fail the run.
28+
#
29+
# Concurrent run-alls (several checkouts on one host): each takes an exclusive 10,000-port block
30+
# (IT_PORT_BASE, else the first free of 20000/30000/40000/50000, locked with flock for the whole
31+
# run), and every Maven and JBang call uses one local repository, IT_M2_REPO (default
32+
# integration-testing/.cache/m2; ~/.m2/repository when CI=true). See README.md, Contracts 7.
2833
set -uo pipefail
2934

3035
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -58,7 +63,7 @@ while [ $# -gt 0 ]; do
5863
--allow-empty) ALLOW_EMPTY=1; shift ;;
5964
--peer|--peers-ref) PASS_ARGS+=("$1" "$2"); shift 2 ;;
6065
--peer=*|--peers-ref=*) PASS_ARGS+=("$1"); shift ;;
61-
-h|--help) sed -n '2,27p' "$0"; exit 0 ;;
66+
-h|--help) sed -n '2,32p' "$0"; exit 0 ;;
6267
*) echo "Unknown option: $1" >&2; exit 2 ;;
6368
esac
6469
done
@@ -160,8 +165,11 @@ if [ ${#missing[@]} -gt 0 ]; then
160165
fi
161166

162167
mkdir -p logs/run-all
163-
echo "Installing the SDK from $REPO_DIR"
164-
if ! (cd "$REPO_DIR" && timeout 900 ./mvnw -q -B -DskipTests install) > logs/run-all/sdk-install.log 2>&1; then
168+
# One Maven local repository for the SDK install, every program build and JBang (IT_M2_REPO).
169+
# shellcheck source=m2-repo.sh
170+
. "$SCRIPT_DIR/m2-repo.sh"
171+
echo "Installing the SDK from $REPO_DIR into $IT_M2_REPO"
172+
if ! (cd "$REPO_DIR" && timeout 900 "$SCRIPT_DIR/mvnw.sh" -q -B -DskipTests install) > logs/run-all/sdk-install.log 2>&1; then
165173
echo "SDK install failed; see $IT_DIR/logs/run-all/sdk-install.log" >&2
166174
tail -40 logs/run-all/sdk-install.log >&2
167175
exit 1
@@ -188,10 +196,39 @@ if [ "$PREPARE_ONLY" -eq 1 ]; then
188196
exit 0
189197
fi
190198

199+
# This run's port block, 10,000 ports no other run-all on this host uses at the same time:
200+
# IT_PORT_BASE if set, else the first of 20000/30000/40000/50000 whose lock file we can flock.
201+
# The lock is held by file descriptor PORT_LOCK_FD for the life of this shell (children inherit
202+
# it, so a block stays taken while anything this run started is alive) and released when the
203+
# last holder exits, however it exits. All four taken: wait for one.
204+
PORT_BASE="${IT_PORT_BASE:-}"
205+
if [ -n "$PORT_BASE" ]; then
206+
case "$PORT_BASE" in *[!0-9]*) echo "IT_PORT_BASE needs a number, got '$PORT_BASE'" >&2; exit 2 ;; esac
207+
elif ! command -v flock > /dev/null 2>&1; then
208+
PORT_BASE=20000
209+
echo "WARN flock not found: using port block 20000 unlocked (set IT_PORT_BASE for concurrent runs)" >&2
210+
else
211+
lock_dir="${XDG_RUNTIME_DIR:-/tmp}"
212+
waited=0
213+
while [ -z "$PORT_BASE" ]; do
214+
for base in 20000 30000 40000 50000; do
215+
exec {PORT_LOCK_FD}>> "$lock_dir/acp-it-ports-$base.lock" || continue
216+
if flock -n "$PORT_LOCK_FD"; then PORT_BASE=$base; break; fi
217+
exec {PORT_LOCK_FD}>&-
218+
done
219+
if [ -z "$PORT_BASE" ]; then
220+
[ "$waited" -eq 0 ] && echo "All port blocks are in use by other runs; waiting for one"
221+
waited=1
222+
sleep 5
223+
fi
224+
done
225+
fi
226+
echo "Port block $PORT_BASE-$((PORT_BASE + 9999))"
227+
191228
# One scenario: its own log directory (logs/<s>/), its own ${TMP}, and a port range no other
192-
# scenario of this run uses (20000 + 100 * (index mod 100), 100 ports wide).
229+
# scenario of this run uses (PORT_BASE + 100 * (index mod 100), 100 ports wide).
193230
run_one() { # <scenario> <index> <tee: 0|1>
194-
local s="$1" idx="$2" from=$((20000 + ($2 % 100) * 100))
231+
local s="$1" idx="$2" from=$((PORT_BASE + ($2 % 100) * 100))
195232
rm -f "logs/$s/result.txt" "logs/run-all/$s.exit"
196233
local cmd=(env IT_PORT_RANGE="$from-$((from + 99))"
197234
timeout 1800 jbang RunScenario.java "$s" --prepared "${PASS_ARGS[@]}")

0 commit comments

Comments
 (0)