-
Notifications
You must be signed in to change notification settings - Fork 898
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
Modularize MiqServer #15014
Closed
Closed
Modularize MiqServer #15014
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7214dac
Remove MiqServer::WorkerManagement::Monitor::Kill
NickLaMuro 896e8b6
Move core MiqServer constants into a module
NickLaMuro 1752ade
Adds MiqServerBaseMethods
NickLaMuro 6621ea7
Adds MiqServerWorkerManagementBase
NickLaMuro 41d2f08
Moves MiqServer::QueueManagement to MiqServerQueueManagement
NickLaMuro 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module MiqServerBaseConstants | ||
RUN_AT_STARTUP = %w(MiqRegion MiqWorker MiqQueue MiqReportResult).freeze | ||
|
||
STATUS_STARTING = 'starting'.freeze | ||
STATUS_STARTED = 'started'.freeze | ||
STATUS_RESTARTING = 'restarting'.freeze | ||
STATUS_STOPPED = 'stopped'.freeze | ||
STATUS_QUIESCE = 'quiesce'.freeze | ||
STATUS_NOT_RESPONDING = 'not responding'.freeze | ||
STATUS_KILLED = 'killed'.freeze | ||
|
||
STATUSES_STOPPED = [STATUS_STOPPED, STATUS_KILLED].freeze | ||
STATUSES_ACTIVE = [STATUS_STARTING, STATUS_STARTED].freeze | ||
STATUSES_ALIVE = STATUSES_ACTIVE + [STATUS_RESTARTING, STATUS_QUIESCE].freeze | ||
|
||
RESTART_EXIT_STATUS = 123 | ||
end |
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,96 @@ | ||
require 'util/extensions/miq-module' | ||
|
||
module MiqServerBaseMethods | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
cattr_accessor :my_guid_cache | ||
|
||
cache_with_timeout(:my_server) { find_by(:guid => my_guid) } | ||
end | ||
|
||
module ClassMethods | ||
# | ||
# Zone and Role methods | ||
# | ||
def my_guid | ||
my_guid_cache ||= begin | ||
guid_file = Rails.root.join("GUID") | ||
File.write(guid_file, SecureRandom.uuid) unless File.exist?(guid_file) | ||
File.read(guid_file).strip | ||
end | ||
end | ||
|
||
def pidfile | ||
@pidfile ||= Rails.root.join("tmp", "pids", "evm.pid") | ||
end | ||
|
||
def running? | ||
p = PidFile.new(pidfile) | ||
p.running? ? p.pid : false | ||
end | ||
|
||
def kill | ||
svr = my_server(true) | ||
svr.kill unless svr.nil? | ||
PidFile.new(pidfile).remove | ||
end | ||
|
||
def stop(sync = false) | ||
svr = my_server(true) rescue nil | ||
svr.stop(sync) unless svr.nil? | ||
PidFile.new(pidfile).remove | ||
end | ||
end | ||
|
||
def kill | ||
# Kill all the workers of this server | ||
kill_all_workers | ||
|
||
# Then kill this server | ||
_log.info("initiated for #{format_full_log_msg}") | ||
update_attributes(:stopped_on => Time.now.utc, :status => "killed", :is_master => false) | ||
(pid == Process.pid) ? shutdown_and_exit : Process.kill(9, pid) | ||
end | ||
|
||
def kill_all_workers | ||
return unless is_local? | ||
|
||
killed_workers = [] | ||
miq_workers.each do |w| | ||
next unless MiqWorker::STATUSES_CURRENT_OR_STARTING.include?(w.status) | ||
|
||
w.kill | ||
worker_delete(w.pid) | ||
killed_workers << w | ||
end | ||
miq_workers.delete(*killed_workers) unless killed_workers.empty? | ||
end | ||
|
||
def stop(sync = false) | ||
return if stopped? | ||
|
||
shutdown_and_exit_queue | ||
wait_for_stopped if sync | ||
end | ||
|
||
def wait_for_stopped | ||
loop do | ||
reload | ||
break if stopped? | ||
sleep stop_poll | ||
end | ||
end | ||
|
||
def is_local? | ||
guid == MiqServer.my_guid | ||
end | ||
|
||
def is_remote? | ||
!is_local? | ||
end | ||
|
||
def stopped? | ||
self.class::STATUSES_STOPPED.include?(status) | ||
end | ||
end |
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 |
---|---|---|
@@ -1,47 +1,11 @@ | ||
require 'miq_server/worker_management_base' | ||
|
||
module MiqServer::WorkerManagement | ||
extend ActiveSupport::Concern | ||
|
||
include_concern 'Dequeue' | ||
include_concern 'Heartbeat' | ||
include_concern 'Monitor' | ||
|
||
included do | ||
has_many :miq_workers | ||
end | ||
|
||
module ClassMethods | ||
def kill_all_workers | ||
svr = my_server(true) | ||
svr.kill_all_workers unless svr.nil? | ||
end | ||
end | ||
|
||
def setup_drb_variables | ||
@workers_lock = Sync.new | ||
@workers = {} | ||
|
||
@queue_messages_lock = Sync.new | ||
@queue_messages = {} | ||
end | ||
|
||
def start_drb_server | ||
require 'drb' | ||
require 'drb/acl' | ||
|
||
setup_drb_variables | ||
|
||
acl = ACL.new(%w( deny all allow 127.0.0.1/32 )) | ||
DRb.install_acl(acl) | ||
|
||
drb = DRb.start_service("druby://127.0.0.1:0", self) | ||
update_attributes(:drb_uri => drb.uri) | ||
end | ||
|
||
def worker_add(worker_pid) | ||
@workers_lock.synchronize(:EX) { @workers[worker_pid] ||= {} } unless @workers_lock.nil? | ||
end | ||
|
||
def worker_delete(worker_pid) | ||
@workers_lock.synchronize(:EX) { @workers.delete(worker_pid) } unless @workers_lock.nil? | ||
end | ||
include MiqServerWorkerManagementBase | ||
end |
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 was deleted.
Oops, something went wrong.
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.
These should be
MiqServer::BaseConstants
, etc. based on your file structure and that there are other modules already using that pattern. Also, the requires at the top probably won't be needed then as autoload should pick them up at the correct location.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.
This is not possible, but I guess I didn't explain (thought I did somewhere, but I will make sure I add to the description at some point).
Basically, it boils down to this:
MiqServer
is a class that inherits fromApplicationRecord
, not a module, and because of load ordering, that always gets loaded first before any of the sub modules in theapp/models/miq_server
folder. For what I am doing with #14916, I don't want to have portions of this requireApplicationRecord
to be loaded, so this needs to be it's own module, but because of that, it can't live within the same namespace. Here is why:If we were to do it in the inverse, then we would have to define
MiqServer
as a class inheriting fromApplicationRecord
, which doesn't work with #14916 where I am trying to define it as vanilla active record.I will push what I have to #14916 where I implement this and the other relevant PRs so you can see how I plan to use this. That commit (and the rest of the PR really...) is very POC and temporary at the moment, but it should provide an idea for why I did it this way.