Skip to content

Commit

Permalink
Merge pull request #24 from daichirata/sfc-gh-cxu-no-recurse
Browse files Browse the repository at this point in the history
Change recursion to loop in generate_path
  • Loading branch information
daichirata authored Sep 19, 2023
2 parents ac406dd + d4881c2 commit d239e53
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
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

0 comments on commit d239e53

Please sign in to comment.