2022-09-22 11:09:39 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-09-21 12:56:40 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.conf import settings
|
2022-09-22 11:09:39 +00:00
|
|
|
|
|
|
|
from common.decorator import Singleton
|
|
|
|
from common.utils import get_logger, get_object_or_none
|
|
|
|
from common.exceptions import JMSException
|
2022-09-21 12:56:40 +00:00
|
|
|
from applications.const import AppCategory
|
|
|
|
from applications.models import Application
|
2022-09-22 09:23:17 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2022-09-21 12:56:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class DBPortManager(object):
|
|
|
|
""" 管理端口-数据库ID的映射, Magnus 要使用 """
|
|
|
|
|
|
|
|
CACHE_KEY = 'PORT_DB_MAPPER'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.port_start = settings.MAGNUS_DB_PORTS_START
|
|
|
|
self.port_limit = settings.MAGNUS_DB_PORTS_LIMIT_COUNT
|
2022-09-22 09:23:17 +00:00
|
|
|
self.port_end = self.port_start + self.port_limit
|
2022-09-21 12:56:40 +00:00
|
|
|
# 可以使用的端口列表
|
2022-09-22 11:09:39 +00:00
|
|
|
self.all_available_ports = list(range(self.port_start, self.port_end))
|
2022-09-22 09:23:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def magnus_listen_port_range(self):
|
2022-09-22 11:22:17 +00:00
|
|
|
return f'{self.port_start}-{self.port_end - 1}'
|
2022-09-21 12:56:40 +00:00
|
|
|
|
|
|
|
def init(self):
|
2022-09-22 10:47:16 +00:00
|
|
|
with tmp_to_root_org():
|
|
|
|
db_ids = Application.objects.filter(category=AppCategory.db).values_list('id', flat=True)
|
2022-09-21 12:56:40 +00:00
|
|
|
db_ids = [str(i) for i in db_ids]
|
2022-09-22 10:47:16 +00:00
|
|
|
mapper = dict(zip(self.all_available_ports, list(db_ids)))
|
2022-09-21 12:56:40 +00:00
|
|
|
self.set_mapper(mapper)
|
|
|
|
|
|
|
|
def add(self, db: Application):
|
|
|
|
mapper = self.get_mapper()
|
2022-09-22 10:47:16 +00:00
|
|
|
available_port = self.get_next_available_port()
|
|
|
|
mapper.update({available_port: str(db.id)})
|
2022-09-21 12:56:40 +00:00
|
|
|
self.set_mapper(mapper)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def pop(self, db: Application):
|
|
|
|
mapper = self.get_mapper()
|
2022-09-22 06:48:20 +00:00
|
|
|
to_delete_port = self.get_port_by_db(db)
|
2022-09-21 12:56:40 +00:00
|
|
|
mapper.pop(to_delete_port, None)
|
|
|
|
self.set_mapper(mapper)
|
|
|
|
|
2022-09-22 06:48:20 +00:00
|
|
|
def get_port_by_db(self, db):
|
|
|
|
mapper = self.get_mapper()
|
|
|
|
for port, db_id in mapper.items():
|
|
|
|
if db_id == str(db.id):
|
|
|
|
return port
|
2022-09-22 10:47:16 +00:00
|
|
|
raise JMSException(
|
|
|
|
'Not matched db port, db id: {}, mapper length: {}'.format(db.id, len(mapper))
|
2022-09-22 07:52:47 +00:00
|
|
|
)
|
2022-09-22 06:48:20 +00:00
|
|
|
|
|
|
|
def get_db_by_port(self, port):
|
2022-09-22 11:18:38 +00:00
|
|
|
try:
|
|
|
|
port = int(port)
|
|
|
|
except Exception as e:
|
|
|
|
raise JMSException('Port type error: {}'.format(e))
|
2022-09-22 06:48:20 +00:00
|
|
|
mapper = self.get_mapper()
|
|
|
|
db_id = mapper.get(port, None)
|
2022-09-22 10:47:16 +00:00
|
|
|
if not db_id:
|
|
|
|
raise JMSException('Database not in port-db mapper, port: {}'.format(port))
|
|
|
|
with tmp_to_root_org():
|
|
|
|
db = get_object_or_none(Application, id=db_id)
|
|
|
|
if not db:
|
|
|
|
raise JMSException('Database not exists, db id: {}'.format(db_id))
|
|
|
|
return db
|
|
|
|
|
|
|
|
def get_next_available_port(self):
|
2022-09-21 12:56:40 +00:00
|
|
|
already_use_ports = self.get_already_use_ports()
|
2022-09-22 10:47:16 +00:00
|
|
|
available_ports = sorted(list(set(self.all_available_ports) - set(already_use_ports)))
|
|
|
|
if len(available_ports) <= 0:
|
2022-09-22 11:09:39 +00:00
|
|
|
msg = _('No ports can be used, check and modify the limit on the number '
|
|
|
|
'of ports that Magnus listens on in the configuration file.')
|
|
|
|
tips = _('All available port count: {}, Already use port count: {}').format(
|
|
|
|
len(self.all_available_ports), len(already_use_ports)
|
2022-09-22 10:47:16 +00:00
|
|
|
)
|
2022-09-22 11:09:39 +00:00
|
|
|
error = msg + tips
|
|
|
|
raise JMSException(error)
|
2022-09-22 10:47:16 +00:00
|
|
|
port = available_ports[0]
|
|
|
|
logger.debug('Get next available port: {}'.format(port))
|
|
|
|
return port
|
2022-09-21 12:56:40 +00:00
|
|
|
|
|
|
|
def get_already_use_ports(self):
|
|
|
|
mapper = self.get_mapper()
|
2022-09-22 09:23:17 +00:00
|
|
|
return sorted(list(mapper.keys()))
|
2022-09-21 12:56:40 +00:00
|
|
|
|
|
|
|
def get_mapper(self):
|
2022-09-22 10:47:16 +00:00
|
|
|
mapper = cache.get(self.CACHE_KEY, {})
|
|
|
|
if not mapper:
|
|
|
|
# redis 可能被清空,重新初始化一下
|
|
|
|
self.init()
|
2022-09-21 12:56:40 +00:00
|
|
|
return cache.get(self.CACHE_KEY, {})
|
|
|
|
|
|
|
|
def set_mapper(self, value):
|
2022-09-22 06:48:20 +00:00
|
|
|
"""
|
|
|
|
value: {
|
|
|
|
port: db_id
|
|
|
|
}
|
|
|
|
"""
|
2022-09-21 12:56:40 +00:00
|
|
|
cache.set(self.CACHE_KEY, value, timeout=None)
|
|
|
|
|
|
|
|
|
|
|
|
db_port_manager = DBPortManager()
|