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

Handle very large filesets gracefully #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 37 additions & 19 deletions lib/redsnapper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'thread/pool'
require 'thread'
require 'open3'
require 'set'
require 'date'
Expand Down Expand Up @@ -32,7 +32,38 @@ def <=>(other)
def initialize(archive, options = {})
@archive = archive
@options = options
@thread_pool = Thread.pool(options[:thread_pool_size] || THREAD_POOL_DEFAULT_SIZE)
@tpsize = options[:thread_pool_size] || THREAD_POOL_DEFAULT_SIZE
@work_qs = (0...@tpsize).map do
Queue.new
end
@thread_pool = (0...@tpsize).map do |i|
Thread.new do
chunk = @work_qs[i].pop()

# The following gross hack works around this gross bug in BSD tar that tarsnap inherits:
# https://github.com/Tarsnap/tarsnap/issues/329
chunk.map! { |file| file.gsub(/([#{Regexp.escape(GLOB_CHARS)}])/) { |m| "\\#{m}" } }
command = [ TARSNAP, '-xpf', @archive, '--null', '-T', '-', *@options[:tarsnap_options] ]
Open3.popen3(*command) do |stin, _, err|
chunk.each do |file|
stin.write file
stin.putc 0
end
stin.flush
stin.close

while line = err.gets
next if line.end_with?(NOT_OLDER_ERROR)
if line == EXIT_ERROR
@error = true
next
end
@@output_mutex.synchronize { warn line.chomp }
end
end
end
end

@error = false
end

Expand Down Expand Up @@ -82,7 +113,7 @@ def files_to_extract
end

def file_groups
groups = (1..@thread_pool.max).map { Group.new }
groups = (1..@tpsize).map { Group.new }
files_to_extract.sort { |a, b| b.last[:size] <=> a.last[:size] }.each do |name, props|

# If the previous batch of files had an entry with the same size and date,
Expand All @@ -98,24 +129,11 @@ def file_groups
end

def run
file_groups.each do |chunk|
@thread_pool.process do
chunk.map! { |file| file.gsub(/([#{Regexp.escape(GLOB_CHARS)}])/) { |m| "\\#{m}" } }
command = [ TARSNAP, '-xvf', @archive, *(@options[:tarsnap_options] + chunk) ]
Open3.popen3(*command) do |_, _, err|
while line = err.gets
next if line.end_with?(NOT_OLDER_ERROR)
if line == EXIT_ERROR
@error = true
next
end
@@output_mutex.synchronize { warn line.chomp }
end
end
end
file_groups.each_with_index do |chunk, idx|
@work_qs[idx].push chunk
end

@thread_pool.shutdown
@thread_pool.map(&:join)
@@output_mutex.synchronize { warn EXIT_ERROR } if @error
end
end