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

    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.



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

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



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

def self.cacheScope
  @@cacheScope
end

.cacheScope=(val) ⇒ Object



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

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

.connectionKey(server, username) ⇒ Object



352
353
354
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 352

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

.debugUpdatesObject



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

def self.debugUpdates
  @@debugUpdates
end

.debugUpdates=(val) ⇒ Object



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

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

.maxObjectsObject



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

def self.maxObjects
  @@maxObjects
end

.maxObjects=(val) ⇒ Object



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

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

.maxWaitObject



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

def self.maxWait
  @@maxWait
end

.maxWait=(val) ⇒ Object



316
317
318
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 316

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

.notifyMethodObject



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

def self.notifyMethod
  @@notifyMethod
end

.notifyMethod=(val) ⇒ Object



274
275
276
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 274

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

.preLoadObject



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

def self.preLoad
  @@preLoad
end

.preLoad=(val) ⇒ Object



245
246
247
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 245

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

.removeSelector(selName) ⇒ Object



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

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.



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

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



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

def self.updateDelay
  @@updateDelay
end

.updateDelay=(val) ⇒ Object



295
296
297
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 295

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

Instance Method Details

#config_lockObject



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

def config_lock
  Sync.new
end

#connection_lockObject



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

def connection_lock
  Sync.new
end

#connectionInfoObject



524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 524

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



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

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

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



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

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



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

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.



264
265
266
267
268
269
270
271
272
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 264

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



570
571
572
573
574
575
576
577
578
579
580
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 570

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



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

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



360
361
362
363
364
365
366
367
368
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 360

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



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

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.



285
286
287
288
289
290
291
292
293
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 285

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



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

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

#releaseSession(sessionId) ⇒ Object



332
333
334
335
336
337
338
339
340
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 332

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



450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 450

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.



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

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



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

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)


513
514
515
516
517
518
519
520
521
522
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 513

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.



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

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



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

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



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

def sync_for_drb
  Sync.new
end

#sync_for_drb_drbconnObject



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

def sync_for_drb_drbconn
  Sync.new
end

#sync_for_lock_hash(_key) ⇒ Object



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

def sync_for_lock_hash(_key)
  Sync.new
end

#updateDelay=(val) ⇒ Object

Instance method changes the updateDelay value of existing connections.



306
307
308
309
310
311
312
313
314
# File 'lib/VMwareWebService/MiqVimBroker.rb', line 306

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