Module: Fluent::ServerModule

Defined in:
lib/fluent/supervisor.rb

Instance Method Summary collapse

Instance Method Details

#after_runObject



72
73
74
75
76
# File 'lib/fluent/supervisor.rb', line 72

def after_run
  stop_rpc_server if @rpc_endpoint
  stop_counter_server if @counter
  Fluent::Supervisor.cleanup_resources
end

#before_runObject



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
# File 'lib/fluent/supervisor.rb', line 46

def before_run
  @fluentd_conf = config[:fluentd_conf]
  @rpc_server = nil
  @counter = nil

  if config[:rpc_endpoint]
    @rpc_endpoint = config[:rpc_endpoint]
    @enable_get_dump = config[:enable_get_dump]
    run_rpc_server
  end
  install_supervisor_signal_handlers

  if config[:signame]
    @signame = config[:signame]
    install_windows_event_handler
  end

  if counter = config[:counter_server]
    run_counter_server(counter)
  end

  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

#install_supervisor_signal_handlersObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fluent/supervisor.rb', line 161

def install_supervisor_signal_handlers
  trap :HUP do
    $log.debug "fluentd supervisor process get SIGHUP"
    supervisor_sighup_handler
  end unless Fluent.windows?

  trap :USR1 do
    $log.debug "fluentd supervisor process get SIGUSR1"
    supervisor_sigusr1_handler
  end unless Fluent.windows?

  trap :USR2 do
    $log.debug 'fluentd supervisor process got SIGUSR2'
    supervisor_sigusr2_handler
  end unless Fluent.windows?
end

#install_windows_event_handlerObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/fluent/supervisor.rb', line 178

def install_windows_event_handler
  Thread.new do
    ev = Win32::Event.new(@signame)
    begin
      ev.reset
      until WaitForSingleObject(ev.handle, 0) == WAIT_OBJECT_0
        sleep 1
      end
      kill_worker
      stop(true)
    ensure
      ev.close
    end
  end
end

#kill_workerObject



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/fluent/supervisor.rb', line 231

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

#run_counter_server(counter_conf) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/fluent/supervisor.rb', line 149

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

#run_rpc_serverObject



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
# File 'lib/fluent/supervisor.rb', line 78

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?
      $log.warn "operation 'flushBuffersAndKillWorkers' is not supported on Windows now."
    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"
    unless Fluent.windows?
      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"
    unless Fluent.windows?
      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

#stop_counter_serverObject



157
158
159
# File 'lib/fluent/supervisor.rb', line 157

def stop_counter_server
  @counter.stop
end

#stop_rpc_serverObject



145
146
147
# File 'lib/fluent/supervisor.rb', line 145

def stop_rpc_server
  @rpc_server.shutdown
end

#supervisor_dump_config_handlerObject



245
246
247
# File 'lib/fluent/supervisor.rb', line 245

def supervisor_dump_config_handler
  $log.info @fluentd_conf
end

#supervisor_get_dump_config_handlerObject



249
250
251
# File 'lib/fluent/supervisor.rb', line 249

def supervisor_get_dump_config_handler
  { conf: @fluentd_conf }
end

#supervisor_sighup_handlerObject



194
195
196
# File 'lib/fluent/supervisor.rb', line 194

def supervisor_sighup_handler
  kill_worker
end

#supervisor_sigusr1_handlerObject



198
199
200
201
# File 'lib/fluent/supervisor.rb', line 198

def supervisor_sigusr1_handler
  reopen_log
  send_signal_to_workers(:USR1)
end

#supervisor_sigusr2_handlerObject



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
# File 'lib/fluent/supervisor.rb', line 203

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