mirror of
				https://github.com/Telecominfraproject/ols-nos.git
				synced 2025-10-31 18:17:52 +00:00 
			
		
		
		
	 c91707ff31
			
		
	
	c91707ff31
	
	
	
		
			
			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
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
| #!/usr/bin/python3
 | |
| from swsscommon import swsscommon
 | |
| import subprocess
 | |
| import time
 | |
| import syslog
 | |
| 
 | |
| while(True):
 | |
|     output = subprocess.Popen(['sonic-db-cli', 'PING'], stdout=subprocess.PIPE, text=True).communicate()[0]
 | |
|     if 'PONG' in output:
 | |
|         break
 | |
|     time.sleep(1)
 | |
| 
 | |
| instlists = swsscommon.SonicDBConfig.getInstanceList()
 | |
| for instname, v in instlists.items():
 | |
|     insthost = v.hostname
 | |
|     instsocket = v.unixSocketPath
 | |
| 
 | |
|     dblists = swsscommon.SonicDBConfig.getDbList()
 | |
|     for dbname in dblists:
 | |
|         dbid = swsscommon.SonicDBConfig.getDbId(dbname)
 | |
|         dbinst = swsscommon.SonicDBConfig.getDbInst(dbname)
 | |
| 
 | |
|         # this DB is on current instance, skip flush
 | |
|         if dbinst == instname:
 | |
|             continue
 | |
| 
 | |
|         try:
 | |
|             # 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))
 |