Added commit for current assignment and message state checks (#53)

This commit is contained in:
Alex Damian
2018-04-20 18:51:44 -04:00
committed by Matias Fontanini
parent c95d790547
commit 83a963c1db
8 changed files with 62 additions and 30 deletions

View File

@@ -64,14 +64,14 @@ class TopicConfiguration;
* Consumer consumer(config);
*
* // Set the assignment callback
* consumer.set_assignment_callback([&](vector<TopicPartition>& topic_partitions) {
* consumer.set_assignment_callback([&](TopicPartitionList& topic_partitions) {
* // Here you could fetch offsets and do something, altering the offsets on the
* // topic_partitions vector if needed
* cout << "Got assigned " << topic_partitions.size() << " partitions!" << endl;
* });
*
* // Set the revocation callback
* consumer.set_revocation_callback([&](const vector<TopicPartition>& topic_partitions) {
* consumer.set_revocation_callback([&](const TopicPartitionList& topic_partitions) {
* cout << topic_partitions.size() << " partitions revoked!" << endl;
* });
*
@@ -126,7 +126,7 @@ public:
* \brief Sets the topic/partition assignment callback
*
* The Consumer class will use rd_kafka_conf_set_rebalance_cb and will handle the
* rebalance, converting from rdkafka topic partition list handles into vector<TopicPartition>
* rebalance, converting from rdkafka topic partition list handles into TopicPartitionList
* and executing the assignment/revocation/rebalance_error callbacks.
*
* \note You *do not need* to call Consumer::assign with the provided topic parttitions. This
@@ -140,7 +140,7 @@ public:
* \brief Sets the topic/partition revocation callback
*
* The Consumer class will use rd_kafka_conf_set_rebalance_cb and will handle the
* rebalance, converting from rdkafka topic partition list handles into vector<TopicPartition>
* rebalance, converting from rdkafka topic partition list handles into TopicPartitionList
* and executing the assignment/revocation/rebalance_error callbacks.
*
* \note You *do not need* to call Consumer::assign with an empty topic partition list or
@@ -155,7 +155,7 @@ public:
* \brief Sets the rebalance error callback
*
* The Consumer class will use rd_kafka_conf_set_rebalance_cb and will handle the
* rebalance, converting from rdkafka topic partition list handles into vector<TopicPartition>
* rebalance, converting from rdkafka topic partition list handles into TopicPartitionList
* and executing the assignment/revocation/rebalance_error callbacks.
*
* \param callback The rebalance error callback
@@ -192,6 +192,24 @@ public:
* parameter
*/
void unassign();
/**
* \brief Commits the current partition assignment
*
* This translates into a call to rd_kafka_commit with a null partition list.
*
* \remark This function is equivalent to calling commit(get_assignment())
*/
void commit();
/**
* \brief Commits the current partition assignment asynchronously
*
* This translates into a call to rd_kafka_commit with a null partition list.
*
* \remark This function is equivalent to calling async_commit(get_assignment())
*/
void async_commit();
/**
* \brief Commits the given message synchronously
@@ -349,7 +367,7 @@ private:
void close();
void commit(const Message& msg, bool async);
void commit(const TopicPartitionList& topic_partitions, bool async);
void commit(const TopicPartitionList* topic_partitions, bool async);
void handle_rebalance(rd_kafka_resp_err_t err, TopicPartitionList& topic_partitions);
AssignmentCallback assignment_callback_;

View File

@@ -33,6 +33,7 @@
#include <memory>
#include <cstdint>
#include <chrono>
#include <cassert>
#include <boost/optional.hpp>
#include <librdkafka/rdkafka.h>
#include "buffer.h"
@@ -83,6 +84,7 @@ public:
* Gets the error attribute
*/
Error get_error() const {
assert(handle_);
return handle_->err;
}
@@ -97,6 +99,7 @@ public:
* Gets the topic that this message belongs to
*/
std::string get_topic() const {
assert(handle_);
return rd_kafka_topic_name(handle_->rkt);
}
@@ -104,6 +107,7 @@ public:
* Gets the partition that this message belongs to
*/
int get_partition() const {
assert(handle_);
return handle_->partition;
}
@@ -125,6 +129,7 @@ public:
* Gets the message offset
*/
int64_t get_offset() const {
assert(handle_);
return handle_->offset;
}
@@ -135,6 +140,7 @@ public:
* attribute
*/
void* get_user_data() const {
assert(handle_);
return handle_->_private;
}

View File

@@ -49,9 +49,9 @@ using TopicPartitionsListPtr = std::unique_ptr<rd_kafka_topic_partition_list_t,
using TopicPartitionList = std::vector<TopicPartition>;
// Conversions between rdkafka handles and TopicPartitionList
CPPKAFKA_API TopicPartitionsListPtr convert(const std::vector<TopicPartition>& topic_partitions);
CPPKAFKA_API std::vector<TopicPartition> convert(const TopicPartitionsListPtr& topic_partitions);
CPPKAFKA_API std::vector<TopicPartition> convert(rd_kafka_topic_partition_list_t* topic_partitions);
CPPKAFKA_API TopicPartitionsListPtr convert(const TopicPartitionList& topic_partitions);
CPPKAFKA_API TopicPartitionList convert(const TopicPartitionsListPtr& topic_partitions);
CPPKAFKA_API TopicPartitionList convert(rd_kafka_topic_partition_list_t* topic_partitions);
CPPKAFKA_API TopicPartitionsListPtr make_handle(rd_kafka_topic_partition_list_t* handle);
CPPKAFKA_API std::ostream& operator<<(std::ostream& output, const TopicPartitionList& rhs);