コード例 #1
0
ファイル: client.py プロジェクト: wayhk/ARK
    def save_data(self, path, data):
        """
        存储数据data到特定的path路径节点

        :param str path: 数据存储路径
        :param str data: 待存储的数据
        :return: None
        :raises: kazoo.exceptions.NoNodeError 节点不存在
        :raises: kazoo.exceptions.ZookeeperError 连接异常
        """
        self.client.set(path, data)
        log.debug("save data success, path:{}, data:{}".format(path, data))
コード例 #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
ファイル: client.py プロジェクト: wayhk/ARK
    def create_node(self, path, value="", acl=None,
                    ephemeral=False, sequence=False, makepath=False):
        """
        根据节点各属性创建节点

        :param str path: 节点路径
        :param str value: 待存数据
        :param str acl: 节点ACl属性
        :param bool ephemeral: 是否是临时节点
        :param bool sequence: 是否是顺序节点
        :param bool makepath: 是否创建父节点
        :return: None
        :raises: kazoo.exceptions.NodeExistsError 节点存在
        :raises: kazoo.exceptions.ZookeeperError 连接异常
        """
        self.client.create(path, value, acl, ephemeral, sequence, makepath)
        log.debug("create node success, path:{}, value:{}, acl:{}, ephemeral:"
                  "{}, sequence:{}, makepath:{}".format(path, value, acl,
                                                        ephemeral, sequence,
                                                        makepath))
コード例 #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
コード例 #5
0
ファイル: client.py プロジェクト: wayhk/ARK
    def http_request(self, host, port, method, url, header=None, data=None,
                     timeout=30,retry_times=2, response_code=None,
                     response_json=True):
        """
        http请求接口

        :param str host: 服务器地址
        :param int port: 服务器端口
        :param str method: http方法
        :param str url: url地址
        :param dict header: http消息头
        :param str data: http body数据
        :param int timeout: 请求超时时间
        :param int retry_times: 请求重试次数
        :param list response_code: 请求正确时的返回码
        :param bool response_json: True 返回json格式的数据,False 返回字符串
        :return: http 请求的数据
        :rtype: str
        :raises EFailedRequest: 请求失败
        """
        log.debug("http request, host:{}, port:{}, method:{}, url:{}, header:"
                  "{}, data:{}, timeout:{}, retry_times:{}, response code:{}, "
                  "response_json:{}".format(host, port, method, url, header,
                                            data, timeout, retry_times,
                                            response_code, response_json))
        header = header or {}
        res_data = None
        for i in range(retry_times + 1):
            stop_tag = True if i == retry_times else False
            sleep_time = (i + 1) * (i + 1)
            try:
                conn = httplib.HTTPConnection(
                    host=host, port=port, timeout=timeout)
                conn.request(method=method, url=url, body=data, headers=header)
                resp = conn.getresponse()
                res_data = resp.read()
                log.info("http request ret: %s" % res_data)
            except Exception as e:
                log.info("http request exe: %s" % e)
                if stop_tag:
                    log.error("http request failed,error:{}".format(e))
                    raise exception.EFailedRequest(
                        "http request failed,error:{}".format(e))
                else:
                    time.sleep(sleep_time)
                    continue
            else:
                log.debug("http request ok")
                if not response_code or not isinstance(response_code, list):
                    if 200 <= resp.status < 300:
                        break
                    elif stop_tag:
                        log.error("request failed,code:{},msg:{}".format(
                                resp.status, resp.msg))
                        raise exception.EFailedRequest(
                            "request failed,code:{},msg:{}".format(
                                resp.status, resp.msg))
                    else:
                        time.sleep((i + 1) * (i + 1))
                        continue
                else:
                    if resp.status in response_code:
                        break
                    elif stop_tag:
                        log.error("request failed,error,code:{},data:{}".format(
                                resp.status, data))
                        raise exception.EFailedRequest(
                            "request failed,error,code:{},data:{}".format(
                                resp.status, data))
                    else:
                        time.sleep(sleep_time)
                        continue
        log.debug("http response data:{}".format(res_data))
        if response_json:
            return json.loads(res_data)
        else:
            return res_data