Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement](plugin) logstash: add retry queue without blocking tasks #44999

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 62 additions & 41 deletions extension/logstash/lib/logstash/outputs/doris.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require "logstash/namespace"
require "logstash/json"
require 'logstash/util/formater'
require 'logstash/util/retry_timer_task'
require "uri"
require "securerandom"
require "json"
Expand Down Expand Up @@ -131,9 +132,24 @@ def register
end
end

# Run named Timer as daemon thread
@timer = java.util.Timer.new("Doris Output #{self.params['id']}", true)

@retry_queue = Queue.new
retry_thread = Thread.new do
while popped = @retry_queue.pop
joker-star-l marked this conversation as resolved.
Show resolved Hide resolved
documents, http_headers, event_num, req_count = popped
handle_request(documents, http_headers, event_num, req_count)
end
end

print_plugin_info()
end # def register

def close
@timer.cancel
end

def multi_receive(events)
return if events.empty?
send_events(events)
Expand All @@ -157,50 +173,55 @@ def send_events(events)
http_headers["label"] = @label_prefix + "_" + @db + "_" + @table + "_" + Time.now.strftime('%Y%m%d_%H%M%S_%L_' + SecureRandom.uuid)
end

req_count = 0
sleep_for = 1
while true
response = make_request(documents, http_headers, @http_query, @http_hosts.sample)

req_count += 1
response_json = {}
begin
response_json = JSON.parse(response.body)
rescue => e
@logger.warn("doris stream load response: #{response} is not a valid JSON")
end
handle_request(documents, http_headers, event_num, 1)
end

status = response_json["Status"]
def sleep_for_attempt(attempt)
sleep_for = attempt**2
sleep_for = sleep_for <= 60 ? sleep_for : 60
(sleep_for/2) + (rand(0..sleep_for)/2)
end

if status == 'Label Already Exists'
@logger.warn("Label already exists: #{response_json['Label']}, skip #{event_num} records.")
break
end
private
def handle_request(documents, http_headers, event_num, req_count)
response = make_request(documents, http_headers, @http_query, @http_hosts.sample)
response_json = {}
begin
response_json = JSON.parse(response.body)
rescue => _
@logger.warn("doris stream load response: #{response} is not a valid JSON")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should return or do something else instead of just go ahead.

end

if status == "Success" || status == "Publish Timeout"
@total_bytes.addAndGet(documents.size)
@total_rows.addAndGet(event_num)
break
else
@logger.warn("FAILED doris stream load response:\n#{response}")

if @max_retries >= 0 && req_count > @max_retries
@logger.warn("DROP this batch after failed #{req_count} times.")
if @save_on_failure
@logger.warn("Try save to disk.Disk file path : #{@save_dir}/#{@table}_#{@save_file}")
save_to_disk(documents)
end
break
end

# sleep and then retry
sleep_for = sleep_for * 2
sleep_for = sleep_for <= 60 ? sleep_for : 60
sleep_rand = (sleep_for / 2) + (rand(0..sleep_for) / 2)
@logger.warn("Will do retry #{req_count} after sleep #{sleep_rand} secs.")
sleep(sleep_rand)
status = response_json["Status"]

if status == 'Label Already Exists'
@logger.warn("Label already exists: #{response_json['Label']}, skip #{event_num} records.")
return
end

if status == "Success" || status == "Publish Timeout"
@total_bytes.addAndGet(documents.size)
@total_rows.addAndGet(event_num)
return
end

@logger.warn("FAILED doris stream load response:\n#{response}")
# if status is Fail, we do not retry
if status == 'Fail' || (@max_retries >= 0 && req_count > @max_retries)
@logger.warn("DROP this batch after failed #{req_count} times.")
if @save_on_failure
@logger.warn("Try save to disk.Disk file path : #{@save_dir}/#{@table}_#{@save_file}")
save_to_disk(documents)
end
return
end

# add to retry_queue
sleep_for = sleep_for_attempt(req_count)
req_count += 1
@logger.warn("Will do retry #{req_count} after #{sleep_for} secs.")
timer_task = RetryTimerTask.new(@retry_queue, [documents, http_headers, event_num, req_count])
@timer.schedule(timer_task, sleep_for*1000)
end

private
Expand Down Expand Up @@ -284,8 +305,8 @@ def save_to_disk(documents)
end

# This is split into a separate method mostly to help testing
def log_failure(message)
@logger.warn("[Doris Output Failure] #{message}")
def log_failure(message, data = {})
@logger.warn("[Doris Output Failure] #{message}", data)
end

def make_request_headers()
Expand Down
31 changes: 31 additions & 0 deletions extension/logstash/lib/logstash/util/retry_timer_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

require 'java'

class RetryTimerTask < java.util.TimerTask
def initialize(retry_queue, event)
@retry_queue = retry_queue
# event style: [documents, http_headers, event_num, req_count]
@event = event
super()
end

def run
@retry_queue << @event
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is there any limit on length of retry_queue?
  2. Why not just add event to retry_queue but use a task and timer to do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. No. Because there won't be too many failures.
  2. Because we need to use a timer to make the event wait for a certain amount of time before being queued and retried, rather than retrying immediately.

end
end
Loading