I'm new in kafka. Now, i'm trying to commit kafka message explicitly like if my all mysql operation successful then i'll commit the record but kafka reads next offset if the previous offset has not been committed yet.
Codes are following:
`$conf = new \RdKafka\Conf();
$conf->set('group.id', $groupId);
$conf->set('metadata.broker.list', $brokers);
$conf->set('auto.offset.reset', 'earliest');
$conf->set('enable.auto.commit', 'true');
$conf->set('enable.auto.offset.store', 'false');
$conf->set('auto.commit.interval.ms', 100);
$conf->set('enable.partition.eof', 'true');
$consumer = new \RdKafka\KafkaConsumer($conf);
$consumer->subscribe($subscriptionArr);
$this->logger->info("Waiting for partition assignment... (make take some time when quickly re-joining the group after leaving it.)", [$groupId, $brokers, $subscriptionArr]);
$tmp=0;
//let add some counters so at least we have some numbers of topics we processed...
$search_array=[];
while ($active) {
$message = $consumer->consume(120*1000);
if ($this->debug) {
$active = false;
}
switch ($message->err) {
case RD_KAFKA_RESP_ERR_NO_ERROR:
$topic_name = $message->topic_name;
$timestamp = $message->timestamp;
$payload = $message->payload;
$message_offset = $message->offset;
$partition = $message->partition;
$timeoutMs = 10000000;
$this->logger->info("topic partition details ", [$partition]);
$topic = $consumer->newTopic($topic_name);
$topicPartition = new \RdKafka\TopicPartition($topic_name, $partition);
$this->logger->info("topic partitions",[$topicPartition]);
$partition_id = $topicPartition->getPartition();
$this->logger->info("topic partitions",[$partition_id]);
$topicPartitionsWithOffsets = $consumer->getCommittedOffsets([$topicPartition], $timeoutMs);
$queryExecutionStatus = $this->sinkConnector->injectKafka($topic_name, $payload, $timestamp);
$this->logger->info($topic_name." query execution status",[$queryExecutionStatus]);
$this->logger->info("before commited offset ", [$topicPartitionsWithOffsets]);
if ($queryExecutionStatus == 1) {
if (array_key_exists($topic_name, $search_array) ) {
$search_array[$topic_name] = $search_array[$topic_name] + 1;
} else {
// set initial value of the counter
$search_array[$topic_name]=1;
}
$this->logger->info("manually commited offset of topic ".$topic_name, [$message_offset]);
$topic->offsetStore($message->partition, $message_offset);
$storeOffset = $topicPartition->getOffset();
$this->logger->info("commited offset ", [$storeOffset]);
} else {
$this->logger->warning("Warning: Message not inserted/updated ".$topic_name, [$message_offset]);
}
break;
case RD_KAFKA_RESP_ERR__PARTITION_EOF:
$this->logger->notice("No more messages on partition; will wait for more", [$search_array]);
$tmp++;
$search_array=[];
break;
case RD_KAFKA_RESP_ERR__TIMED_OUT:
//on-timeouts.we.can.rest.the.cpu.of.the.machine.
$this->logger->notice("Timed out", [$tmp]);
$active = false;
break;
default:
$this->logger->warning("Warning: Kafka-Exception", [message=>$message->errstr(), error=> $message->err, count=> $tmp]);
//throw new \Exception($message->errstr(), $message->err);
$active = false;
break;
}
}
// flush and close the kafka-client and make sure it does not leave any open files...
$this->logger->notice("Disconnecting from the nodes and go to sleep mode");
//$consumer->commit(); //we dont have a no local offset stored...;) this forcefully lowers: ls /proc/$pid/fd/ | wc -l
$consumer->unsubscribe();
$consumer->close();
//force-closure.to.release.fopens
unset($consumer);
$consumer = null;`
I tried commit() of php rdkafka to commit it synchronously. Also tried to change value of auto.commit.interval.ms configuration