Class: Fluent::Plugin::Buffer::FileChunk
Constant Summary
collapse
- FILE_PERMISSION =
0644
Instance Attribute Summary collapse
Attributes inherited from Chunk
#created_at, #metadata, #modified_at, #state, #unique_id
Class Method Summary
collapse
Instance Method Summary
collapse
-
#bytesize ⇒ Object
-
#close ⇒ Object
-
#commit ⇒ Object
-
#concat(bulk, bulk_size) ⇒ Object
-
#create_new_chunk(path, perm) ⇒ Object
-
#empty? ⇒ Boolean
-
#enqueued! ⇒ Object
-
#file_rename(file, old_path, new_path, callback = nil) ⇒ Object
-
#initialize(metadata, path, mode, perm: system_config.file_permission || FILE_PERMISSION, compress: :text) ⇒ FileChunk
constructor
A new instance of FileChunk.
-
#load_existing_enqueued_chunk(path) ⇒ Object
-
#load_existing_staged_chunk(path) ⇒ Object
-
#open(**kwargs, &block) ⇒ Object
-
#purge ⇒ Object
-
#read(**kwargs) ⇒ Object
-
#restore_metadata(bindata) ⇒ Object
-
#restore_metadata_partially(chunk) ⇒ Object
-
#rollback ⇒ Object
-
#size ⇒ Object
-
#write_metadata(update: true) ⇒ Object
#msgpack_factory, #msgpack_packer, #msgpack_unpacker
#system_config, #system_config_override
Methods inherited from Chunk
#append, #closed?, #queued?, #staged!, #staged?, #unstaged!, #unstaged?, #writable?, #write_to
#dump_unique_id_hex, #generate_unique_id
Constructor Details
#initialize(metadata, path, mode, perm: system_config.file_permission || FILE_PERMISSION, compress: :text) ⇒ FileChunk
Returns a new instance of FileChunk.
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 43
def initialize(metadata, path, mode, perm: system_config.file_permission || FILE_PERMISSION, compress: :text)
super(metadata, compress: compress)
@permission = perm
@bytesize = @size = @adding_bytes = @adding_size = 0
@meta = nil
case mode
when :create then create_new_chunk(path, perm)
when :staged then load_existing_staged_chunk(path)
when :queued then load_existing_enqueued_chunk(path)
else
raise ArgumentError, "Invalid file chunk mode: #{mode}"
end
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
41
42
43
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 41
def path
@path
end
|
#permission ⇒ Object
Returns the value of attribute permission.
41
42
43
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 41
def permission
@permission
end
|
Class Method Details
.assume_chunk_state(path) ⇒ Object
148
149
150
151
152
153
154
155
156
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 148
def self.assume_chunk_state(path)
if /\.(b|q)([0-9a-f]+)\.[^\/]*\Z/n =~ path $1 == 'b' ? :staged : :queued
else
:unknown
end
end
|
.generate_queued_chunk_path(path, unique_id) ⇒ Object
170
171
172
173
174
175
176
177
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 170
def self.generate_queued_chunk_path(path, unique_id)
chunk_id = Fluent::UniqueId.hex(unique_id)
if path.index(".b#{chunk_id}.")
path.sub(".b#{chunk_id}.", ".q#{chunk_id}.")
else path + ".q#{chunk_id}.chunk"
end
end
|
.generate_stage_chunk_path(path, unique_id) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 158
def self.generate_stage_chunk_path(path, unique_id)
pos = path.index('.*.')
raise "BUG: buffer chunk path on stage MUST have '.*.'" unless pos
prefix = path[0...pos]
suffix = path[(pos+3)..-1]
chunk_id = Fluent::UniqueId.hex(unique_id)
state = 'b'
"#{prefix}.#{state}#{chunk_id}.#{suffix}"
end
|
.unique_id_from_path(path) ⇒ Object
used only for queued v0.12 buffer path
180
181
182
183
184
185
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 180
def self.unique_id_from_path(path)
if /\.(b|q)([0-9a-f]+)\.[^\/]*\Z/n =~ path return $2.scan(/../).map{|x| x.to_i(16) }.pack('C*')
end
nil
end
|
Instance Method Details
#bytesize ⇒ Object
89
90
91
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 89
def bytesize
@bytesize + @adding_bytes
end
|
#close ⇒ Object
118
119
120
121
122
123
124
125
126
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 118
def close
super
size = @chunk.size
@chunk.close
@meta.close if @meta if size == 0
File.unlink(@path, @meta_path)
end
end
|
#commit ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 68
def commit
write_metadata
@commit_position = @chunk.pos
@size += @adding_size
@bytesize += @adding_bytes
@adding_bytes = @adding_size = 0
@modified_at = Time.now
true
end
|
#concat(bulk, bulk_size) ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 58
def concat(bulk, bulk_size)
raise "BUG: concatenating to unwritable chunk, now '#{self.state}'" unless self.writable?
bulk.force_encoding(Encoding::ASCII_8BIT)
@chunk.write bulk
@adding_bytes += bulk.bytesize
@adding_size += bulk_size
true
end
|
#create_new_chunk(path, perm) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 242
def create_new_chunk(path, perm)
@path = self.class.generate_stage_chunk_path(path, @unique_id)
@meta_path = @path + '.meta'
@chunk = File.open(@path, 'wb+', perm)
@chunk.set_encoding(Encoding::ASCII_8BIT)
@chunk.sync = true
@chunk.binmode
@meta = File.open(@meta_path, 'wb', perm)
@meta.set_encoding(Encoding::ASCII_8BIT)
@meta.sync = true
@meta.binmode
@state = :unstaged
@bytesize = 0
@commit_position = @chunk.pos @adding_bytes = 0
@adding_size = 0
end
|
#empty? ⇒ Boolean
97
98
99
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 97
def empty?
@bytesize == 0
end
|
#enqueued! ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 101
def enqueued!
return unless self.staged?
new_chunk_path = self.class.generate_queued_chunk_path(@path, @unique_id)
new_meta_path = new_chunk_path + '.meta'
write_metadata(update: false)
file_rename(@chunk, @path, new_chunk_path, ->(new_io){ @chunk = new_io })
@path = new_chunk_path
file_rename(@meta, @meta_path, new_meta_path, ->(new_io){ @meta = new_io })
@meta_path = new_meta_path
super
end
|
#file_rename(file, old_path, new_path, callback = nil) ⇒ Object
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 225
def file_rename(file, old_path, new_path, callback=nil)
pos = file.pos
if Fluent.windows?
file.close
File.rename(old_path, new_path)
file = File.open(new_path, 'rb', @permission)
else
File.rename(old_path, new_path)
file.reopen(new_path, 'rb')
end
file.set_encoding(Encoding::ASCII_8BIT)
file.sync = true
file.binmode
file.pos = pos
callback.call(file) if callback
end
|
#load_existing_enqueued_chunk(path) ⇒ Object
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 303
def load_existing_enqueued_chunk(path)
@path = path
@chunk = File.open(@path, 'rb')
@chunk.set_encoding(Encoding::ASCII_8BIT)
@chunk.binmode
@chunk.seek(0, IO::SEEK_SET)
@bytesize = @chunk.size
@commit_position = @chunk.size
@meta_path = @path + '.meta'
if File.readable?(@meta_path)
restore_metadata(File.open(@meta_path){|f| f.set_encoding(Encoding::ASCII_8BIT); f.binmode; f.read })
else
restore_metadata_partially(@chunk)
end
@state = :queued
end
|
#load_existing_staged_chunk(path) ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 261
def load_existing_staged_chunk(path)
@path = path
@meta_path = @path + '.meta'
@meta = nil
if File.exist?(@meta_path)
@chunk = File.open(@path, 'rb+')
@chunk.set_encoding(Encoding::ASCII_8BIT)
@chunk.sync = true
@chunk.seek(0, IO::SEEK_END)
@chunk.binmode
@meta = File.open(@meta_path, 'rb+')
@meta.set_encoding(Encoding::ASCII_8BIT)
@meta.sync = true
@meta.binmode
restore_metadata(@meta.read)
@meta.seek(0, IO::SEEK_SET)
@state = :staged
@bytesize = @chunk.size
@commit_position = @chunk.pos
@adding_bytes = 0
@adding_size = 0
else
@chunk = File.open(@path, 'rb')
@chunk.set_encoding(Encoding::ASCII_8BIT)
@chunk.binmode
@chunk.seek(0, IO::SEEK_SET)
@state = :queued
@bytesize = @chunk.size
restore_metadata_partially(@chunk)
@commit_position = @chunk.size
@unique_id = self.class.unique_id_from_path(@path) || @unique_id
end
end
|
#open(**kwargs, &block) ⇒ Object
141
142
143
144
145
146
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 141
def open(**kwargs, &block)
@chunk.seek(0, IO::SEEK_SET)
val = yield @chunk
@chunk.seek(0, IO::SEEK_END) if self.staged?
val
end
|
#purge ⇒ Object
128
129
130
131
132
133
134
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 128
def purge
super
@chunk.close
@meta.close if @meta
@bytesize = @size = @adding_bytes = @adding_size = 0
File.unlink(@path, @meta_path)
end
|
#read(**kwargs) ⇒ Object
136
137
138
139
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 136
def read(**kwargs)
@chunk.seek(0, IO::SEEK_SET)
@chunk.read
end
|
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 187
def restore_metadata(bindata)
data = msgpack_unpacker(symbolize_keys: true).feed(bindata).read rescue {}
now = Time.now
@unique_id = data[:id] || self.class.unique_id_from_path(@path) || @unique_id
@size = data[:s] || 0
@created_at = Time.at(data.fetch(:c, now.to_i))
@modified_at = Time.at(data.fetch(:m, now.to_i))
@metadata.timekey = data[:timekey]
@metadata.tag = data[:tag]
@metadata.variables = data[:variables]
end
|
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 202
def restore_metadata_partially(chunk)
@unique_id = self.class.unique_id_from_path(chunk.path) || @unique_id
@size = 0
@created_at = chunk.ctime @modified_at = chunk.mtime
@metadata.timekey = nil
@metadata.tag = nil
@metadata.variables = nil
end
|
#rollback ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 80
def rollback
if @chunk.pos != @commit_position
@chunk.seek(@commit_position, IO::SEEK_SET)
@chunk.truncate(@commit_position)
end
@adding_bytes = @adding_size = 0
true
end
|
#size ⇒ Object
93
94
95
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 93
def size
@size + @adding_size
end
|
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/fluent/plugin/buffer/file_chunk.rb', line 213
def write_metadata(update: true)
data = @metadata.to_h.merge({
id: @unique_id,
s: (update ? @size + @adding_size : @size),
c: @created_at.to_i,
m: (update ? Time.now : @modified_at).to_i,
})
@meta.seek(0, IO::SEEK_SET)
@meta.truncate(0)
@meta.write(msgpack_packer.pack(data))
end
|