confluent-kafka-python icon indicating copy to clipboard operation
confluent-kafka-python copied to clipboard

When connected to two down brokers, error_cb only trigger KafkaError._TRANSPORT

Open zjyExcelsior opened this issue 9 years ago • 2 comments

When there is only one broker, error_cb will trigger KafkaError._TRANSPORT and KafkaError._ALL_BROKERS_DOWN:

from confluent_kafka import Producer, KafkaError, KafkaException

def error_cb(err):
    print 'error_cb -------->', err
    if err.code() == KafkaError._ALL_BROKERS_DOWN:
        print 'all brokers down...'

KAFKA_BROKERS = '192.168.7.158:9092' # try to connected to one broker
KAFKA_TOPIC = 'hermes-alarm-messages'
KAFKA_PRODUCER_CONF = {
    'api.version.request': False,  # default, not need to set (yet)
    'broker.version.fallback': '0.8.2.2',
    'bootstrap.servers': KAFKA_BROKERS,
    'client.id': 'message_service',
    'default.topic.config': {'acks': 'all'},
    'error_cb': error_cb,

}
KAFKA_PRODUCER = Producer(**KAFKA_PRODUCER_CONF)
print '======>'
KAFKA_PRODUCER.poll(1)

on the console, we will see:

======>
%3|1491406670.811|FAIL|message_service#producer-1| [thrd:192.168.7.158:9092/bootstrap]: 192.168.7.158:9092/bootstrap: Connect to ipv4#192.168.7.158:9092 failed: Connection refused
error_cb --------> KafkaError{code=_TRANSPORT,val=-195,str="192.168.7.158:9092/bootstrap: Connect to ipv4#192.168.7.158:9092 failed: Connection refused"}
error_cb --------> KafkaError{code=_ALL_BROKERS_DOWN,val=-187,str="1/1 brokers are down"}
all brokers down...

However, when I connected to two down brokers, only trigger KafkaError._TRANSPORT:

from confluent_kafka import Producer, KafkaError, KafkaException

def error_cb(err):
    print 'error_cb -------->', err
    if err.code() == KafkaError._ALL_BROKERS_DOWN:
        print 'all brokers down...'

KAFKA_BROKERS = '192.168.7.158:9092,192.168.7.158:9093' # try to connected to two brokers
KAFKA_TOPIC = 'hermes-alarm-messages'
KAFKA_PRODUCER_CONF = {
    'api.version.request': False,  # default, not need to set (yet)
    'broker.version.fallback': '0.8.2.2',
    'bootstrap.servers': KAFKA_BROKERS,
    'client.id': 'message_service',
    'default.topic.config': {'acks': 'all'},
    'error_cb': error_cb,

}
KAFKA_PRODUCER = Producer(**KAFKA_PRODUCER_CONF)
print '======>'
KAFKA_PRODUCER.poll(1)

on the console, we will see:

======>
%3|1491406868.014|FAIL|message_service#producer-1| [thrd:192.168.7.158:9093/bootstrap]: 192.168.7.158:9093/bootstrap: Connect to ipv4#192.168.7.158:9093 failed: Connection refused
error_cb --------> KafkaError{code=_TRANSPORT,val=-195,str="192.168.7.158:9093/bootstrap: Connect to ipv4#192.168.7.158:9093 failed: Connection refused"}

When I sleep 2 seconds before producer.poll(), I found it works well, What's the matter?

from confluent_kafka import Producer, KafkaError, KafkaException

def error_cb(err):
    print 'error_cb -------->', err
    if err.code() == KafkaError._ALL_BROKERS_DOWN:
        print 'all brokers down...'

KAFKA_BROKERS = '192.168.7.158:9092,192.168.7.158:9093'
KAFKA_TOPIC = 'hermes-alarm-messages'
KAFKA_PRODUCER_CONF = {
    'api.version.request': False,  # default, not need to set (yet)
    'broker.version.fallback': '0.8.2.2',
    'bootstrap.servers': KAFKA_BROKERS,
    'client.id': 'message_service',
    'default.topic.config': {'acks': 'all'},
    'error_cb': error_cb
}
KAFKA_PRODUCER = Producer(**KAFKA_PRODUCER_CONF)
# print dir(KAFKA_PRODUCER)
# print '======>'
import time
time.sleep(2)
KAFKA_PRODUCER.poll(1)

On the console, we will see:

%3|1491410538.414|FAIL|message_service#producer-1| [thrd:192.168.7.158:9092/bootstrap]: 192.168.7.158:9092/bootstrap: Connect to ipv4#192.168.7.158:9092 failed: Connection refused
%3|1491410538.420|FAIL|message_service#producer-1| [thrd:192.168.7.158:9093/bootstrap]: 192.168.7.158:9093/bootstrap: Connect to ipv4#192.168.7.158:9093 failed: Connection refused
error_cb --------> KafkaError{code=_TRANSPORT,val=-195,str="192.168.7.158:9092/bootstrap: Connect to ipv4#192.168.7.158:9092 failed: Connection refused"}
error_cb --------> KafkaError{code=_TRANSPORT,val=-195,str="192.168.7.158:9093/bootstrap: Connect to ipv4#192.168.7.158:9093 failed: Connection refused"}
error_cb --------> KafkaError{code=_ALL_BROKERS_DOWN,val=-187,str="2/2 brokers are down"}
all brokers down...

zjyExcelsior avatar Apr 05 '17 15:04 zjyExcelsior

@edenhill

zjyExcelsior avatar Apr 05 '17 15:04 zjyExcelsior

Logs are printed immediately (from background threads if needed), while the error_cb is only triggered in the application thread when you call poll(). poll() will return as soon as it has handled all the queued events.

So in your case, without the sleep, poll() will finish before the ALL_BROKERS_DOWN event is triggered. By adding the sleep the event is triggered before calling poll() and thus served on the next poll call(). Note that the poll() timeout is the maximum time it will wait for any event, not the minimum time.

edenhill avatar May 02 '17 17:05 edenhill