-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved forking implementation #364
Draft
bestie
wants to merge
1
commit into
zendesk:master
Choose a base branch
from
bestie:alternative-forking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# frozen_string_literal: true | ||
|
||
module Racecar | ||
class ForkingRunner | ||
def initialize(runner:, config:, logger:, parent_monitor: ParentProcessMonitor.new) | ||
@runner = runner | ||
@config = config | ||
@logger = logger | ||
@pids = [] | ||
@parent_monitor = parent_monitor | ||
@running = false | ||
end | ||
|
||
attr_reader :config, :runner, :logger, :pids, :parent_monitor | ||
private :config, :runner, :logger, :pids, :parent_monitor | ||
|
||
def run | ||
config.prefork.call | ||
install_signal_handlers | ||
|
||
@running = true | ||
|
||
@pids = config.forks.times.map do |n| | ||
pid = fork do | ||
parent_monitor.child_post_fork | ||
config.postfork.call | ||
|
||
parent_monitor.on_parent_exit do | ||
logger.warn("Supervisor dead, exiting.") | ||
runner.stop | ||
end | ||
|
||
runner.run | ||
end | ||
logger.debug("Racecar forked consumer process #{pid}.") | ||
|
||
pid | ||
end | ||
|
||
parent_monitor.parent_post_fork | ||
|
||
wait_for_child_processes | ||
end | ||
|
||
def stop | ||
@running = false | ||
logger.debug("Racecar::ForkingRunner runner stopping #{Process.pid}.") | ||
terminate_workers | ||
end | ||
|
||
def running? | ||
!!@running | ||
end | ||
|
||
private | ||
|
||
def terminate_workers | ||
pids.each do |pid| | ||
begin | ||
Process.kill("TERM", pid) | ||
rescue Errno::ESRCH | ||
logger.debug("Racecar::ForkingRunner Process not found #{Process.pid}.") | ||
end | ||
end | ||
end | ||
|
||
def check_workers | ||
pids.each do |pid| | ||
unless worker_running?(pid) | ||
logger.debug("A forked worker has exited unepxectedly. Shutting everything down.") | ||
stop | ||
return | ||
end | ||
end | ||
end | ||
|
||
def worker_running?(pid) | ||
_, status = Process.waitpid2(pid, Process::WNOHANG) | ||
status.nil? | ||
rescue Errno::ECHILD | ||
false | ||
end | ||
|
||
def wait_for_child_processes | ||
pids.each do |pid| | ||
begin | ||
Process.wait(pid) | ||
rescue Errno::ECHILD | ||
end | ||
end | ||
end | ||
|
||
def install_signal_handlers | ||
Signal.trap("CHLD") do |sid| | ||
logger.warn("Received SIGCHLD") | ||
check_workers if running? | ||
end | ||
Signal.trap("TERM") do |sid| | ||
stop | ||
end | ||
Signal.trap("INT") do |sid| | ||
stop | ||
end | ||
end | ||
|
||
class ParentProcessMonitor | ||
def initialize(pipe_ends = IO.pipe) | ||
@read_end, @write_end = pipe_ends | ||
@monitor_thread = nil | ||
end | ||
|
||
attr_reader :read_end, :write_end, :monitor_thread | ||
private :read_end, :write_end, :monitor_thread | ||
|
||
def on_parent_exit(&block) | ||
child_post_fork | ||
monitor_thread = Thread.new do | ||
IO.select([read_end]) | ||
block.call | ||
end | ||
end | ||
|
||
def parent_post_fork | ||
read_end.close | ||
end | ||
|
||
def child_post_fork | ||
write_end.close | ||
end | ||
end | ||
end | ||
end |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we killing the
ForkingRunner
here and all of its children if one child exits? Maybe I'm following that incorrectly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it would unexpected for this to happen, so rather than try to recover from a potentially bad state the whole thing shuts down so Kubernetes can bring up a new replica.
I'm not against trying to recover and replace a lost worker process but that is quite a bit more complex.