I'm trying to send events to Kafka in batches (let's say 10 events in a batch) to maintain transactional guarantees. But I'm able to successfully send only the first batch of events to Kafka. All subsequent batches fail with the reason KafkaError{code=_STATE,val=-172,str="Operation not valid in state Ready"}
Here is the piece of code I'm using to use transactions in Kafka.
DEFAULT_PRODUCER_CONFIG = {
"bootstrap.servers": BOOTSTRAP_SERVERS,
"schema.registry.url": SCHEMA_REGISTRY_URL,
"enable.idempotence": True,
"retries": 3,
"acks": "all",
"security.protocol": "SSL",
"transactional.id": uuid.uuid4().hex
}
producer = AvroProducer(
DEFAULT_PRODUCER_CONFIG, default_value_schema=avro.loads(value_schema)
)
send_in_transaction method is used to send events in a batches.
def send_in_transaction(producer, topic, events):
producer.init_transactions()
producer.begin_transaction()
try:
for event in events:
producer.produce(topic=topic, key=None, value=event)
producer.commit_transaction(60)
except AvroTypeException as ex:
producer.abort_transaction()
logger.error("Avro serialization error: " + str(ex))
raise KafkaException("Error sending events to kafka")
except Exception as ex:
producer.abort_transaction()
logger.error("Error sending events to kafka: " + str(ex))
raise KafkaException("Error sending events to kafka")
Even though, I don't use transactions, I'm still not able to push subsequent batches of events.
Soultions Tried -
- I tried creating kafka producer for every transaction with uuid as producer's transactional.id and it works fine. But this doesn't seem to be a good approach.
*Note - Here, I'm using confluent_kafka==1.7.0 and avro-python3==1.10.0