def _zookeeper_resolver(self, cfg): hosts, path = cfg[5:].split("/", 1) path = "/" + path retry = KazooRetry(max_tries=10) with zookeeper.client(hosts=hosts, read_only=True, connection_retry=retry) as zk: def master_id(key): return int(key.split("_")[-1]) def get_masters(): return [x for x in zk.get_children(path) if re.search("\d+", x)] leader = sorted(get_masters(), key=lambda x: master_id(x)) if len(leader) == 0: raise exceptions.MasterNotAvailableException("cannot find any masters at {0}".format(cfg,)) data, stat = zk.get(os.path.join(path, leader[0])) if not data: exceptions.MasterNotAvailableException("Cannot retrieve valid MasterInfo data from ZooKeeper") try: parsed = json.loads(data) if parsed and "address" in parsed: ip = parsed["address"].get("ip") port = parsed["address"].get("port") if ip and port: return "{ip}:{port}".format(ip=ip, port=port) except ValueError as parse_error: log.debug("[WARN] No JSON content, probably connecting to older Mesos version. " "Reason: {}".format(parse_error)) raise exceptions.MasterNotAvailableException("Failed to parse mesos master ip from ZK")
def _request(self, url, method=requests.get, cached=False, **kwargs): headers = {'User-Agent': get_user_agent()} if cached and self.config.get("use_mesos_cache", False): # TODO: fall back to original host if this fails? host = self.cache_host else: host = self.host try: return method( urljoin(host, url), timeout=self.config["response_timeout"], headers=headers, **kwargs, ) except requests.exceptions.ConnectionError: raise exceptions.MasterNotAvailableException( MISSING_MASTER.format(host), ) except requests.exceptions.TooManyRedirects: raise exceptions.MasterTemporarilyNotAvailableException( ( "Unable to connect to master at %s, likely due to " "an ongoing leader election" ) % host, )
def fetch(self, url, **kwargs): try: return requests.get(urljoin(self.host, url), timeout=self.config["response_timeout"], **kwargs) except requests.exceptions.ConnectionError: raise exceptions.MasterNotAvailableException( MISSING_MASTER.format(self.host))
def _request(self, url, method=requests.get, **kwargs): headers = {'User-Agent': get_user_agent()} try: return method( urljoin(self.host, url), timeout=self.config["response_timeout"], headers=headers, **kwargs, ) except requests.exceptions.ConnectionError: raise exceptions.MasterNotAvailableException(MISSING_MASTER.format(self.host)) except requests.exceptions.TooManyRedirects: raise exceptions.MasterTemporarilyNotAvailableException( "Unable to connect to master at %s, likely due to an ongoing leader election" % self.host, )