Skip to content

Commit 02b1783

Browse files
authored
Fix for fast/cold-boot: call db_migrator only after old config is loaded (#14933)
Why I did it Fix the issue where db_migrator is called before DB is loaded w/ config. This leads to db_migrator: Not finding anything, and resumes to incorrectly migrate every missing config This is not expected. migration should happen after the old config is loaded and only new schema changes need migration. Since DB does not have anything when migrator is called, db_migrator fails when some APIs return None. The reason for incorrect call is that: database service starts db_migrator as part of startup sequence. config-setup service loads data from old-config/minigraph. However, since it has Requires=database.service. Hence, config-setup starts only when database service is started. And database service is started when db_migrator is completed. Fixed by: Check if this is first time boot by checking pending_config_migration flag. If pending_config_migration is enabled, then do not call db_migrator as part of database service startup. Let database service start which triggers config-setup service to start. Now call db_migrator after when config-setup service loads old-config/minigraph
1 parent e5b360d commit 02b1783

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

files/build_templates/docker_image_ctl.j2

+12-4
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,18 @@ function postStartAction()
257257
$SONIC_DB_CLI CONFIG_DB SET "CONFIG_DB_INITIALIZED" "1"
258258
fi
259259

260-
if [[ -x /usr/local/bin/db_migrator.py ]]; then
261-
# Migrate the DB to the latest schema version if needed
262-
if [ -z "$DEV" ]; then
263-
/usr/local/bin/db_migrator.py -o migrate
260+
if [ -e /tmp/pending_config_migration ]; then
261+
# this is first boot to a new image, config-setup execution is pending.
262+
# For fast/cold reboot case, DB contains nothing at this point
263+
# Call db_migrator after config-setup loads the config (from old config or minigraph)
264+
echo "Delaying db_migrator until config migration is over"
265+
else
266+
# this is not a first time boot to a new image. Datbase container starts w/ old pre-existing config
267+
if [[ -x /usr/local/bin/db_migrator.py ]]; then
268+
# Migrate the DB to the latest schema version if needed
269+
if [ -z "$DEV" ]; then
270+
/usr/local/bin/db_migrator.py -o migrate
271+
fi
264272
fi
265273
fi
266274
# Add redis UDS to the redis group and give read/write access to the group

files/image_config/config-setup/config-setup

+13
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ check_all_config_db_present()
304304
return 0
305305
}
306306

307+
# DB schema is subject to change between two images
308+
# Perform DB schema migration after loading backup config from previous image
309+
do_db_migration()
310+
{
311+
if [[ -x /usr/local/bin/db_migrator.py ]]; then
312+
# Migrate the DB to the latest schema version if needed
313+
/usr/local/bin/db_migrator.py -o migrate
314+
fi
315+
}
316+
307317
# Perform configuration migration from backup copy.
308318
# - This step is performed when a new image is installed and SONiC switch boots into it
309319
do_config_migration()
@@ -326,16 +336,19 @@ do_config_migration()
326336
if [ x"${WARM_BOOT}" == x"true" ]; then
327337
echo "Warm reboot detected..."
328338
disable_updategraph
339+
do_db_migration
329340
rm -f /tmp/pending_config_migration
330341
exit 0
331342
elif check_all_config_db_present; then
332343
echo "Use config_db.json from old system..."
333344
reload_configdb
345+
do_db_migration
334346
# Disable updategraph
335347
disable_updategraph
336348
elif [ -r ${MINGRAPH_FILE} ]; then
337349
echo "Use minigraph.xml from old system..."
338350
reload_minigraph
351+
do_db_migration
339352
# Disable updategraph
340353
disable_updategraph
341354
else

0 commit comments

Comments
 (0)