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

[WIP] Ensure workers and miq_worker rows match #22771

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 8 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
Comment on lines 15 to 16
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO I think this has to be after cleanup_orphaned_worker_rows, and doesn't make sense IMO to have sync_deployment_settings in sync_from_system since this is supposed to just be "find what is out there"

end
Expand Down Expand Up @@ -54,6 +50,14 @@ def cleanup_orphaned_worker_rows
end
end

def cleanup_orphaned_workers
orphaned_pods = current_pods.keys - miq_workers.pluck(:system_uid)
return if orphaned_pods.empty?

# TODO destroy orphaned pods
orphaned_pods.each { |_pod| }
end
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment in here just to describe why this is empty?

Copy link
Member

Choose a reason for hiding this comment

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

Same comment for the other "empty" methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

It just isn't implemented yet, I'm testing with Process first


def cleanup_failed_workers
super

Expand Down
14 changes: 14 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,12 @@ def monitor_workers
# Cache a list of the native objects backing the miq_workers (e.g.: pods, services, or processes)
sync_from_system

# Cleanup any worker rows that don't have running workers
cleanup_orphaned_worker_rows

# Cleanup any workers that don't have corresponding miq_workers rows
cleanup_orphaned_workers

sync_monitor

# Sync the workers after sync'ing the child worker settings
Expand Down Expand Up @@ -49,6 +55,14 @@ def sync_workers
sync_stopping_workers
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
28 changes: 26 additions & 2 deletions app/models/miq_server/worker_management/process.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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
Expand All @@ -12,6 +12,22 @@ def sync_stopping_workers
MiqWorker.find_all_stopping.to_a
end

def cleanup_orphaned_worker_rows
orphaned_rows = miq_workers.where.not(:pid => miq_pids)
Copy link
Member

Choose a reason for hiding this comment

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

We use system_uid column in kubernetes - is that column populated with pid in process mode? If so, may be worth using the same column for consistency.

Copy link
Member Author

Choose a reason for hiding this comment

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

It isn't set for process or systemd but we should do that for consistency. That's going to be another PR but we can probably get that in first and make use of it here.

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
orphaned_workers = miq_pids - miq_workers.pluck(:pid)
return if orphaned_workers.empty?

_log.warn("Removing orphaned processes without corresponding worker rows: #{orphaned_workers.inspect}")
orphaned_workers.each { |pid| ::Process.kill(9, pid) }
end

def monitor_workers
super

Expand Down Expand Up @@ -74,5 +90,13 @@ def validate_worker(worker)

private

attr_accessor :miq_processes
attr_reader :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 @@ -29,6 +29,12 @@ def sync_stopping_workers
end
end

def cleanup_orphaned_worker_rows
end

def cleanup_orphaned_workers
end

def cleanup_failed_workers
super

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@

context "#sync_from_system" do
context "#ensure_kube_monitors_started" do
it "podified, ensures pod monitor started and orphaned rows are removed" do
it "podified, ensures pod monitor started" do
expect(server.worker_manager).to receive(:ensure_kube_monitors_started)
expect(server.worker_manager).to receive(:cleanup_orphaned_worker_rows)
server.worker_manager.sync_from_system
end
end
Expand Down