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 s3 file cache sharing temp folder across processes #1650

Merged
2 commits merged into from
May 24, 2022
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
6 changes: 5 additions & 1 deletion cosmos/lib/cosmos/microservices/reducer_microservice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ def process_file(filename, type, entry_seconds, file_seconds)
)
true
rescue => e
Logger.error("Reducer Error #{e}\n#{e.backtrace}")
if File.exist(filename)
Logger.error("Reducer Error: #{filename}:#{File.size(filename)} bytes: \n#{e.formatted}")
else
Logger.error("Reducer Error: #{filename}:(Does Not Exist): \n#{e.formatted}")
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what you expect is happening with the reducer error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this comment means, but this error code is buggy and I need to fix it.

end

def reduce(type, data_keys, reduced)
Expand Down
4 changes: 2 additions & 2 deletions cosmos/lib/cosmos/models/gem_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ def self.put(gem_file_path, gem_install: true, scope:)
return nil
end

def self.install(name_or_path)
def self.install(name_or_path, scope:)
temp_dir = Dir.mktmpdir
begin
if File.exist?(name_or_path)
gem_file_path = name_or_path
else
gem_file_path = get(temp_dir, name_or_path)
end
rubygems_url = get_setting('rubygems_url')
rubygems_url = get_setting('rubygems_url', scope: scope)
Gem.sources = [rubygems_url] if rubygems_url
Gem.done_installing_hooks.clear
Gem.install(gem_file_path, "> 0.pre", :build_args => ['--no-document'], :prerelease => true)
Expand Down
2 changes: 1 addition & 1 deletion cosmos/lib/cosmos/models/plugin_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def self.install_phase2(name, variables, scope:)
gem_file_path = Cosmos::GemModel.get(temp_dir, name)

# Actually install the gem now (slow)
Cosmos::GemModel.install(gem_file_path)
Cosmos::GemModel.install(gem_file_path, scope: scope)

# Extract gem contents
gem_path = File.join(temp_dir, "gem")
Expand Down
18 changes: 12 additions & 6 deletions cosmos/lib/cosmos/utilities/s3_file_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# copyright holder

require 'fileutils'
require 'tmpdir'
require 'cosmos'
require 'cosmos/utilities/s3'

Expand Down Expand Up @@ -48,7 +49,7 @@ def initialize(s3_path, size = 0, priority = 0)

def retrieve
local_path = "#{S3FileCache.instance.cache_dir}/#{File.basename(@s3_path)}"
Cosmos::Logger.info "Retrieving #{@s3_path} from logs bucket"
Cosmos::Logger.debug "Retrieving #{@s3_path} from logs bucket"
@rubys3_client.get_object(bucket: "logs", key: @s3_path, response_target: local_path)
if File.exist?(local_path)
@size = File.size(local_path)
Expand All @@ -57,6 +58,7 @@ def retrieve
rescue => err
@error = err
Cosmos::Logger.error "Failed to retrieve #{@s3_path}\n#{err.formatted}"
raise err
end

def reserve
Expand Down Expand Up @@ -165,11 +167,11 @@ def initialize(name = 'default', max_disk_usage = MAX_DISK_USAGE)
end

# Create local file cache location
@cache_dir = File.join(Dir.tmpdir, 'cosmos', 'file_cache', name)
@cache_dir = Dir.mktmpdir
FileUtils.mkdir_p(@cache_dir)

# Clear out local file cache
FileUtils.rm_f Dir.glob("#{@cache_dir}/*")
at_exit do
FileUtils.remove_dir(@cache_dir, true)
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the at_exit applies to the Thread you create below?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At exit applies to the entire ruby process.


@cached_files = S3FileCollection.new

Expand All @@ -178,7 +180,11 @@ def initialize(name = 'default', max_disk_usage = MAX_DISK_USAGE)
file = @cached_files.get_next_to_retrieve
# Cosmos::Logger.debug "Next file: #{file}"
if file and (file.size + @cached_files.current_disk_usage()) <= @max_disk_usage
file.retrieve
begin
file.retrieve
rescue
# Will be automatically retried
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of raising the error and then silently rescuing and retrying?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error didn't use to be there, but I added it for when using retrieve by itself

else
sleep(1)
end
Expand Down