Skip to content

Instantly share code, notes, and snippets.

@QQGoblin
Last active December 21, 2021 08:03
Show Gist options
  • Save QQGoblin/59a6dc8beefa1d08dab87891caad9321 to your computer and use it in GitHub Desktop.
Save QQGoblin/59a6dc8beefa1d08dab87891caad9321 to your computer and use it in GitHub Desktop.
【Patroni源码阅读】dcs初始化和导入
def get_dcs(config):
# 获取所有DCS实现的名称,dcs_modules()通过pkgutil查询dcs目录下所有python文件的名称
# 最终返回一个List,内容为:patroni.dcs.kubernetes 等等。
modules = dcs_modules()
for module_name in modules:
name = module_name.split('.')[-1] # dcs名称:如etcd、kubernetes等等
if name in config: # 判断配置文件中是否有对应的名称的Section配置段
try:
module = importlib.import_module(module_name)
for key, item in module.__dict__.items(): # iterate through the module content
# try to find implementation of AbstractDCS interface, class name must match with module_name
# 判断DCS文件中的Class是否是DCS的实现
if key.lower() == name and inspect.isclass(item) and issubclass(item, AbstractDCS):
# propagate some parameters
# 将namespace、name、scope等一些关键配置填充到dcs对应的配置
config[name].update({p: config[p] for p in ('namespace', 'name', 'scope', 'loop_wait',
'patronictl', 'ttl', 'retry_timeout') if p in config})
return item(config[name])
except ImportError:
logger.debug('Failed to import %s', module_name)
# 下面的逻辑知识为了打印错误日志,方便定位,没有实际意义
available_implementations = []
for module_name in modules:
name = module_name.split('.')[-1]
try:
module = importlib.import_module(module_name)
available_implementations.extend(name for key, item in module.__dict__.items() if key.lower() == name
and inspect.isclass(item) and issubclass(item, AbstractDCS))
except ImportError:
logger.info('Failed to import %s', module_name)
raise PatroniFatalException("""Can not find suitable configuration of distributed configuration store
Available implementations: """ + ', '.join(sorted(set(available_implementations))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment