Skip to content

Commit

Permalink
Merge pull request #490 from raytung/task/rdkafka2-unknown-topic
Browse files Browse the repository at this point in the history
feat(out_rdkafka2): adds `use_default_for_unknown_topic` configuration
  • Loading branch information
ashie committed May 23, 2023
2 parents c21236f + 6222730 commit efc612d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ You need to install rdkafka gem.
partition_key_key (string) :default => 'partition_key'
message_key_key (string) :default => 'message_key'
default_topic (string) :default => nil
use_default_for_unknown_topic (bool) :default => false
default_partition_key (string) :default => nil
default_message_key (string) :default => nil
exclude_topic_key (bool) :default => false
Expand Down
25 changes: 23 additions & 2 deletions lib/fluent/plugin/out_rdkafka2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Fluent::Rdkafka2Output < Output
config_param :topic_key, :string, :default => 'topic', :desc => "Field for kafka topic"
config_param :default_topic, :string, :default => nil,
:desc => "Default output topic when record doesn't have topic field"
config_param :use_default_for_unknown_topic, :bool, :default => false, :desc => "If true, default_topic is used when topic not found"
config_param :message_key_key, :string, :default => 'message_key', :desc => "Field for kafka message key"
config_param :default_message_key, :string, :default => nil
config_param :partition_key, :string, :default => 'partition', :desc => "Field for kafka partition"
Expand Down Expand Up @@ -233,6 +234,9 @@ def add(level, message = nil)
@rdkafka = Rdkafka::Config.new(config)

if @default_topic.nil?
if @use_default_for_unknown_topic
raise Fluent::ConfigError, "default_topic must be set when use_default_for_unknown_topic is true"
end
if @chunk_keys.include?(@topic_key) && !@chunk_key_tag
log.warn "Use '#{@topic_key}' field of event record for topic but no fallback. Recommend to set default_topic or set 'tag' in buffer chunk keys like <buffer #{@topic_key},tag>"
end
Expand Down Expand Up @@ -466,24 +470,41 @@ def write(chunk)

def enqueue_with_retry(producer, topic, record_buf, message_key, partition, headers, time)
attempt = 0
actual_topic = topic

loop do
begin
@enqueue_rate.raise_if_limit_exceeded(record_buf.bytesize) if @enqueue_rate
return producer.produce(topic: topic, payload: record_buf, key: message_key, partition: partition, headers: headers, timestamp: @use_event_time ? Time.at(time) : nil)
return producer.produce(topic: actual_topic, payload: record_buf, key: message_key, partition: partition, headers: headers, timestamp: @use_event_time ? Time.at(time) : nil)
rescue EnqueueRate::LimitExceeded => e
@enqueue_rate.revert if @enqueue_rate
duration = e.next_retry_clock - Fluent::Clock.now
sleep(duration) if duration > 0.0
rescue Exception => e
@enqueue_rate.revert if @enqueue_rate
if e.respond_to?(:code) && e.code == :queue_full

if !e.respond_to?(:code)
raise e
end

case e.code
when :queue_full
if attempt <= @max_enqueue_retries
log.warn "Failed to enqueue message; attempting retry #{attempt} of #{@max_enqueue_retries} after #{@enqueue_retry_backoff}s"
sleep @enqueue_retry_backoff
attempt += 1
else
raise "Failed to enqueue message although tried retry #{@max_enqueue_retries} times"
end
# https://github.com/confluentinc/librdkafka/blob/c282ba2423b2694052393c8edb0399a5ef471b3f/src/rdkafka.h#LL309C9-L309C41
# RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC
when :unknown_topic
if @use_default_for_unknown_topic && actual_topic != @default_topic
log.debug "'#{actual_topic}' topic not found. Retry with '#{@default_topic}' topic"
actual_topic = @default_topic
retry
end
raise e
else
raise e
end
Expand Down

0 comments on commit efc612d

Please sign in to comment.