Class: MiqVimBroker

Inherits:
Object
  • Object
show all
Defined in:
lib/VMwareWebService/MiqVimBroker.rb

Defined Under Namespace

Classes: VimBrokerIdConv

Constant Summary collapse

MB =
1048576
@@preLoad =
false
@@debugUpdates =
false
@@classModed =
false
@@notifyMethod =
nil
@@updateDelay =
nil
@@cacheScope =
:cache_scope_full
@@selectorHash =
{}
@@maxWait =
60
@@maxObjects =
250

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = :client, uri = nil) ⇒ MiqVimBroker

Returns a new instance of MiqVimBroker.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 36

def initialize(mode = :client, uri = nil)
  if mode == :client
    require 'rubygems'
    require 'httpclient'  # needed for exception classes
    require 'VMwareWebService/MiqVimDump'
    require 'VMwareWebService/MiqVimVdlMod'
    #
    # Modify the meta-class of DRb::DRbObject
    # so we can alias class methods
    #
    unless @@classModed
      class <<DRb::DRbObject
        alias_method :new_with_original, :new_with
        def new_with(uri, ref)
          obj = new_with_original(uri, ref)
          obj.extend(MiqVimVdlVcConnectionMod) if obj.respond_to?(:vdlVcConnection)
          obj.registerBrokerObj($$) if obj.respond_to?(:registerBrokerObj)
          (obj)
        end
      end

      DRb.instance_variable_set(:@mutex, Sync.new)
      DRb::DRbConn.instance_variable_set(:@mutex, Sync.new)
      @@classModed = true
    end

    @mode = :client

    # start DRb service if it hasn't been started before
    begin
      DRb.current_server
    rescue DRb::DRbServerNotFound
      DRb.start_service
    end

    @broker = DRbObject.new(nil, uri)
  elsif mode == :server
    require 'timeout'
    require 'VMwareWebService/broker_timeout'

    # Un-comment following 2 lines to enable Sync lock debugging.
    # require 'broker_sync_debug'
    # extend BrokerSyncDebug

    unless @@classModed
      DRb.instance_variable_set(:@mutex, sync_for_drb)
      DRb::DRbConn.instance_variable_set(:@mutex, sync_for_drb_drbconn)
    end
    @@classModed = true

    require 'VMwareWebService/MiqVimBrokerMods' # only needed by the server
    @mode = :server
    @shuttingDown = false

    @connectionHash = {}
    @lockHash = {}        # Protects individual @connectionHash entries
    @connectionLock = connection_lock # Protects @lockHash

    @configLock   = config_lock
    @selectorHash = @@selectorHash
    @cacheScope   = @@cacheScope

    acl = ACL.new(%w( deny all allow 127.0.0.1/32 ))
    DRb.install_acl(acl)

    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
end

Instance Attribute Details

#cacheScopeObject

Returns the value of attribute cacheScope.



143
144
145
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 143

def cacheScope
  @cacheScope
end

#shuttingDownObject (readonly)

Returns the value of attribute shuttingDown.



21
22
23
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 21

def shuttingDown
  @shuttingDown
end

Class Method Details

.cacheScopeObject



135
136
137
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 135

def self.cacheScope
  @@cacheScope
end

.cacheScope=(val) ⇒ Object



139
140
141
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 139

def self.cacheScope=(val)
  @@cacheScope = val
end

.connectionKey(server, username) ⇒ Object



361
362
363
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 361

def self.connectionKey(server, username)
  "#{server}_#{username}"
end

.debugUpdatesObject



266
267
268
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 266

def self.debugUpdates
  @@debugUpdates
end

.debugUpdates=(val) ⇒ Object



262
263
264
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 262

def self.debugUpdates=(val)
  @@debugUpdates = val
end

.maxObjectsObject



337
338
339
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 337

def self.maxObjects
  @@maxObjects
end

.maxObjects=(val) ⇒ Object



333
334
335
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 333

def self.maxObjects=(val)
  @@maxObjects = val
end

.maxWaitObject



329
330
331
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 329

def self.maxWait
  @@maxWait
end

.maxWait=(val) ⇒ Object



325
326
327
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 325

def self.maxWait=(val)
  @@maxWait = val
end

.notifyMethodObject



287
288
289
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 287

def self.notifyMethod
  @@notifyMethod
end

.notifyMethod=(val) ⇒ Object



283
284
285
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 283

def self.notifyMethod=(val)
  @@notifyMethod = val
end

.preLoadObject



258
259
260
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 258

def self.preLoad
  @@preLoad
end

.preLoad=(val) ⇒ Object



254
255
256
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 254

def self.preLoad=(val)
  @@preLoad = val
end

.removeSelector(selName) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 175

