Skip to content

Commit 46e03b7

Browse files
authored
[warmboot] Use 'redis-cli save' to dump database to file (sonic-net#389)
* Use redis binary dump * Move root check first
1 parent a099681 commit 46e03b7

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

scripts/fast-reboot

+22-34
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ REBOOT_TIME=$(date)
55
REBOOT_CAUSE_FILE="/var/cache/sonic/reboot-cause.txt"
66
REBOOT_TYPE=$(basename $0)
77
WARM_DIR=/host/warmboot
8+
REDIS_FILE=dump.rdb
9+
10+
# Check root privileges
11+
if [[ "$EUID" -ne 0 ]]
12+
then
13+
echo "This command must be run as root" >&2
14+
exit 1
15+
fi
816

917
function clear_warm_boot()
1018
{
1119
config warm_restart disable || /bin/true
1220
/sbin/kexec -u || /bin/true
1321

1422
TIMESTAMP=`date +%Y%m%d-%H%M%S`
15-
if [[ -f ${WARM_DIR}/config_db.json ]]; then
16-
mv -f ${WARM_DIR}/config_db.json ${WARM_DIR}/config_db-${TIMESTAMP}.json || /bin/true
23+
if [[ -f ${WARM_DIR}/${REDIS_FILE} ]]; then
24+
mv -f ${WARM_DIR}/${REDIS_FILE} ${WARM_DIR}/${REDIS_FILE}.${TIMESTAMP} || /bin/true
1725
fi
1826
}
1927

@@ -34,14 +42,6 @@ case "$REBOOT_TYPE" in
3442
;;
3543
esac
3644

37-
# Check root privileges
38-
if [[ "$EUID" -ne 0 ]]
39-
then
40-
echo "This command must be run as root" >&2
41-
exit 1
42-
fi
43-
44-
4545
# Unload the previously loaded kernel if any loaded
4646
if [[ "$(cat /sys/kernel/kexec_loaded)" -eq 1 ]]
4747
then
@@ -139,31 +139,19 @@ systemctl stop syncd
139139
# Warm reboot: dump state to host disk
140140
# Note: even if syncd changed ASIC_DB before killed, we don't care
141141
if [[ "$REBOOT_TYPE" = "warm-reboot" ]]; then
142-
# Dump redis content to directory
143-
# Note: don't use pretty mode redis-dump (1.1) since there is a known bug with key pattern
144-
DUMP_CMD="redis-dump -s /var/run/redis/redis.sock"
142+
# Dump redis content to a file 'dump.rdb' in warmboot directory
145143
mkdir -p $WARM_DIR
146-
# Note: requiring redis-dump-load
147-
# Save applDB in /host/warm-reboot/appl_db.json
148-
local pids=()
149-
$DUMP_CMD -d 0 -o $WARM_DIR/appl_db.json &
150-
pids+=($!)
151-
# Save configDB in /host/warm-reboot/config_db.json
152-
$DUMP_CMD -d 4 -o $WARM_DIR/config_db.json &
153-
pids+=($!)
154-
# Save stateDB (only FDB_TABLE and WARM_RESTART_TABLE) in /host/warm-reboot/state_db.json
155-
# WARNING WARNING WARNING: a trick to dump both FDB_TABLE|* and WARM_RESTA*
156-
# TODO: replace it with readable mechanism to dump multiple key patterns into one single json file
157-
$DUMP_CMD -d 6 -k "[FW][DA][BR][_M][T_][AR][BE][LS][ET][|A]*" -o $WARM_DIR/state_db.json &
158-
pids+=($!)
159-
# Save asicDB in /host/warm-reboot/asic_db.json
160-
$DUMP_CMD -d 1 -o $WARM_DIR/asic_db.json &
161-
pids+=($!)
162-
# Save loglevelDB in /host/warm-reboot/loglevel_db.json
163-
$DUMP_CMD -d 3 -o $WARM_DIR/loglevel_db.json &
164-
pids+=($!)
165-
166-
wait ${pids[@]}
144+
# Delete keys in stateDB except FDB_TABLE|* and WARM_RESTA*
145+
redis-cli -n 6 eval "
146+
for _, k in ipairs(redis.call('keys', '*')) do
147+
if not string.match(k, 'FDB_TABLE|') and not string.match(k, 'WARM_RESTART_TABLE|') then
148+
redis.call('del', k)
149+
end
150+
end
151+
" 0
152+
redis-cli save
153+
docker cp database:/var/lib/redis/$REDIS_FILE $WARM_DIR
154+
docker exec -i database rm /var/lib/redis/$REDIS_FILE
167155
fi
168156

169157
# Kill other containers to make the reboot faster

scripts/reboot

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ function clear_warm_boot()
1919
# If reboot is requested, make sure the outstanding warm-boot is cleared
2020
# So the system will come up from a cold boot.
2121
WARM_DIR="/host/warmboot"
22+
REDIS_FILE=dump.rdb
2223
TIMESTAMP=`date +%Y%m%d-%H%M%S`
23-
if [[ -f ${WARM_DIR}/config_db.json ]]; then
24-
mv -f ${WARM_DIR}/config_db.json ${WARM_DIR}/config_db-${TIMESTAMP}.json
24+
if [[ -f ${WARM_DIR}/${REDIS_FILE} ]]; then
25+
mv -f ${WARM_DIR}/${REDIS_FILE} ${WARM_DIR}/${REDIS_FILE}.${TIMESTAMP} || /bin/true
2526
fi
2627
/sbin/kexec -u || /bin/true
2728
}

0 commit comments

Comments
 (0)