|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Matias Fontanini |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above |
| 12 | + * copyright notice, this list of conditions and the following disclaimer |
| 13 | + * in the documentation and/or other materials provided with the |
| 14 | + * distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * |
| 28 | + */ |
| 29 | + |
| 30 | +#ifndef CPPKAFKA_POLL_STRATEGY_BASE_H |
| 31 | +#define CPPKAFKA_POLL_STRATEGY_BASE_H |
| 32 | + |
| 33 | +#include <map> |
| 34 | +#include <boost/any.hpp> |
| 35 | +#include "../queue.h" |
| 36 | +#include "../topic_partition_list.h" |
| 37 | +#include "poll_interface.h" |
| 38 | + |
| 39 | +namespace cppkafka { |
| 40 | + |
| 41 | +/** |
| 42 | + * \brief Contains a partition queue and generic metadata which can be used to store |
| 43 | + * related (user-specific) information. |
| 44 | + */ |
| 45 | +struct QueueData { |
| 46 | + Queue queue_; |
| 47 | + boost::any metadata_; |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * \class PollStrategyBase |
| 52 | + * |
| 53 | + * \brief Base implementation of the PollInterface |
| 54 | + */ |
| 55 | +class PollStrategyBase : public PollInterface |
| 56 | +{ |
| 57 | +public: |
| 58 | + using QueueMap = std::map<TopicPartition, QueueData>; |
| 59 | + |
| 60 | + /** |
| 61 | + * \brief Constructor |
| 62 | + * |
| 63 | + * \param consumer A reference to the polled consumer instance |
| 64 | + */ |
| 65 | + explicit PollStrategyBase(Consumer& consumer); |
| 66 | + |
| 67 | + /** |
| 68 | + * \brief Destructor |
| 69 | + */ |
| 70 | + ~PollStrategyBase(); |
| 71 | + |
| 72 | + /** |
| 73 | + * \sa PollInterface::set_timeout |
| 74 | + */ |
| 75 | + void set_timeout(std::chrono::milliseconds timeout) override; |
| 76 | + |
| 77 | + /** |
| 78 | + * \sa PollInterface::get_timeout |
| 79 | + */ |
| 80 | + std::chrono::milliseconds get_timeout() override; |
| 81 | + |
| 82 | + /** |
| 83 | + * \sa PollInterface::get_consumer |
| 84 | + */ |
| 85 | + Consumer& get_consumer() final; |
| 86 | + |
| 87 | +protected: |
| 88 | + /** |
| 89 | + * \brief Get the queues from all assigned partitions |
| 90 | + * |
| 91 | + * \return A map of queues indexed by partition |
| 92 | + */ |
| 93 | + QueueMap& get_partition_queues(); |
| 94 | + |
| 95 | + /** |
| 96 | + * \brief Get the main consumer queue which services the underlying Consumer object |
| 97 | + * |
| 98 | + * \return The consumer queue |
| 99 | + */ |
| 100 | + QueueData& get_consumer_queue(); |
| 101 | + |
| 102 | + /** |
| 103 | + * \brief Return the next queue to be processed |
| 104 | + * |
| 105 | + * Depending on the polling strategy, each implementation must define it's own algorithm for |
| 106 | + * determining the next queue to poll. |
| 107 | + * |
| 108 | + * \return A partition queue |
| 109 | + */ |
| 110 | + virtual QueueData& get_next_queue() = 0; |
| 111 | + |
| 112 | + /** |
| 113 | + * \brief Reset the internal state of the queues. |
| 114 | + * |
| 115 | + * Use this function to reset the state of any polling strategy or algorithm. |
| 116 | + * |
| 117 | + * \remark This function gets called by on_assignement(), on_revocation() and on_rebalance_error() |
| 118 | + */ |
| 119 | + virtual void reset_state(); |
| 120 | + |
| 121 | + /** |
| 122 | + * \brief Function to be called when a new partition assignment takes place |
| 123 | + * |
| 124 | + * This method contains a default implementation. It adds all the new queues belonging |
| 125 | + * to the provided partition list and calls reset_state(). |
| 126 | + * |
| 127 | + * \param partitions Assigned topic partitions |
| 128 | + */ |
| 129 | + virtual void on_assignment(TopicPartitionList& partitions); |
| 130 | + |
| 131 | + /** |
| 132 | + * \brief Function to be called when an old partition assignment gets revoked |
| 133 | + * |
| 134 | + * This method contains a default implementation. It removes all the queues |
| 135 | + * belonging to the provided partition list and calls reset_state(). |
| 136 | + * |
| 137 | + * \param partitions Revoked topic partitions |
| 138 | + */ |
| 139 | + virtual void on_revocation(const TopicPartitionList& partitions); |
| 140 | + |
| 141 | + /** |
| 142 | + * \brief Function to be called when a topic rebalance error happens |
| 143 | + * |
| 144 | + * This method contains a default implementation. Calls reset_state(). |
| 145 | + * |
| 146 | + * \param error The rebalance error |
| 147 | + */ |
| 148 | + virtual void on_rebalance_error(Error error); |
| 149 | + |
| 150 | +private: |
| 151 | + Consumer& consumer_; |
| 152 | + QueueData consumer_queue_; |
| 153 | + QueueMap partition_queues_; |
| 154 | + Consumer::AssignmentCallback assignment_callback_; |
| 155 | + Consumer::RevocationCallback revocation_callback_; |
| 156 | + Consumer::RebalanceErrorCallback rebalance_error_callback_; |
| 157 | +}; |
| 158 | + |
| 159 | +} //cppkafka |
| 160 | + |
| 161 | +#endif //CPPKAFKA_POLL_STRATEGY_BASE_H |
0 commit comments