Skip to content

Instantly share code, notes, and snippets.

@QQGoblin
Created December 24, 2021 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QQGoblin/83c7f1f972290b5a65913607b31f6c8f to your computer and use it in GitHub Desktop.
Save QQGoblin/83c7f1f972290b5a65913607b31f6c8f to your computer and use it in GitHub Desktop.
【Patroni源码阅读】pg初始化用户配置
class Ha(object):
def post_bootstrap(self):
# 当PG启动后,master节点执行用户名初始化,以及自定义脚本等工作
with self._async_response:
result = self._async_response.result
# bootstrap has failed if postgres is not running
if not self.state_handler.is_running() or result is False:
# cancel_initialization() 函数会进行以下操作:
# - 删除dcs上的initialize 信息
# - 强制停止pg
# - 清除数据目录
self.cancel_initialization()
# pg正在运行
if result is None:
# pg还处在recovery状态
if not self.state_handler.is_leader():
return 'waiting for end of recovery after bootstrap'
# 作为master启动
self.state_handler.set_role('master')
# 异步线程启动pg
# 此处self.state_handler.bootstrap.post_bootstrap 接受两个参数,第一个是启动配置self.patroni.config['bootstrap'],第二个用户接受启动的返回结果
ret = self._async_executor.try_run_async('post_bootstrap', self.state_handler.bootstrap.post_bootstrap,
args=(self.patroni.config['bootstrap'], self._async_response))
return ret or 'running post_bootstrap'
# 这里清空state_handler.bootstrapping参数,下一个loop不会在进入
self.state_handler.bootstrapping = False
if not self.watchdog.activate():
logger.error('Cancelling bootstrap because watchdog activation failed')
self.cancel_initialization()
# 不知道干啥的?
self._rewind.ensure_checkpoint_after_promote(self.wakeup)
#
self.dcs.initialize(create_new=(self.cluster.initialize is None), sysid=self.state_handler.sysid)
self.dcs.set_config_value(json.dumps(self.patroni.config.dynamic_configuration, separators=(',', ':')))
self.dcs.take_leader()
self.set_is_leader(True)
self.state_handler.call_nowait(ACTION_ON_START)
self.load_cluster_from_dcs()
return 'initialized a new cluster'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment