fluentd 1.12.0.rc1 → 1.12.0.rc2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fluentd might be problematic. Click here for more details.

@@ -22,34 +22,40 @@ module Fluent::Plugin
22
22
  UNWATCHED_POSITION = 0xffffffffffffffff
23
23
  POSITION_FILE_ENTRY_REGEX = /^([^\t]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)/.freeze
24
24
 
25
- def self.load(file, logger:)
26
- pf = new(file, logger: logger)
25
+ def self.load(file, follow_inodes, existing_paths, logger:)
26
+ pf = new(file, follow_inodes, existing_paths, logger: logger)
27
27
  pf.load
28
28
  pf
29
29
  end
30
30
 
31
- def initialize(file, logger: nil)
31
+ def initialize(file, follow_inodes, existing_paths, logger: nil)
32
32
  @file = file
33
33
  @logger = logger
34
34
  @file_mutex = Mutex.new
35
35
  @map = {}
36
+ @follow_inodes = follow_inodes
37
+ @existing_paths = existing_paths
36
38
  end
37
39
 
38
- def [](path)
39
- if m = @map[path]
40
+ def [](target_info)
41
+ if m = @map[@follow_inodes ? target_info.ino : target_info.path]
40
42
  return m
41
43
  end
42
44
 
43
45
  @file_mutex.synchronize {
44
46
  @file.seek(0, IO::SEEK_END)
45
- seek = @file.pos + path.bytesize + 1
46
- @file.write "#{path}\t0000000000000000\t0000000000000000\n"
47
- @map[path] = FilePositionEntry.new(@file, @file_mutex, seek, 0, 0)
47
+ seek = @file.pos + target_info.path.bytesize + 1
48
+ @file.write "#{target_info.path}\t0000000000000000\t0000000000000000\n"
49
+ if @follow_inodes
50
+ @map[target_info.ino] = FilePositionEntry.new(@file, @file_mutex, seek, 0, 0)
51
+ else
52
+ @map[target_info.path] = FilePositionEntry.new(@file, @file_mutex, seek, 0, 0)
53
+ end
48
54
  }
49
55
  end
50
56
 
51
- def unwatch(path)
52
- if (entry = @map.delete(path))
57
+ def unwatch(target_info)
58
+ if (entry = @map.delete(@follow_inodes ? target_info.ino : target_info.path))
53
59
  entry.update_pos(UNWATCHED_POSITION)
54
60
  end
55
61
  end
@@ -69,7 +75,11 @@ module Fluent::Plugin
69
75
  pos = m[2].to_i(16)
70
76
  ino = m[3].to_i(16)
71
77
  seek = @file.pos - line.bytesize + path.bytesize + 1
72
- map[path] = FilePositionEntry.new(@file, @file_mutex, seek, pos, ino)
78
+ if @follow_inodes
79
+ map[ino] = FilePositionEntry.new(@file, @file_mutex, seek, pos, ino)
80
+ else
81
+ map[path] = FilePositionEntry.new(@file, @file_mutex, seek, pos, ino)
82
+ end
73
83
  end
74
84
  end
75
85
 
@@ -94,8 +104,9 @@ module Fluent::Plugin
94
104
  @file.truncate(0)
95
105
  @file.write(entries.values.map(&:to_entry_fmt).join)
96
106
 
97
- entries.each do |path, val|
98
- if (m = @map[path])
107
+ # entry contains path/ino key and value.
108
+ entries.each do |key, val|
109
+ if (m = @map[key])
99
110
  m.seek = val.seek
100
111
  end
101
112
  end
@@ -139,13 +150,25 @@ module Fluent::Plugin
139
150
  @logger.warn("#{path} already exists. use latest one: deleted #{entries[path]}") if @logger
140
151
  end
141
152
 
142
- entries[path] = Entry.new(path, pos, ino, file_pos + path.size + 1)
153
+ if @follow_inodes
154
+ entries[ino] = Entry.new(path, pos, ino, file_pos + path.size + 1)
155
+ else
156
+ entries[path] = Entry.new(path, pos, ino, file_pos + path.size + 1)
157
+ end
143
158
  file_pos += line.size
144
159
  end
145
160
  end
146
161
 
162
+ entries = remove_deleted_files_entries(entries, @existing_paths) if @follow_inodes
147
163
  entries
148
164
  end
