Skip to content

Commit

Permalink
Set database_application_name in workers and server.
Browse files Browse the repository at this point in the history
Previously, pg_stat_activity would look like this:

```
vmdb_production=# select pid, application_name from pg_stat_activity;
 pid   | application_name
 ------+--------------------------------------------------
 13770 | /var/www/miq/vmdb/lib/workers/bin/evm_server.rb
 13950 | /var/www/miq/vmdb/lib/workers/bin/evm_server.rb
 13957 | /var/www/miq/vmdb/lib/workers/bin/evm_server.rb
 13969 | /var/www/miq/vmdb/lib/workers/bin/evm_server.rb
 13975 | /var/www/miq/vmdb/lib/workers/bin/evm_server.rb
 13984 | /var/www/miq/vmdb/lib/workers/bin/evm_server.rb
```

It now looks like this:

```
vmdb_development=# select pid, application_name from pg_stat_activity;
 pid  | application_name
------------------+------+-------------+----------------------
 5844 | MIQ 5835 Server[1r2], default[1r1]
 5888 | MIQ 5886 Generic[1r1111], s[1r2], default[1r1]
 5891 | MIQ 5889 Generic[1r1112], s[1r2], default[1r1]
 5894 | MIQ 5892 Priority[1r1113], s[1r2], default[1r1]
 5897 | MIQ 5895 Priority[1r1114], s[1r2], default[1r1]
 5900 | MIQ 5898 Schedule[1r1115], s[1r2], default[1r1]
 5928 | MIQ 5926 EventHandler[1r1116], s[1r2], default[1r1]
 5932 | MIQ 5929 Reporting[1r1117], s[1r2], default[1r1]
 5934 | MIQ 5931 Reporting[1r1118], s[1r2], default[1r1]
 5943 | MIQ 5941 Ui[1r1120], s[1r2], default[1r1]
 5940 | MIQ 5935 Websocket[1r1119], s[1r2], default[1r1]
 5946 | MIQ 5944 WebService[1r1121], s[1r2], default[1r1]
 5964 | MIQ 5935 Websocket[1r1119], s[1r2], default[1r1]
 5965 | MIQ 5941 Ui[1r1120], s[1r2], default[1r1]
 5966 | MIQ 5944 WebService[1r1121], s[1r2], default[1r1]
```
  • Loading branch information
jrafanie committed Apr 17, 2017
1 parent 242af70 commit b0d765d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 2 deletions.
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
11 changes: 11 additions & 0 deletions app/models/miq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,17 @@ 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
7 changes: 6 additions & 1 deletion 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
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

0 comments on commit b0d765d

Please sign in to comment.