Skip to content

Commit

Permalink
[warmboot] Use 'redis-cli save' to dump database to file (sonic-net#389)
Browse files Browse the repository at this point in the history
* Use redis binary dump
* Move root check first
  • Loading branch information
qiluo-msft authored Nov 21, 2018
1 parent a099681 commit 46e03b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
56 changes: 22 additions & 34 deletions scripts/fast-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ REBOOT_TIME=$(date)
REBOOT_CAUSE_FILE="/var/cache/sonic/reboot-cause.txt"
REBOOT_TYPE=$(basename $0)
WARM_DIR=/host/warmboot
REDIS_FILE=dump.rdb

# Check root privileges
if [[ "$EUID" -ne 0 ]]
then
echo "This command must be run as root" >&2
exit 1
fi

function clear_warm_boot()
{
config warm_restart disable || /bin/true
/sbin/kexec -u || /bin/true

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

Expand All @@ -34,14 +42,6 @@ case "$REBOOT_TYPE" in
;;
esac

# Check root privileges
if [[ "$EUID" -ne 0 ]]
then
echo "This command must be run as root" >&2
exit 1
fi


# Unload the previously loaded kernel if any loaded
if [[ "$(cat /sys/kernel/kexec_loaded)" -eq 1 ]]
then
Expand Down Expand Up @@ -139,31 +139,19 @@ systemctl stop syncd
# Warm reboot: dump state to host disk
# Note: even if syncd changed ASIC_DB before killed, we don't care
if [[ "$REBOOT_TYPE" = "warm-reboot" ]]; then
# Dump redis content to directory
# Note: don't use pretty mode redis-dump (1.1) since there is a known bug with key pattern
DUMP_CMD="redis-dump -s /var/run/redis/redis.sock"
# Dump redis content to a file 'dump.rdb' in warmboot directory
mkdir -p $WARM_DIR
# Note: requiring redis-dump-load
# Save applDB in /host/warm-reboot/appl_db.json
local pids=()
$DUMP_CMD -d 0 -o $WARM_DIR/appl_db.json &
pids+=($!)
# Save configDB in /host/warm-reboot/config_db.json
$DUMP_CMD -d 4 -o $WARM_DIR/config_db.json &
pids+=($!)
# Save stateDB (only FDB_TABLE and WARM_RESTART_TABLE) in /host/warm-reboot/state_db.json
# WARNING WARNING WARNING: a trick to dump both FDB_TABLE|* and WARM_RESTA*
# TODO: replace it with readable mechanism to dump multiple key patterns into one single json file
$DUMP_CMD -d 6 -k "[FW][DA][BR][_M][T_][AR][BE][LS][ET][|A]*" -o $WARM_DIR/state_db.json &
pids+=($!)
# Save asicDB in /host/warm-reboot/asic_db.json
$DUMP_CMD -d 1 -o $WARM_DIR/asic_db.json &
pids+=($!)
# Save loglevelDB in /host/warm-reboot/loglevel_db.json
$DUMP_CMD -d 3 -o $WARM_DIR/loglevel_db.json &
pids+=($!)

wait ${pids[@]}
# Delete keys in stateDB except FDB_TABLE|* and WARM_RESTA*
redis-cli -n 6 eval "
for _, k in ipairs(redis.call('keys', '*')) do
if not string.match(k, 'FDB_TABLE|') and not string.match(k, 'WARM_RESTART_TABLE|') then
redis.call('del', k)
end
end
" 0
redis-cli save
docker cp database:/var/lib/redis/$REDIS_FILE $WARM_DIR
docker exec -i database rm /var/lib/redis/$REDIS_FILE
fi

# Kill other containers to make the reboot faster
Expand Down
5 changes: 3 additions & 2 deletions scripts/reboot
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ function clear_warm_boot()
# If reboot is requested, make sure the outstanding warm-boot is cleared
# So the system will come up from a cold boot.
WARM_DIR="/host/warmboot"
REDIS_FILE=dump.rdb
TIMESTAMP=`date +%Y%m%d-%H%M%S`
if [[ -f ${WARM_DIR}/config_db.json ]]; then
mv -f ${WARM_DIR}/config_db.json ${WARM_DIR}/config_db-${TIMESTAMP}.json
if [[ -f ${WARM_DIR}/${REDIS_FILE} ]]; then
mv -f ${WARM_DIR}/${REDIS_FILE} ${WARM_DIR}/${REDIS_FILE}.${TIMESTAMP} || /bin/true
fi
/sbin/kexec -u || /bin/true
}
Expand Down

0 comments on commit 46e03b7

Please sign in to comment.