def _send_request_wo_reconnect(self, request, space_name=None, field_defs=None, default_type=None): '''\ :rtype: `Response` instance :raise: NetworkError ''' assert isinstance(request, Request) # Repeat request in a loop if the server returns completion_status == 1 (try again) for attempt in xrange(RETRY_MAX_ATTEMPTS): # pylint: disable=W0612 try: self._socket.sendall(bytes(request)) header, body = self._read_response() response = Response(self, header, body, space_name, field_defs, default_type) except socket.error as e: raise NetworkError(e) if response.completion_status != 1: return response warn(response.return_message, RetryWarning) # Raise an error if the maximum number of attempts have been made raise DatabaseError(response.return_code, response.return_message)
def join(self, server_uuid): request = RequestJoin(self, server_uuid) resp = self._send_request(request) while True: yield resp if resp.code == REQUEST_TYPE_OK or resp.code >= REQUEST_TYPE_ERROR: return resp = Response(self, self._read_response()) self.close() # close connection after JOIN
def subscribe(self, cluster_uuid, server_uuid, vclock=None): vclock = vclock or {} request = RequestSubscribe(self, cluster_uuid, server_uuid, vclock) self._socket.sendall(bytes(request)) while True: resp = Response(self, self._read_response()) yield resp if resp.code >= REQUEST_TYPE_ERROR: return self.close() # close connection after SUBSCRIBE
def _join_v16(self, server_uuid): request = RequestJoin(self, server_uuid) self._socket.sendall(bytes(request)) while True: resp = Response(self, self._read_response()) yield resp if resp.code == REQUEST_TYPE_OK or resp.code >= REQUEST_TYPE_ERROR: return self.close() # close connection after JOIN
def subscribe(self, cluster_uuid, server_uuid, vclock={}): # FIXME rudnyh: ^ 'vclock={}'? really? sure? request = RequestSubscribe(self, cluster_uuid, server_uuid, vclock) resp = self._send_request(request) while True: yield resp if resp.code >= REQUEST_TYPE_ERROR: return resp = Response(self, self._read_response()) self.close() # close connection after SUBSCRIBE
def _response_reader(self): # handshake greeting = yield from self._reader.read(IPROTO_GREETING_SIZE) self._salt = base64.decodestring(greeting[64:])[:20] self._greeting_event.set() buf = b"" while not self._reader.at_eof(): tmp_buf = yield from self._reader.read(self.aiobuffer_size) if not tmp_buf: yield from self._do_close( NetworkError(socket.error(errno.ECONNRESET, "Lost connection to server during query"))) buf += tmp_buf len_buf = len(buf) curr = 0 while len_buf - curr >= 5: length_pack = buf[curr:curr + 5] length = msgpack.unpackb(length_pack) if len_buf - curr < 5 + length: break body = buf[curr + 5:curr + 5 + length] curr += 5 + length response = Response(self, body) # unpack response sync = response.sync if sync not in self._waiters: logger.error("aio git happens: {r}", response) continue waiter = self._waiters[sync] if not waiter.cancelled(): if response.return_code != 0: waiter.set_exception(DatabaseError(response.return_code, response.return_message)) else: waiter.set_result(response) del self._waiters[sync] # one cut for buffer if curr: buf = buf[curr:] yield from self._do_close(None)
def _join_v17(self, server_uuid): class JoinState: Handshake, Initial, Final, Done = range(4) request = RequestJoin(self, server_uuid) self._socket.sendall(bytes(request)) state = JoinState.Handshake while True: resp = Response(self, self._read_response()) yield resp if resp.code >= REQUEST_TYPE_ERROR: return elif resp.code == REQUEST_TYPE_OK: state = state + 1 if state == JoinState.Done: return
def _send_request_wo_reconnect(self, request): ''' :rtype: `Response` instance :raise: NetworkError ''' assert isinstance(request, Request) response = None while True: try: self._socket.sendall(bytes(request)) response = Response(self, self._read_response()) break except SchemaReloadException as e: self.update_schema(e.schema_version) continue return response
def _send_request_wo_reconnect(self, request): ''' :rtype: `Response` instance :raise: NetworkError ''' assert isinstance(request, Request) # Repeat request in a loop if the server returns completion_status == 1 # (try again) for attempt in xrange(RETRY_MAX_ATTEMPTS): # pylint: disable=W0612 self._socket.sendall(bytes(request)) response = Response(self, self._read_response()) if response.completion_status != 1: return response warn(response.return_message, RetryWarning) # Raise an error if the maximum number of attempts have been made raise DatabaseError(response.return_code, response.return_message)
# gh-434 Tarantool crashes on multiple iproto requests with WAL enabled admin("box.cfg.wal_mode") admin("space = box.schema.space.create('test', { id = 567 })") admin("index = space:create_index('primary', { type = 'hash' })") admin("box.schema.user.grant('guest', 'read,write,execute', 'space', 'test')") c = Connection('localhost', server.iproto.port) c.connect() request1 = RequestInsert(c, 567, [1, "baobab"]) request2 = RequestInsert(c, 567, [2, "obbaba"]) s = c._socket try: s.send(bytes(request1) + bytes(request2)) except OSError as e: print ' => ', 'Failed to send request' response1 = Response(c, c._read_response()) response2 = Response(c, c._read_response()) print response1.__str__() print response2.__str__() request1 = RequestInsert(c, 567, [3, "occama"]) request2 = RequestSelect(c, 567, 0, [1], 0, 1, 0) s = c._socket try: s.send(bytes(request1) + bytes(request2)) except OSError as e: print ' => ', 'Failed to send request' response1 = Response(c, c._read_response()) response2 = Response(c, c._read_response()) print response1.__str__() print response2.__str__()
async def _response_reader(self): # handshake greeting = await self._reader.read(IPROTO_GREETING_SIZE) self._salt = base64.decodestring(greeting[64:])[:20] self._greeting_event.set() buf = b"" while not self._reader.at_eof(): tmp_buf = await self._reader.read(self.aiobuffer_size) if not tmp_buf: await self._do_close( NetworkError( socket.error(errno.ECONNRESET, "Lost connection to server during query")) ) buf += tmp_buf len_buf = len(buf) curr = 0 while len_buf - curr >= 5: length_pack = buf[curr:curr + 5] length = msgpack.unpackb(length_pack) if len_buf - curr < 5 + length: break body = buf[curr + 5:curr + 5 + length] curr += 5 + length try: response = Response(self, body) # unpack response except SchemaReloadException as exp: if self.encoding is not None: unpacker = msgpack.Unpacker(use_list=True, encoding=self.encoding) else: unpacker = msgpack.Unpacker(use_list=True) unpacker.feed(body) header = unpacker.unpack() sync = header.get(IPROTO_SYNC, 0) waiter = self._waiters[sync] if not waiter.cancelled(): waiter.set_exception(exp) del self._waiters[sync] self.schema.flush() self.schema_version = exp.schema_version continue sync = response.sync if sync not in self._waiters: logger.error("aio git happens: {r}", response) continue waiter = self._waiters[sync] if not waiter.cancelled(): if response.return_code != 0: waiter.set_exception( DatabaseError(response.return_code, response.return_message)) else: waiter.set_result(response) del self._waiters[sync] # one cut for buffer if curr: buf = buf[curr:] await self._do_close(None)