-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support data compression in buffer plugins #1172
Changes from all commits
c2d80ba
d7b2fc7
1a57040
5afbb8f
37afde9
4365b0d
8f93202
d6d05d3
953dfa9
f3a2169
06e38ed
e9b6f15
18b770d
5c34d2b
9be7ae6
fe1f42c
9a6c9fd
0abf6da
32efffd
1709b78
0ed911a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<source> | ||
@type dummy | ||
@label @main | ||
tag "test.data" | ||
size 2 | ||
rate 10 | ||
dummy {"message":"yaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay"} | ||
auto_increment_key number | ||
</source> | ||
|
||
<label @main> | ||
<match test.data> | ||
@type buffered_stdout | ||
<buffer> | ||
@type file | ||
path "#{Dir.pwd}/compressed_buffers" | ||
flush_at_shutdown false | ||
chunk_limit_size 1m | ||
flush_interval 10s | ||
compress gzip | ||
</buffer> | ||
</match> | ||
</label> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,13 @@ | |
# | ||
|
||
require 'fluent/plugin/buffer' | ||
require 'fluent/plugin/compressable' | ||
require 'fluent/unique_id' | ||
require 'fluent/event' | ||
|
||
require 'monitor' | ||
require 'tempfile' | ||
require 'zlib' | ||
|
||
module Fluent | ||
module Plugin | ||
|
@@ -46,7 +49,7 @@ class Chunk | |
|
||
# TODO: CompressedPackedMessage of forward protocol? | ||
|
||
def initialize(metadata) | ||
def initialize(metadata, compress: :text) | ||
super() | ||
@unique_id = generate_unique_id | ||
@metadata = metadata | ||
|
@@ -57,12 +60,15 @@ def initialize(metadata) | |
@size = 0 | ||
@created_at = Time.now | ||
@modified_at = Time.now | ||
|
||
extend Decompressable if compress == :gzip | ||
end | ||
|
||
attr_reader :unique_id, :metadata, :created_at, :modified_at, :state | ||
|
||
# data is array of formatted record string | ||
def append(data) | ||
def append(data, **kwargs) | ||
raise ArgumentError, '`compress: gzip` can be used for Compressable module' if kwargs[:compress] == :gzip | ||
adding = ''.b | ||
data.each do |d| | ||
adding << d.b | ||
|
@@ -141,19 +147,75 @@ def purge | |
self | ||
end | ||
|
||
def read | ||
def read(**kwargs) | ||
raise ArgumentError, '`compressed: gzip` can be used for Compressable module' if kwargs[:compressed] == :gzip | ||
raise NotImplementedError, "Implement this method in child class" | ||
end | ||
|
||
def open(&block) | ||
def open(**kwargs, &block) | ||
raise ArgumentError, '`compressed: gzip` can be used for Compressable module' if kwargs[:compressed] == :gzip | ||
raise NotImplementedError, "Implement this method in child class" | ||
end | ||
|
||
def write_to(io) | ||
def write_to(io, **kwargs) | ||
raise ArgumentError, '`compressed: gzip` can be used for Compressable module' if kwargs[:compressed] == :gzip | ||
open do |i| | ||
IO.copy_stream(i, io) | ||
end | ||
end | ||
|
||
module Decompressable | ||
include Fluent::Plugin::Compressable | ||
|
||
def append(data, **kwargs) | ||
if kwargs[:compress] == :gzip | ||
io = StringIO.new | ||
Zlib::GzipWriter.wrap(io) do |gz| | ||
data.each do |d| | ||
gz.write d | ||
end | ||
end | ||
concat(io.string, data.size) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def open(**kwargs, &block) | ||
if kwargs[:compressed] == :gzip | ||
super | ||
else | ||
super(kwargs) do |chunk_io| | ||
output_io = if chunk_io.is_a?(StringIO) | ||
StringIO.new | ||
else | ||
Tempfile.new('decompressed-data') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
end | ||
decompress(input_io: chunk_io, output_io: output_io) | ||
output_io.seek(0, IO::SEEK_SET) | ||
yield output_io | ||
end | ||
end | ||
end | ||
|
||
def read(**kwargs) | ||
if kwargs[:compressed] == :gzip | ||
super | ||
else | ||
decompress(super) | ||
end | ||
end | ||
|
||
def write_to(io, **kwargs) | ||
open(compressed: :gzip) do |chunk_io| | ||
if kwargs[:compressed] == :gzip | ||
IO.copy_stream(chunk_io, io) | ||
else | ||
decompress(input_io: chunk_io, output_io: io) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'zlib' | ||
|
||
module Fluent | ||
module Plugin | ||
module Compressable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO these methods should be able to handle IO objects as output destination (by specifying it as optional keyword arguments). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote "as output destination". Of course, IO objects as input data source are useful. |
||
def compress(data, **kwargs) | ||
output_io = kwargs[:output_io] | ||
io = output_io || StringIO.new | ||
Zlib::GzipWriter.wrap(io) do |gz| | ||
gz.write data | ||
end | ||
|
||
output_io || io.string | ||
end | ||
|
||
# compressed_data is String like `compress(data1) + compress(data2) + ... + compress(dataN)` | ||
# https://www.ruby-forum.com/topic/971591#979503 | ||
def decompress(compressed_data = nil, output_io: nil, input_io: nil) | ||
case | ||
when input_io && output_io | ||
io_decompress(input_io, output_io) | ||
when input_io | ||
output_io = StringIO.new | ||
io = io_decompress(input_io, output_io) | ||
io.string | ||
when compressed_data.nil? || compressed_data.empty? | ||
# check compressed_data(String) is 0 length | ||
compressed_data | ||
when output_io | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check |
||
# exeucte after checking compressed_data is empty or not | ||
io = StringIO.new(compressed_data) | ||
io_decompress(io, output_io) | ||
else | ||
string_decompress(compressed_data) | ||
end | ||
end | ||
|
||
private | ||
|
||
def string_decompress(compressed_data) | ||
io = StringIO.new(compressed_data) | ||
|
||
out = '' | ||
loop do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note where this hack came from. |
||
gz = Zlib::GzipReader.new(io) | ||
out += gz.read | ||
unused = gz.unused | ||
gz.finish | ||
|
||
break if unused.nil? | ||
adjust = unused.length | ||
io.pos -= adjust | ||
end | ||
|
||
out | ||
end | ||
|
||
def io_decompress(input, output) | ||
loop do | ||
gz = Zlib::GzipReader.new(input) | ||
v = gz.read | ||
output.write(v) | ||
unused = gz.unused | ||
gz.finish | ||
|
||
break if unused.nil? | ||
adjust = unused.length | ||
input.pos -= adjust | ||
end | ||
|
||
output | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no care about
chunk.read
, so that call returns raw value of compressed data.Is it intended?
I think
chunk.read
(without any optional arguments) should return decompressed data.