def self.removeSelector(selName)
  remKeys = nil
  if selName.kind_of?(Symbol)
    remKeys = [selName]
  elsif selName.kind_of?(Hash)
    remKeys = selName.keys
  elsif selName.kind_of?(Array)
    remKeys = selName
  else
    raise "MiqVimBroker.removeSelector: selName must be a symbol, hash or array, received #{selName.class}"
  end
  remKeys.each do |rk|
    raise "MiqVimBroker.removeSelector: keys must be symbols, received #{rk.class}" unless rk.kind_of?(Symbol)
  end

  remKeys.each do |rk|
    @@selectorHash.delete(rk)
  end
end

.setSelector(selSpec) ⇒ Object

The setSelector() and removeSelector() class methods, set the Selector specs that will be inherited by all subsequent MiqVimBroker instances.

Should only be called on the server-side.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 153

def self.setSelector(selSpec)
  raise "MiqVimBroker.setSelector: selSpec must be a hash, received #{selSpec.class}" unless selSpec.kind_of?(Hash)
  selSpec.each_key do |k|
    raise "MiqVimBroker.setSelector: selSpec keys must be symbols, received #{k.class}" unless k.kind_of?(Symbol)
  end
  ov = nil
  selSpec.each_value do |v|
    if v.kind_of?(Array)
      v.each do |vv|
        unless vv.kind_of?(String)
          ov = vv
          break
        end
      end
    else
      ov = v unless v.kind_of?(String)
    end
    raise "MiqVimBroker.setSelector: selSpec values must be strings or arrays of strings, received #{ov.class}" unless ov.nil?
  end
  @@selectorHash.merge!(selSpec)
end

.updateDelayObject



308
309
310
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 308

def self.updateDelay
  @@updateDelay
end

.updateDelay=(val) ⇒ Object



304
305
306
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 304

def self.updateDelay=(val)
  @@updateDelay = val
end

Instance Method Details

#config_lockObject

Can be overridden by BrokerSyncDebug.



116
117
118
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 116

def config_lock
  Sync.new
end

#connection_lockObject

Can be overridden by BrokerSyncDebug.



111
112
113
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 111

def connection_lock
  Sync.new
end

#connectionInfoObject



533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 533

def connectionInfo
  return @broker.connectionInfo if @mode == :client

  # server
  ra = []
  @connectionHash.keys.each do |key|
    @lockHash[key].synchronize(:SH) do
      next if (vim = @connectionHash[key]).nil?
      ra << [vim.server, vim.username]
    end
  end
  ra
end

#connectionKey(server, username) ⇒ Object



365
366
367
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 365

def connectionKey(server, username)
  self.class.connectionKey(server, username)
end

#connSync(mode, server, username, &block) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 379

def connSync(mode, server, username, &block)
  key = connectionKey(server, username)
  lock = lockHash(key)

  begin
    lock.sync_lock(mode)
    if block.arity < 1
      block.call
    elsif block.arity == 1
      block.call(key)
    elsif block.arity == 2
      block.call(key, @connectionHash[key])
    else
      raise "MiqVimBroker.connSync: unexpected number of block args: #{block.arity}"
    end
  ensure
    lock.sync_unlock
  end
end

#connTrySync(mode, server, username, &block) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 399

def connTrySync(mode, server, username, &block)
  key = connectionKey(server, username)
  lock = lockHash(key)

  begin
    locked = lock.sync_try_lock(mode)
    return unless locked

    if block.arity < 1
      block.call
    elsif block.arity == 1
      block.call(key)
    elsif block.arity == 2
      block.call(key, @connectionHash[key])
    else
      raise "MiqVimBroker.connTrySync: unexpected number of block args: #{block.arity}"
    end
  ensure
    lock.sync_unlock if locked
  end
end

#debugUpdates=(val) ⇒ Object

Instance method changes the debugUpdates value of existing connections.



273
274
275
276
277
278
279
280
281
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 273

def debugUpdates=(val)
  return if @mode == :client
  @connectionHash.keys.each do |key|
    @lockHash[key].synchronize(:SH) do
      next if (vim = @connectionHash[key]).nil?
      vim.debugUpdates = val
    end
  end
end

#forceGCObject



579
580
581
582
583
584
585
586
587
588
589
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 579

def forceGC
  if @mode == :client
    @broker.forceGC
    return
  end

  log_prefix = "MiqVimBroker.forceGC"
  $vim_log.info "#{log_prefix}: GC.start...Starting"
  GC.start
  $vim_log.info "#{log_prefix}: GC.start...Complete"
end

#getMiqVim(server, username, password) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 421

