2019-01-21 14:02:03 +00:00
|
|
|
#include <Storages/Kafka/ReadBufferFromKafkaConsumer.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
2019-01-25 12:48:59 +00:00
|
|
|
const auto READ_POLL_MS = 500; /// How long to wait for a batch of messages.
|
2019-01-21 14:02:03 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-01-25 12:48:59 +00:00
|
|
|
void ReadBufferFromKafkaConsumer::commit()
|
2019-01-21 14:02:03 +00:00
|
|
|
{
|
2019-01-25 12:48:59 +00:00
|
|
|
if (messages.empty() || current == messages.begin())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto & previous = *std::prev(current);
|
|
|
|
LOG_TRACE(log, "Committing message with offset " << previous.get_offset());
|
|
|
|
consumer->async_commit(previous);
|
|
|
|
}
|
2019-01-21 14:02:03 +00:00
|
|
|
|
2019-01-25 12:48:59 +00:00
|
|
|
/// Do commit messages implicitly after we processed the previous batch.
|
|
|
|
bool ReadBufferFromKafkaConsumer::nextImpl()
|
|
|
|
{
|
|
|
|
if (current == messages.end())
|
2019-01-21 14:02:03 +00:00
|
|
|
{
|
2019-01-25 12:48:59 +00:00
|
|
|
commit();
|
|
|
|
messages = consumer->poll_batch(batch_size, std::chrono::milliseconds(READ_POLL_MS));
|
|
|
|
current = messages.begin();
|
|
|
|
|
|
|
|
LOG_TRACE(log, "Polled batch of " << messages.size() << " messages");
|
2019-01-21 14:02:03 +00:00
|
|
|
}
|
2019-01-25 12:48:59 +00:00
|
|
|
|
|
|
|
if (messages.empty() || current == messages.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (auto err = current->get_error())
|
2019-01-21 14:02:03 +00:00
|
|
|
{
|
2019-01-25 12:48:59 +00:00
|
|
|
++current;
|
|
|
|
|
|
|
|
// TODO: should throw exception instead
|
2019-01-21 14:02:03 +00:00
|
|
|
LOG_ERROR(log, "Consumer error: " << err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: very fishy place with const casting.
|
2019-01-25 12:48:59 +00:00
|
|
|
auto new_position = reinterpret_cast<char *>(const_cast<unsigned char *>(current->get_payload().get_data()));
|
|
|
|
BufferBase::set(new_position, current->get_payload().get_size(), 0);
|
|
|
|
|
|
|
|
++current;
|
2019-01-23 11:00:43 +00:00
|
|
|
|
2019-01-21 14:02:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace DB
|