1717from collections import Mapping , namedtuple
1818
1919from pymongo .errors import ConfigurationError
20- from pymongo .server_selectors import (near_member_with_tags_server_selector ,
21- near_secondary_with_tags_server_selector ,
20+ from pymongo .server_selectors import (member_with_tags_server_selector ,
21+ secondary_with_tags_server_selector ,
2222 writable_server_selector )
2323
2424
@@ -64,20 +64,15 @@ class ServerMode(object):
6464 """Base class for all read preferences.
6565 """
6666
67- __slots__ = ("__mongos_mode" , "__mode" , "__threshold" , " __tag_sets" )
67+ __slots__ = ("__mongos_mode" , "__mode" , "__tag_sets" )
6868
69- def __init__ (self , mode , local_threshold_ms = 15 , tag_sets = None ):
69+ def __init__ (self , mode , tag_sets = None ):
7070 if mode == _PRIMARY and tag_sets is not None :
7171 raise ConfigurationError ("Read preference primary "
7272 "cannot be combined with tags" )
7373 self .__mongos_mode = _MONGOS_MODES [mode ]
7474 self .__mode = mode
7575 self .__tag_sets = _validate_tag_sets (tag_sets )
76- try :
77- self .__threshold = float (local_threshold_ms )
78- except (ValueError , TypeError ):
79- raise ConfigurationError ("local_threshold_ms must "
80- "be a positive integer or float" )
8176
8277 @property
8378 def name (self ):
@@ -99,22 +94,6 @@ def mode(self):
9994 """
10095 return self .__mode
10196
102- @property
103- def local_threshold_ms (self ):
104- """An integer. Any replica-set member whose ping time is within
105- ``local_threshold_ms`` of the nearest member may accept reads.
106- When used with mongos high availability, any mongos whose ping
107- time is within ``local_threshold_ms`` of the nearest mongos
108- may be chosen as the new mongos during a failover. Default 15
109- milliseconds.
110-
111- .. note:: ``local_threshold_ms`` is ignored when talking
112- to a replica set through a mongos. The equivalent is the
113- `localThreshold <http://docs.mongodb.org/manual/reference/mongos/#cmdoption--localThreshold>`_
114- command line option.
115- """
116- return self .__threshold
117-
11897 @property
11998 def tag_sets (self ):
12099 """Set ``tag_sets`` to a list of dictionaries like [{'dc': 'ny'}] to
@@ -131,13 +110,14 @@ def tag_sets(self):
131110 return self .__tag_sets or [{}]
132111
133112 def __repr__ (self ):
134- return "%s(local_threshold_ms=%d, tag_sets=%r)" % (
135- self .name , self .__threshold , self . __tag_sets )
113+ return "%s(tag_sets=%r)" % (
114+ self .name , self .__tag_sets )
136115
137116 def __eq__ (self , other ):
138- return (self .mode == other .mode and
139- self .local_threshold_ms == other .local_threshold_ms and
140- self .tag_sets == other .tag_sets )
117+ if isinstance (other , ServerMode ):
118+ return (self .mode == other .mode and
119+ self .tag_sets == other .tag_sets )
120+ raise NotImplementedError
141121
142122 def __ne__ (self , other ):
143123 return not self == other
@@ -151,14 +131,10 @@ class Primary(ServerMode):
151131 * When connected to a mongos queries are sent to the primary of a shard.
152132 * When connected to a replica set queries are sent to the primary of
153133 the replica set.
154-
155- :Parameters:
156- - `local_threshold_ms`: Used for mongos high availability. The
157- :attr:`~local_threshold_ms` when selecting a failover mongos.
158134 """
159135
160- def __init__ (self , local_threshold_ms = 15 ):
161- super (Primary , self ).__init__ (_PRIMARY , local_threshold_ms )
136+ def __init__ (self ):
137+ super (Primary , self ).__init__ (_PRIMARY )
162138
163139 def __call__ (self , server_descriptions ):
164140 """Return matching ServerDescriptions from a list."""
@@ -167,6 +143,11 @@ def __call__(self, server_descriptions):
167143 def __repr__ (self ):
168144 return "Primary"
169145
146+ def __eq__ (self , other ):
147+ if isinstance (other , ServerMode ):
148+ return other .mode == _PRIMARY
149+ raise NotImplementedError
150+
170151
171152class PrimaryPreferred (ServerMode ):
172153 """PrimaryPreferred read preference.
@@ -179,25 +160,21 @@ class PrimaryPreferred(ServerMode):
179160 available, otherwise a secondary.
180161
181162 :Parameters:
182- - `local_threshold_ms`: The :attr:`~local_threshold_ms` when
183- selecting a secondary.
184163 - `tag_sets`: The :attr:`~tag_sets` to use if the primary is not
185164 available.
186165 """
187166
188- def __init__ (self , local_threshold_ms = 15 , tag_sets = None ):
189- super (PrimaryPreferred , self ).__init__ (
190- _PRIMARY_PREFERRED , local_threshold_ms , tag_sets )
167+ def __init__ (self , tag_sets = None ):
168+ super (PrimaryPreferred , self ).__init__ (_PRIMARY_PREFERRED , tag_sets )
191169
192170 def __call__ (self , server_descriptions ):
193171 """Return matching ServerDescriptions from a list."""
194172 writable_servers = writable_server_selector (server_descriptions )
195173 if writable_servers :
196174 return writable_servers
197175 else :
198- return near_secondary_with_tags_server_selector (
176+ return secondary_with_tags_server_selector (
199177 self .tag_sets ,
200- self .local_threshold_ms ,
201178 server_descriptions )
202179
203180
@@ -212,20 +189,16 @@ class Secondary(ServerMode):
212189 secondaries. An error is raised if no secondaries are available.
213190
214191 :Parameters:
215- - `local_threshold_ms`: The :attr:`~local_threshold_ms` when
216- selecting a secondary.
217192 - `tag_sets`: The :attr:`~tag_sets` to use with this read_preference
218193 """
219194
220- def __init__ (self , local_threshold_ms = 15 , tag_sets = None ):
221- super (Secondary , self ).__init__ (
222- _SECONDARY , local_threshold_ms , tag_sets )
195+ def __init__ (self , tag_sets = None ):
196+ super (Secondary , self ).__init__ (_SECONDARY , tag_sets )
223197
224198 def __call__ (self , server_descriptions ):
225199 """Return matching ServerDescriptions from a list."""
226- return near_secondary_with_tags_server_selector (
200+ return secondary_with_tags_server_selector (
227201 self .tag_sets ,
228- self .local_threshold_ms ,
229202 server_descriptions )
230203
231204
@@ -240,20 +213,16 @@ class SecondaryPreferred(ServerMode):
240213 secondaries, or the primary if no secondary is available.
241214
242215 :Parameters:
243- - `local_threshold_ms`: The :attr:`~local_threshold_ms` when
244- selecting a secondary.
245216 - `tag_sets`: The :attr:`~tag_sets` to use with this read_preference
246217 """
247218
248- def __init__ (self , local_threshold_ms = 15 , tag_sets = None ):
249- super (SecondaryPreferred , self ).__init__ (
250- _SECONDARY_PREFERRED , local_threshold_ms , tag_sets )
219+ def __init__ (self , tag_sets = None ):
220+ super (SecondaryPreferred , self ).__init__ (_SECONDARY_PREFERRED , tag_sets )
251221
252222 def __call__ (self , server_descriptions ):
253223 """Return matching ServerDescriptions from a list."""
254- secondaries = near_secondary_with_tags_server_selector (
224+ secondaries = secondary_with_tags_server_selector (
255225 self .tag_sets ,
256- self .local_threshold_ms ,
257226 server_descriptions )
258227
259228 if secondaries :
@@ -273,33 +242,29 @@ class Nearest(ServerMode):
273242 members.
274243
275244 :Parameters:
276- - `local_threshold_ms`: The :attr:`~local_threshold_ms` when
277- selecting a secondary.
278245 - `tag_sets`: The :attr:`~tag_sets` to use with this read_preference
279246 """
280247
281- def __init__ (self , local_threshold_ms = 15 , tag_sets = None ):
282- super (Nearest , self ).__init__ (
283- _NEAREST , local_threshold_ms , tag_sets )
248+ def __init__ (self , tag_sets = None ):
249+ super (Nearest , self ).__init__ (_NEAREST , tag_sets )
284250
285251 def __call__ (self , server_descriptions ):
286252 """Return matching ServerDescriptions from a list."""
287- return near_member_with_tags_server_selector (
253+ return member_with_tags_server_selector (
288254 self .tag_sets or [{}],
289- self .local_threshold_ms ,
290255 server_descriptions )
291256
292257
293258_ALL_READ_PREFERENCES = (Primary , PrimaryPreferred ,
294259 Secondary , SecondaryPreferred , Nearest )
295260
296- def make_read_preference (mode , local_threshold_ms , tag_sets ):
261+ def make_read_preference (mode , tag_sets ):
297262 if mode == _PRIMARY :
298263 if tag_sets not in (None , [{}]):
299264 raise ConfigurationError ("Read preference primary "
300265 "cannot be combined with tags" )
301- return Primary (local_threshold_ms )
302- return _ALL_READ_PREFERENCES [mode ](local_threshold_ms , tag_sets )
266+ return Primary ()
267+ return _ALL_READ_PREFERENCES [mode ](tag_sets )
303268
304269
305270_MODES = (
0 commit comments