Skip to content

Commit 9a17108

Browse files
[MultiDB] add MultiDB warmboot support - backing up database (sonic-net#1205)
* lua script to backup all database into one database and create rdb file - following earlier multiDB warmboot design at https://github.com/Azure/SONiC/blob/master/doc/database/multi_database_instances.md * copy this rdb file as before to WARM_DIR * restoring database part is in another PR at sonic-buildimage sonic-net#5773, they depend on each other
1 parent d4eb2d9 commit 9a17108

3 files changed

Lines changed: 71 additions & 5 deletions

File tree

scripts/centralize_database

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/python
2+
from __future__ import print_function
3+
import sys
4+
import swsssdk
5+
import redis
6+
import argparse
7+
8+
def centralize_to_target_db(target_dbname):
9+
target_dbport = swsssdk.SonicDBConfig.get_port(target_dbname)
10+
target_dbhost = swsssdk.SonicDBConfig.get_hostname(target_dbname)
11+
12+
dblists = swsssdk.SonicDBConfig.get_dblist()
13+
for dbname in dblists:
14+
dbport = swsssdk.SonicDBConfig.get_port(dbname)
15+
dbhost = swsssdk.SonicDBConfig.get_hostname(dbname)
16+
# if the db is on the same instance, no need to move
17+
if dbport == target_dbport and dbhost == target_dbhost:
18+
continue
19+
20+
dbsocket = swsssdk.SonicDBConfig.get_socket(dbname)
21+
dbid = swsssdk.SonicDBConfig.get_dbid(dbname)
22+
23+
r = redis.Redis(host=dbhost, unix_socket_path=dbsocket, db=dbid)
24+
25+
script = """
26+
local cursor = 0;
27+
repeat
28+
local dat = redis.call('SCAN', cursor, 'COUNT', 7000);
29+
cursor = dat[1];
30+
redis.call('MIGRATE', KEYS[1], KEYS[2], '', KEYS[3], 5000, 'COPY', 'REPLACE', 'KEYS', unpack(dat[2]));
31+
until cursor == '0';
32+
"""
33+
r.eval(script, 3, target_dbhost, target_dbport, dbid)
34+
35+
#SAVE rdb file
36+
r = redis.Redis(host=target_dbhost, port=target_dbport)
37+
r.save()
38+
39+
def main():
40+
parser = argparse.ArgumentParser(description='centralize all db data into one db instances',
41+
formatter_class=argparse.RawTextHelpFormatter,
42+
epilog=
43+
"""
44+
Example : centralize_database APPL_DB
45+
""")
46+
parser.add_argument('target_db', type=str, help='move all db data into the instance where target db locates')
47+
args = parser.parse_args()
48+
49+
if args.target_db:
50+
try:
51+
centralize_to_target_db(args.target_db)
52+
print(swsssdk.SonicDBConfig.get_instancename(args.target_db))
53+
except Exception as ex:
54+
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
55+
message = template.format(type(ex).__name__, ex.args)
56+
print(message, file=sys.stderr)
57+
sys.exit(1)
58+
else:
59+
parser.print_help()
60+
61+
if __name__ == "__main__":
62+
main()

scripts/fast-reboot

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,13 @@ function backup_database()
247247
end
248248
end
249249
" 0 > /dev/null
250-
sonic-db-cli SAVE > /dev/null
251-
#TODO : need a script to copy all rdb files if there is multiple db instances config
252-
docker cp database:/var/lib/redis/$REDIS_FILE $WARM_DIR
253-
docker exec -i database rm /var/lib/redis/$REDIS_FILE
250+
251+
# move all db data into the instance where APPL_DB locates
252+
target_db_inst=`centralize_database APPL_DB`
253+
254+
# Dump redis content to a file 'dump.rdb' in warmboot directory
255+
docker cp database:/var/lib/$target_db_inst/$REDIS_FILE $WARM_DIR
256+
docker exec -i database rm /var/lib/$target_db_inst/$REDIS_FILE
254257
}
255258
256259
function setup_control_plane_assistant()

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@
110110
'scripts/warm-reboot',
111111
'scripts/watermarkstat',
112112
'scripts/watermarkcfg',
113-
'scripts/sonic-kdump-config'
113+
'scripts/sonic-kdump-config',
114+
'scripts/centralize_database'
114115
],
115116
entry_points={
116117
'console_scripts': [

0 commit comments

Comments
 (0)