-
Notifications
You must be signed in to change notification settings - Fork 0
/
warehouse.sh
429 lines (353 loc) · 11.6 KB
/
warehouse.sh
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env bash
PATH_TO_LEDGER_DIR=$1
PATH_TO_LEDGER_SNAPSHOT_DIR=$2
PATH_TO_IDENTITY_KEYPAIR=$3
PATH_TO_LOGS=$4
ledger_dir=$PATH_TO_LEDGER_DIR
ledger_snapshots_dir=$PATH_TO_LEDGER_SNAPSHOT_DIR
# |touch ~/warehouse-exit-signal| will trigger a clean shutdown
exit_signal_file=~/warehouse-exit-signal
# |touch ~/warehouse-no-archive| to prevent the node from archiving even when it's time
no_archive_signal_file=~/warehouse-no-archive
set -x
set -e
shopt -s nullglob
here=$(dirname "$0")
panic() {
echo "error: $*" >&2
exit 1
}
#shellcheck source=/dev/null
source ~/service-env.sh
#shellcheck source=/dev/null
source ~/service-env-warehouse-*.sh
# Delete any zero-length snapshots that can cause validator startup to fail
find "$ledger_snapshots_dir" -name 'snapshot-*' -size 0 -print -exec rm {} \; || true
#shellcheck source=./configure-metrics.sh
source "$here"/configure-metrics.sh
if [[ -z $ENTRYPOINT ]]; then
echo ENTRYPOINT environment variable not defined
exit 1
fi
if [[ -z $EXPECTED_GENESIS_HASH ]]; then
echo EXPECTED_GENESIS_HASH environment variable not defined
exit 1
fi
if [[ -z $EXPECTED_SHRED_VERSION ]]; then
echo EXPECTED_SHRED_VERSION environment variable not defined
exit 1
fi
if [[ -z $STORAGE_BUCKET ]]; then
echo STORAGE_BUCKET environment variable not defined
exit 1
fi
if [[ -z $RPC_URL ]]; then
echo RPC_URL environment variable not defined
exit 1
fi
# MINIMUM_MINUTES_BETWEEN_ARCHIVE=720 is useful to define in devnet's service-env.sh
# since the devnet epochs are so short
if [[ -z $MINIMUM_MINUTES_BETWEEN_ARCHIVE ]]; then
MINIMUM_MINUTES_BETWEEN_ARCHIVE=1
fi
if [[ -f $exit_signal_file ]]; then
echo $exit_signal_file present, refusing to start
exit 0
fi
identity_keypair=$PATH_TO_IDENTITY_KEYPAIR
identity_pubkey=$(solana-keygen pubkey "$identity_keypair")
datapoint_error() {
declare event=$1
declare args=$2
declare comma=
if [[ -n $args ]]; then
comma=,
fi
$metricsWriteDatapoint "infra-warehouse-node,host_id=$identity_pubkey error=1,event=\"$event\"$comma$args"
}
datapoint() {
declare event=$1
declare args=$2
declare comma=
if [[ -n $args ]]; then
comma=,
fi
$metricsWriteDatapoint "infra-warehouse-node,host_id=$identity_pubkey error=0,event=\"$event\"$comma$args"
}
args=(
--dynamic-port-range 8002-8012
--entrypoint "$ENTRYPOINT"
--expected-genesis-hash "$EXPECTED_GENESIS_HASH"
--gossip-port 8001
--rpc-port 8899
--private-rpc
--identity "$identity_keypair"
--ledger "$ledger_dir"
--log $PATH_TO_LOGS
--no-voting
--skip-poh-verify
--enable-rpc-transaction-history
--no-port-check
--no-untrusted-rpc
--init-complete-file ~/.init-complete
--wal-recovery-mode skip_any_corrupted_record
--snapshots "$ledger_snapshots_dir"
--limit-ledger-size 400000000
)
if ! [[ $(solana --version) =~ \ 1\.4\.[0-9]+ ]]; then
if [[ -n $ENABLE_BPF_JIT ]]; then
args+=(--bpf-jit)
fi
if [[ -n $DISABLE_ACCOUNTSDB_CACHE ]]; then
args+=(--no-accounts-db-caching)
fi
if [[ -n $ENABLE_CPI_AND_LOG_STORAGE ]]; then
args+=(--enable-cpi-and-log-storage)
fi
for entrypoint in "${ENTRYPOINTS[@]}"; do
args+=(--entrypoint "$entrypoint")
done
fi
for tv in "${TRUSTED_VALIDATOR_PUBKEYS[@]}"; do
[[ $tv = "$identity_pubkey" ]] || args+=(--trusted-validator "$tv")
done
if [[ -d "$ledger_dir" ]]; then
args+=(--no-genesis-fetch)
fi
if [[ -d "$ledger_snapshots_dir" ]]; then
args+=(--no-snapshot-fetch)
fi
if [[ -w /mnt/accounts/ ]]; then
args+=(--accounts /mnt/accounts)
fi
if [[ -n $GOOGLE_APPLICATION_CREDENTIALS ]]; then
args+=(--enable-bigtable-ledger-upload)
fi
if [[ -n $EXPECTED_SHRED_VERSION ]]; then
args+=(--expected-shred-version "$EXPECTED_SHRED_VERSION")
fi
if [[ -n $SNAPSHOT_COMPRESSION ]]; then
args+=(--snapshot-compression "$SNAPSHOT_COMPRESSION")
fi
for hard_fork in "${HARD_FORKS[@]}"; do
args+=(--hard-fork "$hard_fork")
done
if [[ -n "$EXPECTED_BANK_HASH" ]]; then
args+=(--expected-bank-hash "$EXPECTED_BANK_HASH")
if [[ -n "$WAIT_FOR_SUPERMAJORITY" ]]; then
args+=(--wait-for-supermajority "$WAIT_FOR_SUPERMAJORITY")
fi
elif [[ -n "$WAIT_FOR_SUPERMAJORITY" ]]; then
echo "WAIT_FOR_SUPERMAJORITY requires EXPECTED_BANK_HASH be specified as well!" 1>&2
exit 1
fi
pid=
kill_node() {
# Note: do not echo anything from this function to ensure $pid is actually
# killed when stdout/stderr are redirected
set +ex
if [[ -n $pid ]]; then
declare _pid=$pid
pid=
kill "$_pid" || true
wait "$_pid" || true
fi
}
kill_node_and_exit() {
kill_node
exit
}
trap 'kill_node_and_exit' INT TERM ERR
get_latest_snapshot() {
declare dir="$1"
if [[ ! -d "$dir" ]]; then
panic "get_latest_snapshot: not a directory: $dir"
fi
find "$dir" -name snapshot-\*.tar\* | sort -ns | tail -n1
}
get_snapshot_slot() {
declare snapshot="$1"
snapshot="$(basename "$snapshot")"
snapshot="${snapshot#snapshot-}"
snapshot="${snapshot%-*}"
echo "$snapshot"
}
archive_snapshot_slot=invalid
last_archive_epoch=invalid
minutes_since_last_ledger_archive=invalid
prepare_archive_location() {
# If a current archive directory does not exist, create it and save the latest
# snapshot in it (if not at genesis)
if [[ ! -d ~/ledger-archive ]]; then
mkdir -p ~/ledger-archive
declare archive_snapshot
archive_snapshot=$(get_latest_snapshot "$ledger_snapshots_dir")
if [[ -n "$archive_snapshot" ]]; then
ln "$archive_snapshot" ~/ledger-archive/
fi
fi
# Determine the current archive slot
declare archive_snapshot
archive_snapshot=$(get_latest_snapshot ~/ledger-archive)
if [[ -n "$archive_snapshot" ]]; then
archive_snapshot_slot=$(get_snapshot_slot "$archive_snapshot")
else
archive_snapshot_slot=0
fi
minutes_since_last_ledger_archive=0
}
prepare_archive_location
while true; do
rm -f ~/.init-complete
solana-validator "${args[@]}" &
pid=$!
datapoint validator-started
echo "pid: $pid"
caught_up=false
initialized=false
SECONDS=
while true; do
if [[ -z $pid ]] || ! kill -0 "$pid"; then
datapoint_error unexpected-validator-exit
# cool down for a minute before restarting to avoid a tight restart loop
# if there's a failure very early in the validator boot
sleep 60
break # validator exited unexpectedly, restart it
fi
if ! $initialized; then
if [[ ! -f ~/.init-complete ]]; then
echo "waiting for node to initialize..."
if [[ $SECONDS -gt 600 ]]; then
datapoint_error validator-not-initialized
SECONDS=
fi
sleep 10
continue
fi
echo Validator has initialized
datapoint validator-initialized
initialized=true
fi
if ! $caught_up; then
if ! timeout 15m solana catchup --url "$RPC_URL" "$identity_pubkey" http://127.0.0.1:8899; then
echo "catchup failed..."
if [[ $SECONDS -gt 600 ]]; then
datapoint_error validator-not-caught-up
SECONDS=
fi
sleep 60
continue
fi
echo Validator has caught up
datapoint validator-caught-up
caught_up=true
fi
last_archive_epoch=$(cat ~/ledger-archive/epoch || true)
if [[ -z "$last_archive_epoch" ]]; then
if ! solana --url "$RPC_URL" epoch > ~/ledger-archive/epoch; then
datapoint_error failed-to-set-epoch
sleep 10
fi
continue
fi
sleep 60
current_epoch=""
for _ in $(seq 1 10); do
current_epoch=$(solana --url "$RPC_URL" epoch || true)
if [[ -n "$current_epoch" ]]; then
break
fi
sleep 2
done
if [[ -z "$current_epoch" ]]; then
datapoint_error failed-to-get-epoch
continue
fi
if [[ -f $exit_signal_file ]]; then
echo "$exit_signal_file present, forcing ledger archive for epoch $current_epoch"
else
if ((minutes_since_last_ledger_archive < MINIMUM_MINUTES_BETWEEN_ARCHIVE)) ||
[[ $current_epoch == "$last_archive_epoch" ]] || [[ -f $no_archive_signal_file ]]; then
((++minutes_since_last_ledger_archive))
# Every hour while waiting for the next epoch, emit a metric and verify/archive a snapshot
if ((minutes_since_last_ledger_archive % 60 == 0)); then
echo "Current epoch: $current_epoch"
if [[ -f $no_archive_signal_file ]]; then
echo "Archiving disabled due to $no_archive_signal_file"
fi
datapoint waiting-to-archive "minutes_since=$minutes_since_last_ledger_archive"
latest_snapshot=$(get_latest_snapshot "$ledger_snapshots_dir")
if [[ -n $latest_snapshot ]]; then
latest_snapshot_slot=$(get_snapshot_slot "$latest_snapshot")
if [[ $latest_snapshot_slot = "$last_known_good_snapshot_slot" ]]; then
# Problem! No new snapshot in the last hour
datapoint_error snapshot-stuck "slot=$latest_snapshot_slot"
else
# Archive the hourly snapshot
mkdir -p ~/ledger-archive/hourly
ln -f "$latest_snapshot" ~/ledger-archive/hourly/
# Sanity check: ensure the snapshot verifies
echo "Verifying snapshot for $latest_snapshot_slot: $latest_snapshot"
rm -rf ~/snapshot-check
mkdir ~/snapshot-check
ln -s "$ledger_dir"/genesis.bin ~/snapshot-check/
ln "$latest_snapshot" ~/snapshot-check/
if solana-ledger-tool --ledger ~/snapshot-check verify; then
datapoint snapshot-verification-ok "slot=$latest_snapshot_slot"
last_known_good_snapshot_slot=$latest_snapshot_slot
else
datapoint_error snapshot-verification-failed "slot=$latest_snapshot_slot"
fi
fi
fi
fi
continue
else
echo "Time to archive. Current epoch:$current_epoch, last archive epoch: $last_archive_epoch"
fi
fi
latest_snapshot=$(get_latest_snapshot "$ledger_snapshots_dir")
if [[ -z $latest_snapshot ]]; then
echo "Validator has not produced a snapshot yet"
datapoint_error snapshot-missing
continue
fi
latest_snapshot_slot=$(get_snapshot_slot "$latest_snapshot")
echo "Latest snapshot: slot $latest_snapshot_slot: $latest_snapshot"
if [[ "$archive_snapshot_slot" = "$latest_snapshot_slot" ]]; then
echo "Validator has not produced a new snapshot yet"
datapoint_error stale-snapshot
continue
fi
echo Killing the node
datapoint validator-terminated
kill_node
echo "Archiving snapshot from $archive_snapshot_slot and subsequent ledger"
SECONDS=
(
set -x
solana-ledger-tool --ledger "$ledger_dir" bounds | tee ~/ledger-archive/bounds.txt
solana-ledger-tool --version | tee ~/ledger-archive/version.txt
)
ledger_bounds="$(cat ~/ledger-archive/bounds.txt)"
datapoint ledger-archived "label=\"$archive_snapshot_slot\",duration_secs=$SECONDS,bounds=\"$ledger_bounds\""
mv "$ledger_dir"/rocksdb ~/ledger-archive/
mkdir -p ~/"$STORAGE_BUCKET".inbox
mv ~/ledger-archive ~/"$STORAGE_BUCKET".inbox/"$archive_snapshot_slot"
# Clean out the ledger directory from all artifacts other than genesis and
# the snapshot archives, so the warehouse node restarts cleanly from its
# last snapshot
rm -rf "$ledger_dir"/accounts "$ledger_snapshots_dir"/snapshot
# Remove the tower state to avoid a panic on validator restart due to the manual
# manipulation of the ledger directory
rm -f "$ledger_dir"/tower*.bin
# Prepare for next archive
rm -rf ~/ledger-archive
prepare_archive_location
if [[ -f $exit_signal_file ]]; then
echo $exit_signal_file present, exiting
exit 0
fi
break
done
done