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

Migrate flush_unused_database from py-redis to sonic-swss-common #15511

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Changes from all commits
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
18 changes: 14 additions & 4 deletions dockers/docker-database/flush_unused_database
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/python3
from swsscommon import swsscommon
import redis
import subprocess
import time
import syslog
Expand All @@ -26,7 +25,18 @@ for instname, v in instlists.items():
continue

try:
r = redis.Redis(host=insthost, unix_socket_path=instsocket, db=dbid)
r.flushdb()
except (redis.exceptions.ConnectionError):
# Migrate code from py-redis to swsscommon, original code:
# r = redis.Redis(host=insthost, unix_socket_path=instsocket, db=dbid)
# py-redis will use TCP connection when unix_socket_path is None
# https://github.com/redis/redis-py/blob/d95d8a24ed2af3eae80b7b0f14cbccc9dbe86e96/redis/client.py#L1006
if instsocket is not None:
# connect with Unix socket
connector = swsscommon.DBConnector(dbid, instsocket, 0)
else:
# connect with TCP socket
port = swsscommon.SonicDBConfig.getDbPort(dbname);
connector = swsscommon.DBConnector(dbid, insthost, port, 0)

connector.flushdb()
except RuntimeError:
syslog.syslog(syslog.LOG_INFO,"flushdb:Redis Unix Socket connection error for path {} and dbaname {}".format(instsocket, dbname))