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

Set database application name in workers and server #13856

Merged
merged 2 commits into from
Apr 25, 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: 1 addition & 0 deletions app/models/embedded_ansible_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def update_embedded_ansible_provider

# Base class methods we override since we don't have a separate process. We might want to make these opt-in features in the base class that this subclass can choose to opt-out.
def set_process_title; end
def set_database_application_name; end
def set_connection_pool_size; end
def message_sync_active_roles(*_args); end
def message_sync_config(*_args); end
Expand Down
4 changes: 4 additions & 0 deletions app/models/miq_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ def who_am_i
@who_am_i ||= "#{name} #{my_zone} #{self.class.name} #{id}"
end

def database_application_name
"MIQ #{Process.pid} Server[#{compressed_id}], #{zone.name}[#{zone.compressed_id}]".truncate(64)
end

def is_local?
guid == MiqServer.my_guid
end
Expand Down
15 changes: 15 additions & 0 deletions app/models/miq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,21 @@ def friendly_name

delegate :normalized_type, :to => :class

def abbreviated_class_name
type.sub(/^ManageIQ::Providers::/, "")
end

def minimal_class_name
abbreviated_class_name
.sub(/Miq/, "")
.sub(/Worker/, "")
end

def database_application_name
zone = MiqServer.my_server.zone
"MIQ #{Process.pid} #{minimal_class_name}[#{compressed_id}], s[#{miq_server.compressed_id}], #{zone.name}[#{zone.compressed_id}]".truncate(64)
end

def format_full_log_msg
"Worker [#{self.class}] with ID: [#{id}], PID: [#{pid}], GUID: [#{guid}]"
end
Expand Down
16 changes: 12 additions & 4 deletions app/models/miq_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ def initialize(cfg = {})
def worker_initialization
starting_worker_record
set_process_title

# Sync the config and roles early since heartbeats and logging require the configuration
sync_active_roles
sync_config

set_connection_pool_size
end

# More process specific stuff :-(
def set_database_application_name
ArApplicationName.name = @worker.database_application_name
end

def set_connection_pool_size
cur_size = ActiveRecord::Base.connection_pool.instance_variable_get(:@size)
new_size = worker_settings[:connection_pool_size] || cur_size
Expand Down Expand Up @@ -144,6 +148,7 @@ def recover_from_temporary_failure
end

def prepare
set_database_application_name
ObjectSpace.garbage_collect
started_worker_record
do_wait_for_worker_monitor if self.class.wait_for_worker_monitor?
Expand Down Expand Up @@ -473,12 +478,15 @@ def clean_broker_connection
_log.info("#{log_prefix} Releasing any broker connections for pid: [#{Process.pid}], ERROR: #{err.message}")
end

def set_process_title
type = @worker.type.sub(/^ManageIQ::Providers::/, "")
def process_title
type = @worker.abbreviated_class_name
title = "#{MiqWorker::PROCESS_TITLE_PREFIX} #{type} id: #{@worker.id}"
title << ", queue: #{@worker.queue_name}" if @worker.queue_name
title << ", uri: #{@worker.uri}" if @worker.uri
title
end

Process.setproctitle(title)
def set_process_title
Process.setproctitle(process_title)
end
end
16 changes: 16 additions & 0 deletions lib/extensions/ar_application_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module ArApplicationName
# We need to set the PGAPPNAME env variable and force the 'pg' gem objects to be
# recreated with this env variable set. This is done by disconnecting all of the
# connections in the pool. We do this because reconnect! on an instance of the
# AR adapter created prior to our change to PGAPPNAME will have old connection
# options, which will reset our application_name.
#
# Because we fork workers from the server, if we don't disconnect the pool,
# any call to reconnect! on a connection will cause the worker's connection
# to have the server's application_name.
def self.name=(name)
# TODO: this is postgresql specific
ENV['PGAPPNAME'] = name
ActiveRecord::Base.connection_pool.disconnect!
end
end
9 changes: 9 additions & 0 deletions lib/workers/evm_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def start

PidFile.create(MiqServer.pidfile)
set_process_title
set_database_application_name
MiqServer.start
rescue Interrupt => e
process_hard_signal(e.message)
Expand All @@ -77,6 +78,14 @@ def set_process_title
Process.setproctitle(SERVER_PROCESS_TITLE) if Process.respond_to?(:setproctitle)
end

def set_database_application_name
ArApplicationName.name = database_application_name
end

def database_application_name
MiqServer.my_server.database_application_name
end

def self.start(*args)
# Parse the args into the global config variable
cfg = {}
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/workers/evm_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

before do
allow(MiqServer).to receive_messages(:running? => false)
allow(server).to receive(:set_database_application_name)
allow(server).to receive(:set_process_title)
allow(PidFile).to receive(:create)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/models/miq_schedule_worker/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@worker1 = FactoryGirl.create(:miq_worker, :status => MiqWorker::STATUS_STOPPED)
@dispatch1 = FactoryGirl.create(:miq_queue, {:zone => @zone1.name, :handler_type => @worker1.class.name, :handler_id => @worker1.id}.merge(@opts))

@zone2 = FactoryGirl.create(:zone, :name => 'zone2')
@zone2 = FactoryGirl.create(:zone)
@worker2 = FactoryGirl.create(:miq_worker, :status => MiqWorker::STATUS_STOPPED)

allow(MiqServer).to receive(:my_zone).and_return(@zone1.name)
Expand Down