perf: refresh oracle ports if need

pull/14062/head
ibuler 2024-08-27 11:32:38 +08:00 committed by 老广
parent b9e64747ac
commit f866b93f96
1 changed files with 17 additions and 5 deletions

View File

@ -13,7 +13,7 @@ logger = get_logger(__file__)
@Singleton @Singleton
class DBPortManager(object): class DBPortManager:
""" 管理端口-数据库ID的映射, Magnus 要使用 """ """ 管理端口-数据库ID的映射, Magnus 要使用 """
CACHE_KEY = 'PORT_DB_MAPPER' CACHE_KEY = 'PORT_DB_MAPPER'
@ -52,7 +52,9 @@ class DBPortManager(object):
db_ids_to_pop = set(mapper.values()) - set(db_ids) db_ids_to_pop = set(mapper.values()) - set(db_ids)
mapper = self.bulk_pop(db_ids_to_pop, mapper) mapper = self.bulk_pop(db_ids_to_pop, mapper)
self.set_mapper(mapper)
if db_ids_to_add or db_ids_to_pop:
self.set_mapper(mapper)
if settings.DEBUG: if settings.DEBUG:
logger.debug("Oracle listen ports: {}".format(len(mapper.keys()))) logger.debug("Oracle listen ports: {}".format(len(mapper.keys())))
@ -63,6 +65,7 @@ class DBPortManager(object):
db_ids = [str(i) for i in db_ids] db_ids = [str(i) for i in db_ids]
mapper = dict(zip(self.all_avail_ports, list(db_ids))) mapper = dict(zip(self.all_avail_ports, list(db_ids)))
self.set_mapper(mapper) self.set_mapper(mapper)
return mapper
def bulk_add(self, db_ids, mapper): def bulk_add(self, db_ids, mapper):
for db_id in db_ids: for db_id in db_ids:
@ -125,12 +128,20 @@ class DBPortManager(object):
mapper = self.get_mapper() mapper = self.get_mapper()
return sorted([int(i) for i in mapper.keys()]) return sorted([int(i) for i in mapper.keys()])
@staticmethod
def oracle_ports_setting_changed():
oracle_ports_cache = cache.get('MAGNUS_ORACLE_PORTS') or ''
if settings.MAGNUS_ORACLE_PORTS.split('-')[0] != oracle_ports_cache.split('-')[0]:
logger.info('Oracle ports setting changed')
return True
return False
def get_mapper(self): def get_mapper(self):
mapper = cache.get(self.CACHE_KEY, {}) mapper = cache.get(self.CACHE_KEY, {})
if not mapper: if not mapper or self.oracle_ports_setting_changed():
# redis 可能被清空,重新初始化一下 # redis 可能被清空,重新初始化一下
self.init() mapper = self.init()
return cache.get(self.CACHE_KEY, {}) return mapper
def set_mapper(self, value): def set_mapper(self, value):
""" """
@ -139,6 +150,7 @@ class DBPortManager(object):
} }
""" """
cache.set(self.CACHE_KEY, value, timeout=None) cache.set(self.CACHE_KEY, value, timeout=None)
cache.set('MAGNUS_ORACLE_PORTS', settings.MAGNUS_ORACLE_PORTS)
db_port_manager = DBPortManager() db_port_manager = DBPortManager()