Skip to content

Commit c84be84

Browse files
committed
Stop waiting for services that are not meant to be running
Both open timeout reports come from the same place: the wait loop treats "fewer replicas running than wanted" as "still converging", which is wrong for any service that is not supposed to be running. A `mode: replicated-job` service that has finished reports its replicas as "0/1 (1/1 completed)". The completion count was cut away before anything looked at it, leaving current(0) != target(1) forever. Read the whole field and use the count: a job is settled once every requested run has completed, and reported as job_running until then. A service the stack file declares with `replicas: 0` is a different case, and indistinguishable from the one above by the replicas field alone -- there is no completion count to disambiguate a finished run from a service that failed to start. So consult the spec instead of the observed target. The two disagree exactly when this bites: stack deploy returns before the new spec is applied, and an external scheduler may have scaled the service since. The spec is re-read every poll, so a stale target corrects itself rather than being trusted once. A service whose spec really does ask for replicas still times out; that is covered by a test, because the shortcut above would be easy to widen into swallowing genuine failures. On either failure the task list is now dumped via docker stack ps. The per-service log only reports changes, so working out which service stalled previously meant reading back through the whole log. Not included: passing --detach=false to docker stack deploy, suggested on the second issue. It would make deploy block until convergence, which replaces this script's configurable timeout with docker's own waiting and could hang on precisely the services above. Worth its own discussion rather than folding in here. Closes #13 Closes #19
1 parent f5aa703 commit c84be84

6 files changed

Lines changed: 102 additions & 13 deletions

File tree

‎scripts/stack-wait.sh‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ check_timeout() {
3939
cutoff_epoc=$((start_epoc + opt_t - opt_s))
4040
if [ "$cur_epoc" -gt "$cutoff_epoc" ]; then
4141
echo "Error: Timeout exceeded"
42+
print_stack_state
4243
print_service_logs
4344
exit 1
4445
fi
@@ -74,6 +75,14 @@ service_state() {
7475
eval cache_${service_safe}=\"\$state\"
7576
fi
7677
}
78+
print_stack_state() {
79+
# On failure the per-service state log only shows what changed, so the state
80+
# each service was left in has to be reconstructed by reading back through
81+
# it. Dump the task list instead: it names the service that stalled and
82+
# carries the error column.
83+
echo "Stack state at failure:"
84+
docker stack ps --no-trunc "${stack_name}" || true
85+
}
7786
print_service_logs() {
7887
if [ "$opt_p" != "0" ]; then
7988
service_ids=$(get_service_ids)
@@ -130,10 +139,41 @@ while [ "$stack_done" != "1" ]; do
130139

131140
# identify/report current state
132141
if [ "$service_done" != "2" ]; then
133-
replicas=$(docker service ls --format '{{.Replicas}}' --filter "id=$service_id" | cut -d' ' -f1)
142+
# The whole field, which is "1/1" for a plain service but carries a
143+
# completion count for a job: "0/1 (1/1 completed)".
144+
replicas_full=$(docker service ls --format '{{.Replicas}}' --filter "id=$service_id")
145+
replicas=$(echo "$replicas_full" | cut -d' ' -f1)
134146
current=$(echo "$replicas" | cut -d/ -f1)
135147
target=$(echo "$replicas" | cut -d/ -f2)
136-
if [ "$current" != "$target" ]; then
148+
149+
# Runs finished and runs wanted, for a `mode: replicated-job` service.
150+
# Empty for every other mode, which is what selects the branch below.
151+
job_done=$(echo "$replicas_full" | sed -n 's/.*(\([0-9]*\)\/[0-9]* completed).*/\1/p')
152+
job_total=$(echo "$replicas_full" | sed -n 's/.*([0-9]*\/\([0-9]*\) completed).*/\1/p')
153+
154+
# Replicas the stack file asks for, which is not always what
155+
# `docker service ls` reports as the target yet: `docker stack deploy`
156+
# returns before the new spec has been applied, and an external
157+
# scheduler may have scaled the service in the meantime. Re-read every
158+
# poll so a stale target corrects itself.
159+
spec_replicas=$(docker service inspect \
160+
--format '{{if .Spec.Mode.Replicated}}{{.Spec.Mode.Replicated.Replicas}}{{end}}' \
161+
"$service_id" 2>/dev/null || echo "")
162+
163+
if [ -n "$job_total" ]; then
164+
# A job is finished when every requested run has completed. Its
165+
# running count goes back to 0 and must not be read as "not started".
166+
if [ "$job_done" = "$job_total" ]; then
167+
state="job_completed"
168+
else
169+
service_done=0
170+
state="job_running $job_done/$job_total"
171+
fi
172+
elif [ "$spec_replicas" = "0" ]; then
173+
# Deliberately stopped by the stack file, so there is nothing to wait
174+
# for even if the target has not caught up yet.
175+
state="scaled_to_zero"
176+
elif [ "$current" != "$target" ]; then
137177
# actively replicating service
138178
service_done=0
139179
state="replicating $replicas"
@@ -142,9 +182,11 @@ while [ "$stack_done" != "1" ]; do
142182
service_state "$service" "$state"
143183

144184
# check for states that indicate an update is done
185+
# Keep this list in sync with the states assigned above: a state that is
186+
# settled but missing here falls into the catch-all and waits forever.
145187
if [ "$service_done" = "1" ]; then
146188
case "$state" in
147-
deployed|completed|rollback_completed)
189+
deployed|completed|rollback_completed|job_completed|scaled_to_zero)
148190
service_done=1
149191
;;
150192
*)
@@ -165,6 +207,7 @@ while [ "$stack_done" != "1" ]; do
165207
done
166208
if [ "$stack_done" = "2" ]; then
167209
echo "Error: This deployment will not complete"
210+
print_stack_state
168211
print_service_logs
169212
exit 1
170213
fi
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Looks identical to the zero-replica case in the replicas field, but the
2+
# stack file really does want 1 replica (fifth field), so this is a service
3+
# that failed to start and must still be reported as a failure.
4+
svc_backend|demo_backend|deployed|0/1|1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# A replicated-job still on its first run, then finished.
2+
svc_migrate|demo_migrate|deployed|1/1 (0/1 completed)
3+
svc_backend|demo_backend|deployed|1/1
4+
---
5+
svc_migrate|demo_migrate|deployed|0/1 (1/1 completed)
6+
svc_backend|demo_backend|deployed|1/1
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Issue #19: a service the stack file declares with `replicas: 0`, which an
22
# external scheduler (swarm-cronjob) has since scaled to a target of 1.
3-
# Indistinguishable from issue #13 at the replicas field alone -- there is no
4-
# completion suffix here to disambiguate a finished run from a failed start.
5-
svc_dbpack|demo_dbpack|deployed|0/1
3+
#
4+
# Fifth field is the spec replicas, i.e. what the stack file asks for. It
5+
# diverges from the target in the replicas field here on purpose: that
6+
# divergence is the bug, and reading the spec is what resolves it.
7+
svc_dbpack|demo_dbpack|deployed|0/1|0
68
svc_backend|demo_backend|deployed|1/1

