Class: SidekiqTamer::Mongo::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_tamer/mongo/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Server

Returns a new instance of Server.



22
23
24
25
# File 'lib/sidekiq_tamer/mongo/server.rb', line 22

def initialize(host, port)
  @host = host
  @port = port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



21
22
23
# File 'lib/sidekiq_tamer/mongo/server.rb', line 21

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



21
22
23
# File 'lib/sidekiq_tamer/mongo/server.rb', line 21

def port
  @port
end

Class Method Details

.clear_serversObject



17
18
19
# File 'lib/sidekiq_tamer/mongo/server.rb', line 17

def self.clear_servers
  @servers = {}
end

.server_for(host, port) ⇒ Object



7
8
9
10
11
# File 'lib/sidekiq_tamer/mongo/server.rb', line 7

def self.server_for(host, port)
  key = "#{host}:#{port}"
  servers[key] ||= Server.new(host, port)
  servers[key]
end

.serversObject



13
14
15
# File 'lib/sidekiq_tamer/mongo/server.rb', line 13

def self.servers
  @servers ||= {}
end

Instance Method Details

#available_ticketsObject



44
45
46
# File 'lib/sidekiq_tamer/mongo/server.rb', line 44

def available_tickets
  client.database.command('serverStatus'=> 1).documents[0]['wiredTiger']['concurrentTransactions']
end

#clean_wired_tiger_ticketsObject



54
55
56
# File 'lib/sidekiq_tamer/mongo/server.rb', line 54

def clean_wired_tiger_tickets
  @wiredtiger_tickets_read_at = nil
end

#clientObject



48
49
50
51
52
# File 'lib/sidekiq_tamer/mongo/server.rb', line 48

def client
  @client ||= Mongo::Client.new(
    [ "#{host}:#{port}" ], database: 'admin', user: Configuration.user, password: Configuration.password, auth_source: 'admin',
  )
end

#is_operation_safe?(operation) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/sidekiq_tamer/mongo/server.rb', line 27

def is_operation_safe?(operation)
  average = wired_tiger_history.map { |entry|
    entry[:concurrentTransactions][operation.to_s]['available']
  }.sum / wired_tiger_history.count.to_f

  average >= Configuration.ticket_threshold
end

#wired_tiger_historyObject



35
36
37
38
39
40
41
42
# File 'lib/sidekiq_tamer/mongo/server.rb', line 35

def wired_tiger_history
  @wired_tiger_history ||= []
  if !@wiredtiger_tickets_read_at || @wiredtiger_tickets_read_at < Time.now - 30
    @wired_tiger_history.push({ concurrentTransactions: available_tickets, read_at: Time.now })
    @wiredtiger_tickets_read_at = Time.now
  end
  @wired_tiger_history = @wired_tiger_history.select { |entry| entry[:read_at] >= Time.now - 60 * 2 }
end