示例#1
0
 def __init__(self, start_scheduler_func, stop_scheduler_func):
     """
     初始化方法
     
     :param func start_scheduler_func: 实例成为主时回调函数
     :param func stop_scheduler_func: 实例成为从时回调函数
     """
     self.path = "/{}/alive_clients".format(
         config.GuardianConfig.get(GUARDIAN_ID_NAME))
     self._start_scheduler_func = start_scheduler_func
     self._stop_scheduler_func = stop_scheduler_func
     self.zk = ZkClient()
     self.zk.client.add_listener(self.state_listener)
     self.is_leader = False
示例#2
0
文件: context.py 项目: wayhk/ARK
    def load_context(cls):
        """
        加载context,首次加载时,新建GuardianContext对象,否则从状态服务反序列化对象

        :return: context对象
        :rtype: GuardianContext
        """
        context_path = CONTEXT_PATH.format(
            config.GuardianConfig.get(GUARDIAN_ID))
        data = ZkClient().get_data(context_path)
        log.debug("load context from zookeeper success")
        guardian_context = pickle.loads(data) if data else GuardianContext()
        cls._context = guardian_context
        return guardian_context
示例#3
0
文件: context.py 项目: wayhk/ARK
    def save_context(self):
        """
        运行数据持久化,当当前Guardian为主(lock属性为True)时,可持久化数据,否则失败

        :return: 无返回
        :rtype: None
        :raises EInvalidOperation: 非法操作
        """
        if not self.lock:
            log.error("current guardian instance no privilege to save context")
            raise exception.EInvalidOperation(
                "current guardian instance no privilege to save context")
        context_path = CONTEXT_PATH.format(self.__guardian_id)
        ZkClient().save_data(context_path, pickle.dumps(self))
        log.info("save context success")
示例#4
0
    def init_environment(self):
        """
        初始化Guardian运行环境

        :return: 无返回
        :rtype: None
        :raises EFailedRequest: 状态服务请求异常
        """
        config.GuardianConfig.load_config()
        guardian_id = config.GuardianConfig.get("GUARDIAN_ID")
        guardian_root_path = "/{}".format(guardian_id)
        guardian_client_path = "{}/alive_clients".format(guardian_root_path)
        context_path = guardian_root_path + "/context"
        try:
            zkc = ZkClient()
        except exception.EFailedRequest as e:
            raise e
        try:
            if not zkc.exists(guardian_root_path):
                zkc.create_node(path=guardian_root_path)
                log.debug("zk node %s created!" % guardian_root_path)
            if not zkc.exists(context_path):
                zkc.create_node(path=context_path)
                log.debug("zk node %s created!" % context_path)
            if not zkc.exists(guardian_client_path):
                zkc.create_node(path=guardian_client_path)
                log.debug("zk node %s created!" % guardian_client_path)
        except Exception as e:
            raise e