def getMiqVim(server, username, password)
  if @mode == :client
    vim = @broker.getMiqVim(server, username, password)
    vim.extend(MiqVimDump)
    vim.extend(MiqVimVdlConnectionMod)
    return(vim)
  else # :server
    connSync(:EX, server, username) do |key, vim|
      if vim
        $vim_log.info "MiqVimBroker.getMiqVim: found connection for #{key}"
        if vim.isAlive?
          $vim_log.info "MiqVimBroker.getMiqVim: returning existing connection for #{key}"
          return(vim)
        end
        $vim_log.info "MiqVimBroker.getMiqVim: existing connection for #{key} not alive"
        removeMiqVimSS(key, vim)
      end
      begin
        vim = DMiqVim.new(server.untaint, username, password, self, @@preLoad, @@debugUpdates, @@notifyMethod, @cacheScope, @@maxWait, @@maxObjects)
        vim.updateDelay = @@updateDelay if @@updateDelay
        vim.setSelector(@selectorHash) unless @selectorHash.empty?
        $vim_log.info "MiqVimBroker.getMiqVim: returning new connection for #{key}"
        @connectionHash[key] = vim
        return(vim)
      rescue Exception => err
        $vim_log.error "MiqVimBroker.getMiqVim: failed to create new connection for #{key}"
        $vim_log.error "#{err.class}: #{err}"
        if $vim_log.debug?
          $vim_log.debug "Stack trace START"
          $vim_log.debug err.backtrace.join("\n")
          $vim_log.debug "Stack trace END"
        end
        raise err
      end
    end
  end # :server
end

#lockHash(key) ⇒ Object



369
370
371
372
373
374
375
376
377
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 369

def lockHash(key)
  @connectionLock.synchronize(:EX) do
    raise "MiqVimBroker is shutting down" if @shuttingDown
    #
    # Once set, @lockHash[key] doesn't change.
    #
    @lockHash[key] ||= sync_for_lock_hash(key)
  end
end

#logStatusObject



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 547

def logStatus
  if @mode == :client
    @broker.logStatus
    return
  end

  return unless $vim_log.info?

  # server
  $vim_log.info "MiqVimBroker status start"
  $vim_log.info "\tMiqVimBroker: Threads = #{Thread.list.length}"

  $vim_log.info "\tMiqVimBroker client object counts by type:"
  objectCounts.each { |t, c| $vim_log.info "\t\t#{t}: #{c}" }
  $vim_log.info "\tEnd MiqVimBroker client object counts by type"

  brokerCacheSz = 0

  $vim_log.info "\tMiqVimBroker open connections: #{@connectionHash.keys.length}"
  @connectionHash.keys.each do |k|
    @lockHash[k].synchronize(:SH) do
      next if (vim = @connectionHash[k]).nil?
      $vim_log.info "\t\tMiqVimBroker connection #{k} cache counts:"
      # vim.logCacheCounts("\t\t\t")
      brokerCacheSz += vim.cacheStats("\t\t\t#{k} - ")
      $vim_log.info "\t\tEnd MiqVimBroker connection #{k} cache counts"
    end
  end
  $vim_log.info "\tEnd MiqVimBroker open connections"
  $vim_log.info "MiqVimBroker status end - Total broker cache size = #{brokerCacheSz}"
end

#notifyMethod=(val) ⇒ Object

Instance method changes the notifyMethod value of existing connections.



294
295
296
297
298
299
300
301
302
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 294

def notifyMethod=(val)
  return if @mode == :client
  @connectionHash.keys.each do |key|
    @lockHash[key].synchronize(:SH) do
      next if (vim = @connectionHash[key]).nil?
      vim.notifyMethod = val
    end
  end
end

#objectCountsObject



351
352
353
354
355
356
357
358
359
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 351

def objectCounts
  if @mode == :client
    @broker.objectCounts
  else
    $miqBrokerObjRegistryLock.synchronize(:SH) do
      return $miqBrokerObjCounts.dup
    end
  end
end

#releaseSession(sessionId) ⇒ Object



341
342
343
344
345
346
347
348
349
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 341

def releaseSession(sessionId)
  if @mode == :client
    $vim_log.info "Client releaseSession: #{sessionId}"
    @broker.releaseSession(sessionId)
  else
    $vim_log.info "Server releaseSession: #{sessionId}"
    $miqBrokerObjRegistry[sessionId].dup.each(&:release)
  end
end

#removeMiqVim(server, username) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 459

def removeMiqVim(server, username)
  if @mode == :client
    @broker.removeMiqVim(server, username)
    return
  end

  # server
  log_prefix = "MiqVimBroker.removeMiqVim"
  $vim_log.info "#{log_prefix}: client request to remove connection (#{server}, #{username})...Starting"
  connSync(:EX, server, username) do |key, vim|
    removeMiqVimSS(key, vim)
  end
  $vim_log.info "#{log_prefix}: client request to remove connection (#{server}, #{username})...Comolete"
