Skip to content

Commit f8755d0

Browse files
daipomWatson1978
andcommitted
Restart without downtime
Add a new feature: Update/Reload without downtime. 1. The supervisor receives SIGUSR2. 2. Spawn a new supervisor. 3. Take over shared sockets. 4. Launch new workers, and stop old processes in parallel. * Launch new workers with source-only mode * Limit to restart_without_downtime_ready? input plugin * Send SIGTERM to the old supervisor after 10s delay from 3. 5. The old supervisor stops and sends SIGRTMIN(34) to the new one. 6. The new workers run fully. Problem to solve: Updating Fluentd or reloading a config causes downtime. Plugins that receive data as a server, such as `in_udp`, `in_tcp`, and `in_syslog`, cannot receive data during this time. This means that the data sent by a client is lost during this time unless the client has a re-sending feature. This makes updating Fluentd or reloading a config difficult in some cases. Note: need these feature * #4661 * treasure-data/serverengine#146 Co-authored-by: Shizuo Fujita <fujita@clear-code.com> Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
1 parent 7ab49c3 commit f8755d0

File tree

7 files changed

+167
-27
lines changed

7 files changed

+167
-27
lines changed

lib/fluent/engine.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def initialize
4949

5050
attr_reader :root_agent, :system_config, :supervisor_mode
5151

52-
def init(system_config, supervisor_mode: false)
52+
def init(system_config, supervisor_mode: false, start_in_parallel: false)
5353
@system_config = system_config
5454
@supervisor_mode = supervisor_mode
5555

@@ -58,7 +58,7 @@ def init(system_config, supervisor_mode: false)
5858

5959
@log_event_verbose = system_config.log_event_verbose unless system_config.log_event_verbose.nil?
6060

61-
@root_agent = RootAgent.new(log: log, system_config: @system_config)
61+
@root_agent = RootAgent.new(log: log, system_config: @system_config, start_in_parallel: start_in_parallel)
6262

6363
self
6464
end

lib/fluent/plugin/in_syslog.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ def multi_workers_ready?
156156
true
157157
end
158158

159+
def restart_without_downtime_ready?
160+
true
161+
end
162+
159163
def start
160164
super
161165

lib/fluent/plugin/in_tcp.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def multi_workers_ready?
101101
true
102102
end
103103

104+
def restart_without_downtime_ready?
105+
true
106+
end
107+
104108
def start
105109
super
106110

lib/fluent/plugin/in_udp.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def multi_workers_ready?
6565
true
6666
end
6767

68+
def restart_without_downtime_ready?
69+
true
70+
end
71+
6872
def start
6973
super
7074

lib/fluent/plugin/input.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def metric_callback(es)
7070
def multi_workers_ready?
7171
false
7272
end
73+
74+
def restart_without_downtime_ready?
75+
false
76+
end
7377
end
7478
end
7579
end

lib/fluent/root_agent.rb

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,43 @@ module Fluent
4848
class RootAgent < Agent
4949
ERROR_LABEL = "@ERROR".freeze # @ERROR is built-in error label
5050

51-
def initialize(log:, system_config: SystemConfig.new)
51+
class SourceOnlyMode
52+
DISABELD = 0
53+
NORMAL = 1
54+
RESTART_WITHOUT_DOWNTIME_READY_ONLY = 2
55+
56+
def initialize(with_source_only, start_in_parallel)
57+
if start_in_parallel
58+
@mode = RESTART_WITHOUT_DOWNTIME_READY_ONLY
59+
elsif with_source_only
60+
@mode = NORMAL
61+
else
62+
@mode = DISABELD
63+
end
64+
end
65+
66+
def source_only?
67+
@mode != DISABELD
68+
end
69+
70+
def restart_without_downtime_ready_only?
71+
@mode == RESTART_WITHOUT_DOWNTIME_READY_ONLY
72+
end
73+
74+
def disable!
75+
@mode = DISABELD
76+
end
77+
end
78+
79+
def initialize(log:, system_config: SystemConfig.new, start_in_parallel: false)
5280
super(log: log)
5381

5482
@labels = {}
5583
@inputs = []
5684
@suppress_emit_error_log_interval = 0
5785
@next_emit_error_log_time = nil
5886
@without_source = system_config.without_source || false
59-
@with_source_only = system_config.with_source_only || false
87+
@source_only_mode = SourceOnlyMode.new(system_config.with_source_only, start_in_parallel)
6088
@source_only_buffer_agent = nil
6189
@enable_input_metrics = system_config.enable_input_metrics || false
6290

