Skip to content

Commit 5dba397

Browse files
committed
Only upload temporary files to s3 when they are > 0 bytes
Fixes #195
1 parent 8527b24 commit 5dba397

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ if Dir.exist?(logstash_path) && use_logstash_source
99
gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
1010
gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
1111
end
12+
13+
if RUBY_VERSION == "1.9.3"
14+
gem 'rake', '12.2.1'
15+
end

lib/logstash/outputs/s3.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,12 @@ def restore_from_crash
385385
.select { |file| ::File.file?(file) }
386386
.each do |file|
387387
temp_file = TemporaryFile.create_from_existing_file(file, temp_folder_path)
388-
@logger.debug("Recovering from crash and uploading", :file => temp_file.path)
389-
@crash_uploader.upload_async(temp_file, :on_complete => method(:clean_temporary_file), :upload_options => upload_options)
388+
if temp_file.size > 0
389+
@logger.debug("Recovering from crash and uploading", :file => temp_file.path)
390+
@crash_uploader.upload_async(temp_file, :on_complete => method(:clean_temporary_file), :upload_options => upload_options)
391+
else
392+
clean_temporary_file(temp_file)
393+
end
390394
end
391395
end
392396
end

spec/integration/restore_from_crash_spec.rb

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,56 @@
1111

1212
let(:number_of_files) { 5 }
1313
let(:dummy_content) { "foobar\n" * 100 }
14+
let(:factory) { LogStash::Outputs::S3::TemporaryFileFactory.new(prefix, tags, "none", temporary_directory)}
1415

1516
before do
1617
clean_remote_files(prefix)
17-
# Use the S3 factory to create mutliples files with dummy content
18-
factory = LogStash::Outputs::S3::TemporaryFileFactory.new(prefix, tags, "none", temporary_directory)
18+
end
1919

20-
# Creating a factory always create a file
21-
factory.current.write(dummy_content)
22-
factory.current.fsync
2320

24-
(number_of_files - 1).times do
25-
factory.rotate!
21+
context 'with a non-empty tempfile' do
22+
before do
23+
# Creating a factory always create a file
2624
factory.current.write(dummy_content)
2725
factory.current.fsync
26+
27+
(number_of_files - 1).times do
28+
factory.rotate!
29+
factory.current.write(dummy_content)
30+
factory.current.fsync
31+
end
32+
end
33+
it "uploads the file to the bucket" do
34+
subject.register
35+
try(20) do
36+
expect(bucket_resource.objects(:prefix => prefix).count).to eq(number_of_files)
37+
expect(Dir.glob(File.join(temporary_directory, "*")).size).to eq(0)
38+
expect(bucket_resource.objects(:prefix => prefix).first.acl.grants.collect(&:permission)).to include("READ", "WRITE")
39+
end
2840
end
2941
end
3042

31-
it "uploads the file to the bucket" do
32-
subject.register
33-
try(20) do
34-
expect(bucket_resource.objects(:prefix => prefix).count).to eq(number_of_files)
35-
expect(Dir.glob(File.join(temporary_directory, "*")).size).to eq(0)
36-
expect(bucket_resource.objects(:prefix => prefix).first.acl.grants.collect(&:permission)).to include("READ", "WRITE")
43+
context 'with an empty tempfile' do
44+
before do
45+
factory.current
46+
factory.rotate!
47+
end
48+
49+
it "should remove the temporary file" do
50+
expect(Dir.glob(::File.join(temporary_directory, "**", "*")).size).to be > 0
51+
subject.register
52+
puts Dir.glob(::File.join(temporary_directory, "**", "*"))
53+
expect(Dir.glob(::File.join(temporary_directory, "**", "*")).size).to eq(0)
54+
end
55+
56+
it "should not upload the file to the bucket" do
57+
expect(bucket_resource.objects(:prefix => prefix).count).to eq(0)
58+
expect(Dir.glob(::File.join(temporary_directory, "**", "*")).size).to be > 0
59+
subject.register
60+
61+
# Sleep to give enough time for plugin upload to s3 if it attempts to upload empty temporary file to S3
62+
sleep 5
63+
expect(bucket_resource.objects(:prefix => prefix).count).to eq(0)
3764
end
3865
end
3966
end

0 commit comments

Comments
 (0)