Skip to content

Commit c91707f

Browse files
authored
Migrate flush_unused_database from py-redis to sonic-swss-common (#15511)
Migrate flush_unused_database from py-redis to sonic-swss-common #### Why I did it flush_unused_database using py-redis, but sonic-swss-common already support flushdb, so we need migrate to sonic-swss-common ##### Work item tracking - Microsoft ADO **(number only)**: 24292565 #### How I did it Migrate flush_unused_database from py-redis to sonic-swss-common #### How to verify it Pass all UT and E2E test #### Description for the changelog Migrate flush_unused_database from py-redis to sonic-swss-common
1 parent aa46167 commit c91707f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

dockers/docker-database/flush_unused_database

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/python3
22
from swsscommon import swsscommon
3-
import redis
43
import subprocess
54
import time
65
import syslog
@@ -26,7 +25,18 @@ for instname, v in instlists.items():
2625
continue
2726

2827
try:
29-
r = redis.Redis(host=insthost, unix_socket_path=instsocket, db=dbid)
30-
r.flushdb()
31-
except (redis.exceptions.ConnectionError):
28+
# Migrate code from py-redis to swsscommon, original code:
29+
# r = redis.Redis(host=insthost, unix_socket_path=instsocket, db=dbid)
30+
# py-redis will use TCP connection when unix_socket_path is None
31+
# https://github.com/redis/redis-py/blob/d95d8a24ed2af3eae80b7b0f14cbccc9dbe86e96/redis/client.py#L1006
32+
if instsocket is not None:
33+
# connect with Unix socket
34+
connector = swsscommon.DBConnector(dbid, instsocket, 0)
35+
else:
36+
# connect with TCP socket
37+
port = swsscommon.SonicDBConfig.getDbPort(dbname);
38+
connector = swsscommon.DBConnector(dbid, insthost, port, 0)
39+
40+
connector.flushdb()
41+
except RuntimeError:
3242
syslog.syslog(syslog.LOG_INFO,"flushdb:Redis Unix Socket connection error for path {} and dbaname {}".format(instsocket, dbname))

0 commit comments

Comments
 (0)