def _flush(self): """ feed all cached messages to carbon server """ if not self.skt and not self._connect(): return False buf = '' while not self.cache.empty(): try: c_msg = self.cache.get(block=False) except QEmpty: continue buf += c_msg.pack() SocketHelper.send_bytes_ex(self.skt, buf, self.timeout)
def feed(self, key, value, timestamp=0, caching=True): """ feed a single message to carbon server @param: caching - True: add to cache, False: send immediately """ if not self.skt and not self._connect(): return False c_msg = CarbonMessage(key, value, timestamp) if caching: if self.cache.full(): self._flush() try: self.cache.put(c_msg, block=False) except QFull: SocketHelper.send_bytes_ex(self.skt, c_msg.pack(), self.timeout) else: SocketHelper.send_bytes_ex(self.skt, c_msg.pack(), self.timeout)
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, 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