‎tests/helpers/bin/docker‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ case "${1:-}" in
131131
*'.Spec.Name'*)
132132
lookup "${target}" 2
133133
;;
134+
*'.Spec.Mode.Replicated'*)
135+
# Replicas the stack file asks for. Fixtures may state it in a
136+
# fifth field when it differs from what `service ls` reports --
137+
# that difference is what issue #19 is about. Otherwise it is
138+
# just the target from the replicas field.
139+
spec=$(lookup "${target}" 5)
140+
if [ -z "${spec}" ]; then
141+
spec=$(lookup "${target}" 4 | cut -d' ' -f1 | cut -d/ -f2)
142+
fi
143+
printf '%s\n' "${spec}"
144+
;;
134145
*'.UpdateStatus'*)
135146
lookup "${target}" 3
136147
;;

‎tests/stack-wait.bats‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,51 @@ setup() {
109109
[[ "$output" == *"[opts] stack_name"* ]]
110110
}
111111

112-
# --- Known bugs, to be un-skipped by the fixes -------------------------------
112+
# --- Services that are not meant to be running (issues #13, #19) -------------------------------
113113

114-
@test "issue #13: a completed replicated-job counts as converged" {
115-
skip "https://github.com/kitconcept/docker-stack-deploy/issues/13 -- the '(1/1 completed)' suffix is discarded, so current(0) != target(1) forever"
114+
@test "a completed replicated-job counts as converged (issue #13)" {
116115
setup_stub replicated-job
117116
run "${STACK_WAIT}" -s 1 -t 5 demo
118117
[ "$status" -eq 0 ]
119118
[[ "$output" != *"Timeout exceeded"* ]]
120119
}
121120

122-
@test "issue #19: a service scaled to zero does not block the deploy" {
123-
skip "https://github.com/kitconcept/docker-stack-deploy/issues/19 -- target is 1 because an external scheduler scaled it, so the wait never settles"
121+
@test "a service the stack file scales to zero does not block (issue #19)" {
124122
setup_stub zero-replicas
125123
run "${STACK_WAIT}" -s 1 -t 5 demo
126124
[ "$status" -eq 0 ]
127125
[[ "$output" != *"Timeout exceeded"* ]]
128126
}
129127

130-
@test "issue #19: the final stack state is logged on timeout" {
131-
skip "https://github.com/kitconcept/docker-stack-deploy/issues/19 -- 'docker stack ps' diagnostics are not implemented yet"
128+
@test "the final stack state is logged on timeout (issue #19)" {
132129
setup_stub stuck
133130
run "${STACK_WAIT}" -s 1 -t 3 demo
134131
[ "$status" -eq 1 ]
135132
[[ "$(stub_calls)" == *"stack ps"* ]]
136133
}
134+
135+
@test "a still-running replicated-job is waited for (issue #13)" {
136+
setup_stub running-job
137+
run "${STACK_WAIT}" -s 1 -t 20 demo
138+
[ "$status" -eq 0 ]
139+
# Reported as running while incomplete, then settled once it finishes.
140+
[[ "$output" == *"demo_migrate state: job_running 0/1"* ]]
141+
[[ "$output" == *"demo_migrate state: job_completed"* ]]
142+
}
143+
144+
@test "a genuinely failed service still times out (issue #19 regression guard)" {
145+
# Same 0/1 as the zero-replica case, but the stack file wants 1 replica.
146+
# This must NOT be swallowed by the scaled_to_zero shortcut.
147+
setup_stub failed-start
148+
run "${STACK_WAIT}" -s 1 -t 3 demo
149+
[ "$status" -eq 1 ]
150+
[[ "$output" == *"Timeout exceeded"* ]]
151+
}
152+
153+
@test "the final stack state is logged when a deployment cannot complete" {
154+
setup_stub paused
155+
run "${STACK_WAIT}" -s 1 -t 10 demo
156+
[ "$status" -eq 1 ]
157+
[[ "$output" == *"Stack state at failure:"* ]]
158+
[[ "$(stub_calls)" == *"stack ps"* ]]
159+
}

0 commit comments

Comments
 (0)