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 #19

Closed
wants to merge 1 commit into from
Closed
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/
32 changes: 20 additions & 12 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 @@ -130,7 +131,7 @@ def write(chunk)
}
opts.merge!(@encryption_opts)

log.debug { "out_gcs: upload chunk:#{chunk.key} to gcs://#{@bucket}/#{path} options: #{opts}" }
log.debug "out_gcs: upload chunk:#{chunk.key} to gcs://#{@bucket}/#{path} options: #{opts}"
@gcs_bucket.upload_file(obj.path, path, opts)
end
end
Expand Down Expand Up @@ -170,23 +171,30 @@ 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

until i < 0 do # Until overflow
tags["%{index}"] = i
path = @object_key_format.gsub(Regexp.union(tags.keys), tags)
path = extract_placeholders(path, chunk)
log.debug "checking if GCS path `#{path}` exists"
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
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