async def start_handshake(self): try: logger.info("client %s:%s start handshake.", *self.address) self.status = SessionStatus.HANDSHAKEING auth = Handshake(self.stream) handshake_result = await auth.start() if handshake_result: self.status = SessionStatus.HANDSHAKED self.start() else: await self.close() except Exception as e: logger.warning("error with %s", str(e)) await self.close()
async def _ending(self, xid: int, action: A2PCAction) -> A2PCResponse: body = json.dumps({"action": action, "xid": xid}) n = (xid % self.mod) if self.mod > 0 else 0 server = self.services[n] url = "http://{}:{}/transactions".format(server[0], server[1]) req = HTTPRequest(url, "PUT", self.header, body) client = AsyncHTTPClient() try: logger.info("put: {}, param: {}".format(url, body)) response = await client.fetch(req) logger.info("response: {}".format(response.body)) return self._parse_result(response) except Exception as e: logger.warning("Error: %s" % e) raise e
async def write(rs: List[result.Result], stream: Stream, packet_number: int): logger.info(rs) out = None for r in rs: if isinstance(r, result.ResultSet): out = ResultSet.new_from_result(r) return await out.encode(0, 32, stream, packet_number) elif isinstance(r, result.Error): out = Error.new_from_result(r).encode() header = PacketHeader.new(len(out), packet_number).encode() return await stream.write(header + out) elif isinstance(r, result.OK): out = OK.new_from_result(r).encode() header = PacketHeader.new(len(out), packet_number).encode() return await stream.write(header + out) else: raise Exception("unkonwn packet type.")
async def start_serving(self): try: i = 0 while True: packet = await mysql.PacketBytesReader.read_execute_packet( self.stream) execute = Execute(packet.length, Command(packet.command), packet.args, packet.query) r = await self.dsession.execute(execute) i += 1 num = int(i < 7) if i > 6: num = 0 logger.info(r[0]) if r is not None: await mysql.ResultWriter.write(r, self.stream, num) except torio.StreamClosedError: logger.warning("client has close with.") await self.close() except Exception as e: traceback.print_exc() logger.warning("error with %s", str(e))
async def acquire_lock(self, xid: int, node: str, table: str, lock_key: Dict[str, Any], sql: str) -> A2PCResponse: body = json.dumps({ "action": A2PCAction.ACQUIRE_LOCK, "xid": xid, "node": node, "table": table, "lock_key": lock_key, "context": sql }) n = (xid % self.mod) if self.mod > 0 else 0 server = self.services[n] url = "http://{}:{}/transactions".format(server[0], server[1]) req = HTTPRequest(url, "PUT", self.header, body) client = AsyncHTTPClient() try: logger.info("put: {}, param: {}".format(url, body)) response = await client.fetch(req) logger.info("response: {}".format(response.body)) return self._parse_result(response) except Exception as e: logger.warning("Error: %s" % e) raise e
def start(self): self.init_config() tcpserver = ProxyTCPServer() tcpserver.listen(port=self.conf.port, address=self.conf.host) logger.info("start listen %s:%s", self.conf.host, self.conf.port) IOLoop.current().start()
async def handle_stream(self, stream: IOStream, address: Tuple[str, int]): logger.info("get connection from %s:%s", *address) conn = Connection(stream, address, self) self.conns.add(conn) conn.start()
def start(self): self.bootstrap() self.listen(port=self._conf.port, address=self._conf.host) logger.info("start listen %s:%s", self._conf.host, self._conf.port) IOLoop.current().start()
async def handle_stream(self, stream: IOStream, address: Tuple[str, int]): logger.info("get connection from %s:%s", *address) sess = Session(stream, address, self, self.dserver) self.sessions.add(sess) sess.start()