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, port = 9001) ⇒ MiqVimBroker

Returns a new instance of MiqVimBroker.



35
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
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 35

def initialize(mode = :client, port = 9001)
  if mode == :client
    require 'rubygems'
    require 'httpclient'  # needed for exception classes
    require 'VMwareWebService/MiqVimDump'
    require 'VMwareWebService/MiqVimVdlMod'
    require 'VMwareWebService/MiqVimDrbDebug'

    #
    # 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, "druby://127.0.0.1:#{port}")
  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)
    DRb.start_service("druby://127.0.0.1:#{port}", self, :idconv => VimBrokerIdConv.new)
  else
    raise "MiqVimBroker: unrecognized mode #{mode}"
  end
end

Instance Attribute Details

#cacheScopeObject

Returns the value of attribute cacheScope.



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

def cacheScope
  @cacheScope
end

#shuttingDownObject (readonly)

Returns the value of attribute shuttingDown.



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

def shuttingDown
  @shuttingDown
end

Class Method Details

.cacheScopeObject



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

def self.cacheScope
  @@cacheScope
end

.cacheScope=(val) ⇒ Object



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

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

.connectionKey(server, username) ⇒ Object



357
358
359
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 357

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

.debugUpdatesObject



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

def self.debugUpdates
  @@debugUpdates
end

.debugUpdates=(val) ⇒ Object



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

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

.maxObjectsObject



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

def self.maxObjects
  @@maxObjects
end

.maxObjects=(val) ⇒ Object



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

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

.maxWaitObject



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

def self.maxWait
  @@maxWait
end

.maxWait=(val) ⇒ Object



321
322
323
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 321

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

.notifyMethodObject



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

def self.notifyMethod
  @@notifyMethod
end

.notifyMethod=(val) ⇒ Object



279
280
281
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 279

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

.preLoadObject



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

def self.preLoad
  @@preLoad
end

.preLoad=(val) ⇒ Object



250
251
252
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 250

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

.removeSelector(selName) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 171

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.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 149

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



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

def self.updateDelay
  @@updateDelay
end

.updateDelay=(val) ⇒ Object



300
301
302
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 300

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

Instance Method Details

#config_lockObject

Can be overridden by BrokerSyncDebug.



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

def config_lock
  Sync.new
end

#connection_lockObject

Can be overridden by BrokerSyncDebug.



107
108
109
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 107

def connection_lock
  Sync.new
end

#connectionInfoObject



529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 529

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



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

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

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



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 375

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



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 395

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.



269
270
271
272
273
274
275
276
277
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 269

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



575
576
577
578
579
580
581
582
583
584
585
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 575

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



417
418
419
420
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
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 417

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



365
366
367
368
369
370
371
372
373
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 365

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



543
544
545
546
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
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 543

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.



290
291
292
293
294
295
296
297
298
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 290

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



347
348
349
350
351
352
353
354
355
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 347

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

#releaseSession(sessionId) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 337

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



455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 455

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.



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 474

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



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

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)


518
519
520
521
522
523
524
525
526
527
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 518

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.



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

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



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 499

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.



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

def sync_for_drb
  Sync.new
end

#sync_for_drb_drbconnObject

Can be overridden by BrokerSyncDebug.



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

def sync_for_drb_drbconn
  Sync.new
end

#sync_for_lock_hash(_key) ⇒ Object

Can be overridden by BrokerSyncDebug.



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

def sync_for_lock_hash(_key)
  Sync.new
end

#updateDelay=(val) ⇒ Object

Instance method changes the updateDelay value of existing connections.



311
312
313
314
315
316
317
318
319
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 311

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