-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathwatched_file.rb
440 lines (368 loc) · 11.5 KB
/
watched_file.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# encoding: utf-8
module FileWatch
class WatchedFile
PATH_BASED_STAT = 0
IO_BASED_STAT = 1
attr_reader :bytes_read, :state, :file, :buffer, :recent_states, :bytes_unread
attr_reader :path, :accessed_at, :pathname, :filename
attr_reader :listener, :read_loop_count, :read_chunk_size, :stat
attr_reader :loop_count_type, :loop_count_mode
attr_accessor :last_open_warning_at
# this class represents a file that has been discovered
# path based stat is taken at discovery
def initialize(pathname, stat, settings)
@settings = settings
@pathname = Pathname.new(pathname) # given arg pathname might be a string or a Pathname object
@path = @pathname.to_path.freeze
@filename = @pathname.basename.to_s
full_state_reset(stat)
watch
set_standard_read_loop
set_accessed_at
end
def full_state_reset(this_stat = nil)
if this_stat.nil?
begin
this_stat = PathStatClass.new(pathname)
rescue Errno::ENOENT
delay_delete
return
end
end
@bytes_read = 0 # tracks bytes read from the open file or initialized from a matched sincedb_value off disk.
@bytes_unread = 0 # tracks bytes not yet read from the open file. So we can warn on shrink when unread bytes are seen.
file_close
set_stat(this_stat)
@listener = nil
@last_open_warning_at = nil
# initial as true means we have not associated this watched_file with a previous sincedb value yet.
# and we should read from the beginning if necessary
@initial = true
@recent_states = [] # keep last 8 states, managed in set_state
# the prepare_inode method is sourced from the mixed module above
watch if active? || @state.nil?
end
def rotate_from(other)
# move all state from other to this one
set_standard_read_loop
file_close
@bytes_read = other.bytes_read
@bytes_unread = other.bytes_unread
@listener = nil
@initial = false
@recent_states = other.recent_states
@accessed_at = other.accessed_at
if !other.delayed_delete?
# we don't know if a file exists at the other.path yet
# so no reset
other.full_state_reset
end
set_stat PathStatClass.new(pathname)
ignore
end
def set_stat(stat)
@stat = stat
@size = @stat.size
@sdb_key_v1 = @stat.inode_struct
end
private :set_stat
def rotate_as_file(bytes_read = 0)
# rotation, when a sincedb record exists for new inode, but no watched file to rotate from
# probably caused by a deletion detected in the middle of the rename cascade
# RARE due to delayed_delete - there would have to be a large time span between the renames.
@bytes_read = bytes_read # tracks bytes read from the open file or initialized from a matched sincedb_value off disk.
@bytes_unread = 0 # tracks bytes not yet read from the open file. So we can warn on shrink when unread bytes are seen.
@last_open_warning_at = nil
# initial as true means we have not associated this watched_file with a previous sincedb value yet.
# and we should read from the beginning if necessary
@initial = false
@recent_states = [] # keep last 8 states, managed in set_state
set_stat(PathStatClass.new(pathname))
reopen
watch
end
def stat_sincedb_key
@stat.inode_struct
end
def rotation_detected?
stat_sincedb_key != sincedb_key
end
# @return true if the file was modified since last stat
def restat!
modified_at # to always be able to detect changes
@stat.restat
if rotation_detected?
# switch to new state now
rotation_in_progress
return true
else
@size = @stat.size
update_bytes_unread
modified_at_changed?
end
end
def modified_at(update = false)
if update || @modified_at.nil?
@modified_at = @stat.modified_at
else
@modified_at
end
end
# @return whether modified_at changed since it was last read
# @see #restat!
def modified_at_changed?
modified_at != @stat.modified_at
end
def position_for_new_sincedb_value
if @initial
# this file was found in first discovery
@settings.start_new_files_at == :beginning ? 0 : last_stat_size
else
# always start at the beginning if found after first discovery
0
end
end
def last_stat_size
@stat.size
end
def current_size
@size
end
def shrunk?
@size < @bytes_read
end
def grown?
@size > @bytes_read
end
def size_changed?
# called from closed and ignored
# before the last stat was taken file should be fully read.
@size != @bytes_read
end
def all_read?
@bytes_read >= @size
end
def file_at_path_found_again
restore_previous_state
end
def set_listener(observer)
@listener = observer.listener_for(@path)
end
def unset_listener
@listener = nil
end
def has_listener?
end
def sincedb_key
@sdb_key_v1
end
def initial_completed
@initial = false
end
def set_accessed_at
@accessed_at = Time.now.to_f
end
def initial?
@initial
end
def compressed?
@path.end_with?('.gz','.gzip')
end
def reopen
if file_open?
file_close
open
end
end
def open
file_add_opened(FileOpener.open(@path))
end
def file_add_opened(rubyfile)
@file = rubyfile
@buffer = BufferedTokenizer.new(@settings.delimiter) if @buffer.nil?
end
def file_close
return if @file.nil? || @file.closed?
@file.close
@file = nil
end
def file_seek(amount, whence = IO::SEEK_SET)
@file.sysseek(amount, whence)
end
def file_read(amount = nil)
set_accessed_at
@file.sysread(amount || @read_chunk_size)
end
def file_open?
end
def reset_buffer
@buffer.flush
end
def read_extract_lines(amount)
data = file_read(amount)
result = buffer_extract(data)
increment_bytes_read(data.bytesize)
result
end
def buffer_extract(data)
warning, additional = "", {}
lines = @buffer.extract(data)
if lines.empty?
warning.concat("buffer_extract: a delimiter can't be found in current chunk")
warning.concat(", maybe there are no more delimiters or the delimiter is incorrect")
warning.concat(" or the text before the delimiter, a 'line', is very large")
warning.concat(", if this message is logged often try increasing the `file_chunk_size` setting.")
additional["delimiter"] = @settings.delimiter
additional["read_position"] = @bytes_read
additional["bytes_read_count"] = data.bytesize
additional["last_known_file_size"] = last_stat_size
additional["file_path"] = @path
end
BufferExtractResult.new(lines, warning, additional)
end
def increment_bytes_read(delta)
return if delta.nil?
@bytes_read += delta
update_bytes_unread
@bytes_read
end
def update_bytes_read(total_bytes_read)
return if total_bytes_read.nil?
@bytes_read = total_bytes_read
update_bytes_unread
@bytes_read
end
def rotation_in_progress
set_state :rotation_in_progress
end
def activate
set_state :active
end
def ignore
set_state :ignored
end
def ignore_as_unread
ignore
@bytes_read = @size
end
def close
set_state :closed
end
def watch
set_state :watched
end
def unwatch
set_state :unwatched
end
def delay_delete
set_state :delayed_delete
end
def restore_previous_state
set_state @recent_states.pop
end
def rotation_in_progress?
@state == :rotation_in_progress
end
def active?
@state == :active
end
def delayed_delete?
@state == :delayed_delete
end
def ignored?
@state == :ignored
end
def closed?
@state == :closed
end
def watched?
@state == :watched
end
def unwatched?
@state == :unwatched
end
def expiry_close_enabled?
[email protected]_older.nil?
end
def expiry_ignore_enabled?
[email protected]_older.nil?
end
def set_standard_read_loop
@read_loop_count = @settings.file_chunk_count
@read_chunk_size = @settings.file_chunk_size
# e.g. 1 * 10 bytes -> 10 or 256 * 65536 -> 1677716 or 140737488355327 * 32768 -> 4611686018427355136
@standard_loop_max_bytes = @read_loop_count * @read_chunk_size
end
def set_maximum_read_loop
# used to quickly fully read an open file when rotation is detected
@read_loop_count = FileWatch::MAX_ITERATIONS
@read_chunk_size = FileWatch::FILE_READ_SIZE
@standard_loop_max_bytes = @read_loop_count * @read_chunk_size
end
def loop_control_adjusted_for_stat_size
more = false
to_read = current_size - @bytes_read
return LoopControlResult.new(0, 0, more) if to_read < 1
return LoopControlResult.new(1, to_read, more) if to_read < @read_chunk_size
# set as if to_read is greater than or equal to max_bytes
# use the ones from settings and don't indicate more
count = @read_loop_count
if to_read < @standard_loop_max_bytes
# if the defaults are used then this branch will be taken
# e.g. to_read is 100 and max_bytes is 4 * 30 -> 120
# will overrun and trigger EOF, build less iterations
# will generate 3 * 30 -> 90 this time and we indicate more
# a 2GB file in read mode will get one loop of 64666 x 32768 (2119006656 / 32768)
# and a second loop with 1 x 31168
count = to_read / @read_chunk_size
more = true
end
LoopControlResult.new(count, @read_chunk_size, more)
end
def reset_bytes_unread
# called from shrink
@bytes_unread = 0
end
def set_state(value)
@recent_states.shift if @recent_states.size == 8
@recent_states << @state unless @state.nil?
@state = value
end
def recent_state_history
@recent_states + Array(@state)
end
def file_closable?
file_can_close? && all_read?
end
def file_ignorable?
return false unless expiry_ignore_enabled?
# (Time.now - stat.mtime) <- in jruby, this does int and float
# conversions before the subtraction and returns a float.
# so use all floats upfront
(Time.now.to_f - modified_at) > @settings.ignore_older
end
def file_can_close?
return false unless expiry_close_enabled?
(Time.now.to_f - @accessed_at) > @settings.close_older
end
def details
detail = "@filename='#{@filename}', @state=#{@state.inspect}, @recent_states=#{@recent_states.inspect}, "
detail.concat("@bytes_read=#{@bytes_read}, @bytes_unread=#{@bytes_unread}, current_size=#{current_size}, ")
detail.concat("last_stat_size=#{last_stat_size}, file_open?=#{file_open?}, @initial=#{@initial}")
"<FileWatch::WatchedFile: #{detail}, sincedb_key='#{sincedb_key}'>"
end
def inspect
"<FileWatch::WatchedFile: @filename='#{@filename}', @state=#{@state.inspect}, current_size=#{current_size}, sincedb_key='#{sincedb_key}'>"
end
def to_s
inspect
end
private
def update_bytes_unread
unread = current_size - @bytes_read
@bytes_unread = unread < 0 ? 0 : unread
end
end
end