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

Change recursion to loop in generate_path #24

Merged
merged 2 commits into from
Sep 19, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
/vendor/
38 changes: 25 additions & 13 deletions lib/fluent/plugin/out_gcs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class GCSOutput < Output
def initialize
super
require "google/cloud/storage"
Google::Apis.logger = log
end

config_param :project, :string, default: nil,
Expand Down Expand Up @@ -159,7 +160,7 @@ def check_object_exists(path)
end
end

def generate_path(chunk, i = 0, prev = nil)
def generate_path(chunk)
metadata = chunk.metadata
time_slice = if metadata.timekey.nil?
''.freeze
Expand All @@ -170,23 +171,34 @@ def generate_path(chunk, i = 0, prev = nil)
"%{file_extension}" => @object_creator.file_extension,
"%{hex_random}" => hex_random(chunk),
"%{hostname}" => Socket.gethostname,
"%{index}" => i,
"%{path}" => @path,
"%{time_slice}" => time_slice,
"%{uuid_flush}" => SecureRandom.uuid,
}
path = @object_key_format.gsub(Regexp.union(tags.keys), tags)
path = extract_placeholders(path, chunk)
return path unless check_object_exists(path)

if path == prev
if @overwrite
log.warn "object `#{path}` already exists but overwrites it"
return path

prev = nil
i = 0

until i < 0 do # Until overflow
tags["%{uuid_flush}"] = SecureRandom.uuid
tags["%{index}"] = i

path = @object_key_format.gsub(Regexp.union(tags.keys), tags)
path = extract_placeholders(path, chunk)
return path unless check_object_exists(path)

if path == prev
if @overwrite
log.warn "object `#{path}` already exists but overwrites it"
return path
end
raise "object `#{path}` already exists"
end
raise "object `#{path}` already exists"

i += 1
prev = path
end
generate_path(chunk, i + 1, path)

raise "cannot find an unoccupied GCS path"
end

# This is stolen from Fluentd
Expand Down
3 changes: 3 additions & 0 deletions test/plugin/test_out_gcs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def setup
@type memory
timekey_use_utc true
</buffer>
<system>
log_level debug
</system>
EOC

def create_driver(conf = CONFIG)
Expand Down