Callback invoker to sink all thrown exceptions (#74)

This commit is contained in:
Alex Damian
2018-06-01 19:35:56 -04:00
committed by Matias Fontanini
parent 15fdab6943
commit 9714bec5bf
9 changed files with 180 additions and 66 deletions

View File

@@ -38,6 +38,7 @@
#include "queue.h"
#include "macros.h"
#include "error.h"
#include "detail/callback_invoker.h"
namespace cppkafka {

View File

@@ -0,0 +1,127 @@
/*
* Copyright (c) 2017, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef CPPKAFKA_CALLBACK_INVOKER_H
#define CPPKAFKA_CALLBACK_INVOKER_H
#include <sstream>
#include <assert.h>
#include "../logging.h"
#include "../kafka_handle_base.h"
namespace cppkafka {
// Error values
template <typename T>
T error_value() { return T{}; }
template<> inline
void error_value<void>() {};
template<> inline
bool error_value<bool>() { return false; }
template<> inline
int error_value<int>() { return -1; }
/**
* \brief Wraps an std::function object and runs it while preventing all exceptions from escaping
* \tparam Func An std::function object
*/
template <typename Func>
class CallbackInvoker
{
public:
using RetType = typename Func::result_type;
using LogCallback = std::function<void(KafkaHandleBase& handle,
int level,
const std::string& facility,
const std::string& message)>;
CallbackInvoker(const char* callback_name,
const Func& callback,
KafkaHandleBase* handle)
: callback_name_(callback_name),
callback_(callback),
handle_(handle) {
}
explicit operator bool() const {
return (bool)callback_;
}
template <typename ...Args>
RetType operator()(Args&&... args) const {
static const char* library_name = "cppkafka";
std::ostringstream error_msg;
try {
if (callback_) {
return callback_(std::forward<Args>(args)...);
}
return error_value<RetType>();
}
catch (const std::exception& ex) {
if (handle_) {
error_msg << "Caught exception in " << callback_name_ << " callback: " << ex.what();
}
}
catch (...) {
if (handle_) {
error_msg << "Caught unknown exception in " << callback_name_ << " callback";
}
}
// Log error
if (handle_) {
if (handle_->get_configuration().get_log_callback()) {
try {
// Log it
handle_->get_configuration().get_log_callback()(*handle_,
static_cast<int>(LogLevel::LOG_ERR),
library_name,
error_msg.str());
}
catch (...) {} // sink everything
}
else {
rd_kafka_log_print(handle_->get_handle(),
static_cast<int>(LogLevel::LOG_ERR),
library_name,
error_msg.str().c_str());
}
}
return error_value<RetType>();
}
private:
const char* callback_name_;
const Func& callback_;
KafkaHandleBase* handle_;
};
}
#endif

View File

@@ -33,6 +33,7 @@
#include <chrono>
#include <functional>
#include <thread>
#include <string>
#include "../consumer.h"
#include "backoff_performer.h"
@@ -118,6 +119,7 @@ public:
*/
void commit(const TopicPartitionList& topic_partitions);
private:
// Return true to abort and false to continue committing
template <typename T>
bool do_commit(const T& object) {
try {
@@ -131,13 +133,11 @@ private:
if (ex.get_error() == RD_KAFKA_RESP_ERR__NO_OFFSET) {
return true;
}
// If there's a callback and it returns false for this message, abort
if (callback_ && !callback_(ex.get_error())) {
return true;
}
// If there's a callback and it returns false for this message, abort.
// Otherwise keep committing.
CallbackInvoker<ErrorCallback> callback("backoff committer", callback_, &consumer_);
return callback && !callback(ex.get_error());
}
// In any other case, we failed. Keep committing
return false;
}
Consumer& consumer_;

View File

@@ -364,9 +364,9 @@ void BufferedProducer<BufferType>::flush() {
produce_message(flush_queue.front());
}
catch (const HandleException& ex) {
if (flush_failure_callback_ &&
flush_failure_callback_(flush_queue.front(), ex.get_error())) {
// retry again later
// If we have a flush failure callback and it returns true, we retry producing this message later
CallbackInvoker<FlushFailureCallback> callback("flush failure", flush_failure_callback_, &producer_);
if (callback && callback(flush_queue.front(), ex.get_error())) {
do_add_message(std::move(flush_queue.front()), MessagePriority::Low, false);
}
}
@@ -519,19 +519,18 @@ void BufferedProducer<BufferType>::on_delivery_report(const Message& message) {
--pending_acks_;
assert(pending_acks_ != (size_t)-1); // Prevent underflow
// We should produce this message again if it has an error and we either don't have a
// produce failure callback or we have one but it returns true
bool should_produce = message.get_error() &&
(!produce_failure_callback_ || produce_failure_callback_(message));
if (should_produce) {
// Re-enqueue for later retransmission with higher priority (i.e. front of the queue)
do_add_message(Builder(message), MessagePriority::High, false);
if (message.get_error()) {
// We should produce this message again if we don't have a produce failure callback
// or we have one but it returns true
CallbackInvoker<ProduceFailureCallback> callback("produce failure", produce_failure_callback_, &producer_);
if (!callback || callback(message)) {
// Re-enqueue for later retransmission with higher priority (i.e. front of the queue)
do_add_message(Builder(message), MessagePriority::High, false);
}
}
else {
// Successful delivery
if (produce_success_callback_) {
produce_success_callback_(message);
}
CallbackInvoker<ProduceSuccessCallback>("delivery success", produce_success_callback_, &producer_)(message);
// Increment the total successful transmissions
++total_messages_produced_;
}