Skip to content
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

Fork workers no more 🔥 🔥 #16130

Merged
merged 1 commit into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ gem "manageiq-api-client", "~>0.1.0", :require => false
gem "manageiq-network_discovery", "~>0.1.2", :require => false
gem "mime-types", "~>2.6.1", :path => "mime-types-redirector"
gem "more_core_extensions", "~>3.3"
gem "nakayoshi_fork", "~>0.0.3" # provides a more CoW friendly fork (GC a few times before fork)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still useful for other uses of fork, such as the parallel gem?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt it. The only place that cared about trying to eek out more shared memory was worker creation. We can keep it but this line was not added for users of parallel .

gem "net-ldap", "~>0.14.0", :require => false
gem "net-ping", "~>1.7.4", :require => false
gem "net-ssh", "=3.2.0", :require => false
Expand Down
45 changes: 1 addition & 44 deletions app/models/miq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,29 +304,6 @@ def send_message_to_worker_monitor(message, *args)
)
end

def self.before_fork
preload_for_worker_role if respond_to?(:preload_for_worker_role)
end

def self.after_fork
close_pg_sockets_inherited_from_parent
DRb.stop_service
renice(Process.pid)
end

# When we fork, the children inherits the parent's file descriptors
# so we need to close any inherited raw pg sockets in the child.
def self.close_pg_sockets_inherited_from_parent
owner_to_pool = ActiveRecord::Base.connection_handler.instance_variable_get(:@owner_to_pool)
owner_to_pool[Process.ppid].values.compact.each do |pool|
pool.connections.each do |conn|
socket = conn.raw_connection.socket
_log.info("Closing socket: #{socket}")
IO.for_fd(socket).close
end
end
end

# Overriding queue_name as now some queue names can be
# arrays of names for some workers not just a singular name.
# We use JSON.parse as the array of names is stored as a string.
Expand All @@ -339,26 +316,6 @@ def queue_name
end
end

def start_runner
if ENV['MIQ_SPAWN_WORKERS'] || !Process.respond_to?(:fork)
start_runner_via_spawn
else
start_runner_via_fork
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mess up @carbonin 's containers fork?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but don't worry about that. The rebase is going to be a disaster either way 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

womp womp


def start_runner_via_fork
self.class.before_fork
pid = fork(:cow_friendly => true) do
self.class.after_fork
self.class::Runner.start_worker(worker_options)
exit!
end

Process.detach(pid)
pid
end

def self.build_command_line(guid)
command_line = "#{Gem.ruby} #{runner_script} --heartbeat --guid=#{guid} #{name}"
ENV['APPLIANCE'] ? "nice #{nice_increment} #{command_line}" : command_line
Expand All @@ -370,7 +327,7 @@ def self.runner_script
script
end

def start_runner_via_spawn
def start_runner
pid = Kernel.spawn(self.class.build_command_line(guid), [:out, :err] => [Rails.root.join("log", "evm.log"), "a"])
Process.detach(pid)
pid
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/bin/run_single_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
require File.expand_path("../../../config/environment", __dir__)

worker_class = worker_class.constantize
worker_class.before_fork
worker_class.preload_for_worker_role if worker_class.respond_to?(:preload_for_worker_role)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks @Fryguy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might soon not be needed since I think the only thing that actually makes use of this is the UIWorker, and that is for the UIConstants, if I am not mistaken. Might be something else that I am missing though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that, guess we configure the secret token in that line...

https://github.com/ManageIQ/manageiq/blob/874cad6/app/models/mixins/miq_web_server_worker_mixin.rb#L25-L27

Nevermind... 😞

Copy link
Member

@NickLaMuro NickLaMuro Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry... grepping and responding as I find new stuff... sorry for the spam, but found something else:

https://github.com/ManageIQ/manageiq/blob/874cad6/lib/vmdb/initializer.rb#L13

Does that mean we do this twice anyway?

(Also, for what it is worth, this code isn't hidden somewhere else in our other repos it seems: https://github.com/search?q=org%3AManageIQ+preload_for_worker_role&type=Code )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean we do this twice anyway?

@NickLaMuro Good catch. We gate that if you're a Rails::Server. We can probably better consolidate these in followup PRs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably better consolidate these in followup PRs

Yup, for sure, and wasn't trying to suggest we do that in this PR. The before_fork stuff was just something I noticed when I original was getting the run_single_worker script going, so thought I would make a mention of it since you are working on it here.

unless options[:dry_run]
create_options = {:pid => Process.pid}
runner_options = {}
Expand Down