Skip to content

Commit

Permalink
Merge pull request ManageIQ#13856 from jrafanie/set_database_applicat…
Browse files Browse the repository at this point in the history
…ion_name

Set database application name in workers and server
(cherry picked from commit f241df4)
  • Loading branch information
gtanzillo authored and jrafanie committed Apr 26, 2017
1 parent 07a82c2 commit f283e06
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/models/miq_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,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 @@ -472,6 +472,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 @@ -142,6 +146,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 @@ -469,12 +474,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 @@ -78,6 +79,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

0 comments on commit f283e06

Please sign in to comment.