165
+
166
+ def remove_deleted_files_entries(existent_entries, existing_paths)
167
+ filtered_entries = existent_entries.select {|file_entry|
168
+ existing_paths.key?(file_entry)
169
+ }
170
+ filtered_entries
171
+ end
149
172
  end
150
173
 
151
174
  Entry = Struct.new(:path, :pos, :ino, :seek) do
@@ -224,5 +247,7 @@ module Fluent::Plugin
224
247
  @inode
225
248
  end
226
249
  end
250
+
251
+ TargetInfo = Struct.new(:path, :ino)
227
252
  end
228
253
  end
@@ -1252,7 +1252,13 @@ module Fluent
1252
1252
  log.debug "buffer queue cleared"
1253
1253
  @retry = nil
1254
1254
  else
1255
- @retry.step
1255
+ # Ensure that the current time is greater than or equal to @retry.next_time to avoid the situation when
1256
+ # @retry.step is called almost as many times as the number of flush threads in a short time.
1257
+ if Time.now >= @retry.next_time
1258
+ @retry.step
1259
+ else
1260
+ @retry.recalc_next_time # to prevent all flush threads from retrying at the same time
1261
+ end
1256
1262
  if error
1257
1263
  if using_secondary
1258
1264
  msg = "failed to flush the buffer with secondary output."
@@ -81,7 +81,7 @@ module Fluent
81
81
 
82
82
  def build_handler
83
83
  @methods.group_by(&:first).each do |(path, rest)|
84
- klass = Fluent::PluginHelper::HttpServer::Compat::WebrickHandler.build(Hash[rest.map { |e| [e[1], e[2]] }])
84
+ klass = Fluent::PluginHelper::HttpServer::Compat::WebrickHandler.build(**Hash[rest.map { |e| [e[1], e[2]] }])
85
85
  @server.mount(path, klass)
86
86
  end
87
87
  end
@@ -127,6 +127,10 @@ module Fluent
127
127
  nil
128
128
  end
129
129
 
130
+ def recalc_next_time
131
+ @next_time = calc_next_time
132
+ end
133
+
130
134
  def limit?
131
135
  if @forever
132
136
  false
@@ -53,11 +53,11 @@ module Fluent
53
53
  @enable_get_dump = config[:enable_get_dump]
54
54
  run_rpc_server
55
55
  end
56
- install_supervisor_signal_handlers
57
56
 
58
- if config[:signame]
59
- @signame = config[:signame]
57
+ if Fluent.windows?
60
58
  install_windows_event_handler
59
+ else
60
+ install_supervisor_signal_handlers
61
61
  end
62
62
 
63
63
  if counter = config[:counter_server]
@@ -70,6 +70,7 @@ module Fluent
70
70
  end
71
71
 
72
72
  def after_run
73
+ stop_windows_event_thread if Fluent.windows?
73
74
  stop_rpc_server if @rpc_endpoint
74
75
  stop_counter_server if @counter
75
76
  Fluent::Supervisor.cleanup_resources
@@ -92,7 +93,8 @@ module Fluent
92
93
  @rpc_server.mount_proc('/api/processes.flushBuffersAndKillWorkers') { |req, res|
93
94
  $log.debug "fluentd RPC got /api/processes.flushBuffersAndKillWorkers request"
94
95
  if Fluent.windows?
95
- $log.warn "operation 'flushBuffersAndKillWorkers' is not supported on Windows now."
96
+ supervisor_sigusr1_handler
97
+ stop(true)
96
98
  else
97
99
  Process.kill :USR1, $$
98
100
  Process.kill :TERM, $$
@@ -101,7 +103,9 @@ module Fluent
101
103
  }
