Skip to content

Commit 9df1596

Browse files
bestbeforetodaydenyeart
authored andcommitted
Detect node start using healthz
Use the operations service health check to detect when nodes are started in test-network-nano-bash instead of using arbitrary sleeps. Depending on the host machine and whether CAs are used, this can reduce network start time by up to 15 seconds. Also ensure the Docker socket location is set correctly for Docker contexts that use a non-default location. This is required to prevent the health service from returning Docker connection errors. Signed-off-by: Mark S. Lewis <[email protected]>
1 parent 7daf8e4 commit 9df1596

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

test-network-nano-bash/network.sh

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ printHelp() {
1414
echo " Starts the test network"
1515
echo
1616
echo " Flags:"
17-
echo " -d <delay> - CLI delays for a certain number of seconds (defaults to 3)"
1817
echo " -o <orderer_type> - Specify the orderer type. BFT or etcdraft. (defaults to etcdraft)"
1918
echo " -ca - Use CAs instead of cryptogen. (defaults to cryptogen)"
20-
echo " -h - Print this message"
19+
echo " -h - Print this message"
2120
elif [ "$USAGE" = "clean" ]; then
2221
echo "Usage: "
2322
echo " network.sh clean [Flags]"
@@ -49,8 +48,6 @@ networkStop() {
4948
}
5049

5150
networkStart() {
52-
: "${CLI_DELAY:=5}"
53-
5451
# shellcheck disable=SC2064
5552
trap networkStop 0 1 2 3 15
5653

@@ -62,8 +59,9 @@ networkStart() {
6259
./ordererca.sh > ./logs/ordererca.log 2>&1 &
6360
./org1ca.sh > ./logs/org1ca.log 2>&1 &
6461
./org2ca.sh > ./logs/org2ca.log 2>&1 &
65-
echo "Waiting ${CLI_DELAY}s..."
66-
sleep "${CLI_DELAY}"
62+
63+
echo 'Waiting for CAs to start...'
64+
waitForHealthzOK 9843 9844 9845
6765
fi
6866

6967
if [ -d "${PWD}"/channel-artifacts ] && [ -d "${PWD}"/crypto-config ]; then
@@ -84,22 +82,26 @@ networkStart() {
8482
./orderer2.sh "${ORDERER_TYPE}" > ./logs/orderer2.log 2>&1 &
8583
./orderer3.sh "${ORDERER_TYPE}" > ./logs/orderer3.log 2>&1 &
8684

85+
ORDERER_HEALTHZ_PORTS="8443 8444 8445"
86+
8787
#start one additional orderer for BFT consensus
8888
if [ "$ORDERER_TYPE" = "BFT" ]; then
8989
./orderer4.sh "${ORDERER_TYPE}" > ./logs/orderer4.log 2>&1 &
90+
ORDERER_HEALTHZ_PORTS="${ORDERER_HEALTHZ_PORTS} 8450"
9091
fi
9192

92-
echo "Waiting ${CLI_DELAY}s..."
93-
sleep "${CLI_DELAY}"
93+
echo 'Waiting for orderers to start...'
94+
# shellcheck disable=SC2086
95+
waitForHealthzOK ${ORDERER_HEALTHZ_PORTS}
9496

9597
echo "Starting peers..."
9698
./peer1.sh > ./logs/peer1.log 2>&1 &
9799
./peer2.sh > ./logs/peer2.log 2>&1 &
98100
./peer3.sh > ./logs/peer3.log 2>&1 &
99101
./peer4.sh > ./logs/peer4.log 2>&1 &
100102

101-
echo "Waiting ${CLI_DELAY}s..."
102-
sleep "${CLI_DELAY}"
103+
echo 'Waiting for peers to start...'
104+
waitForHealthzOK 8446 8447 8448 8449
103105

104106
if [ "${CREATE_CHANNEL}" = "true" ]; then
105107
echo "Joining orderers to channel..."
@@ -126,6 +128,25 @@ networkStart() {
126128
wait
127129
}
128130

131+
waitForHealthzOK() {
132+
while [ $# -ge 1 ]; do
133+
HEALTHZ_STATUS=
134+
HEALTHZ_PORT="$1"
135+
shift
136+
137+
while [ "${HEALTHZ_STATUS}" != 'OK' ]; do
138+
# Sleep before a retry
139+
if [ "${HEALTHZ_STATUS}" ]; then
140+
[ "${HEALTHZ_STATUS}" != 'OFFLINE' ] && echo "Healthz port ${HEALTHZ_PORT}: ${HEALTHZ_RESPONSE}"
141+
sleep 0.5
142+
fi
143+
144+
HEALTHZ_RESPONSE=$(curl --silent "http://127.0.0.1:${HEALTHZ_PORT}/healthz" || echo '{"status":"OFFLINE"}')
145+
HEALTHZ_STATUS=$(echo "${HEALTHZ_RESPONSE}" | jq --raw-output '.status')
146+
done
147+
done
148+
}
149+
129150
networkClean() {
130151
echo "Removing directories: channel-artifacts crypto-config data logs"
131152
rm -r "${PWD}"/channel-artifacts || true
@@ -142,7 +163,7 @@ if [ $# -lt 1 ] ; then
142163
printHelp
143164
exit 0
144165
else
145-
MODE=$1
166+
MODE="$1"
146167
shift
147168
fi
148169

@@ -153,13 +174,19 @@ INCLUDE_CA=false
153174
while [ $# -ge 1 ] ; do
154175
key="$1"
155176
case $key in
156-
-d )
157-
CLI_DELAY="$2"
158-
shift
159-
;;
160177
-o )
161-
ORDERER_TYPE="$2"
178+
ORDERER_TYPE="$(echo "$2" | tr '[:upper:]' '[:lower:]')"
162179
shift
180+
181+
if [ "${ORDERER_TYPE}" = bft ]; then
182+
ORDERER_TYPE=BFT
183+
fi
184+
185+
if [ "${ORDERER_TYPE}" != etcdraft ] && [ "${ORDERER_TYPE}" != BFT ]; then
186+
echo "Unsupported orderer type: ${ORDERER_TYPE}" >&2
187+
printHelp "$MODE"
188+
exit 1
189+
fi
163190
;;
164191
-ca )
165192
INCLUDE_CA=true
@@ -175,7 +202,16 @@ while [ $# -ge 1 ] ; do
175202
;;
176203
esac
177204
shift
178-
done
205+
done
206+
207+
# Ensure peers are configured with the correct docker socket location, otherwise
208+
# health checks will fail.
209+
export CORE_VM_ENDPOINT
210+
if [ -z "${DOCKER_HOST:-}" ]; then
211+
CORE_VM_ENDPOINT="$(docker context inspect --format '{{.Endpoints.docker.Host}}')"
212+
else
213+
CORE_VM_ENDPOINT="${DOCKER_HOST}"
214+
fi
179215

180216
if [ "$MODE" = "start" ]; then
181217
networkStart

0 commit comments

Comments
 (0)