@@ -67,7 +95,7 @@ def initialize(log:, system_config: SystemConfig.new)
6795
attr_reader :labels
6896

6997
def source_only_router
70-
raise "[BUG] 'RootAgent#source_only_router' should not be called when 'with_source_only' is false" unless @with_source_only
98+
raise "[BUG] 'RootAgent#source_only_router' should not be called when 'with_source_only' is false" unless @source_only_mode.source_only?
7199
@source_only_buffer_agent.event_router
72100
end
73101

@@ -154,7 +182,7 @@ def configure(conf)
154182

155183
super
156184

157-
setup_source_only_buffer_agent if @with_source_only
185+
setup_source_only_buffer_agent if @source_only_mode.source_only?
158186

159187
# initialize <source> elements
160188
if @without_source
@@ -184,7 +212,7 @@ def setup_source_only_buffer_agent(flush: false)
184212

185213
def lifecycle(desc: false, kind_callback: nil, kind_or_agent_list: nil)
186214
unless kind_or_agent_list
187-
if @with_source_only
215+
if @source_only_mode.source_only?
188216
kind_or_agent_list = [:input, @source_only_buffer_agent]
189217
elsif @source_only_buffer_agent
190218
# source_only_buffer_agent can re-reroute events, so the priority is equal to output_with_router.
@@ -210,6 +238,9 @@ def lifecycle(desc: false, kind_callback: nil, kind_or_agent_list: nil)
210238
end
211239
display_kind = (kind == :output_with_router ? :output : kind)
212240
list.each do |instance|
241+
if @source_only_mode.restart_without_downtime_ready_only?
242+
next unless instance.restart_without_downtime_ready?
243+
end
213244
yield instance, display_kind
214245
end
215246
end
@@ -254,9 +285,9 @@ def flush!
254285

255286
def cancel_source_only!
256287
# TODO exclusive lock
257-
if @with_source_only
288+
if @source_only_mode.source_only?
258289
log.info "cancel --with-source-only mode and start the other plugins"
259-
@with_source_only = false
290+
@source_only_mode.disable!
260291
start
261292

262293
lifecycle_control_list[:input].each(&:event_emitter_cancel_source_only)
@@ -371,7 +402,7 @@ def add_source(type, conf)
371402
# See also 'fluentd/plugin/input.rb'
372403
input.context_router = @event_router
373404
input.configure(conf)
374-
input.event_emitter_set_source_only if @with_source_only
405+
input.event_emitter_set_source_only if @source_only_mode.source_only?
375406
if @enable_input_metrics
376407
@event_router.add_metric_callbacks(input.plugin_id, Proc.new {|es| input.metric_callback(es) })
377408
end

lib/fluent/supervisor.rb

Lines changed: 110 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def before_run
4343
@rpc_endpoint = nil
4444
@rpc_server = nil
4545
@counter = nil
46+
@socket_manager_server = nil
47+
@starting_new_supervisor_without_downtime = false
48+
@new_supervisor_pid = nil
49+
start_in_parallel = ENV.key?("FLUENT_RUNNING_IN_PARALLEL_WITH_OLD")
4650

4751
@fluentd_lock_dir = Dir.mktmpdir("fluentd-lock-")
4852
ENV['FLUENTD_LOCK_DIR'] = @fluentd_lock_dir
@@ -65,18 +69,31 @@ def before_run
6569

6670
if config[:disable_shared_socket]
6771
$log.info "shared socket for multiple workers is disabled"
72+
elsif start_in_parallel
73+
begin
74+
raise "[BUG] SERVERENGINE_SOCKETMANAGER_PATH env var must exist when starting in parallel" unless ENV.key?('SERVERENGINE_SOCKETMANAGER_PATH')
75+
@socket_manager_server = ServerEngine::SocketManager::Server.share_sockets_with_another_server(ENV['SERVERENGINE_SOCKETMANAGER_PATH'])
76+
$log.info "restart-without-downtime: took over the shared sockets", path: ENV['SERVERENGINE_SOCKETMANAGER_PATH']
77+
rescue => e
78+
$log.error "restart-without-downtime: cancel sequence because failed to take over the shared sockets", error: e
79+
raise
80+
end
6881
else
69-
server = ServerEngine::SocketManager::Server.open
70-
ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = server.path.to_s
82+
@socket_manager_server = ServerEngine::SocketManager::Server.open
83+
ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @socket_manager_server.path.to_s
7184
end
85+
86+
stop_parallel_old_supervisor_after_delay if start_in_parallel
7287
end
7388

