def _read_packet(self, packet_type=MysqlPacket): """Read an entire "mysql packet" in its entirety from the network and return a MysqlPacket type that represents the results. """ buff = b'' try: while True: packet_header = yield from self._reader.readexactly(4) btrl, btrh, packet_number = struct.unpack( '<HBB', packet_header) bytes_to_read = btrl + (btrh << 16) # Outbound and inbound packets are numbered sequentialy, so # we increment in both write_packet and read_packet. The count # is reset at new COMMAND PHASE. if packet_number != self._next_seq_id: raise InternalError( "Packet sequence number wrong - got %d expected %d" % (packet_number, self._next_seq_id)) self._next_seq_id = (self._next_seq_id + 1) % 256 recv_data = yield from self._reader.readexactly(bytes_to_read) buff += recv_data if bytes_to_read < MAX_PACKET_LEN: break except (OSError, EOFError) as exc: msg = "MySQL server has gone away (%s)" raise OperationalError(2006, msg % (exc, )) from exc packet = packet_type(buff, self._encoding) packet.check_error() return packet
def test__is_galera_returns_true_on_galera_node(mock_connect): logging.basicConfig() mock_cursor = mock.MagicMock() mock_cursor.execute.side_effect = InternalError( 1193, "Unknown system variable " "'wsrep_on'") mock_connect.return_value.__enter__.return_value. \ cursor.return_value.__enter__.return_value = mock_cursor source = MySQLSource(MySQLConnectInfo(None), 'daily', 'full') assert source.is_galera() is False
async def _read_packet(self, packet_type=MysqlPacket): """Read an entire "mysql packet" in its entirety from the network and return a MysqlPacket type that represents the results. """ buff = b'' while True: try: packet_header = await self._read_bytes(4) except asyncio.CancelledError: self._close_on_cancel() raise btrl, btrh, packet_number = struct.unpack( '<HBB', packet_header) bytes_to_read = btrl + (btrh << 16) # Outbound and inbound packets are numbered sequentialy, so # we increment in both write_packet and read_packet. The count # is reset at new COMMAND PHASE. if packet_number != self._next_seq_id: raise InternalError( "Packet sequence number wrong - got %d expected %d" % (packet_number, self._next_seq_id)) self._next_seq_id = (self._next_seq_id + 1) % 256 try: recv_data = await self._read_bytes(bytes_to_read) except asyncio.CancelledError: self._close_on_cancel() raise buff += recv_data # https://dev.mysql.com/doc/internals/en/sending-more-than-16mbyte.html if bytes_to_read == 0xffffff: continue if bytes_to_read < MAX_PACKET_LEN: break packet = packet_type(buff, self._encoding) packet.check_error() return packet