@@ -155,31 +155,113 @@ the operation will succeed::
155155ReplicaSetConnection
156156--------------------
157157
158- In Pymongo-2.1 a new ReplicaSetConnection class was added that provides
159- some new features not supported in the original Connection class. The most
160- important of these is the ability to distribute queries to the secondary
161- members of a replica set. To connect using ReplicaSetConnection just
162- provide a host:port pair and the name of the replica set::
158+ Using a :class: ` ~pymongo.replica_set_connection. ReplicaSetConnection` instead
159+ of a simple : class: ` ~pymongo.connection.Connection ` offers two key features:
160+ secondary reads and replica set health monitoring. To connect using
161+ ` ReplicaSetConnection ` just provide a host:port pair and the name of the
162+ replica set::
163163
164164 >>> from pymongo import ReplicaSetConnection
165165 >>> ReplicaSetConnection("morton.local:27017", replicaSet='foo')
166166 ReplicaSetConnection([u'morton.local:27019', u'morton.local:27017', u'morton.local:27018'])
167167
168+ Secondary Reads
169+ '''''''''''''''
170+
168171By default an instance of ReplicaSetConnection will only send queries to
169- the primary member of the replica set. To use secondary members for queries
170- we have to change the read preference ::
172+ the primary member of the replica set. To use secondaries for queries
173+ we have to change the :class: ` ~pymongo.read_preference.ReadPreference ` ::
171174
172175 >>> db = ReplicaSetConnection("morton.local:27017", replicaSet='foo').test
173- >>> from pymongo import ReadPreference
174- >>> db.read_preference = ReadPreference.SECONDARY
176+ >>> from pymongo.read_preference import ReadPreference
177+ >>> db.read_preference = ReadPreference.SECONDARY_PREFERRED
175178
176179Now all queries will be sent to the secondary members of the set. If there are
177180no secondary members the primary will be used as a fallback. If you have
178181queries you would prefer to never send to the primary you can specify that
179- using the SECONDARY_ONLY read preference::
182+ using the `` SECONDARY `` read preference::
180183
181- >>> db.read_preference = ReadPreference.SECONDARY_ONLY
184+ >>> db.read_preference = ReadPreference.SECONDARY
182185
183186Read preference can be set on a connection, database, collection, or on a
184- per-query basis.
185-
187+ per-query basis, e.g.::
188+
189+ >>> db.collection.find_one(read_preference=ReadPreference.PRIMARY)
190+
191+ Reads are configured using three options: **read_preference **, **tag_sets **,
192+ and **secondary_acceptable_latency_ms **.
193+
194+ **read_preference **:
195+
196+ - ``PRIMARY ``: Read from the primary. This is the default, and provides the
197+ strongest consistency. If no primary is available, raise
198+ :class: `~pymongo.errors.AutoReconnect `.
199+
200+ - ``PRIMARY_PREFERRED ``: Read from the primary if available, or if there is
201+ none, read from a secondary matching your choice of ``tag_sets `` and
202+ ``secondary_acceptable_latency_ms ``.
203+
204+ - ``SECONDARY ``: Read from a secondary matching your choice of ``tag_sets `` and
205+ ``secondary_acceptable_latency_ms ``. If no matching secondary is available,
206+ raise :class: `~pymongo.errors.AutoReconnect `.
207+
208+ - ``SECONDARY_PREFERRED ``: Read from a secondary matching your choice of
209+ ``tag_sets `` and ``secondary_acceptable_latency_ms `` if available, otherwise
210+ from primary (regardless of the primary's tags and latency).
211+
212+ - ``NEAREST ``: Read from any member matching your choice of ``tag_sets `` and
213+ ``secondary_acceptable_latency_ms ``.
214+
215+ **tag_sets **:
216+
217+ Replica-set members can be `tagged
218+ <http://www.mongodb.org/display/DOCS/Data+Center+Awareness> `_ according to any
219+ criteria you choose. By default, ReplicaSetConnection ignores tags when
220+ choosing a member to read from, but it can be configured with the ``tag_sets ``
221+ parameter. ``tag_sets `` must be a list of dictionaries, each dict providing tag
222+ values that the replica set member must match. ReplicaSetConnection tries each
223+ set of tags in turn until it finds a set of tags with at least one matching
224+ member. For example, to prefer reads from the New York data center, but fall
225+ back to the San Francisco data center, tag your replica set members according
226+ to their location and create a ReplicaSetConnection like so:
227+
228+ >>> rsc = ReplicaSetConnection(
229+ ... " morton.local:27017" ,
230+ ... replicaSet= ' foo'
231+ ... read_preference= ReadPreference.SECONDARY ,
232+ ... tag_sets= [{' dc' : ' ny' }, {' dc' : ' sf' }]
233+ ... )
234+
235+ ReplicaSetConnection tries to find secondaries in New York, then San Francisco,
236+ and raises :class: `~pymongo.errors.AutoReconnect ` if none are available. As an
237+ additional fallback, specify a final, empty tag set, ``{} ``, which means "read
238+ from any member that matches the mode, ignoring tags."
239+
240+ **secondary_acceptable_latency_ms **:
241+
242+ If multiple members match the mode and tag sets, ReplicaSetConnection reads
243+ from among the nearest members, chosen according to ping time. By default,
244+ only members whose ping times are within 15 milliseconds of the nearest
245+ are used for queries. You can choose to distribute reads among members with
246+ higher latencies by setting ``secondary_acceptable_latency_ms `` to a larger
247+ number. In that case, ReplicaSetConnection distributes reads among matching
248+ members within ``secondary_acceptable_latency_ms `` of the closest member's
249+ ping time.
250+
251+ Health Monitoring
252+ '''''''''''''''''
253+
254+ When ReplicaSetConnection is initialized it launches a background task to
255+ monitor the replica set for changes in:
256+
257+ * Health: detect when a member goes down or comes up, or if a different member
258+ becomes primary
259+ * Configuration: detect changes in tags
260+ * Latency: track a moving average of each member's ping time
261+
262+ Replica-set monitoring ensures queries are continually routed to the proper
263+ members as the state of the replica set changes.
264+
265+ It is critical to call
266+ :meth: `~pymongo.replica_set_connection.ReplicaSetConnection.close ` to terminate
267+ the monitoring task before your process exits.
0 commit comments