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

fix hex_random for v0.14 #148

Merged
merged 1 commit into from
Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 7 additions & 20 deletions lib/fluent/plugin/out_s3.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Fluent
require 'fluent/mixin/config_placeholders'
require 'securerandom'

class S3Output < Fluent::TimeSlicedOutput
Fluent::Plugin.register_output('s3', self)
Expand Down Expand Up @@ -180,9 +179,6 @@ def start

check_apikeys if @check_apikey_on_start
ensure_bucket

# Securerandom.hex(2) returns 4 length hex
@hex_random_n = (@hex_random_length + 1) / 2
end

def format(tag, time, record)
Expand Down Expand Up @@ -257,25 +253,16 @@ def write(chunk)

private

# tsuffix is the one which file buffer filename has
def tsuffix(chunk)
if chunk.is_a?(Fluent::FileBufferChunk)
unique_id = chunk.unique_id
tsuffix = unique_id[0...(unique_id.size/2)].unpack('C*').map {|x| x.to_s(16) }.join('') # size: 16
else
nil
end
# v0.14 has a useful Fluent::UniqueId.hex(unique_id) method, though
def unique_hex(chunk)
unique_id = chunk.unique_id
unique_id.unpack('C*').map {|x| x.to_s(16) }.join('')
end

def hex_random(chunk)
if chunk.is_a?(Fluent::FileBufferChunk)
# let me use tsuffix because its value is kept on retrying even after rebooting
tsuffix = tsuffix(chunk)
tsuffix.reverse! # tsuffix is like (time_sec, time_usec, rand) => reversing gives more randomness
tsuffix[0...@hex_random_length]
else
SecureRandom.hex(@hex_random_n)[0...@hex_random_length]
end
unique_hex = unique_hex(chunk)
unique_hex.reverse! # unique_hex is like (time_sec, time_usec, rand) => reversing gives more randomness
unique_hex[0...@hex_random_length]
end

def ensure_bucket
Expand Down
30 changes: 8 additions & 22 deletions test/test_out_s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,38 +330,24 @@ def test_write_with_custom_s3_object_key_format_containing_uuid_flush_placeholde
Dir.glob('tmp/*').each {|file| FileUtils.rm_f(file) }
end

def test_write_with_custom_s3_object_key_format_containing_hex_random_placeholder_memory_buffer
hex = "012345"
mock(SecureRandom).hex(3) { hex }

config = CONFIG_TIME_SLICE.gsub(/%{hostname}/,"%{hex_random}") << "\nhex_random_length 5"
write_with_custom_s3_object_key_format_containing_hex_random_placeholder(config, hex[0...5])
end

def test_write_with_custom_s3_object_key_format_containing_hex_random_placeholder_file_buffer
tsuffix = "5226c3c4fb3d49b1"
any_instance_of(Fluent::FileBufferChunk) do |klass|
unique_id = "R&\xC3\xC4\xFB=I\xB1R&\xC3\xC4\xFB=I\xB1" # corresponding unique_id with tsuffxi
stub(klass).unique_id { unique_id }
end
hex = tsuffix.reverse
# ToDo: need to test hex_random does not change on retry, but it is difficult with
# the current fluentd test helper because it does not provide a way to run with the same chunks
def test_write_with_custom_s3_object_key_format_containing_hex_random_placeholder
unique_hex = "5226c3c4fb3d49b15226c3c4fb3d49b1"
hex_random = unique_hex.reverse[0...5]

config = CONFIG_TIME_SLICE.gsub(/%{hostname}/,"%{hex_random}") << "\nhex_random_length 16"
config = CONFIG_TIME_SLICE.gsub(/%{hostname}/,"%{hex_random}") << "\nhex_random_length #{hex_random.length}"
config = config.gsub(/buffer_type memory/, "buffer_type file\nbuffer_path test/tmp/buf")
write_with_custom_s3_object_key_format_containing_hex_random_placeholder(config, hex)
end

# ToDo: need to test hex_random does not change on retry, but it is difficult with
# the current fluentd test helper because it does not provide a way to run with the same chunks
def write_with_custom_s3_object_key_format_containing_hex_random_placeholder(config, hex)
# Partial mock the S3Bucket, not to make an actual connection to Amazon S3
setup_mocks(true)

s3path = "log/events/ts=20110102-13/events_0-#{hex}.gz"
s3path = "log/events/ts=20110102-13/events_0-#{hex_random}.gz"
s3_local_file_path = "/tmp/s3-test.txt"
setup_s3_object_mocks(s3_local_file_path: s3_local_file_path, s3path: s3path)

d = create_time_sliced_driver(config)
stub(d.instance).unique_hex { unique_hex }

time = Time.parse("2011-01-02 13:14:15 UTC").to_i
d.emit({"a"=>1}, time)
Expand Down