コード例 #1
0
ファイル: context.py プロジェクト: baidu/ARK
    def load_context(cls):
        """
        加载context,首次加载时,新建GuardianContext对象,否则从状态服务反序列化对象

        :return: context对象
        :rtype: GuardianContext
        """
        context_path = config.GuardianConfig.get_persistent_path("context")
        data = persistence.PersistenceDriver().get_data(context_path)
        log.i("load context success")
        guardian_context = pickle.loads(data) if data else GuardianContext()
        # load状态机信息
        operations_path = config.GuardianConfig.get_persistent_path("operations")
        # operations子节点名称均为operation_id
        operation_ids = persistence.PersistenceDriver().get_children(operations_path)
        for operation_id in operation_ids:
            operation_path = operations_path + "/" + operation_id
            try:
                operation_data = persistence.PersistenceDriver().get_data(operation_path)
                log.i("load operation[{}] success".format(operation_id))
                operation = pickle.loads(operation_data)
                guardian_context.operations[operation_id] = operation
            except Exception as e:
                log.f("load operation {} failed".format(operation_id))

        cls._context = guardian_context
        return guardian_context
コード例 #2
0
ファイル: context.py プロジェクト: baidu/ARK
 def save_operation(self, operation):
     """
     持久化状态机信息
     :param operation:
     :return:
     """
     if not self.lock:
         log.e("current guardian instance no privilege to save operation")
         raise exception.EInvalidOperation(
             "current guardian instance no privilege to save operation")
     operation_path = config.GuardianConfig.get_persistent_path("operations") + "/" + operation.operation_id
     if not persistence.PersistenceDriver().exists(operation_path):
         persistence.PersistenceDriver().create_node(path=operation_path)
     persistence.PersistenceDriver().save_data(operation_path, pickle.dumps(operation))
     log.d("save operation_id:{} success".format(operation.operation_id))
コード例 #3
0
    def init_environment(cls):
        """
        初始化Guardian运行环境

        :return: 无返回
        :rtype: None
        :raises EFailedRequest: 状态服务请求异常
        """

        guardian_base = config.GuardianConfig.get_persistent_path()
        guardian_client_path = config.GuardianConfig.get_persistent_path("alive_clients")
        context_path = config.GuardianConfig.get_persistent_path("context")
        operations_path = config.GuardianConfig.get_persistent_path("operations")
        pd = persistence.PersistenceDriver()

        if not pd.exists(guardian_base):
            pd.create_node(path=guardian_base, makepath=True)
            log.d("persistent node %s created!" % guardian_base)
        if not pd.exists(context_path):
            pd.create_node(path=context_path)
            log.d("persistent node %s created!" % context_path)
        if not pd.exists(guardian_client_path):
            pd.create_node(path=guardian_client_path)
            log.d("persistent node %s created!" % guardian_client_path)
        if not pd.exists(operations_path):
            pd.create_node(path=operations_path)
            log.d("persistent node %s created!" % operations_path)
コード例 #4
0
ファイル: context.py プロジェクト: baidu/ARK
    def delete_operation(self, operation_id):
        """
        删除一个操作,一般在一个事件操作结束时调用

        :param str operation_id: 操作id
        :return: 无返回
        :rtype: None
        """
        del self.operations[operation_id]
        operation_path = config.GuardianConfig.get_persistent_path("operations") + "/" + operation_id
        persistence.PersistenceDriver().delete_node(operation_path)
        log.d("delete operation from context success, operation_id:{}".
              format(operation_id))
コード例 #5
0
 def __init__(self, start_scheduler_func, stop_scheduler_func):
     """
     初始化方法
     
     :param func start_scheduler_func: 实例成为主时回调函数
     :param func stop_scheduler_func: 实例成为从时回调函数
     """
     self.path = config.GuardianConfig.get_persistent_path("alive_clients")
     self._start_scheduler_func = start_scheduler_func
     self._stop_scheduler_func = stop_scheduler_func
     self._pd = persistence.PersistenceDriver()
     self._pd.add_listener(self.state_listener)
     self.is_leader = False
コード例 #6
0
ファイル: context.py プロジェクト: pspk/ARK
    def save_context(self):
        """
        运行数据持久化,当当前Guardian为主(lock属性为True)时,可持久化数据,否则失败

        :return: 无返回
        :rtype: None
        :raises EInvalidOperation: 非法操作
        """
        if not self.lock:
            log.e("current guardian instance no privilege to save context")
            raise exception.EInvalidOperation(
                "current guardian instance no privilege to save context")
        context_path = config.GuardianConfig.get_persistent_path("context")
        context_to_persist = copy.deepcopy(self)
        # 考虑反序列化时可能异常,不使用del删除属性operations
        context_to_persist.operations = {}
        persistence.PersistenceDriver().save_data(
            context_path, pickle.dumps(context_to_persist))

        log.i("save context success")
コード例 #7
0
ファイル: context.py プロジェクト: baidu/ARK
    def save_context(self):
        """
        运行数据持久化,当当前Guardian为主(lock属性为True)时,可持久化数据,否则失败

        :return: 无返回
        :rtype: None
        :raises EInvalidOperation: 非法操作
        """
        if not self.lock:
            log.e("current guardian instance no privilege to save context")
            raise exception.EInvalidOperation(
                "current guardian instance no privilege to save context")
        context_path = config.GuardianConfig.get_persistent_path("context")
        context_to_persist = self
        operations_tmp = self.operations
        context_to_persist.operations = {}
        try:
            persistence.PersistenceDriver().save_data(context_path, pickle.dumps(context_to_persist))
        except Exception as e:
            self.operations = operations_tmp
            log.r(e, "save context fail")

        self.operations = operations_tmp
        log.d("save context success")