-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
supervisor.rb
1170 lines (1026 loc) · 37.2 KB
/
supervisor.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# 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 'fileutils'
require 'open3'
require 'pathname'
require 'fluent/config'
require 'fluent/counter'
require 'fluent/env'
require 'fluent/engine'
require 'fluent/error'
require 'fluent/log'
require 'fluent/plugin'
require 'fluent/rpc'
require 'fluent/system_config'
require 'fluent/msgpack_factory'
require 'fluent/variable_store'
require 'serverengine'
if Fluent.windows?
require 'win32/ipc'
require 'win32/event'
end
module Fluent
module ServerModule
def before_run
@fluentd_conf = config[:fluentd_conf]
@rpc_endpoint = nil
@rpc_server = nil
@counter = nil
@fluentd_lock_dir = Dir.mktmpdir("fluentd-lock-")
ENV['FLUENTD_LOCK_DIR'] = @fluentd_lock_dir
if config[:rpc_endpoint]
@rpc_endpoint = config[:rpc_endpoint]
@enable_get_dump = config[:enable_get_dump]
run_rpc_server
end
if Fluent.windows?
install_windows_event_handler
else
install_supervisor_signal_handlers
end
if counter = config[:counter_server]
run_counter_server(counter)
end
if config[:disable_shared_socket]
$log.info "shared socket for multiple workers is disabled"
else
socket_manager_path = ServerEngine::SocketManager::Server.generate_path
ServerEngine::SocketManager::Server.open(socket_manager_path)
ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
end
end
def after_run
stop_windows_event_thread if Fluent.windows?
stop_rpc_server if @rpc_endpoint
stop_counter_server if @counter
cleanup_lock_dir
Fluent::Supervisor.cleanup_resources
end
def cleanup_lock_dir
FileUtils.rm(Dir.glob(File.join(@fluentd_lock_dir, "fluentd-*.lock")))
FileUtils.rmdir(@fluentd_lock_dir)
end
def run_rpc_server
@rpc_server = RPC::Server.new(@rpc_endpoint, $log)
# built-in RPC for signals
@rpc_server.mount_proc('/api/processes.interruptWorkers') { |req, res|
$log.debug "fluentd RPC got /api/processes.interruptWorkers request"
Process.kill :INT, $$
nil
}
@rpc_server.mount_proc('/api/processes.killWorkers') { |req, res|
$log.debug "fluentd RPC got /api/processes.killWorkers request"
Process.kill :TERM, $$
nil
}
@rpc_server.mount_proc('/api/processes.flushBuffersAndKillWorkers') { |req, res|
$log.debug "fluentd RPC got /api/processes.flushBuffersAndKillWorkers request"
if Fluent.windows?
supervisor_sigusr1_handler
stop(true)
else
Process.kill :USR1, $$
Process.kill :TERM, $$
end
nil
}
@rpc_server.mount_proc('/api/plugins.flushBuffers') { |req, res|
$log.debug "fluentd RPC got /api/plugins.flushBuffers request"
if Fluent.windows?
supervisor_sigusr1_handler
else
Process.kill :USR1, $$
end
nil
}
@rpc_server.mount_proc('/api/config.reload') { |req, res|
$log.debug "fluentd RPC got /api/config.reload request"
if Fluent.windows?
# restart worker with auto restarting by killing
kill_worker
else
Process.kill :HUP, $$
end
nil
}
@rpc_server.mount_proc('/api/config.dump') { |req, res|
$log.debug "fluentd RPC got /api/config.dump request"
$log.info "dump in-memory config"
supervisor_dump_config_handler
nil
}
@rpc_server.mount_proc('/api/config.gracefulReload') { |req, res|
$log.debug "fluentd RPC got /api/config.gracefulReload request"
if Fluent.windows?
supervisor_sigusr2_handler
else
Process.kill :USR2, $$
end
nil
}
@rpc_server.mount_proc('/api/config.getDump') { |req, res|
$log.debug "fluentd RPC got /api/config.getDump request"
$log.info "get dump in-memory config via HTTP"
res.body = supervisor_get_dump_config_handler
[nil, nil, res]
} if @enable_get_dump
@rpc_server.start
end
def stop_rpc_server
@rpc_server.shutdown
end
def run_counter_server(counter_conf)
@counter = Fluent::Counter::Server.new(
counter_conf.scope,
{host: counter_conf.bind, port: counter_conf.port, log: $log, path: counter_conf.backup_path}
)
@counter.start
end
def stop_counter_server
@counter.stop
end
def install_supervisor_signal_handlers
return if Fluent.windows?
trap :HUP do
$log.debug "fluentd supervisor process get SIGHUP"
supervisor_sighup_handler
end
trap :USR1 do
$log.debug "fluentd supervisor process get SIGUSR1"
supervisor_sigusr1_handler
end
trap :USR2 do
$log.debug 'fluentd supervisor process got SIGUSR2'
supervisor_sigusr2_handler
end
end
if Fluent.windows?
# Override some methods of ServerEngine::MultiSpawnWorker
# Since Fluentd's Supervisor doesn't use ServerEngine's HUP, USR1 and USR2
# handlers (see install_supervisor_signal_handlers), they should be
# disabled also on Windows, just send commands to workers instead.
def restart(graceful)
@monitors.each do |m|
m.send_command(graceful ? "GRACEFUL_RESTART\n" : "IMMEDIATE_RESTART\n")
end
end
def reload
@monitors.each do |m|
m.send_command("RELOAD\n")
end
end
end
def install_windows_event_handler
return unless Fluent.windows?
@pid_signame = "fluentd_#{$$}"
@signame = config[:signame]
Thread.new do
ipc = Win32::Ipc.new(nil)
events = [
{win32_event: Win32::Event.new("#{@pid_signame}_STOP_EVENT_THREAD"), action: :stop_event_thread},
{win32_event: Win32::Event.new("#{@pid_signame}"), action: :stop},
{win32_event: Win32::Event.new("#{@pid_signame}_HUP"), action: :hup},
{win32_event: Win32::Event.new("#{@pid_signame}_USR1"), action: :usr1},
{win32_event: Win32::Event.new("#{@pid_signame}_USR2"), action: :usr2},
{win32_event: Win32::Event.new("#{@pid_signame}_CONT"), action: :cont},
]
if @signame
signame_events = [
{win32_event: Win32::Event.new("#{@signame}"), action: :stop},
{win32_event: Win32::Event.new("#{@signame}_HUP"), action: :hup},
{win32_event: Win32::Event.new("#{@signame}_USR1"), action: :usr1},
{win32_event: Win32::Event.new("#{@signame}_USR2"), action: :usr2},
{win32_event: Win32::Event.new("#{@signame}_CONT"), action: :cont},
]
events.concat(signame_events)
end
begin
loop do
infinite = 0xFFFFFFFF
ipc_idx = ipc.wait_any(events.map {|e| e[:win32_event]}, infinite)
event_idx = ipc_idx - 1
if event_idx >= 0 && event_idx < events.length
$log.debug("Got Win32 event \"#{events[event_idx][:win32_event].name}\"")
else
$log.warn("Unexpected return value of Win32::Ipc#wait_any: #{ipc_idx}")
end
case events[event_idx][:action]
when :stop
stop(true)
when :hup
supervisor_sighup_handler
when :usr1
supervisor_sigusr1_handler
when :usr2
supervisor_sigusr2_handler
when :cont
supervisor_dump_handler_for_windows
when :stop_event_thread
break
end
end
ensure
events.each { |event| event[:win32_event].close }
end
end
end
def stop_windows_event_thread
if Fluent.windows?
ev = Win32::Event.open("#{@pid_signame}_STOP_EVENT_THREAD")
ev.set
ev.close
end
end
def supervisor_sighup_handler
kill_worker
end
def supervisor_sigusr1_handler
reopen_log
send_signal_to_workers(:USR1)
end
def supervisor_sigusr2_handler
conf = nil
t = Thread.new do
$log.info 'Reloading new config'
# Validate that loading config is valid at first
conf = Fluent::Config.build(
config_path: config[:config_path],
encoding: config[:conf_encoding],
additional_config: config[:inline_config],
use_v1_config: config[:use_v1_config],
)
Fluent::VariableStore.try_to_reset do
Fluent::Engine.reload_config(conf, supervisor: true)
end
end
t.report_on_exception = false # Error is handled by myself
t.join
reopen_log
send_signal_to_workers(:USR2)
@fluentd_conf = conf.to_s
rescue => e
$log.error "Failed to reload config file: #{e}"
end
def supervisor_dump_handler_for_windows
# As for UNIX-like, SIGCONT signal to each process makes the process output its dump-file,
# and it is implemented before the implementation of the function for Windows.
# It is possible to trap SIGCONT and handle it here also on UNIX-like,
# but for backward compatibility, this handler is currently for a Windows-only.
raise "[BUG] This function is for Windows ONLY." unless Fluent.windows?
Thread.new do
begin
FluentSigdump.dump_windows
rescue => e
$log.error "failed to dump: #{e}"
end
end
send_signal_to_workers(:CONT)
rescue => e
$log.error "failed to dump: #{e}"
end
def kill_worker
if config[:worker_pid]
pids = config[:worker_pid].clone
config[:worker_pid].clear
pids.each_value do |pid|
if Fluent.windows?
Process.kill :KILL, pid
else
Process.kill :TERM, pid
end
end
end
end
def supervisor_dump_config_handler
$log.info @fluentd_conf
end
def supervisor_get_dump_config_handler
{ conf: @fluentd_conf }
end
private
def reopen_log
if (log = config[:logger_initializer])
# Creating new thread due to mutex can't lock
# in main thread during trap context
Thread.new do
log.reopen!
end
end
end
def send_signal_to_workers(signal)
return unless config[:worker_pid]
if Fluent.windows?
send_command_to_workers(signal)
else
config[:worker_pid].each_value do |pid|
# don't rescue Errno::ESRCH here (invalid status)
Process.kill(signal, pid)
end
end
end
def send_command_to_workers(signal)
# Use SeverEngine's CommandSender on Windows
case signal
when :HUP
restart(false)
when :USR1
restart(true)
when :USR2
reload
when :CONT
dump_all_windows_workers
end
end
def dump_all_windows_workers
@monitors.each do |m|
m.send_command("DUMP\n")
end
end
end
module WorkerModule
def spawn(process_manager)
main_cmd = config[:main_cmd]
env = {
'SERVERENGINE_WORKER_ID' => @worker_id.to_i.to_s,
}
@pm = process_manager.spawn(env, *main_cmd)
end
def after_start
(config[:worker_pid] ||= {})[@worker_id] = @pm.pid
end
end
class Supervisor
def self.load_config(path, params = {})
pre_loadtime = 0
pre_loadtime = params['pre_loadtime'].to_i if params['pre_loadtime']
pre_config_mtime = nil
pre_config_mtime = params['pre_config_mtime'] if params['pre_config_mtime']
config_mtime = File.mtime(path)
# reuse previous config if last load time is within 5 seconds and mtime of the config file is not changed
if (Time.now - Time.at(pre_loadtime) < 5) && (config_mtime == pre_config_mtime)
return params['pre_conf']
end
log_level = params['log_level']
suppress_repeated_stacktrace = params['suppress_repeated_stacktrace']
ignore_repeated_log_interval = params['ignore_repeated_log_interval']
ignore_same_log_interval = params['ignore_same_log_interval']
log_path = params['log_path']
chuser = params['chuser']
chgroup = params['chgroup']
chumask = params['chumask']
log_rotate_age = params['log_rotate_age']
log_rotate_size = params['log_rotate_size']
log_opts = {suppress_repeated_stacktrace: suppress_repeated_stacktrace, ignore_repeated_log_interval: ignore_repeated_log_interval,
ignore_same_log_interval: ignore_same_log_interval}
logger_initializer = Supervisor::LoggerInitializer.new(
log_path, log_level, chuser, chgroup, log_opts,
log_rotate_age: log_rotate_age,
log_rotate_size: log_rotate_size
)
# this #init sets initialized logger to $log
logger_initializer.init(:supervisor, 0)
logger_initializer.apply_options(format: params['log_format'], time_format: params['log_time_format'])
logger = $log
command_sender = Fluent.windows? ? "pipe" : "signal"
# ServerEngine's "daemonize" option is boolean, and path of pid file is brought by "pid_path"
pid_path = params['daemonize']
daemonize = !!params['daemonize']
se_config = {
worker_type: 'spawn',
workers: params['workers'],
log_stdin: false,
log_stdout: false,
log_stderr: false,
enable_heartbeat: true,
auto_heartbeat: false,
unrecoverable_exit_codes: [2],
stop_immediately_at_unrecoverable_exit: true,
root_dir: params['root_dir'],
logger: logger,
log: logger.out,
log_path: log_path,
log_level: log_level,
logger_initializer: logger_initializer,
chuser: chuser,
chgroup: chgroup,
chumask: chumask,
suppress_repeated_stacktrace: suppress_repeated_stacktrace,
ignore_repeated_log_interval: ignore_repeated_log_interval,
ignore_same_log_interval: ignore_same_log_interval,
daemonize: daemonize,
rpc_endpoint: params['rpc_endpoint'],
counter_server: params['counter_server'],
enable_get_dump: params['enable_get_dump'],
windows_daemon_cmdline: [ServerEngine.ruby_bin_path,
File.join(File.dirname(__FILE__), 'daemon.rb'),
ServerModule.name,
WorkerModule.name,
path,
JSON.dump(params)],
command_sender: command_sender,
fluentd_conf: params['fluentd_conf'],
conf_encoding: params['conf_encoding'],
inline_config: params['inline_config'],
config_path: path,
main_cmd: params['main_cmd'],
signame: params['signame'],
disable_shared_socket: params['disable_shared_socket'],
restart_worker_interval: params['restart_worker_interval'],
}
if daemonize
se_config[:pid_path] = pid_path
end
pre_params = params.dup
params['pre_loadtime'] = Time.now.to_i
params['pre_config_mtime'] = config_mtime
params['pre_conf'] = se_config
# prevent pre_conf from being too big by reloading many times.
pre_params['pre_conf'] = nil
params['pre_conf'][:windows_daemon_cmdline][5] = JSON.dump(pre_params)
se_config
end
class LoggerInitializer
def initialize(path, level, chuser, chgroup, opts, log_rotate_age: nil, log_rotate_size: nil)
@path = path
@level = level
@chuser = chuser
@chgroup = chgroup
@opts = opts
@log_rotate_age = log_rotate_age
@log_rotate_size = log_rotate_size
end
# Create a unique path for each process.
#
# >>> per_process_path(:worker, 1, "C:/tmp/test.log")
# C:/tmp/test-1.log
# >>> per_process_path(:supervisor, 0, "C:/tmp/test.log")
# C:/tmp/test-supervisor-0.log
def self.per_process_path(path, process_type, worker_id)
path = Pathname(path)
ext = path.extname
if process_type == :supervisor
suffix = "-#{process_type}-0#{ext}" # "-0" for backword compatibility.
else
suffix = "-#{worker_id}#{ext}"
end
return path.sub_ext(suffix).to_s
end
def init(process_type, worker_id)
@opts[:process_type] = process_type
@opts[:worker_id] = worker_id
if @path && @path != "-"
unless File.exist?(@path)
FileUtils.mkdir_p(File.dirname(@path))
end
if @log_rotate_age || @log_rotate_size
# We need to prepare a unique path for each worker since
# Windows locks files.
if Fluent.windows?
path = LoggerInitializer.per_process_path(@path, process_type, worker_id)
else
path = @path
end
@logdev = Fluent::LogDeviceIO.new(path, shift_age: @log_rotate_age, shift_size: @log_rotate_size)
else
@logdev = File.open(@path, "a")
end
if @chuser || @chgroup
chuid = @chuser ? ServerEngine::Privilege.get_etc_passwd(@chuser).uid : nil
chgid = @chgroup ? ServerEngine::Privilege.get_etc_group(@chgroup).gid : nil
File.chown(chuid, chgid, @path)
end
else
@logdev = STDOUT
end
dl_opts = {}
# subtract 1 to match serverengine daemon logger side logging severity.
dl_opts[:log_level] = @level - 1
dl_opts[:log_rotate_age] = @log_rotate_age if @log_rotate_age
dl_opts[:log_rotate_size] = @log_rotate_size if @log_rotate_size
logger = ServerEngine::DaemonLogger.new(@logdev, dl_opts)
$log = Fluent::Log.new(logger, @opts)
$log.enable_color(false) if @path
$log.enable_debug if @level <= Fluent::Log::LEVEL_DEBUG
$log.info "init #{process_type} logger", path: path, rotate_age: @log_rotate_age, rotate_size: @log_rotate_size
end
def stdout?
@logdev == STDOUT
end
def reopen!
if @path && @path != "-"
@logdev.reopen(@path, "a")
end
self
end
def apply_options(format: nil, time_format: nil, log_dir_perm: nil, ignore_repeated_log_interval: nil, ignore_same_log_interval: nil)
$log.format = format if format
$log.time_format = time_format if time_format
$log.ignore_repeated_log_interval = ignore_repeated_log_interval if ignore_repeated_log_interval
$log.ignore_same_log_interval = ignore_same_log_interval if ignore_same_log_interval
if @path && log_dir_perm
File.chmod(log_dir_perm || Fluent::DEFAULT_DIR_PERMISSION, File.dirname(@path))
end
end
def level=(level)
@level = level
$log.level = level
end
end
def self.default_options
{
config_path: Fluent::DEFAULT_CONFIG_PATH,
plugin_dirs: [Fluent::DEFAULT_PLUGIN_DIR],
log_level: Fluent::Log::LEVEL_INFO,
log_path: nil,
daemonize: nil,
libs: [],
setup_path: nil,
chuser: nil,
chgroup: nil,
chumask: "0",
root_dir: nil,
suppress_interval: 0,
suppress_repeated_stacktrace: true,
ignore_repeated_log_interval: nil,
without_source: nil,
enable_input_metrics: nil,
enable_size_metrics: nil,
use_v1_config: true,
strict_config_value: nil,
supervise: true,
standalone_worker: false,
signame: nil,
conf_encoding: 'utf-8',
disable_shared_socket: nil,
config_file_type: :guess,
}
end
def self.cleanup_resources
unless Fluent.windows?
if ENV.has_key?('SERVERENGINE_SOCKETMANAGER_PATH')
FileUtils.rm_f(ENV['SERVERENGINE_SOCKETMANAGER_PATH'])
end
end
end
def initialize(opt)
@config_file_type = opt[:config_file_type]
@daemonize = opt[:daemonize]
@standalone_worker= opt[:standalone_worker]
@config_path = opt[:config_path]
@inline_config = opt[:inline_config]
@use_v1_config = opt[:use_v1_config]
@conf_encoding = opt[:conf_encoding]
@log_path = opt[:log_path]
@show_plugin_config = opt[:show_plugin_config]
@libs = opt[:libs]
@plugin_dirs = opt[:plugin_dirs]
@chgroup = opt[:chgroup]
@chuser = opt[:chuser]
@chumask = opt[:chumask]
@log_rotate_age = opt[:log_rotate_age]
@log_rotate_size = opt[:log_rotate_size]
@signame = opt[:signame]
@cl_opt = opt
@conf = nil
# parse configuration immediately to initialize logger in early stage
if @config_path and File.exist?(@config_path)
@conf = Fluent::Config.build(config_path: @config_path,
encoding: @conf_encoding ? @conf_encoding : 'utf-8',
additional_config: @inline_config ? @inline_config : nil,
use_v1_config: !!@use_v1_config,
type: @config_file_type,
)
@system_config = build_system_config(@conf)
if @system_config.log
@log_rotate_age ||= @system_config.log.rotate_age
@log_rotate_size ||= @system_config.log.rotate_size
end
@conf = nil
end
log_opts = {suppress_repeated_stacktrace: opt[:suppress_repeated_stacktrace], ignore_repeated_log_interval: opt[:ignore_repeated_log_interval],
ignore_same_log_interval: opt[:ignore_same_log_interval]}
@log = LoggerInitializer.new(
@log_path, opt[:log_level], @chuser, @chgroup, log_opts,
log_rotate_age: @log_rotate_age,
log_rotate_size: @log_rotate_size
)
@finished = false
end
def run_supervisor(dry_run: false)
if dry_run
$log.info "starting fluentd-#{Fluent::VERSION} as dry run mode", ruby: RUBY_VERSION
end
if @system_config.workers < 1
raise Fluent::ConfigError, "invalid number of workers (must be > 0):#{@system_config.workers}"
end
root_dir = @system_config.root_dir
if root_dir
if File.exist?(root_dir)
unless Dir.exist?(root_dir)
raise Fluent::InvalidRootDirectory, "non directory entry exists:#{root_dir}"
end
else
begin
FileUtils.mkdir_p(root_dir, mode: @system_config.dir_permission || Fluent::DEFAULT_DIR_PERMISSION)
rescue => e
raise Fluent::InvalidRootDirectory, "failed to create root directory:#{root_dir}, #{e.inspect}"
end
end
end
begin
ServerEngine::Privilege.change(@chuser, @chgroup)
MessagePackFactory.init(enable_time_support: @system_config.enable_msgpack_time_support)
Fluent::Engine.init(@system_config, supervisor_mode: true)
Fluent::Engine.run_configure(@conf, dry_run: dry_run)
rescue Fluent::ConfigError => e
$log.error 'config error', file: @config_path, error: e
$log.debug_backtrace
exit!(1)
end
if dry_run
$log.info 'finished dry run mode'
exit 0
else
supervise
end
end
def options
{
'config_path' => @config_path,
'pid_file' => @daemonize,
'plugin_dirs' => @plugin_dirs,
'log_path' => @log_path,
'root_dir' => @system_config.root_dir,
}
end
def run_worker
begin
require 'sigdump/setup'
rescue Exception
# ignore LoadError and others (related with signals): it may raise these errors in Windows
end
Process.setproctitle("worker:#{@system_config.process_name}") if @process_name
if @standalone_worker && @system_config.workers != 1
raise Fluent::ConfigError, "invalid number of workers (must be 1 or unspecified) with --no-supervisor: #{@system_config.workers}"
end
install_main_process_signal_handlers
# This is the only log messsage for @standalone_worker
$log.info "starting fluentd-#{Fluent::VERSION} without supervision", pid: Process.pid, ruby: RUBY_VERSION if @standalone_worker
main_process do
create_socket_manager if @standalone_worker
if @standalone_worker
ServerEngine::Privilege.change(@chuser, @chgroup)
File.umask(@chumask.to_i(8))
end
MessagePackFactory.init(enable_time_support: @system_config.enable_msgpack_time_support)
Fluent::Engine.init(@system_config)
Fluent::Engine.run_configure(@conf)
Fluent::Engine.run
self.class.cleanup_resources if @standalone_worker
exit 0
end
end
def configure(supervisor: false)
if supervisor
@log.init(:supervisor, 0)
else
worker_id = ENV['SERVERENGINE_WORKER_ID'].to_i
process_type = case
when @standalone_worker then :standalone
when worker_id == 0 then :worker0
else :workers
end
@log.init(process_type, worker_id)
end
if @show_plugin_config
show_plugin_config
end
if @inline_config == '-'
$log.warn('the value "-" for `inline_config` is deprecated. See https://github.com/fluent/fluentd/issues/2711')
@inline_config = STDIN.read
end
@conf = Fluent::Config.build(
config_path: @config_path,
encoding: @conf_encoding,
additional_config: @inline_config,
use_v1_config: @use_v1_config,
type: @config_file_type,
)
@system_config = build_system_config(@conf)
@log.level = @system_config.log_level
@log.apply_options(
format: @system_config.log.format,
time_format: @system_config.log.time_format,
log_dir_perm: @system_config.dir_permission,
ignore_repeated_log_interval: @system_config.ignore_repeated_log_interval,
ignore_same_log_interval: @system_config.ignore_same_log_interval
)
$log.info :supervisor, 'parsing config file is succeeded', path: @config_path
@libs.each do |lib|
require lib
end
@plugin_dirs.each do |dir|
if Dir.exist?(dir)
dir = File.expand_path(dir)
Fluent::Plugin.add_plugin_dir(dir)
end
end
if supervisor
# plugins / configuration dumps
Gem::Specification.find_all.select { |x| x.name =~ /^fluent(d|-(plugin|mixin)-.*)$/ }.each do |spec|
$log.info("gem '#{spec.name}' version '#{spec.version}'")
end
end
end
private
def create_socket_manager
socket_manager_path = ServerEngine::SocketManager::Server.generate_path
ServerEngine::SocketManager::Server.open(socket_manager_path)
ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
end
def show_plugin_config
name, type = @show_plugin_config.split(":") # input:tail
$log.info "show_plugin_config option is deprecated. Use fluent-plugin-config-format --format=txt #{name} #{type}"
exit 0
end
def supervise
Process.setproctitle("supervisor:#{@system_config.process_name}") if @system_config.process_name
$log.info "starting fluentd-#{Fluent::VERSION}", pid: Process.pid, ruby: RUBY_VERSION
fluentd_spawn_cmd = build_spawn_command
$log.info "spawn command to main: ", cmdline: fluentd_spawn_cmd
params = {
'main_cmd' => fluentd_spawn_cmd,
'daemonize' => @daemonize,
'inline_config' => @inline_config,
'log_path' => @log_path,
'log_rotate_age' => @log_rotate_age,
'log_rotate_size' => @log_rotate_size,
'chuser' => @chuser,
'chgroup' => @chgroup,
'use_v1_config' => @use_v1_config,
'conf_encoding' => @conf_encoding,
'signame' => @signame,
'fluentd_conf' => @conf.to_s,
'workers' => @system_config.workers,
'root_dir' => @system_config.root_dir,
'log_level' => @system_config.log_level,
'suppress_repeated_stacktrace' => @system_config.suppress_repeated_stacktrace,
'ignore_repeated_log_interval' => @system_config.ignore_repeated_log_interval,
'rpc_endpoint' => @system_config.rpc_endpoint,
'enable_get_dump' => @system_config.enable_get_dump,
'counter_server' => @system_config.counter_server,
'log_format' => @system_config.log.format,
'log_time_format' => @system_config.log.time_format,
'disable_shared_socket' => @system_config.disable_shared_socket,
'restart_worker_interval' => @system_config.restart_worker_interval,
}
se = ServerEngine.create(ServerModule, WorkerModule){
Fluent::Supervisor.load_config(@config_path, params)
}
se.run
end
def install_main_process_signal_handlers
# Fluentd worker process (worker of ServerEngine) don't use code in serverengine to set signal handlers,
# because it does almost nothing.
# This method is the only method to set signal handlers in Fluentd worker process.
# When user use Ctrl + C not SIGINT, SIGINT is sent to all process in same process group.
# ServerEngine server process will send SIGTERM to child(spawned) processes by that SIGINT, so
# worker process SHOULD NOT do anything with SIGINT, SHOULD just ignore.
trap :INT do
$log.debug "fluentd main process get SIGINT"
# When Fluentd is launched without supervisor, worker should handle ctrl-c by itself
if @standalone_worker
@finished = true
$log.debug "getting start to shutdown main process"
Fluent::Engine.stop
end
end
trap :TERM do
$log.debug "fluentd main process get SIGTERM"
unless @finished
@finished = true
$log.debug "getting start to shutdown main process"
Fluent::Engine.stop
end
end
if Fluent.windows?
install_main_process_command_handlers
else
trap :USR1 do
flush_buffer
end
trap :USR2 do
reload_config
end
end
end
def install_main_process_command_handlers
command_pipe = $stdin.dup
$stdin.reopen(File::NULL, "rb")
command_pipe.binmode
command_pipe.sync = true
Thread.new do
loop do
cmd = command_pipe.gets
break unless cmd
case cmd.chomp!
when "GRACEFUL_STOP", "IMMEDIATE_STOP"
$log.debug "fluentd main process get #{cmd} command"
@finished = true
$log.debug "getting start to shutdown main process"
Fluent::Engine.stop
break
when "GRACEFUL_RESTART"
$log.debug "fluentd main process get #{cmd} command"
flush_buffer
when "RELOAD"
$log.debug "fluentd main process get #{cmd} command"
reload_config
when "DUMP"
$log.debug "fluentd main process get #{cmd} command"
dump
else
$log.warn "fluentd main process get unknown command [#{cmd}]"
end
end
end
end
def flush_buffer
# Creating new thread due to mutex can't lock
# in main thread during trap context
Thread.new do
begin
$log.debug "fluentd main process get SIGUSR1"
$log.info "force flushing buffered events"
@log.reopen!
Fluent::Engine.flush!
$log.debug "flushing thread: flushed"
rescue Exception => e
$log.warn "flushing thread error: #{e}"
end
end
end
def reload_config
Thread.new do
$log.debug('worker got SIGUSR2')
begin