end

#removeMiqVimSS(key, vim) ⇒ Object

Server-side removal of VIM connection. Must be called within a connSync(:EX, server, username) context.



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 478

def removeMiqVimSS(key, vim)
  log_prefix = "MiqVimBroker.removeMiqVimSS"
  $vim_log.info "#{log_prefix}: removing connection for #{key}...Starting"

  if @shuttingDown
    $vim_log.info "#{log_prefix}: not removing connection for #{key} - broker shutting down"
    return
  end
  if vim.nil?
    $vim_log.info "#{log_prefix}: not removing connection for #{key} - connection not found"
    return
  end
  if vim.connectionRemoved?
    $vim_log.info "#{log_prefix}: not removing connection for #{key} - connection already removed"
    return
  end

  vim.shutdownConnection
  $miqBrokerObjRegistryByConn[key].dup.each(&:release)
  @connectionHash.delete(key)
  vim.connectionRemoved

  $vim_log.info "#{log_prefix}: removing connection for #{key}...Complete"
end

#removeSelector(selName) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 228

def removeSelector(selName)
  if @mode == :client
    return @broker.removeSelector(selName)
  end

  remKeys = nil
  if selName.kind_of?(Symbol)
    remKeys = [selName]
  elsif selName.kind_of?(Hash)
    remKeys = selName.keys
  elsif selName.kind_of?(Array)
    remKeys = selName
  else
    raise "removeSelector: selName must be a symbol, hash or array, received #{selName.class}"
  end
  remKeys.each do |rk|
    raise "removeSelector: keys must be symbols, received #{rk.class}" unless rk.kind_of?(Symbol)
  end

  @configLock.synchronize(:EX) do
    remKeys.each do |rk|
      @selectorHash.delete(rk)
    end
  end
end

#serverAlive?Boolean

Returns:

  • (Boolean)


522
523
524
525
526
527
528
529
530
531
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 522

def serverAlive?
  if @mode == :client
    begin
      return @broker.serverAlive?
    rescue DRb::DRbConnError
      return false
    end
  end
  true
end

#setSelector(selSpec) ⇒ Object

The setSelector() and removeSelector() instance methods, set the Selector specs that will be inherited by all subsequent connections.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 199

def setSelector(selSpec)
  if @mode == :client
    return @broker.setSelector(selSpec)
  end

  raise "setSelector: selSpec must be a hash, received #{selSpec.class}" unless selSpec.kind_of?(Hash)
  selSpec.each_key do |k|
    raise "setSelector: selSpec keys must be symbols, received #{k.class}" unless k.kind_of?(Symbol)
  end
  ov = nil
  selSpec.each_value do |v|
    if v.kind_of?(Array)
      v.each do |vv|
        unless vv.kind_of?(String)
          ov = vv
          break
        end
      end
    else
      ov = v unless v.kind_of?(String)
    end
    raise "setSelector: selSpec values must be strings or arrays of strings, received #{ov.class}" unless ov.nil?
  end

  @configLock.synchronize(:EX) do
    @selectorHash.merge!(selSpec)
  end
end

#shutdownObject



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 503

def shutdown
  raise "MiqVimBroker: shutdown cannot be called from client" if @mode == :client
  log_prefix = "MiqVimBroker.shutdown"
  $vim_log.info "#{log_prefix}...Starting"
  @connectionLock.synchronize(:EX) do
    @shuttingDown = true
  end
  @connectionHash.keys.each do |id|
    @lockHash[id].synchronize(:EX) do
      next if (vim = @connectionHash[id]).nil?
      vim.shutdownConnection
      @connectionHash.delete(id)
      vim.connectionRemoved
    end
  end
  DRb.stop_service
  $vim_log.info "#{log_prefix}...Complete"
end

#sync_for_drbObject

Can be overridden by BrokerSyncDebug.



131
132
133
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 131

def sync_for_drb
  Sync.new
end

#sync_for_drb_drbconnObject

Can be overridden by BrokerSyncDebug.



126
127
128
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 126

def sync_for_drb_drbconn
  Sync.new
end

#sync_for_lock_hash(_key) ⇒ Object

Can be overridden by BrokerSyncDebug.



121
122
123
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 121

def sync_for_lock_hash(_key)
  Sync.new
end

#updateDelay=(val) ⇒ Object

Instance method changes the updateDelay value of existing connections.



315
316
317
318
319
320
321
322
323
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 315

def updateDelay=(val)
  return if @mode == :client
  @connectionHash.keys.each do |key|
    @lockHash[key].synchronize(:SH) do
      next if (vim = @connectionHash[key]).nil?
      vim.updateDelay = val
    end
  end
end