Skip to content

Commit

Permalink
PAUSED: Use git resume to continue working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Nov 2, 2021
1 parent 3773816 commit 29d751e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 111 deletions.
10 changes: 2 additions & 8 deletions app/models/miq_server/worker_management/kubernetes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def sync_monitor
def sync_from_system
# All miq_server instances have to reside on the same Kubernetes cluster, so
# we only have to sync the list of pods and deployments once
ensure_kube_monitors_started if my_server_is_primary?
ensure_kube_monitors_started

# Before syncing the workers check for any orphaned worker rows that don't have
# a current pod and delete them
Expand Down Expand Up @@ -66,7 +66,7 @@ def sync_deployment_settings

def podified_miq_workers
# Cockpit is a threaded worker in the orchestrator that spins off a process it monitors and isn't a pod worker.
miq_workers.where.not(:type => %w[MiqCockpitWsWorker])
MiqWorker.in_my_region.where.not(:type => %w[MiqCockpitWsWorker])
end

def deployment_resource_constraints_changed?(worker)
Expand Down Expand Up @@ -94,12 +94,6 @@ def constraints_changed?(current, desired)

private

# In podified there is only one "primary" miq_server whose zone is "default", the
# other miq_server instances are simply to allow for additional zones
def my_server_is_primary?
my_server.zone&.name == "default"
end

def cpu_value_eql?(current, desired)
# Convert to millicores if not already converted: "1" -> 1000; "1000m" -> 1000
current = current.to_s[-1] == "m" ? current.to_f : current.to_f * 1000
Expand Down
109 changes: 6 additions & 103 deletions lib/workers/evm_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ class EvmServer

SERVER_PROCESS_TITLE = 'MIQ Server'.freeze

attr_accessor :servers_to_monitor

def initialize
$log ||= Rails.logger
@servers_to_monitor = servers_from_db
end

def start
Expand All @@ -27,83 +24,31 @@ def start
EvmDatabase.seed_primordial
check_migrations_up_to_date

start_servers
monitor_servers
start_server
monitor
rescue Interrupt => e
_log.info("Received #{e.message} signal, killing server")
kill_servers
kill_server
exit 1
rescue SignalException => e
_log.info("Received #{e.message} signal, shutting down server")
stop_servers
stop_server
exit 0
end

def start_servers
@servers_to_monitor = servers_from_db if servers_to_monitor.empty?

as_each_server { start_server }
end

def monitor_servers
loop do
refresh_servers_to_monitor
as_each_server { monitor }
sleep ::Settings.server.monitor_poll.to_i_with_method
end
end

def stop_servers
as_each_server { @current_server.shutdown }
end

def kill_servers
as_each_server do
@current_server.worker_manager.kill_all_workers
@current_server.update(:stopped_on => Time.now.utc, :status => "killed", :is_master => false)
end
end

def refresh_servers_to_monitor
servers_to_start = servers_from_db - servers_to_monitor
servers_to_shutdown = servers_to_monitor - servers_from_db

servers_to_start.each do |s|
servers_to_monitor << s
impersonate_server(s)
start_server
end

servers_to_shutdown.each do |s|
servers_to_monitor.delete(s)
s.shutdown
end
end

def self.start(*args)
new.start
end

private

def servers_from_db
my_server = MiqServer.my_server(true)

if MiqEnvironment::Command.is_podified?
# Ensure that the "primary" miq_server is first in the list of servers.
# This ensures that any work that is only done on the primary is completed
# before processing the rest of the servers.
MiqServer.in_my_region.to_a.unshift(my_server).uniq.compact
else
[my_server].compact
end
end

def set_process_title
Process.setproctitle(SERVER_PROCESS_TITLE) if Process.respond_to?(:setproctitle)
end

def start_server
@current_server = MiqServer.my_server(true)

Vmdb::Settings.activate

save_local_network_info
Expand Down Expand Up @@ -254,46 +199,4 @@ def start_workers
#############################################################
@current_server.start_workers
end

######################################################################
# Warning:
#
# The following methods can lead to unexpected (and likely unpleasant)
# behavior if used out of the scope of the orchestrator process.
#
# They change the global state which is used to determine the current
# server's identity. This intentionally will alter the values of calls
# such as MiqServer.my_server and MiqServer.my_guid, and also the
# contents of the global ::Settings constant.
######################################################################
def as_each_server
initial_server = @current_server
servers_to_monitor.each do |s|
impersonate_server(s)
yield
end
ensure
clear_server_caches if @current_server != initial_server
end

def impersonate_server(s)
return if s == @current_server

_log.info("Impersonating server - id: #{s.id}, guid: #{s.guid}")

MiqServer.my_server_clear_cache
MiqServer.my_guid = s.guid

# It is important that we continue to use the same server instance here.
# A lot of "global" state is stored in instance variables on the server.
@current_server = s
Vmdb::Settings.reset_settings_constant(s.settings_for_resource)
end

def clear_server_caches
MiqServer.my_guid = nil
MiqServer.my_server_clear_cache
# Use Vmdb::Settings.for_resource(:my_server) here as MiqServer.my_server might be nil
Vmdb::Settings.reset_settings_constant(Vmdb::Settings.for_resource(:my_server))
end
end

0 comments on commit 29d751e

Please sign in to comment.