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
Send data in batches #3
Merged
chupakabr
merged 12 commits into
chupakabr:master
from
envato-archive:send-data-in-batches
Feb 11, 2018
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dd455d4
Send data in batches where possible
3900278
Refactor tests
2efc4b6
Fix parameters to put_batch
518f15d
Fix bug in sending events infinate times
76213c1
Fix race condition where we may pop nil events
fca3c9b
Only catch AWS errors, not all exceptions
2c336c0
Also don't squash all exceptions here
df63b82
Ensure we don't try and send a payload larger than 4MB
b80a00d
Don't try and send events that are too big for Firehose
6b47f24
Refactor
2b58d2f
Remove unused code
b7dca09
Merge branch 'master' into send-data-in-batches
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,41 +1,131 @@ | ||
# encoding: utf-8 | ||
require "logstash/devutils/rspec/spec_helper" | ||
require "logstash/outputs/firehose" | ||
require "logstash/codecs/plain" | ||
require "logstash/codecs/line" | ||
require "logstash/codecs/json_lines" | ||
require "logstash/event" | ||
require "aws-sdk" | ||
require "timecop" | ||
|
||
describe LogStash::Outputs::Firehose do | ||
dataStr = "123,someValue,1234567890" | ||
|
||
let(:sample_event) { LogStash::Event.new("message" => dataStr) } | ||
let(:expected_event) { LogStash::Event.new("message" => dataStr) } | ||
let(:output) { LogStash::Outputs::Firehose.new({"codec" => "plain"}) } | ||
let(:time_now) { Time.now } | ||
let(:expected_event) { "#{time_now.strftime("%FT%H:%M:%S.%3NZ")} %{host} 123,someValue,1234567890" } | ||
let(:firehose_double) { instance_double(Aws::Firehose::Client) } | ||
let(:stream_name) { "aws-test-stream" } | ||
let(:oversized_event) { "A" * 999_999 } | ||
subject { LogStash::Outputs::Firehose.new({"codec" => "plain"}) } | ||
|
||
before do | ||
Thread.abort_on_exception = true | ||
|
||
# Setup Firehose client | ||
output.stream = "aws-test-stream" | ||
subject.stream = stream_name | ||
output.access_key_id = "Key ID" | ||
output.secret_access_key = "Secret key" | ||
output.register | ||
subject.register | ||
|
||
allow(Aws::Firehose::Client).to receive(:new).and_return(firehose_double) | ||
allow(firehose_double).to receive(:put_record) | ||
allow(firehose_double).to receive(:put_record_batch) | ||
end | ||
|
||
describe "receive message with plain codec" do | ||
subject { | ||
expect(output).to receive(:handle_event) do |arg| | ||
arg | ||
describe "receive one message" do | ||
it "returns same string" do | ||
expect(firehose_double).to receive(:put_record).with({ | ||
delivery_stream_name: stream_name, | ||
record: { | ||
data: expected_event | ||
} | ||
}) | ||
Timecop.freeze(time_now) do | ||
subject.receive(sample_event) | ||
end | ||
output.receive(sample_event) | ||
} | ||
end | ||
|
||
it "doesn't attempt to send a record greater than 1000 KB" do | ||
expect(firehose_double).not_to receive(:put_record) | ||
subject.receive([oversized_event * 2]) | ||
end | ||
end | ||
|
||
describe "receive multiple messages" do | ||
let(:sample_event_1) { LogStash::Event.new("message" => "abc") } | ||
let(:sample_event_2) { LogStash::Event.new("message" => "def") } | ||
let(:sample_event_3) { LogStash::Event.new("message" => "ghi") } | ||
let(:time_now) { Time.now } | ||
let(:expected_event_1) { "#{time_now.strftime("%FT%H:%M:%S.%3NZ")} %{host} abc" } | ||
let(:expected_event_2) { "#{time_now.strftime("%FT%H:%M:%S.%3NZ")} %{host} def" } | ||
let(:expected_event_3) { "#{time_now.strftime("%FT%H:%M:%S.%3NZ")} %{host} ghi" } | ||
it "returns same string" do | ||
expect(subject).not_to eq(nil) | ||
expect(subject.include? expected_event["message"]).to be_truthy | ||
# expect(subject).to eq(expected_event["message"]) | ||
expect(firehose_double).to receive(:put_record_batch).with({ | ||
delivery_stream_name: stream_name, | ||
records: [ | ||
{ | ||
data: expected_event | ||
}, | ||
{ | ||
data: expected_event | ||
}, | ||
{ | ||
data: expected_event | ||
}, | ||
] | ||
}) | ||
Timecop.freeze(time_now) do | ||
subject.multi_receive([sample_event, sample_event, sample_event]) | ||
end | ||
end | ||
|
||
it "sends each message once" do | ||
expect(firehose_double).to receive(:put_record_batch).with({ | ||
delivery_stream_name: stream_name, | ||
records: [ | ||
{ | ||
data: expected_event_1 | ||
}, | ||
{ | ||
data: expected_event_2 | ||
}, | ||
] | ||
}).once | ||
expect(firehose_double).to receive(:put_record_batch).with({ | ||
delivery_stream_name: stream_name, | ||
records: [ | ||
{ | ||
data: expected_event_3 | ||
}, | ||
] | ||
}).once | ||
Timecop.freeze(time_now) do | ||
subject.multi_receive([sample_event_1, sample_event_2]) | ||
subject.multi_receive([sample_event_3]) | ||
subject.multi_receive([]) | ||
end | ||
end | ||
|
||
it "doesn't crash if no events are sent" do | ||
# Necessary to replicate our race condition | ||
a = Thread.new { subject.multi_receive(Array.new(499, sample_event_1)) } | ||
b = Thread.new { subject.multi_receive([sample_event_1, sample_event_2]) } | ||
expect { subject.multi_receive([sample_event_1, sample_event_2]) }.not_to raise_exception | ||
# Ensure rspec doubles don't leak into other examples | ||
a.join | ||
b.join | ||
end | ||
|
||
context "oversized events are sent" do | ||
it "doesn't attempt to send payloads greater than 4MB" do | ||
expect(firehose_double).to receive(:put_record_batch).twice | ||
subject.multi_receive(Array.new(5, oversized_event)) | ||
end | ||
|
||
it "doesn't attempt to send a record greater than 1000 KB" do | ||
expect(firehose_double).not_to receive(:put_record_batch) | ||
subject.multi_receive([oversized_event * 2]) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to have these properties configurable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a hard limit from AWS, you cannot change it so I'm not sure the value in making it configurable.