[warmboot] Load database from redis-cli save (#2287)

* [warmboot] Load database from `redis-cli save`

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>

* Add trivial statement to make bash function valid

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>

* Update submodule sonic-utilities: Use 'redis-cli save' to dump database to file

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>

* Move configdb-load.sh outside docker, and only run in cold

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>

* Fix for more strict warm check

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
This commit is contained in:
Qi Luo 2018-11-22 15:13:35 -08:00 committed by lguohan
parent 6e71cc7887
commit c2ae736f2e
6 changed files with 42 additions and 40 deletions

View File

@ -36,6 +36,5 @@ RUN sed -ri 's/^(save .*$)/# \1/g; \
' /etc/redis/redis.conf ' /etc/redis/redis.conf
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"] COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
COPY ["files/configdb-load.sh", "/usr/bin/"]
ENTRYPOINT ["/usr/bin/supervisord"] ENTRYPOINT ["/usr/bin/supervisord"]

View File

@ -12,18 +12,10 @@ stdout_logfile=syslog
stderr_logfile=syslog stderr_logfile=syslog
[program:redis-server] [program:redis-server]
command=/usr/bin/redis-server /etc/redis/redis.conf command=/bin/bash -c "{ [[ -s /var/lib/redis/dump.rdb ]] || rm -f /var/lib/redis/dump.rdb; } && exec /usr/bin/redis-server /etc/redis/redis.conf"
priority=2 priority=2
autostart=true autostart=true
autorestart=false autorestart=false
stdout_logfile=syslog stdout_logfile=syslog
stderr_logfile=syslog stderr_logfile=syslog
[program:configdb-load.sh]
command=/usr/bin/configdb-load.sh
priority=3
autostart=true
autorestart=false
startsecs=0
stdout_logfile=syslog
stderr_logfile=syslog

View File

@ -4,7 +4,7 @@ Requires=docker.service
After=docker.service After=docker.service
[Service] [Service]
User={{ sonicadmin_user }} User=root
ExecStartPre=/usr/bin/{{docker_container_name}}.sh start ExecStartPre=/usr/bin/{{docker_container_name}}.sh start
ExecStart=/usr/bin/{{docker_container_name}}.sh attach ExecStart=/usr/bin/{{docker_container_name}}.sh attach
ExecStop=/usr/bin/{{docker_container_name}}.sh stop ExecStop=/usr/bin/{{docker_container_name}}.sh stop

View File

@ -16,40 +16,46 @@ function getBootType()
TYPE='warm' TYPE='warm'
;; ;;
*) *)
TYPE="normal" TYPE='cold'
esac esac
echo $TYPE echo $TYPE
} }
function preStartAction()
{
{%- if docker_container_name == "database" %}
WARM_DIR=/host/warmboot
if [[ "$BOOT_TYPE" == "warm" && -f $WARM_DIR/dump.rdb ]]; then
# Load redis content from /host/warm-reboot/dump.rdb
docker cp $WARM_DIR/dump.rdb database:/var/lib/redis/dump.rdb
else
# Create an emtpy file and overwrite any RDB if already there
echo -n > /tmp/dump.rdb
docker cp /tmp/dump.rdb database:/var/lib/redis/
fi
{%- else %}
: # nothing
{%- endif %}
}
function postStartAction() function postStartAction()
{ {
{%- if docker_container_name == "database" %} {%- if docker_container_name == "database" %}
# Wait until redis starts
REDIS_SOCK="/var/run/redis/redis.sock" REDIS_SOCK="/var/run/redis/redis.sock"
until [[ $(/usr/bin/docker exec database redis-cli -s $REDIS_SOCK ping | grep -c PONG) -gt 0 ]]; do until [[ $(/usr/bin/docker exec database redis-cli -s $REDIS_SOCK ping | grep -c PONG) -gt 0 ]]; do
sleep 1; sleep 1;
done done
WARM_DIR=/host/warmboot
SUDO="sudo -n" if [[ "$BOOT_TYPE" == "warm" && -f $WARM_DIR/dump.rdb ]]; then
BOOT_TYPE=`getBootType` rm -f $WARM_DIR/dump.rdb
if [[ "$BOOT_TYPE" == "warm" && -d $WARM_DIR ]]; then else
function redisLoadAndDelete() # If there is a config db dump file, load it
{ if [ -r /etc/sonic/config_db.json ]; then
DB="$1" sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
FILENAME="$2" fi
test -e $FILENAME || { echo "No file $FILENAME" >&2; exit 10; }
$SUDO redis-load -s $REDIS_SOCK -d $DB -e EMPTY $FILENAME || { echo "Failed to redis-load $FILENAME" >&2; exit 11; } redis-cli -n 4 SET "CONFIG_DB_INITIALIZED" "1"
$SUDO rm $FILENAME || exit 12
}
# Load applDB from /host/warm-reboot/appl_db.json
redisLoadAndDelete 0 $WARM_DIR/appl_db.json
# Load configDB from /host/warm-reboot/config_db.json
redisLoadAndDelete 4 $WARM_DIR/config_db.json
# Load stateDB from /host/warm-reboot/state_db.json
redisLoadAndDelete 6 $WARM_DIR/state_db.json
# Load asicDB from /host/warm-reboot/asic_db.json
redisLoadAndDelete 1 $WARM_DIR/asic_db.json
# Load loglevelDB from /host/warm-reboot/loglevel_db.json
redisLoadAndDelete 3 $WARM_DIR/loglevel_db.json
fi fi
{%- elif docker_container_name == "swss" %} {%- elif docker_container_name == "swss" %}
docker exec swss rm -f /ready # remove cruft docker exec swss rm -f /ready # remove cruft
@ -79,6 +85,9 @@ function postStartAction()
} }
start() { start() {
# Obtain boot type from kernel arguments
BOOT_TYPE=`getBootType`
# Obtain our platform as we will mount directories with these names in each docker # Obtain our platform as we will mount directories with these names in each docker
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform` PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
@ -95,6 +104,7 @@ start() {
DOCKERMOUNT=`getMountPoint "$DOCKERCHECK"` DOCKERMOUNT=`getMountPoint "$DOCKERCHECK"`
if [ "$DOCKERMOUNT" == "$HWSKU" ]; then if [ "$DOCKERMOUNT" == "$HWSKU" ]; then
echo "Starting existing {{docker_container_name}} container with HWSKU $HWSKU" echo "Starting existing {{docker_container_name}} container with HWSKU $HWSKU"
preStartAction
docker start {{docker_container_name}} docker start {{docker_container_name}}
postStartAction postStartAction
exit $? exit $?
@ -106,11 +116,11 @@ start() {
fi fi
{%- if docker_container_name == "database" %} {%- if docker_container_name == "database" %}
echo "Starting new {{docker_container_name}} container" echo "Creating new {{docker_container_name}} container"
{%- else %} {%- else %}
echo "Starting new {{docker_container_name}} container with HWSKU $HWSKU" echo "Creating new {{docker_container_name}} container with HWSKU $HWSKU"
{%- endif %} {%- endif %}
docker run -d {{docker_image_run_opt}} \ docker create {{docker_image_run_opt}} \
{%- if '--log-driver=json-file' in docker_image_run_opt or '--log-driver' not in docker_image_run_opt %} {%- if '--log-driver=json-file' in docker_image_run_opt or '--log-driver' not in docker_image_run_opt %}
--log-opt max-size=2M --log-opt max-file=5 \ --log-opt max-size=2M --log-opt max-file=5 \
{%- endif %} {%- endif %}
@ -133,6 +143,8 @@ start() {
exit 4 exit 4
} }
preStartAction
docker start {{docker_container_name}}
postStartAction postStartAction
} }

View File

@ -12,4 +12,3 @@ $(DOCKER_DATABASE)_RUN_OPT += --net=host --privileged -t
$(DOCKER_DATABASE)_RUN_OPT += -v /etc/sonic:/etc/sonic:ro $(DOCKER_DATABASE)_RUN_OPT += -v /etc/sonic:/etc/sonic:ro
$(DOCKER_DATABASE)_BASE_IMAGE_FILES += redis-cli:/usr/bin/redis-cli $(DOCKER_DATABASE)_BASE_IMAGE_FILES += redis-cli:/usr/bin/redis-cli
$(DOCKER_DATABASE)_FILES += $(CONFIGDB_LOAD_SCRIPT)

@ -1 +1 @@
Subproject commit a099681c56339ebcffd4aea9d2e232db07ed2cc0 Subproject commit 46e03b76d7555090366524f9846d3747673a1a08