7489
def after_run
7590
stop_windows_event_thread if Fluent.windows?
7691
stop_rpc_server if @rpc_endpoint
7792
stop_counter_server if @counter
7893
cleanup_lock_dir
79-
Fluent::Supervisor.cleanup_resources
94+
Fluent::Supervisor.cleanup_socketmanager_path unless @starting_new_supervisor_without_downtime
95+
96+
notify_new_supervisor_that_old_one_has_stopped if @starting_new_supervisor_without_downtime
8097
end
8198

8299
def cleanup_lock_dir
@@ -138,7 +155,7 @@ def run_rpc_server
138155
@rpc_server.mount_proc('/api/config.gracefulReload') { |req, res|
139156
$log.debug "fluentd RPC got /api/config.gracefulReload request"
140157
if Fluent.windows?
141-
supervisor_sigusr2_handler
158+
graceful_reload
142159
else
143160
Process.kill :USR2, Process.pid
144161
end
@@ -172,6 +189,47 @@ def stop_counter_server
172189
@counter.stop
173190
end
174191

192+
def stop_parallel_old_supervisor_after_delay
193+
# TODO if the new supervisor fails to start and this is not called,
194+
# it would be necessary to update the pid in the PID file to the old one when daemonized.
195+
196+
Thread.new do
197+
# Delay to avoid worker downtime as much as possible.
198+
# Even if the downtime occurs, it is no problem because the socket buffer works,
199+
# as long as the capacity is not exceeded.
200+
sleep 10
201+
old_pid = ENV["FLUENT_RUNNING_IN_PARALLEL_WITH_OLD"]&.to_i
202+
if old_pid
203+
$log.info "restart-without-downtime: stop the old supervisor"
204+
Process.kill :TERM, old_pid
205+
end
206+
rescue => e
207+
$log.warn "restart-without-downtime: failed to stop the old supervisor." +
208+
" If the old one does not exist, please send '34' signal to this new process to start to work fully." +
209+
" If it exists, something went wrong. Please kill the old one manually.",
210+
error: e
211+
end
212+
end
213+
214+
def notify_new_supervisor_that_old_one_has_stopped
215+
if config[:pid_path]
216+
new_pid = File.read(config[:pid_path]).to_i
217+
else
218+
raise "[BUG] new_supervisor_pid is not saved" unless @new_supervisor_pid
219+
new_pid = @new_supervisor_pid
220+
end
221+
222+
$log.info "restart-without-downtime: notify the new supervisor (pid: #{new_pid}) that old one has stopped"
223+
Process.kill 34, new_pid
224+
rescue => e
225+
$log.error(
226+
"restart-without-downtime: failed to notify the new supervisor." +
227+
" Please send '34' signal to the new supervisor process manually" +
228+
" if it does not start to work fully.",
229+
error: e
230+
)
231+
end
232+
175233
def install_supervisor_signal_handlers
176234
return if Fluent.windows?
177235

@@ -187,7 +245,11 @@ def install_supervisor_signal_handlers
187245

188246
trap :USR2 do
189247
$log.debug 'fluentd supervisor process got SIGUSR2'
190-
supervisor_sigusr2_handler
248+
if Fluent.windows?
249+
graceful_reload
250+
else
251+
restart_without_downtime
252+
end
191253
end
192254

193255
trap 34 do
@@ -259,7 +321,7 @@ def install_windows_event_handler
259321
when :usr1
260322
supervisor_sigusr1_handler
261323
when :usr2
262-
supervisor_sigusr2_handler
324+
graceful_reload
263325
when :cont
264326
supervisor_dump_handler_for_windows
265327
when :stop_event_thread
@@ -289,7 +351,7 @@ def supervisor_sigusr1_handler
289351
send_signal_to_workers(:USR1)
290352
end
291353

292-
def supervisor_sigusr2_handler
354+
def graceful_reload
293355
conf = nil
294356
t = Thread.new do
295357
$log.info 'Reloading new config'
@@ -317,7 +379,38 @@ def supervisor_sigusr2_handler
317379
$log.error "Failed to reload config file: #{e}"
318380
end
319381

382+
def restart_without_downtime
383+
# TODO exclusive lock
384+
385+
$log.info "start restart-without-downtime sequence"
386+
387+
if @starting_new_supervisor_without_downtime
388+
$log.warn "restart-without-downtime: canceled because it is already starting"
389+
return
390+
end
391+
if ENV.key?("FLUENT_RUNNING_IN_PARALLEL_WITH_OLD")
392+
$log.warn "restart-without-downtime: canceled because the previous sequence is still running"
393+
return
394+
end
395+
396+
@starting_new_supervisor_without_downtime = true
397+
commands = [ServerEngine.ruby_bin_path, $0] + ARGV
398+
env_to_add = {
399+
"SERVERENGINE_SOCKETMANAGER_INTERNAL_TOKEN" => ServerEngine::SocketManager::INTERNAL_TOKEN,
400+
"FLUENT_RUNNING_IN_PARALLEL_WITH_OLD" => "#{Process.pid}",
401+
}
402+
pid = Process.spawn(env_to_add, commands.join(" "))
403+
@new_supervisor_pid = pid unless config[:daemonize]
404+
rescue => e
405+
$log.error "restart-without-downtime: failed", error: e
406+
@starting_new_supervisor_without_downtime = false
407+
end
408+
320409
def cancel_source_only
410+
if ENV.key?("FLUENT_RUNNING_IN_PARALLEL_WITH_OLD")
411+
$log.info "restart-without-downtime: done all sequences, now the new workers starts to work fully"
412+
ENV.delete("FLUENT_RUNNING_IN_PARALLEL_WITH_OLD")
413+
end
321414
send_signal_to_workers(34)
322415
end
323416

@@ -509,12 +602,11 @@ def self.default_options
509602
}
510603
end
511604

