Skip to content

Commit

Permalink
modify fast-reboot table structure, add db migrator ut
Browse files Browse the repository at this point in the history
  • Loading branch information
arfeigin committed Feb 28, 2023
1 parent 5dccee0 commit 57019f1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
18 changes: 13 additions & 5 deletions scripts/db_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import traceback
import re
import subprocess

from sonic_py_common import device_info, logger
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector, SonicDBConfig
Expand Down Expand Up @@ -854,12 +855,19 @@ def version_4_0_0(self):
log.log_info('Handling version_4_0_0')
# Update state-db fast-reboot entry to enable if set to enable fast-reboot finalizer when using upgrade with fast-reboot
# since upgrading from previous version FAST_REBOOT table will be deleted when the timer will expire.
fastreboot_state = self.stateDB.get(self.stateDB.STATE_DB, 'FAST_REBOOT|system', '1')
if fastreboot_state == 'true':
enable_state = 'enable'
# reading FAST_REBOOT table can't be done with stateDB.get as it uses hget behind the scenes and the table structure is
# not using hash and won't work.
fb_system_state = 0
cmd = ['sonic-db-cli', 'STATE_DB', 'get', "FAST_REBOOT|system"]
proc = subprocess.Popen(cmd, universal_newlines=True, stdout=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if proc.returncode == 0 and stdout:
fb_system_state = stdout.rstrip('\n')
if fb_system_state == 1:
enable_state = 'true'
else:
enable_state = 'disable'
self.stateDB.set(self.stateDB.STATE_DB, 'FAST_RESTART_ENABLE_TABLE', 'system', enable_state)
enable_state = 'false'
self.stateDB.set(self.stateDB.STATE_DB, 'FAST_RESTART_ENABLE_TABLE|system', 'enable', enable_state)
self.set_version('version_4_0_1')
return 'version_4_0_1'

Expand Down
4 changes: 2 additions & 2 deletions scripts/fast-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function clear_boot()
#clear_fast_boot
if [[ "$REBOOT_TYPE" = "fast-reboot" ]]; then
sonic-db-cli STATE_DB SET "FAST_RESTART_ENABLE_TABLE|system" "disable" &>/dev/null || /bin/true
sonic-db-cli STATE_DB HSET "FAST_RESTART_ENABLE_TABLE|system" "enable" "false" &>/dev/null || /bin/true
fi
}
Expand Down Expand Up @@ -532,7 +532,7 @@ case "$REBOOT_TYPE" in
check_warm_restart_in_progress
BOOT_TYPE_ARG=$REBOOT_TYPE
trap clear_boot EXIT HUP INT QUIT TERM KILL ABRT ALRM
sonic-db-cli STATE_DB SET "FAST_RESTART_ENABLE_TABLE|system" "enable" &>/dev/null
sonic-db-cli STATE_DB HSET "FAST_RESTART_ENABLE_TABLE|system" "enable" "true" &>/dev/null
config warm_restart enable system
;;
"warm-reboot")
Expand Down
4 changes: 2 additions & 2 deletions sonic-utilities-data/templates/service_mgmt.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function check_warm_boot()

function check_fast_boot()
{
SYSTEM_FAST_REBOOT=`sonic-db-cli STATE_DB GET "FAST_RESTART_ENABLE_TABLE|system"`
if [[ ${SYSTEM_FAST_REBOOT} == "enable" ]]; then
SYSTEM_FAST_REBOOT=`sonic-db-cli STATE_DB GET "FAST_RESTART_ENABLE_TABLE|system" enable`
if [[ x"${SYSTEM_FAST_REBOOT}" == x"true" ]]; then
FAST_BOOT="true"
else
FAST_BOOT="false"
Expand Down
5 changes: 5 additions & 0 deletions tests/db_migrator_input/state_db/fast_reboot_expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"FAST_RESTART_ENABLE_TABLE|system": {
"enable": "false"
}
}
2 changes: 2 additions & 0 deletions tests/db_migrator_input/state_db/fast_reboot_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
31 changes: 31 additions & 0 deletions tests/db_migrator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,37 @@ def test_move_logger_tables_in_warm_upgrade(self):
diff = DeepDiff(resulting_table, expected_table, ignore_order=True)
assert not diff

class TestFastRebootTableModification(object):
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "2"

@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
dbconnector.dedicated_dbs['STATE_DB'] = None

def mock_dedicated_state_db(self):
dbconnector.dedicated_dbs['STATE_DB'] = os.path.join(mock_db_path, 'state_db')

def test_rename_fast_reboot_table_check_enable(self):
device_info.get_sonic_version_info = get_sonic_version_info_mlnx
dbconnector.dedicated_dbs['STATE_DB'] = os.path.join(mock_db_path, 'state_db', 'fast_reboot_input')

import db_migrator
dbmgtr = db_migrator.DBMigrator(None)
dbmgtr.migrate()

dbconnector.dedicated_dbs['STATE_DB'] = os.path.join(mock_db_path, 'state_db', 'fast_reboot_expected')

expected_db = Db()

resulting_table = dbmgtr.stateDB.get_table('FAST_RESTART_ENABLE_TABLE')
expected_table = expected_db.stateDB.get_table('FAST_RESTART_ENABLE_TABLE')

diff = DeepDiff(resulting_table, expected_table, ignore_order=True)
assert not diff

class TestWarmUpgrade_to_2_0_2(object):
@classmethod
def setup_class(cls):
Expand Down

0 comments on commit 57019f1

Please sign in to comment.