Example #1
0
    def __init__(self,
                 *,
                 loop,
                 bootstrap_servers='localhost',
                 client_id='aiokafka-' + __version__,
                 metadata_max_age_ms=300000,
                 request_timeout_ms=40000,
                 retry_backoff_ms=100,
                 ssl_context=None,
                 security_protocol='PLAINTEXT',
                 api_version='auto',
                 connections_max_idle_ms=540000,
                 sasl_mechanism='PLAIN',
                 sasl_plain_username=None,
                 sasl_plain_password=None,
                 sasl_kerberos_service_name='kafka',
                 sasl_kerberos_domain_name=None):
        if security_protocol not in ('SSL', 'PLAINTEXT', 'SASL_PLAINTEXT',
                                     'SASL_SSL'):
            raise ValueError("`security_protocol` should be SSL or PLAINTEXT")
        if security_protocol in ["SSL", "SASL_SSL"] and ssl_context is None:
            raise ValueError(
                "`ssl_context` is mandatory if security_protocol=='SSL'")
        if security_protocol in ["SASL_SSL", "SASL_PLAINTEXT"]:
            if sasl_mechanism not in ("PLAIN", "GSSAPI"):
                raise ValueError("only `PLAIN` and `GSSAPI` sasl_mechanism "
                                 "are supported at the moment")
            if sasl_mechanism == "PLAIN" and \
               (sasl_plain_username is None or sasl_plain_password is None):
                raise ValueError(
                    "sasl_plain_username and sasl_plain_password required for "
                    "PLAIN sasl")

        self._bootstrap_servers = bootstrap_servers
        self._client_id = client_id
        self._metadata_max_age_ms = metadata_max_age_ms
        self._request_timeout_ms = request_timeout_ms
        if api_version != "auto":
            api_version = parse_kafka_version(api_version)
        self._api_version = api_version
        self._security_protocol = security_protocol
        self._ssl_context = ssl_context
        self._retry_backoff = retry_backoff_ms / 1000
        self._connections_max_idle_ms = connections_max_idle_ms
        self._sasl_mechanism = sasl_mechanism
        self._sasl_plain_username = sasl_plain_username
        self._sasl_plain_password = sasl_plain_password
        self._sasl_kerberos_service_name = sasl_kerberos_service_name
        self._sasl_kerberos_domain_name = sasl_kerberos_domain_name

        self.cluster = ClusterMetadata(metadata_max_age_ms=metadata_max_age_ms)

        self._topics = set()  # empty set will fetch all topic metadata
        self._conns = {}
        self._loop = loop
        self._sync_task = None

        self._md_update_fut = None
        self._md_update_waiter = create_future(loop=self._loop)
        self._get_conn_lock = asyncio.Lock(loop=loop)
Example #2
0
 async def fetch_all_metadata(self):
     cluster_md = ClusterMetadata(
         metadata_max_age_ms=self._metadata_max_age_ms)
     updated = await self._metadata_update(cluster_md, [])
     if not updated:
         raise KafkaError(
             'Unable to get cluster metadata over all known brokers')
     return cluster_md
Example #3
0
async def check_if_kafka_is_alive(my_client: AIOKafkaClient):
    while 1:
        conns: Dict[str,
                    AIOKafkaConnection] = my_client.__getattribute__('_conns')

        print(my_client._bootstrap_servers)

        print('Host = ', my_client.hosts)
        if not is_connected(conns):
            print('RENEW CONNECTION')
            try:
                my_client.__setattr__(
                    'cluster', ClusterMetadata(metadata_max_age_ms=300000))
                my_client.__setattr__('_topics', set())
                my_client.__setattr__('_conns', {})
                my_client.__setattr__('_sync_task', None)

                loop = asyncio.get_event_loop()
                my_client.__setattr__('_md_update_fut', None)
                my_client.__setattr__('_md_update_waiter',
                                      loop.create_future())
                my_client.__setattr__('_get_conn_lock',
                                      asyncio.Lock(loop=loop))

                await my_client.bootstrap()
            except ConnectionError:
                pass
        else:
            for conn in conns.items():
                print(conn)
                print(conn[1].connected())
                try:
                    if not conn[1].connected():
                        print('TRY RE CONNECTION')
                        await conn[1].connect()
                        if not conn[1].connected():
                            print('RENEW CONNECTION')
                            await my_client.bootstrap()
                except Exception as err:
                    print(err)
        await asyncio.sleep(1)
Example #4
0
    def __init__(self,
                 *,
                 loop,
                 bootstrap_servers='localhost',
                 client_id='aiokafka-' + __version__,
                 metadata_max_age_ms=300000,
                 request_timeout_ms=40000,
                 retry_backoff_ms=100,
                 ssl_context=None,
                 security_protocol='PLAINTEXT',
                 api_version='auto',
                 connections_max_idle_ms=540000):
        if security_protocol not in ('SSL', 'PLAINTEXT'):
            raise ValueError("`security_protocol` should be SSL or PLAINTEXT")
        if security_protocol == "SSL" and ssl_context is None:
            raise ValueError(
                "`ssl_context` is mandatory if security_protocol=='SSL'")

        self._bootstrap_servers = bootstrap_servers
        self._client_id = client_id
        self._metadata_max_age_ms = metadata_max_age_ms
        self._request_timeout_ms = request_timeout_ms
        if api_version != "auto":
            api_version = parse_kafka_version(api_version)
        self._api_version = api_version
        self._security_protocol = security_protocol
        self._ssl_context = ssl_context
        self._retry_backoff = retry_backoff_ms / 1000
        self._connections_max_idle_ms = connections_max_idle_ms

        self.cluster = ClusterMetadata(metadata_max_age_ms=metadata_max_age_ms)

        self._topics = set()  # empty set will fetch all topic metadata
        self._conns = {}
        self._loop = loop
        self._sync_task = None

        self._md_update_fut = None
        self._md_update_waiter = create_future(loop=self._loop)
        self._get_conn_lock = asyncio.Lock(loop=loop)