102
104
  @rpc_server.mount_proc('/api/plugins.flushBuffers') { |req, res|
103
105
  $log.debug "fluentd RPC got /api/plugins.flushBuffers request"
104
- unless Fluent.windows?
106
+ if Fluent.windows?
107
+ supervisor_sigusr1_handler
108
+ else
105
109
  Process.kill :USR1, $$
106
110
  end
107
111
  nil
@@ -125,7 +129,9 @@ module Fluent
125
129
 
126
130
  @rpc_server.mount_proc('/api/config.gracefulReload') { |req, res|
127
131
  $log.debug "fluentd RPC got /api/config.gracefulReload request"
128
- unless Fluent.windows?
132
+ if Fluent.windows?
133
+ supervisor_sigusr2_handler
134
+ else
129
135
  Process.kill :USR2, $$
130
136
  end
131
137
 
@@ -159,37 +165,101 @@ module Fluent
159
165
  end
160
166
 
161
167
  def install_supervisor_signal_handlers
168
+ return if Fluent.windows?
169
+
162
170
  trap :HUP do
163
171
  $log.debug "fluentd supervisor process get SIGHUP"
164
172
  supervisor_sighup_handler
165
- end unless Fluent.windows?
173
+ end
166
174
 
167
175
  trap :USR1 do
168
176
  $log.debug "fluentd supervisor process get SIGUSR1"
169
177
  supervisor_sigusr1_handler
170
- end unless Fluent.windows?
178
+ end
171
179
 
172
180
  trap :USR2 do
173
181
  $log.debug 'fluentd supervisor process got SIGUSR2'
174
182
  supervisor_sigusr2_handler
175
- end unless Fluent.windows?
183
+ end
184
+ end
185
+
186
+ if Fluent.windows?
187
+ # Override some methods of ServerEngine::MultiSpawnWorker
188
+ # Since Fluentd's Supervisor doesn't use ServerEngine's HUP, USR1 and USR2
189
+ # handlers (see install_supervisor_signal_handlers), they should be
190
+ # disabled also on Windows, just send commands to workers instead.
191
+ def restart(graceful)
192
+ @monitors.each do |m|
193
+ m.send_command(graceful ? "GRACEFUL_RESTART\n" : "IMMEDIATE_RESTART\n")
194
+ end
195
+ end
196
+
197
+ def reload
198
+ @monitors.each do |m|
199
+ m.send_command("RELOAD\n")
200
+ end
201
+ end
176
202
  end
177
203
 
178
204
  def install_windows_event_handler
205
+ return unless Fluent.windows?
206
+
207
+ @pid_signame = "fluentd_#{$$}"
208
+ @signame = config[:signame]
209
+
179
210
  Thread.new do
180
- ev = Win32::Event.new(@signame)
211
+ ipc = Win32::Ipc.new(nil)
212
+ events = [
213
+ Win32::Event.new("#{@pid_signame}_STOP_EVENT_THREAD"),
214
+ Win32::Event.new("#{@pid_signame}"),
215
+ Win32::Event.new("#{@pid_signame}_HUP"),
216
+ Win32::Event.new("#{@pid_signame}_USR1"),
217
+ Win32::Event.new("#{@pid_signame}_USR2"),
218
+ ]
219
+ if @signame
220
+ signame_events = [
221
+ Win32::Event.new("#{@signame}"),
222
+ Win32::Event.new("#{@signame}_HUP"),
223
+ Win32::Event.new("#{@signame}_USR1"),
224
+ Win32::Event.new("#{@signame}_USR2"),
225
+ ]
226
+ events.concat(signame_events)
227
+ end
181
228
  begin
182
- ev.reset
183
- until WaitForSingleObject(ev.handle, 0) == WAIT_OBJECT_0
184
- sleep 1
229
+ loop do
230
+ idx = ipc.wait_any(events, Windows::Synchronize::INFINITE)
231
+ if idx > 0 && idx <= events.length
232
+ $log.debug("Got Win32 event \"#{events[idx - 1].name}\"")
233
+ else
234
+ $log.warn("Unexpected reutrn value of Win32::Ipc#wait_any: #{idx}")
235
+ end
236
+ case idx
237
+ when 2, 6
238
+ stop(true)
239
+ when 3, 7
240
+ supervisor_sighup_handler
241
+ when 4, 8
242
+ supervisor_sigusr1_handler
243
+ when 5, 9
244
+ supervisor_sigusr2_handler
245
+ when 1
246
+ break
247
+ end
185
248
  end
186
- stop(true)
187
249
  ensure
188
- ev.close
250
+ events.each { |event| event.close }
189
251
  end
190
252
  end
191
253
  end
192
254
 
255
+ def stop_windows_event_thread
256
+ if Fluent.windows?
257
+ ev = Win32::Event.open("#{@pid_signame}_STOP_EVENT_THREAD")
258
+ ev.set
259
+ ev.close
260
+ end
261
+ end
262
+
193
263
  def supervisor_sighup_handler
194
264
  kill_worker
195
265
  end
@@ -264,9 +334,25 @@ module Fluent
264
334
  def send_signal_to_workers(signal)
265
335
  return unless config[:worker_pid]
266
336
 
267
- config[:worker_pid].each_value do |pid|
268
- # don't rescue Errno::ESRCH here (invalid status)
269
- Process.kill(signal, pid)
337
+ if Fluent.windows?
338
+ send_command_to_workers(signal)
339
+ else
340
+ config[:worker_pid].each_value do |pid|
341
+ # don't rescue Errno::ESRCH here (invalid status)
342
+ Process.kill(signal, pid)
343
+ end
344
+ end
345
+ end
346
+
347
+ def send_command_to_workers(signal)
348
+ # Use SeverEngine's CommandSender on Windows
349
+ case signal
350
+ when :HUP
351
+ restart(false)
352
+ when :USR1
353
+ restart(true)
354
+ when :USR2
355
+ reload
270
356
  end
271
357
  end
272
358
  end
@@ -745,33 +831,45 @@ module Fluent
745
831
  end
746
832
  end
747
833
 
748
- trap :USR1 do
749
- flush_buffer
750
- end unless Fluent.windows?
834
+ if Fluent.windows?
835
+ install_main_process_command_handlers
836
+ else
837
+ trap :USR1 do
838
+ flush_buffer
839
+ end
751
840
 
752
- trap :USR2 do
753
- reload_config
754
- end unless Fluent.windows?
841
+ trap :USR2 do
842
+ reload_config
843
+ end
844
+ end
845
+ end
755
846
 
756
- if Fluent.windows?
757
- command_pipe = STDIN.dup
758
- STDIN.reopen(File::NULL, "rb")
759
- command_pipe.binmode
760
- command_pipe.sync = true
847
+ def install_main_process_command_handlers
848
+ command_pipe = $stdin.dup
849
+ $stdin.reopen(File::NULL, "rb")
850
+ command_pipe.binmode
851
+ command_pipe.sync = true
761
852
 
762
- Thread.new do
763
- loop do
764
- cmd = command_pipe.gets.chomp
765
- case cmd
766
- when "GRACEFUL_STOP", "IMMEDIATE_STOP"
767
- $log.debug "fluentd main process get #{cmd} command"
768
- @finished = true
769
- $log.debug "getting start to shutdown main process"
770
- Fluent::Engine.stop
771
- break
772
- else
773
- $log.warn "fluentd main process get unknown command [#{cmd}]"
774
- end
853
+ Thread.new do
854
+ loop do
855
+ cmd = command_pipe.gets
856
+ break unless cmd
857
+
858
+ case cmd.chomp!
859
+ when "GRACEFUL_STOP", "IMMEDIATE_STOP"
860
+ $log.debug "fluentd main process get #{cmd} command"
861
+ @finished = true
862
+ $log.debug "getting start to shutdown main process"
863
+ Fluent::Engine.stop
864
+ break
865
+ when "GRACEFUL_RESTART"
866
+ $log.debug "fluentd main process get #{cmd} command"
867
+ flush_buffer
868
+ when "RELOAD"
869
+ $log.debug "fluentd main process get #{cmd} command"
870
+ reload_config
871
+ else
872
+ $log.warn "fluentd main process get unknown command [#{cmd}]"
775
873
  end
776
874
  end
777
875
  end
@@ -16,6 +16,6 @@
16
16
 
17
17
  module Fluent
18
18
 
19
- VERSION = '1.12.0.rc1'
19
+ VERSION = '1.12.0.rc2'
20
20
 
21
21
  end
@@ -61,7 +61,6 @@ begin
61
61
  end
62
62
 
63
63
  def service_main
64
-
65
64
  @pid = service_main_start(@service_name)
66
65
  while running?
67
66
  sleep 10
@@ -69,13 +68,32 @@ begin
69
68
  end
70
69
 
71
70
  def service_stop
72
- ev = Win32::Event.open(@service_name)
73
- ev.set
74
- ev.close
71
+ set_event(@service_name)
75
72
  if @pid > 0
76
73
  Process.waitpid(@pid)
77
74
  end
78
75
  end
76
+
77
+ def service_paramchange
78
+ set_event("#{@service_name}_USR2")
79
+ end
80
+
81
+ def service_user_defined_control(code)
82
+ case code
83
+ when 128
84
+ set_event("#{@service_name}_HUP")
85
+ when 129
86
+ set_event("#{@service_name}_USR1")
87
+ end
88
+ end
89
+
90
+ private
91
+
92
+ def set_event(event_name)
93
+ ev = Win32::Event.open(event_name)
94
+ ev.set
95
+ ev.close
96
+ end
79
97
  end
80
98
 
81
99
  FluentdService.new(opts[:service_name]).mainloop