Exemplo n.º 1
0
    async def log_kafka_sample_async(self, topic: str, sample: dict):
        """logs sample to Kafka topic asynchronously
        Sample for monitoring purpose:
        Supports logging samples to Kafka via REST API (Confluent)

        Column guidelines:
        time: epoch in seconds
        sample_rate: pre-sampled record shall set this to sample rate, e.g., 100 means one sample is logged out of 100
        column type shall be log int, str, or vector of str
        """
        if self.cluster_config.MONITORING.KAFKA_REST_ADDRESS == "":
            return
        url = "http://{}/topics/{}".format(
            self.cluster_config.MONITORING.KAFKA_REST_ADDRESS, topic)
        try:
            record_data = json.dumps({"records": [{"value": sample}]})
            headers = {
                "Content-Type": "application/vnd.kafka.json.v2+json",
                "Accept": "application/vnd.kafka.v2+json",
            }
            session = aiohttp.ClientSession()
            response = await session.post(url,
                                          data=record_data,
                                          headers=headers)
            if response.status != 200:
                raise Exception("non-OK response status code: {}".format(
                    response.status_code))
        except Exception as ex:
            Logger.error_every_n(
                "Failed to log sample to Kafka: {}".format(ex), 100)
        finally:
            await session.close()
Exemplo n.º 2
0
 async def receive_handshake(
     self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
 ) -> None:
     ip, socket, *_ = writer.get_extra_info("peername")
     remote_address = Address(ip, socket)
     if self.peer_pool.chk_dialin_blacklist(remote_address):
         Logger.info_every_n(
             "{} has been blacklisted, refusing connection".format(remote_address),
             100,
         )
         reader.feed_eof()
         writer.close()
     expected_exceptions = (
         TimeoutError,
         PeerConnectionLost,
         HandshakeFailure,
         asyncio.IncompleteReadError,
         HandshakeDisconnectedFailure,
     )
     try:
         await self._receive_handshake(reader, writer)
     except expected_exceptions as e:
         self.logger.debug("Could not complete handshake: %s", e)
         Logger.error_every_n("Could not complete handshake: {}".format(e), 100)
         reader.feed_eof()
         writer.close()
     except OperationCancelled:
         self.logger.error("OperationCancelled")
         reader.feed_eof()
         writer.close()
     except Exception as e:
         self.logger.exception("Unexpected error handling handshake")
         reader.feed_eof()
         writer.close()
Exemplo n.º 3
0
    async def connect(self, remote: Node) -> BasePeer:
        """
        Connect to the given remote and return a Peer instance when successful.
        Returns None if the remote is unreachable, times out or is useless.
        """
        if remote.pubkey == self.privkey.public_key:
            Logger.warning_every_n(
                "Skipping {} that has the same public key as local node, quite possible we are trying to connect to ourselves"
                .format(remote),
                100,
            )
            return None
        if remote in self.connected_nodes:
            self.logger.debug("Skipping %s; already connected to it", remote)
            return None
        if self.chk_dialout_blacklist(remote.address):
            Logger.warning_every_n(
                "failed to connect {} at least once, will not connect again; discovery should have removed it"
                .format(remote.address), 100)
            return None
        blacklistworthy_exceptions = (
            HandshakeFailure,  # after secure handshake handshake, when negotiating p2p command, eg. parsing hello failed; no matching p2p capabilities
            PeerConnectionLost,  # conn lost while reading
            TimeoutError,  # eg. read timeout (raised by CancelToken)
            UnreachablePeer,  # ConnectionRefusedError, OSError
        )
        expected_exceptions = (
            HandshakeDisconnectedFailure,  # during secure handshake, disconnected before getting ack; or got Disconnect cmd for some known reason
        )
        try:
            self.logger.debug("Connecting to %s...", remote)
            # We use self.wait() as well as passing our CancelToken to handshake() as a workaround
            # for https://github.com/ethereum/py-evm/issues/670.
            peer = await self.wait(handshake(remote, self.get_peer_factory()))

            return peer
        except OperationCancelled:
            # Pass it on to instruct our main loop to stop.
            raise
        except BadAckMessage:
            # This is kept separate from the `expected_exceptions` to be sure that we aren't
            # silencing an error in our authentication code.
            Logger.error_every_n("Got bad auth ack from {}".format(remote),
                                 100)
            # dump the full stacktrace in the debug logs
            self.logger.debug("Got bad auth ack from %r",
                              remote,
                              exc_info=True)
            self.dialout_blacklist(remote.address)
        except MalformedMessage:
            # This is kept separate from the `expected_exceptions` to be sure that we aren't
            # silencing an error in how we decode messages during handshake.
            Logger.error_every_n(
                "Got malformed response from {} during handshake".format(
                    remote), 100)
            # dump the full stacktrace in the debug logs
            self.logger.debug("Got malformed response from %r",
                              remote,
                              exc_info=True)
            self.dialout_blacklist(remote.address)
        except blacklistworthy_exceptions as e:
            self.logger.debug("Could not complete handshake with %r: %s",
                              remote, repr(e))
            Logger.error_every_n(
                "Could not complete handshake with {}: {}".format(
                    repr(remote), repr(e)), 100)
            self.dialout_blacklist(remote.address)
        except expected_exceptions as e:
            self.logger.debug("Disconnected during handshake %r: %s", remote,
                              repr(e))
            Logger.error_every_n(
                "Disconnected during handshake {}: {}".format(
                    repr(remote), repr(e)), 100)
        except Exception:
            self.logger.exception(
                "Unexpected error during auth/p2p handshake with %r", remote)
            self.dialout_blacklist(remote.address)
        if remote.__repr__() in auth.opened_connections:
            reader, writer = auth.opened_connections[remote.__repr__()]
            reader.feed_eof()
            writer.close()
            Logger.error_every_n(
                "Closing connection to {}".format(remote.__repr__()), 100)
            del auth.opened_connections[remote.__repr__()]
        return None