-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add shutdown controller to force exit on stalled shutdown
* start logstash with --force-shutdown to force_exit on stall * stall detection kicks in when SIGTERM/SIGINT is received * add DeadLetterPostOffice to receive dead letter events * plugins have a dead_letter(event) api to send to dead_letter * dead_letter destination is a file
- Loading branch information
Showing
9 changed files
with
293 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# encoding: utf-8 | ||
|
||
class LogStash::DeadLetterPostOffice | ||
|
||
def self.logger=(logger) | ||
@logger = logger | ||
end | ||
|
||
def self.logger | ||
@logger ||= Cabin::Channel.get(LogStash) | ||
end | ||
|
||
def self.destination=(destination) | ||
logger.info("Dead letter events will be sent to \"#{destination.location}\".") | ||
@destination = destination | ||
end | ||
|
||
def self.<<(events) | ||
events = [events] unless events.is_a?(Array) | ||
|
||
events.each do |event| | ||
logger.warn("dead letter received!", :event => event.to_hash) | ||
event.tag("_dead_letter") | ||
event.cancel | ||
@destination << event | ||
end | ||
end | ||
|
||
module Destination | ||
|
||
class Base | ||
def location; end | ||
def <<(event); end | ||
end | ||
|
||
class File < Base | ||
|
||
START_TIME = Time.now | ||
DUMP_PATH = ::File.join("/tmp", "dump.#{START_TIME.strftime("%Y%m%d%H%M%S")}.log") | ||
|
||
def initialize(path=DUMP_PATH) | ||
@path = path | ||
@file = ::File.open(path, "w") | ||
end | ||
|
||
def location | ||
@path | ||
end | ||
|
||
def <<(event) | ||
@file.puts(event.to_json) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# encoding: utf-8 | ||
module LogStash | ||
class ShutdownController | ||
|
||
REPORT_CYCLE = 5 # seconds | ||
REPORTS = [] | ||
NUM_REPORTS = 3 | ||
|
||
def self.force_shutdown=(boolean) | ||
@force_shutdown = boolean | ||
end | ||
|
||
def self.force_shutdown? | ||
@force_shutdown | ||
end | ||
|
||
def logger=(logger) | ||
@logger = logger | ||
end | ||
|
||
def logger | ||
@logger ||= Cabin::Channel.get(LogStash) | ||
end | ||
|
||
def initialize(pipeline) | ||
@pipeline = pipeline | ||
end | ||
|
||
def start(cycle=REPORT_CYCLE) | ||
@thread ||= Thread.new do | ||
Stud.interval(cycle) do | ||
REPORTS << @pipeline.inflight_count | ||
REPORTS.delete_at(0) if REPORTS.size > NUM_REPORTS # expire old report | ||
report(REPORTS.last) | ||
if self.class.force_shutdown? && stalled? | ||
logger.fatal("Stalled pipeline detected. Forcefully quitting logstash..") | ||
DeadLetterPostOffice << @pipeline.dump | ||
@pipeline.force_exit() | ||
break | ||
end | ||
end | ||
end | ||
end | ||
|
||
def stop! | ||
@thread.terminate if @thread.is_a?(Thread) | ||
@thread = nil | ||
end | ||
|
||
def report(report) | ||
logger.warn ["INFLIGHT_EVENTS_REPORT", Time.now.iso8601, report] | ||
end | ||
|
||
def stalled? | ||
return false unless REPORTS.size == NUM_REPORTS | ||
# is stalled if inflight count is either constant or increasing | ||
REPORTS.each_cons(2).all? do |prev_report, next_report| | ||
prev_report["total"] <= next_report["total"] | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# encoding: utf-8 | ||
require "spec_helper" | ||
|
||
describe LogStash::DeadLetterPostOffice do | ||
|
||
describe ".<<" do | ||
subject { LogStash::DeadLetterPostOffice } | ||
let(:filter) { LogStash::Filter::Base.new } | ||
let(:event) { LogStash::Event.new("message" => "test") } | ||
let(:destination) { LogStash::DeadLetterPostOffice::Destination::Base.new } | ||
|
||
before :each do | ||
subject.destination = destination | ||
allow(destination).to receive(:<<) {|event| } | ||
end | ||
|
||
it "should send event to destination" do | ||
expect(destination).to receive(:<<).with(event) | ||
subject << event | ||
end | ||
|
||
it "should tag the event with \"_dead_letter\"" do | ||
subject << event | ||
expect(event["tags"]).to include("_dead_letter") | ||
end | ||
|
||
it "should cancel the event" do | ||
subject << event | ||
expect(event).to be_cancelled | ||
end | ||
|
||
context "array of events" do | ||
let(:event1) { LogStash::Event.new("message" => "test1") } | ||
let(:event2) { LogStash::Event.new("message" => "test2") } | ||
let(:event3) { LogStash::Event.new("message" => "test3") } | ||
let(:events) { [event1, event2, event3] } | ||
|
||
it "should push each event to the destination" do | ||
expect(destination).to receive(:<<).with(event1) | ||
expect(destination).to receive(:<<).with(event2) | ||
expect(destination).to receive(:<<).with(event3) | ||
subject << events | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.