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
# 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'
    #
    # 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.



137
138
139
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 137

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



129
130
131
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 129

def self.cacheScope
  @@cacheScope
end

.cacheScope=(val) ⇒ Object



133
134
135
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 133

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

.connectionKey(server, username) ⇒ Object



355
356
357
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 355

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

.debugUpdatesObject



260
261
262
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 260

def self.debugUpdates
  @@debugUpdates
end

.debugUpdates=(val) ⇒ Object



256
257
258
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 256

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

.maxObjectsObject



331
332
333
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 331

def self.maxObjects
  @@maxObjects
end

.maxObjects=(val) ⇒ Object



327
328
329
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 327

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

.maxWaitObject



323
324
325
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 323

def self.maxWait
  @@maxWait
end

.maxWait=(val) ⇒ Object



319
320
321
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 319

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

.notifyMethodObject



281
282
283
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 281

def self.notifyMethod
  @@notifyMethod
end

.notifyMethod=(val) ⇒ Object



277
278
279
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 277

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

.preLoadObject



252
253
254
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 252

def self.preLoad
  @@preLoad
end

.preLoad=(val) ⇒ Object



248
249
250
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 248

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

.removeSelector(selName) ⇒ Object



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

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.



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

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



302
303
304
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 302

def self.updateDelay
  @@updateDelay
end

.updateDelay=(val) ⇒ Object



298
299
300
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 298

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

Instance Method Details

#config_lockObject

Can be overridden by BrokerSyncDebug.



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

def config_lock
  Sync.new
end

#connection_lockObject

Can be overridden by BrokerSyncDebug.



105
106
107
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 105

def connection_lock
  Sync.new
end

#connectionInfoObject



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

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



359
360
361
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 359

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

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



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

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



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

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.



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

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



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

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



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

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



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

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



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

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.



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

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



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

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

#releaseSession(sessionId) ⇒ Object



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

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



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

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.



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

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



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

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)


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

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.



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

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



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

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.



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

def sync_for_drb
  Sync.new
end

#sync_for_drb_drbconnObject

Can be overridden by BrokerSyncDebug.



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

def sync_for_drb_drbconn
  Sync.new
end

#sync_for_lock_hash(_key) ⇒ Object

Can be overridden by BrokerSyncDebug.



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

def sync_for_lock_hash(_key)
  Sync.new
end

#updateDelay=(val) ⇒ Object

Instance method changes the updateDelay value of existing connections.



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

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