Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MultiDB] add MultiDB warmboot support - backing up database #1205

Merged
merged 2 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions scripts/fast-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@ function backup_database()
end
end
" 0 > /dev/null
sonic-db-cli SAVE > /dev/null
#TODO : need a script to copy all rdb files if there is multiple db instances config
docker cp database:/var/lib/redis/$REDIS_FILE $WARM_DIR
docker exec -i database rm /var/lib/redis/$REDIS_FILE

# move all db data into instances where APPL_DB locates
migrate_warmboot_database APPL_DB
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

migrate_warmboot_database [](start = 4, length = 25)

The name is misleading. It is not directly related to warmboot.
Suggestion centralize_database
#Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


inst=`/usr/bin/python -c "import swsssdk; print(swsssdk.SonicDBConfig.get_instancename('APPL_DB'))"`
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inst [](start = 4, length = 4)

Instead of using a new process to locate APPL_DB, you may just print it to stdout in above command. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

# Dump redis content to a file 'dump.rdb' in warmboot directory
docker cp database:/var/lib/$inst/$REDIS_FILE $WARM_DIR
docker exec -i database rm /var/lib/$inst/$REDIS_FILE
}

function setup_control_plane_assistant()
Expand Down
61 changes: 61 additions & 0 deletions scripts/migrate_warmboot_database
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python
from __future__ import print_function
import sys
import swsssdk
import redis
import argparse

def migrate(target_dbname):
target_dbport = swsssdk.SonicDBConfig.get_port(target_dbname)
target_dbhost = swsssdk.SonicDBConfig.get_hostname(target_dbname)

dblists = swsssdk.SonicDBConfig.get_dblist()
for dbname in dblists:
dbport = swsssdk.SonicDBConfig.get_port(dbname)
# if the db is on the same instance, no need to migrate
if dbport == target_dbport:
continue
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it enough to determine "the same instance" ? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. hostname plus port is enough to determine the "unique instance" . Updated


dbsocket = swsssdk.SonicDBConfig.get_socket(dbname)
dbid = swsssdk.SonicDBConfig.get_dbid(dbname)
dbhost = swsssdk.SonicDBConfig.get_hostname(dbname)

r = redis.Redis(host=dbhost, unix_socket_path=dbsocket, db=dbid)

script = """
local cursor = 0;
repeat
local dat = redis.call('SCAN', cursor, 'COUNT', 7000);
cursor = dat[1];
redis.call('MIGRATE', KEYS[1], KEYS[2], '', KEYS[3], 5000, 'COPY', 'REPLACE', 'KEYS', unpack(dat[2]));
until cursor == '0';
"""
r.eval(script, 3, target_dbhost, target_dbport, dbid)

#SAVE rdb file
r = redis.Redis(host=target_dbhost, port=target_dbport)
r.save()

def main():
parser = argparse.ArgumentParser(description='migrate all db data into one db instances',
formatter_class=argparse.RawTextHelpFormatter,
epilog=
"""
Example : migrate_warmboot_database APPL_DB
""")
parser.add_argument('target_db', type=str, help='move all db data into the instance where target db locates')
args = parser.parse_args()

if args.target_db:
try:
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        [](start = 0, length = 12)

too much indentation. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

migrate(args.target_db)
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message, file=sys.stderr)
sys.exit(1)
else:
parser.print_help()

if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@
'scripts/warm-reboot',
'scripts/watermarkstat',
'scripts/watermarkcfg',
'scripts/sonic-kdump-config'
'scripts/sonic-kdump-config',
'scripts/migrate_warmboot_database'
],
entry_points={
'console_scripts': [
Expand Down