def _recv(self): print "<%s> observe receive responses" % self.__class__.__name__ responses = [] for server in self.obs_keys.iterkeys(): hdr = '' while len(hdr) < ObservePktFmt.OBS_RES_HDR_LEN: try: hdr += self.conns[server].s.recv(ObservePktFmt.OBS_RES_HDR_LEN) except Exception as e: print "<%s> failed to recv observe pkt: %s" % (self.__class__.__name__, e) return None res = ObserveResponse() if not res.unpack_hdr(hdr): return None body = '' while len(body) < res.body_len: body += self.conns[server].s.recv(res.body_len) res.unpack_body(body) # TODO: error check self.save_latency_stats(res.persist_stat/1000) print "res::<%s>" % server print res responses.append(res) return responses
def _recv(self): responses = {} # {server: [responses]} for server in self.obs_keys.iterkeys(): hdr = '' while len(hdr) < ObservePktFmt.OBS_RES_HDR_LEN: try: hdr += self.conns[server].s.recv( ObservePktFmt.OBS_RES_HDR_LEN) except KeyError as e: print "<%s> failed to recv observe pkt : %s" % ( self.__class__.__name__, e) self._add_conn(server) return None except Exception as e: print "<%s> failed to recv observe pkt: %s" % ( self.__class__.__name__, e) self._refresh_conns() return None res = ObserveResponse() if not res.unpack_hdr(hdr): if res.status == ERR_NOT_MY_VBUCKET: self._refresh_conns() return None body = '' while len(body) < res.body_len: body += self.conns[server].s.recv(res.body_len) res.unpack_body(body) # TODO: error check self.save_latency_stats(res.persist_stat / 1000) print "res::<%s>" % server print res vals = responses.get(server, []) vals.append(res) responses[server] = vals return responses
def _recv(self): responses = {} # {server: [responses]} for server in self.obs_keys.iterkeys(): hdr = '' while len(hdr) < ObservePktFmt.OBS_RES_HDR_LEN: try: hdr += self.conns[server].s.recv(ObservePktFmt.OBS_RES_HDR_LEN) except KeyError as e: print "<%s> failed to recv observe pkt : %s" % (self.__class__.__name__, e) self._add_conn(server) return None except Exception as e: print "<%s> failed to recv observe pkt: %s" % (self.__class__.__name__, e) self._refresh_conns() return None res = ObserveResponse() if not res.unpack_hdr(hdr): if res.status == ERR_NOT_MY_VBUCKET: self._refresh_conns() return None body = '' while len(body) < res.body_len: body += self.conns[server].s.recv(res.body_len) res.unpack_body(body) # TODO: error check self.save_latency_stats(res.persist_stat/1000) print "res::<%s>" % server print res vals = responses.get(server, []) vals.append(res) responses[server] = vals return responses
def observe_single(self, key, server="", timeout=0): """ send an observe command and get the response back parse the response afterwards @return (status, cas) @status -1 : network error @status -2 : protocol error @status ObserveKeyState """ cas = "" if not key: print "<%s> observe_single: invalid key" % self.__class__.__name__ return -1 vbucketid = \ VbucketHelper.get_vbucket_id(key, self.cfg.get("vbuckets", 0)) if not server: server = self._get_server_str(vbucketid) req_key = ObserveRequestKey(key, vbucketid) req = ObserveRequest([req_key]) pkt = req.pack() try: skt = self.conns[server].s except KeyError: print "<%s> observe_single: KeyError: %s" \ % (self.__class__.__name__, server) self._add_conn(server) return -1, cas try: SocketHelper.send_bytes(skt, pkt, timeout) except IOError: print "<%s> observe_single: IOError: " \ "failed to send observe pkt : %s" \ % (self.__class__.__name__, pkt) self._reconnect(self.conns[server]) self._refresh_conns() return -1, cas except socket.timeout: print "<%s> observe_single: timeout: " \ "failed to send observe pkt : %s" \ % (self.__class__.__name__, pkt) return -1, cas except Exception as e: print "<%s> observe_single: failed to send observe pkt : %s" \ % (self.__class__.__name__, e) return -1, cas try: hdr = SocketHelper.recv_bytes( skt, ObservePktFmt.OBS_RES_HDR_LEN, timeout) res = ObserveResponse() if not res.unpack_hdr(hdr): if res.status == ERR_NOT_MY_VBUCKET: self._refresh_conns() return -1, cas body = SocketHelper.recv_bytes(skt, res.body_len, timeout) res.unpack_body(body) except IOError: print "<%s> observe_single: IOError: failed to recv observe pkt" \ % self.__class__.__name__ self._reconnect(self.conns[server]) self._refresh_conns() return -1, cas except socket.timeout: print "<%s> observe_single: timeout: failed to recv observe pkt" \ % self.__class__.__name__ return -1, cas except Exception as e: print "<%s> observe_single: failed to recv observe pkt : %s" \ % (self.__class__.__name__, e) return -1, cas if not res: print "<%s> observe_single: empty response" \ % self.__class__.__name__ return -1, cas key_len = len(res.keys) if key_len != 1: # we are not supposed to receive responses for more than one key, # otherwise, it's a server side protocol error print "<%s> observe_single: invalid number of keys in response: %d"\ % (self.s.__name__, key_len) return -2, cas res_key = res.keys[0] cas = res_key.cas if res_key.key != key: print "<%s> observe_single: invalid key %s in response"\ % self.__class__.__name__ return -2, cas return res_key.key_state, cas
def observe_single(self, key, timeout=0): """ send an observe command and get the response back """ if not key: print "<%s> observe_single: invalid key" % self.__class__.__name__ return None vbucketid = VbucketHelper.get_vbucket_id(key, self.cfg.get("vbuckets", 0)) server = self._get_server_str(vbucketid) req_key = ObserveRequestKey(key, vbucketid) req = ObserveRequest([req_key]) pkt = req.pack() try: skt = self.conns[server].s except KeyError: print "<%s> KeyError: %s" % (self.__class__.__name__, server) self._add_conn(server) return None try: SocketHelper.send_bytes(skt, pkt, timeout) except IOError: print "<%s> IOError: failed to send observe pkt : %s" \ % (self.__class__.__name__, pkt) self._reconnect(self.conns[server]) self._refresh_conns() return None except socket.timeout: print "<%s> timeout: failed to send observe pkt : %s" \ % (self.__class__.__name__, pkt) return None except Exception as e: print "<%s> failed to send observe pkt : %s" \ % (self.__class__.__name__, e) return None try: hdr = SocketHelper.recv_bytes(skt, ObservePktFmt.OBS_RES_HDR_LEN, timeout) res = ObserveResponse() if not res.unpack_hdr(hdr): if res.status == ERR_NOT_MY_VBUCKET: self._refresh_conns() return None body = SocketHelper.recv_bytes(skt, res.body_len, timeout) res.unpack_body(body) except IOError: print "<%s> IOError: failed to recv observe pkt" \ % self.__class__.__name__ self._reconnect(self.conns[server]) self._refresh_conns() return None except socket.timeout: print "<%s> timeout: failed to recv observe pkt" \ % self.__class__.__name__ return None except Exception as e: print "<%s> failed to recv observe pkt : %s" \ % (self.__class__.__name__, e) return None return res