Class: Mongo::PoolManager

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

Direct Known Subclasses

ShardingPoolManager

Instance Attribute Summary collapse

Instance Method Summary collapse

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

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

Instance Attribute Details

#arbitersObject (readonly)

Returns the value of attribute arbiters.



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

def arbiters
  @arbiters
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#hostsObject (readonly)

Returns the value of attribute hosts.



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

def hosts
  @hosts
end

#max_bson_sizeObject (readonly)

Returns the value of attribute max_bson_size.



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

def max_bson_size
  @max_bson_size
end

#membersObject (readonly)

Returns the value of attribute members.



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

def members
  @members
end

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#pinned_poolsObject

Returns the value of attribute pinned_pools.



8
9
10
# File 'lib/mongo/util/pool_manager.rb', line 8

def pinned_pools
  @pinned_pools
end

#primaryObject (readonly)

Returns the value of attribute primary.



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

def primary
  @primary
end

#primary_poolObject (readonly)

Returns the value of attribute primary_pool.



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

def primary_pool
  @primary_pool
end

#secondariesObject (readonly)

Returns the value of attribute secondaries.



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

def secondaries
  @secondaries
end

#secondary_poolObject (readonly)

Returns the value of attribute secondary_pool.



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

def secondary_pool
  @secondary_pool
end

#secondary_poolsObject (readonly)

Returns the value of attribute secondary_pools.



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

def secondary_pools
  @secondary_pools
end

#seedsObject (readonly)

Returns the value of attribute seeds.



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

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.



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

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

  config = seed.set_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



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

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

#closed?Boolean



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

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

#connectObject



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

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



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

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

#poolsObject



136
137
138
# File 'lib/mongo/util/pool_manager.rb', line 136

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

#readObject



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

def read
  read_pool.host_port
end

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mongo/util/pool_manager.rb', line 102

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

  if mode == :primary && !tags.empty?
    raise MongoArgumentError, "Read preferecy :primary cannot be combined with tags"
  end

  pinned = @pinned_pools[Thread.current]
  if pinned && pinned.matches_mode(mode) && pinned.matches_tag_sets(tags) && pinned.up?
    pool = pinned
  else
    pool = case mode
    when :primary
      @primary_pool
    when :primary_preferred
      @primary_pool || select_pool(@secondary_pools, tags, acceptable_latency)
    when :secondary
      select_pool(@secondary_pools, tags, acceptable_latency)
    when :secondary_preferred
      select_pool(@secondary_pools, tags, acceptable_latency) || @primary_pool
    when :nearest
      select_pool(pools, tags, acceptable_latency)
    end
  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.



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

def refresh_required?
  @refresh_required
end