Class: Mongo::PoolManager

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

Direct Known Subclasses

ShardingPoolManager

Constant Summary

Constants included from ReadPreference

ReadPreference::MONGOS_MODES, ReadPreference::READ_PREFERENCES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ThreadLocalVariableManager

#thread_local

Methods included from ReadPreference

mongos, #select_near_pool, #select_pool, #select_secondary_pool, validate

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.



17
18
19
20
21
# File 'lib/mongo/util/pool_manager.rb', line 17

def initialize(client, seeds=[])
  @client               = client
  @seeds                = seeds
  @previously_connected = false
end

Instance Attribute Details

#arbitersObject (readonly)

Returns the value of attribute arbiters.



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

def arbiters
  @arbiters
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#hostsObject (readonly)

Returns the value of attribute hosts.



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

def hosts
  @hosts
end

#max_bson_sizeObject (readonly)

Returns the value of attribute max_bson_size.



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

def max_bson_size
  @max_bson_size
end

#membersObject (readonly)

Returns the value of attribute members.



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

def members
  @members
end

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#primaryObject (readonly)

Returns the value of attribute primary.



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

def primary
  @primary
end

#primary_poolObject (readonly)

Returns the value of attribute primary_pool.



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

def primary_pool
  @primary_pool
end

#secondariesObject (readonly)

Returns the value of attribute secondaries.



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

def secondaries
  @secondaries
end

#secondary_poolObject (readonly)

Returns the value of attribute secondary_pool.



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

def secondary_pool
  @secondary_pool
end

#secondary_poolsObject (readonly)

Returns the value of attribute secondary_pools.



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

def secondary_pools
  @secondary_pools
end

#seedsObject (readonly)

Returns the value of attribute seeds.



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

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.



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

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

  config = seed.config
  if config
    @refresh_required = true
    seed.close
    return
  end

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

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

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

  seed.close
end

#close(opts = {}) ⇒ Object



90
91
92
93
94
95
# File 'lib/mongo/util/pool_manager.rb', line 90

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

#closed?Boolean

Returns:



86
87
88
# File 'lib/mongo/util/pool_manager.rb', line 86

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

#connectObject



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

def connect
  close if @previously_connected

  initialize_data
  members = connect_to_members
  initialize_pools(members)
  cache_discovered_seeds(members)

  @members = members
  @previously_connected = true
end

#inspectObject



23
24
25
# File 'lib/mongo/util/pool_manager.rb', line 23

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

#poolsObject



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

def pools
  [@primary_pool, *@secondary_pools].compact
end

#readObject



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

def read
  read_pool.host_port
end

#read_pool(mode = @client.read, tags = @client.tag_sets, acceptable_latency = @client.acceptable_latency) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mongo/util/pool_manager.rb', line 101

def read_pool(mode=@client.read,
              tags=@client.tag_sets,
              acceptable_latency=@client.acceptable_latency)

  pinned = thread_local[:pinned_pools][self.object_id]

  if pinned && pinned.matches_mode(mode) && pinned.matches_tag_sets(tags) && pinned.up?
    pool = pinned
  else
    pool = select_pool(mode, tags, acceptable_latency)
  end

  unless pool
    raise ConnectionFailure, "No replica set member available for query " +
      "with read preference matching mode #{mode} and tags matching #{tags}."
  end

  pool
end

#refresh_required?Boolean

The replica set connection should initiate a full refresh.

Returns:



82
83
84
# File 'lib/mongo/util/pool_manager.rb', line 82

def refresh_required?
  @refresh_required
end