|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// |
| 13 | +/// \file KafkaConsumer.cxx |
| 14 | +/// \author Adam Wegrzynek <[email protected]> |
| 15 | +/// |
| 16 | + |
| 17 | +#include "KafkaConsumer.h" |
| 18 | +#include <string> |
| 19 | +#include "../MonLogger.h" |
| 20 | + |
| 21 | +namespace o2 |
| 22 | +{ |
| 23 | +/// ALICE O2 Monitoring system |
| 24 | +namespace monitoring |
| 25 | +{ |
| 26 | +/// Monitoring transports |
| 27 | +namespace transports |
| 28 | +{ |
| 29 | + |
| 30 | +KafkaConsumer::KafkaConsumer(const std::string& host, unsigned int port, const std::string& topic) : mTopic(topic) |
| 31 | +{ |
| 32 | + std::string errstr; |
| 33 | + std::unique_ptr<RdKafka::Conf> conf{RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL)}; |
| 34 | + if (conf->set("bootstrap.servers", host + ":" + std::to_string(port), errstr) != RdKafka::Conf::CONF_OK |
| 35 | + || conf->set("enable.partition.eof", "false", errstr) != RdKafka::Conf::CONF_OK |
| 36 | + || conf->set("group.id", "o2-monitoring-group", errstr) != RdKafka::Conf::CONF_OK |
| 37 | + || conf->set("auto.offset.reset", "latest", errstr) != RdKafka::Conf::CONF_OK |
| 38 | + || conf->set("heartbeat.interval.ms", "2000", errstr) != RdKafka::Conf::CONF_OK |
| 39 | + || conf->set("session.timeout.ms", "6000", errstr) != RdKafka::Conf::CONF_OK |
| 40 | + ) { |
| 41 | + throw MonitoringException("Kafka Consumer", errstr); |
| 42 | + } |
| 43 | + |
| 44 | + mConsumer = RdKafka::KafkaConsumer::create(conf.get(), errstr); |
| 45 | + if (!mConsumer) { |
| 46 | + MonLogger::Get(Severity::Warn) << "Could not initialize Kafka consumer" << MonLogger::End(); |
| 47 | + } |
| 48 | + if (mConsumer->subscribe({mTopic})) { |
| 49 | + MonLogger::Get(Severity::Warn) << "Failed to subscribe to topic" << MonLogger::End(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +std::vector<std::string> KafkaConsumer::receive() |
| 54 | +{ |
| 55 | + std::vector<std::string> received; |
| 56 | + size_t batch_size = 5; |
| 57 | + int remaining_timeout = 1000; |
| 58 | + auto start = std::chrono::high_resolution_clock::now(); |
| 59 | + |
| 60 | + while (received.size() < batch_size) { |
| 61 | + std::unique_ptr<RdKafka::Message> message{mConsumer->consume(remaining_timeout)}; |
| 62 | + switch (message->err()) { |
| 63 | + case RdKafka::ERR__TIMED_OUT: |
| 64 | + break; |
| 65 | + case RdKafka::ERR_NO_ERROR: |
| 66 | + received.push_back(std::string(static_cast<char*>(message->payload()), message->len())); |
| 67 | + break; |
| 68 | + default: |
| 69 | + std::cerr << "%% Consumer error: " << message->errstr() << std::endl; |
| 70 | + return received; |
| 71 | + } |
| 72 | + auto now = std::chrono::high_resolution_clock::now(); |
| 73 | + remaining_timeout = std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count(); |
| 74 | + if (remaining_timeout > 1000) { |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + return received; |
| 79 | +} |
| 80 | + |
| 81 | +KafkaConsumer::~KafkaConsumer() |
| 82 | +{ |
| 83 | + if (mConsumer) { |
| 84 | + mConsumer->close(); |
| 85 | + } |
| 86 | + delete mConsumer; |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace transports |
| 90 | +} // namespace monitoring |
| 91 | +} // namespace o2 |
0 commit comments