Skip to content

Commit

Permalink
Ensure workers and miq_worker rows match
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Nov 2, 2023
1 parent b9698d8 commit f368402
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
7 changes: 3 additions & 4 deletions app/models/miq_server/worker_management/kubernetes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ def sync_from_system
# we only have to sync the list of pods and deployments once
ensure_kube_monitors_started if my_server_is_primary?

# Before syncing the workers check for any orphaned worker rows that don't have
# a current pod and delete them
cleanup_orphaned_worker_rows

# Update worker deployments with updated settings such as cpu/memory limits
sync_deployment_settings
end
Expand Down Expand Up @@ -50,6 +46,9 @@ def cleanup_orphaned_worker_rows
end
end

def cleanup_orphaned_workers
end

def cleanup_failed_workers
super

Expand Down
12 changes: 12 additions & 0 deletions app/models/miq_server/worker_management/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def monitor_workers
# Cache a list of the native objects backing the miq_workers (e.g.: pods, services, or processes)
sync_from_system

cleanup_orphaned_worker_rows

cleanup_orphaned_workers

sync_monitor

# Sync the workers after sync'ing the child worker settings
Expand Down Expand Up @@ -48,6 +52,14 @@ def sync_workers
end
end

def cleanup_orphaned_worker_rows
raise NotImplementedError, "cleanup_orphaned_worker_rows must be implemented in a subclass"
end

def cleanup_orphaned_workers
raise NotImplementedError, "cleanup_orphaned_workers must be implemented in a subclass"
end

def cleanup_failed_workers
check_pending_stop
clean_worker_records
Expand Down
29 changes: 27 additions & 2 deletions app/models/miq_server/worker_management/process.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
class MiqServer::WorkerManagement::Process < MiqServer::WorkerManagement
def sync_from_system
require "sys/proctable"
self.miq_processes = Sys::ProcTable.ps.select { |proc| proc.ppid == my_server.pid }
@miq_processes_by_pid = Sys::ProcTable.ps.select { |proc| proc.ppid == my_server.pid }.index_by(&:pid)
end

def sync_starting_workers
MiqWorker.find_all_starting.to_a
end

def cleanup_orphaned_worker_rows
orphaned_rows = miq_workers.where.not(:pid => miq_pids)
return if orphaned_rows.empty?

_log.warn("Removing orphaned worker rows without corresponding processes: #{orphaned_rows.collect(&:pid).inspect}")
orphaned_rows.destroy_all
end

def cleanup_orphaned_workers
worker_pids = miq_workers.pluck(:pid)
orphaned_workers = miq_processes.reject { |process| worker_pids.include?(process.pid) }
return if orphaned_workers.empty?

_log.warn("Removing orphaned processes without corresponding worker rows: #{orphaned_workers.collect(&:pid).inspect}")
orphaned_workers.each(&:kill)
end

def monitor_workers
super

Expand Down Expand Up @@ -70,5 +87,13 @@ def validate_worker(worker)

private

attr_accessor :miq_processes
attr_accessor :miq_processes_by_pid

def miq_processes
miq_processes_by_pid.values
end

def miq_pids
miq_processes_by_pid.keys
end
end
6 changes: 6 additions & 0 deletions app/models/miq_server/worker_management/systemd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def sync_starting_workers
starting
end

def cleanup_orphaned_worker_rows
end

def cleanup_orphaned_workers
end

def cleanup_failed_workers
super

Expand Down

0 comments on commit f368402

Please sign in to comment.