def getRack(): try: def watcher(event): logger.info("/rack children changed, need update memory") getRack() zk_client.get('/rack', watcher) children = zk_client.get_children('/rack') for child in children: rack_name = child.encode('utf-8') RACK_STORE[rack_name] = [] path1 = join('/rack', rack_name) children1 = zk_client.get_children(path1) for child1 in children1: store_id = child1.encode('utf-8') RACK_STORE[rack_name].append(store_id) path2 = join(path1, store_id) data, stat = zk_client.get(path2) if data: parsed_data = json.loads(data) ip = parsed_data['stat'].split(':')[0].encode('utf-8') STORE_TO_IP[store_id] = ip IP_TO_STORE[ip] = store_id STORE_RACK[store_id] = rack_name STORE_INFO[FREE_VOLUME_KEY+store_id] = -1 STORE_INFO[VOLUME_KEY+store_id] = 0 else: logger.warn("getRack() called zk data is None path: %s", path2) return False return True except Exception as ex: logger.error("getRack() called error: %s", str(ex)) return False
def request(self, method, url, body=None, headers={}): '''return [True|False], status, data''' try: if self.http_conn is None: self.http_conn = httplib.HTTPConnection(self.host, self.port, timeout=HttpConnection.DEFAULT_TIMEOUT) self.http_conn.request(method, url, body, headers) conn_resp = self.http_conn.getresponse() status = conn_resp.status data = conn_resp.read() return (True, status, data) except Exception as ex: self.http_conn = None logger.warn('HttpConnection.request, %s %s, except %s', method, url, str(ex)) return (False, None, None)
def request(self, method, url, body=None, headers={}): '''return [True|False], status, data''' try: print url,body if self.http_conn is None: self.http_conn = httplib.HTTPConnection(self.host, self.port, timeout=HttpConnection.DEFAULT_TIMEOUT) self.http_conn.request(method, url, body, headers) conn_resp = self.http_conn.getresponse() status = conn_resp.status data = conn_resp.read() return (True, status, data) except Exception as ex: self.http_conn = None logger.warn('HttpConnection.request, %s %s, except %s', method, url, str(ex)) return (False, None, None)