Skip to content

Commit

Permalink
Merge pull request #42 from agrare/use_unix_socket_for_drb_server
Browse files Browse the repository at this point in the history
Use a UNIX socket for the DRb server
  • Loading branch information
Fryguy authored Jul 23, 2018
2 parents a8bafc2 + dcf1d59 commit 7672ec3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
12 changes: 9 additions & 3 deletions lib/VMwareWebService/MiqVimBroker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'drb'
require 'drb/acl'
require 'sync'
require 'tmpdir'

require 'VMwareWebService/MiqVimInventory'
require 'VMwareWebService/VimTypes'
Expand Down Expand Up @@ -32,7 +33,7 @@ def to_obj(ref)
@@maxWait = 60
@@maxObjects = 250

def initialize(mode = :client, port = 9001)
def initialize(mode = :client, uri = nil)
if mode == :client
require 'rubygems'
require 'httpclient' # needed for exception classes
Expand Down Expand Up @@ -66,7 +67,8 @@ def new_with(uri, ref)
rescue DRb::DRbServerNotFound
DRb.start_service
end
@broker = DRbObject.new(nil, "druby://127.0.0.1:#{port}")

@broker = DRbObject.new(nil, uri)
elsif mode == :server
require 'timeout'
require 'VMwareWebService/broker_timeout'
Expand Down Expand Up @@ -95,7 +97,11 @@ def new_with(uri, ref)

acl = ACL.new(%w( deny all allow 127.0.0.1/32 ))
DRb.install_acl(acl)
DRb.start_service("druby://127.0.0.1:#{port}", self, :idconv => VimBrokerIdConv.new)

Dir::Tmpname.create('MiqVimBroker', nil) do |path|
DRb.start_service("drbunix://#{path}", self, :idconv => VimBrokerIdConv.new)
FileUtils.chmod(0o750, path)
end
else
raise "MiqVimBroker: unrecognized mode #{mode}"
end
Expand Down
40 changes: 20 additions & 20 deletions lib/VMwareWebService/miq_fault_tolerant_vim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ def initialize(*options)

@use_broker = options.key?(:use_broker) ? options[:use_broker] : true
if @use_broker
if options[:vim_broker_drb_port].respond_to?(:call)
@vim_broker_drb_port_method = options[:vim_broker_drb_port]
@vim_broker_drb_port = @vim_broker_drb_port_method.call
if options[:vim_broker_drb_uri].respond_to?(:call)
@vim_broker_drb_uri_method = options[:vim_broker_drb_uri]
@vim_broker_drb_uri = @vim_broker_drb_uri_method.call
else
@vim_broker_drb_port_method = nil
@vim_broker_drb_port = options[:vim_broker_drb_port]
@vim_broker_drb_uri_method = nil
@vim_broker_drb_uri = options[:vim_broker_drb_uri]
end
end

begin
_connect
rescue MiqException::MiqVimBrokerUnavailable
retry if _handle_broker_port_change
retry if _handle_broker_uri_change
raise
end
end
Expand Down Expand Up @@ -57,8 +57,8 @@ def _use_broker
@use_broker
end

def _vim_broker_drb_port
@vim_broker_drb_port
def _vim_broker_drb_uri
@vim_broker_drb_uri
end

def _reconnect
Expand Down Expand Up @@ -116,7 +116,7 @@ def _execute_with_broker(state)
# 'DRb::DRbConnError: druby://localhost:9001 - #<Errno::ECONNREFUSED: Connection refused - connect(2)>'
#
if err.kind_of?(DRb::DRbConnError)
if _handle_broker_port_change
if _handle_broker_uri_change
_connect
retry
end
Expand Down Expand Up @@ -204,9 +204,9 @@ def _broker_client

def _connect_broker_client
return unless $vim_broker_client.nil?
raise MiqException::MiqVimBrokerUnavailable, "Broker is not available (not running)." if @vim_broker_drb_port.blank?
$vim_broker_client = MiqVimBroker.new(:client, @vim_broker_drb_port)
$vim_broker_client_port = @vim_broker_drb_port
raise MiqException::MiqVimBrokerUnavailable, "Broker is not available (not running)." if @vim_broker_drb_uri.blank?
$vim_broker_client = MiqVimBroker.new(:client, @vim_broker_drb_uri)
$vim_broker_client_uri = @vim_broker_drb_uri
end

def _disconnect_broker_client
Expand All @@ -229,16 +229,16 @@ def _classify_error(err)
err.to_s
end

def _handle_broker_port_change
log_header = "MIQ(#{self.class.name}._handle_broker_port_change)"
if @vim_broker_drb_port_method
new_port = @vim_broker_drb_port_method.call
if new_port != $vim_broker_client_port
$log.warn("#{log_header} Retrying communication via VimBroker to [#{_ems_address}] because [Broker DRb Port changed from #{$vim_broker_client_port} to #{new_port}]") if $log && !new_port.blank?
@vim_broker_drb_port = new_port
def _handle_broker_uri_change
log_header = "MIQ(#{self.class.name}._handle_broker_uri_change)"
if @vim_broker_drb_uri_method
new_uri = @vim_broker_drb_uri_method.call
if new_uri != $vim_broker_client_uri
$log.warn("#{log_header} Retrying communication via VimBroker to [#{_ems_address}] because [Broker DRb URI changed from #{$vim_broker_client_uri} to #{new_uri}]") if $log && !new_uri.blank?
@vim_broker_drb_uri = new_uri
_disconnect
_disconnect_broker_client
return (new_port.blank? ? false : true)
return (new_uri.blank? ? false : true)
end
end
false
Expand Down

0 comments on commit 7672ec3

Please sign in to comment.