Class: Mongo::PoolManager

Inherits:
Object show all
Includes:
ThreadLocalVariableManager
Defined in:
lib/mongo/util/pool_manager.rb

Direct Known Subclasses

ShardingPoolManager

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ThreadLocalVariableManager

#thread_local

Constructor Details

#initialize(client, seeds = []) ⇒ PoolManager

Create a new set of connection pools.

The pool manager will by default use the original seed list passed to the connection objects, accessible via connection.seeds. In addition, the user may pass an additional list of seeds nodes discovered in real time. The union of these lists will be used when attempting to connect, with the newly-discovered nodes being used first.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mongo/util/pool_manager.rb', line 24

def initialize(client, seeds=[])
  @client               = client
  @seeds                = seeds

  @pools                = Set.new
  @primary              = nil
  @primary_pool         = nil
  @secondaries          = Set.new
  @secondary_pools      = []
  @hosts                = Set.new
  @members              = Set.new
  @refresh_required     = false
end

Instance Attribute Details

#arbitersObject (readonly)

Returns the value of attribute arbiters.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def arbiters
  @arbiters
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def client
  @client
end

#hostsObject (readonly)

Returns the value of attribute hosts.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def hosts
  @hosts
end

#membersObject (readonly)

Returns the value of attribute members.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def members
  @members
end

#nodesObject (readonly)

Returns the value of attribute nodes.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def nodes
  @nodes
end

#poolsObject (readonly)

Returns the value of attribute pools.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def pools
  @pools
end

#primaryObject (readonly)

Returns the value of attribute primary.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def primary
  @primary
end

#primary_poolObject (readonly)

Returns the value of attribute primary_pool.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def primary_pool
  @primary_pool
end

#secondariesObject (readonly)

Returns the value of attribute secondaries.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def secondaries
  @secondaries
end

#secondary_poolsObject (readonly)

Returns the value of attribute secondary_pools.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def secondary_pools
  @secondary_pools
end

#seedsObject (readonly)

Returns the value of attribute seeds.



5
6
7
# File 'lib/mongo/util/pool_manager.rb', line 5

def seeds
  @seeds
end

Instance Method Details

#check_connection_healthObject

We’re healthy if all members are pingable and if the view of the replica set returned by isMaster is equivalent to our view. If any of these isn’t the case, set @refresh_required to true, and return.



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
# File 'lib/mongo/util/pool_manager.rb', line 59

def check_connection_health
  begin
    seed = get_valid_seed_node
  rescue ConnectionFailure
    @refresh_required = true
    return
  end

  unless current_config = seed.config
    @refresh_required = true
    seed.close
    return
  end

  if current_config['hosts'].length != @members.length
    @refresh_required = true
    seed.close
    return
  end

  current_config['hosts'].each do |host|
    member = @members.detect do |m|
      m.address == host
    end

    if member && validate_existing_member(current_config, member)
      next
    else
      @refresh_required = true
      seed.close
      return false
    end
  end

  seed.close
end

#close(opts = {}) ⇒ Object



105
106
107
108
109
110
# File 'lib/mongo/util/pool_manager.rb', line 105

def close(opts={})
  begin
    pools.each { |pool| pool.close(opts) }
  rescue ConnectionFailure
  end
end

#closed?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/mongo/util/pool_manager.rb', line 101

def closed?
  pools.all? { |pool| pool.closed? }
end

#connectObject



42
43
44
45
46
47
48
# File 'lib/mongo/util/pool_manager.rb', line 42

def connect
  @refresh_required = false
  disconnect_old_members
  connect_to_members
  initialize_pools(@members)
  cache_discovered_seeds
end

#inspectObject



38
39
40
# File 'lib/mongo/util/pool_manager.rb', line 38

def inspect
  "<Mongo::PoolManager:0x#{self.object_id.to_s(16)} @seeds=#{@seeds}>"
end

#max_bson_sizeObject



116
117
118
# File 'lib/mongo/util/pool_manager.rb', line 116

def max_bson_size
  @max_bson_size ||= config_min('maxBsonObjectSize', DEFAULT_MAX_BSON_SIZE)
end

#max_message_sizeObject



120
121
122
# File 'lib/mongo/util/pool_manager.rb', line 120

def max_message_size
  @max_message_size ||= config_min('maxMessageSizeBytes', DEFAULT_MAX_MESSAGE_SIZE)
end

#readObject



112
113
114
# File 'lib/mongo/util/pool_manager.rb', line 112

def read
  read_pool.host_port
end

#refresh!(additional_seeds) ⇒ Object



50
51
52
53
# File 'lib/mongo/util/pool_manager.rb', line 50

def refresh!(additional_seeds)
  @seeds |= additional_seeds
  connect
end

#refresh_required?Boolean

The replica set connection should initiate a full refresh.

Returns:

  • (Boolean)


97
98
99
# File 'lib/mongo/util/pool_manager.rb', line 97

def refresh_required?
  @refresh_required
end