-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30-test-cluster-writes.sh
More file actions
executable file
·152 lines (120 loc) · 3.63 KB
/
30-test-cluster-writes.sh
File metadata and controls
executable file
·152 lines (120 loc) · 3.63 KB
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
#!/usr/bin/env bash
set -euo pipefail
readonly TOKEN="${HYPERCACHE_TOKEN:-dev-token}"
readonly COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.cluster.yml}"
readonly SURVIVING_PORTS="${SURVIVING_PORTS:-8081 8082 8084 8085}"
readonly KEY_COUNT="${KEY_COUNT:-50}"
fail_count=0
log_fail() {
if [[ -t 1 ]]; then
printf '\033[31mFAIL\033[0m %s\n' "$1"
else
printf 'FAIL %s\n' "$1"
fi
fail_count=$((fail_count + 1))
}
log_ok() {
if [[ -t 1 ]]; then
printf '\033[32m OK \033[0m %s\n' "$1"
else
printf ' OK %s\n' "$1"
fi
}
# put_batch writes KEY_COUNT keys with the given prefix to the
# given port. Each PUT is asserted to return 200; any non-200
# bumps the failure count but the loop continues so we can see
# the full picture.
put_batch() {
local port="$1"
local prefix="$2"
local fails=0
for i in $(seq 1 "$KEY_COUNT"); do
status=$(curl -sS -o /dev/null -w '%{http_code}' \
-H "Authorization: Bearer $TOKEN" \
-X PUT --data "value-$i" \
"http://localhost:$port/v1/cache/${prefix}-${i}" || echo "000")
if [[ "$status" != "200" ]]; then
fails=$((fails + 1))
fi
done
if [[ "$fails" -gt 0 ]]; then
log_fail "PUT ${prefix}-* on :$port: ${fails}/${KEY_COUNT} writes failed"
return 1
fi
log_ok "PUT ${prefix}-* on :$port: all ${KEY_COUNT} writes succeeded"
return 0
}
# verify_batch_visible asserts that GET /v1/cache/<prefix>-N on the
# given port succeeds for every N in 1..KEY_COUNT. Used to confirm
# the cluster routes correctly to surviving owners while one node
# is down.
verify_batch_visible() {
local port="$1"
local prefix="$2"
local missing=0
for i in $(seq 1 "$KEY_COUNT"); do
status=$(curl -sS -o /dev/null -w '%{http_code}' \
-H "Authorization: Bearer $TOKEN" \
"http://localhost:$port/v1/cache/${prefix}-${i}" || echo "000")
if [[ "$status" != "200" ]]; then
missing=$((missing + 1))
fi
done
if [[ "$missing" -gt 0 ]]; then
log_fail "GET ${prefix}-* on :$port: ${missing}/${KEY_COUNT} keys missing"
return 1
fi
log_ok "GET ${prefix}-* on :$port: all ${KEY_COUNT} keys visible"
return 0
}
# count_visible returns the number of keys (0..KEY_COUNT) currently
# visible on the given port — used by the recovery polling loop.
count_visible() {
local port="$1"
local prefix="$2"
local found=0
for i in $(seq 1 "$KEY_COUNT"); do
status=$(curl -sS -o /dev/null -w '%{http_code}' \
-H "Authorization: Bearer $TOKEN" \
"http://localhost:$port/v1/cache/${prefix}-${i}" || echo "000")
if [[ "$status" == "200" ]]; then
found=$((found + 1))
fi
done
echo "$found"
}
echo "=== Phase 1: seed batch on :8081, verify cluster-wide ==="
put_batch 8081 "first" || true
sleep 1
# Spot-check pre-batch on every surviving port + the to-be-killed
# port. They should all see all 50 keys.
for port in 8081 8082 8083 8084 8085; do
verify_batch_visible "$port" "first" || true
done
echo ""
echo "=== Phase 2: write second batch on :8081 ==="
# Some of these keys' primary or replicas will be the down node;
# the writes succeed by quorum on the surviving 4 nodes, with
# hints queued for the down node's replicas (Phase B.2 contract).
put_batch 8081 "second" || true
sleep 1
echo ""
echo "=== Phase 3: nodes serve every key (first + second) ==="
for port in $SURVIVING_PORTS; do
verify_batch_visible "$port" "first" || true
verify_batch_visible "$port" "second" || true
done
echo ""
if [[ "$fail_count" -gt 0 ]]; then
if [[ -t 1 ]]; then
printf '\033[31m=== %d assertion(s) failed ===\033[0m\n' "$fail_count"
else
printf '=== %d assertion(s) failed ===\n' "$fail_count"
fi
exit 1
fi
if [[ -t 1 ]]; then
printf '\033[32m=== write test passed ===\033[0m\n'
else
printf '=== write test passed ===\n'
fi