512-
def self.cleanup_resources
513-
unless Fluent.windows?
514-
if ENV.has_key?('SERVERENGINE_SOCKETMANAGER_PATH')
515-
FileUtils.rm_f(ENV['SERVERENGINE_SOCKETMANAGER_PATH'])
516-
end
517-
end
605+
def self.cleanup_socketmanager_path
606+
return if Fluent.windows?
607+
return unless ENV.key?('SERVERENGINE_SOCKETMANAGER_PATH')
608+
609+
FileUtils.rm_f(ENV['SERVERENGINE_SOCKETMANAGER_PATH'])
518610
end
519611

520612
def initialize(cl_opt)
@@ -578,7 +670,7 @@ def run_supervisor(dry_run: false)
578670
begin
579671
ServerEngine::Privilege.change(@chuser, @chgroup)
580672
MessagePackFactory.init(enable_time_support: @system_config.enable_msgpack_time_support)
581-
Fluent::Engine.init(@system_config, supervisor_mode: true)
673+
Fluent::Engine.init(@system_config, supervisor_mode: true, start_in_parallel: ENV.key?("FLUENT_RUNNING_IN_PARALLEL_WITH_OLD"))
582674
Fluent::Engine.run_configure(@conf, dry_run: dry_run)
583675
rescue Fluent::ConfigError => e
584676
$log.error 'config error', file: @config_path, error: e
@@ -623,10 +715,10 @@ def run_worker
623715
File.umask(@chumask.to_i(8))
624716
end
625717
MessagePackFactory.init(enable_time_support: @system_config.enable_msgpack_time_support)
626-
Fluent::Engine.init(@system_config)
718+
Fluent::Engine.init(@system_config, start_in_parallel: ENV.key?("FLUENT_RUNNING_IN_PARALLEL_WITH_OLD"))
627719
Fluent::Engine.run_configure(@conf)
628720
Fluent::Engine.run
629-
self.class.cleanup_resources if @standalone_worker
721+
self.class.cleanup_socketmanager_path if @standalone_worker
630722
exit 0
631723
end
632724
end
@@ -844,7 +936,8 @@ def install_main_process_signal_handlers
844936
end
845937

846938
trap :USR2 do
847-
reload_config
939+
# Do nothing
940+
# TODO consider suitable code for this
848941
end
849942

850943
trap :CONT do

0 commit comments

Comments
 (0)