+\hVdZddlmZddlZddlmZddlmZmZm Z m Z m Z ddl m Z ddlmZddlmZmZer dd lmZdd lmZdZd Zd Zd ZdZdZe eefZe e eefZd)dZd*dZ d+dZ!d,dZ"GddZ#Gdde#Z$Gdde#Z%Gdde#Z&Gdde#Z'Gdde#Z(Gd d!Z)e$e%e&e'e(fZ* d- d.d"Z+d#Z,Gd$d%Z-d/d&Z.Gd'd(Z/y)0zUtilities for choosing which member of a replica set to read from. .. seealso:: This module is compatible with both the synchronous and asynchronous PyMongo APIs. ) annotationsN)abc) TYPE_CHECKINGAnyMappingOptionalSequence)max_staleness_selectors)ConfigurationError) member_with_tags_server_selector#secondary_with_tags_server_selector) Selection)TopologyDescription)primaryprimaryPreferred secondarysecondaryPreferrednearestc||St|ttfstd|dt |dk(rt d|d|D]+}t|t jrtd|dt|S)z$Validate tag sets for a MongoClient.z Tag sets z invalid, must be a sequencerz: invalid, must be None or contain at least one set of tagszTag set zg invalid, must be an instance of dict, bson.son.SON or other type that inherits from collection.Mapping) isinstancelisttuple TypeErrorlen ValueErrorrr)tag_setstagss [/root/niggaflix-v3/playground/venv/lib/python3.12/site-packages/pymongo/read_preferences.py_validate_tag_setsr#9s hu .)Hc d|zS)Nz6maxStalenessSeconds must be a positive integer, not %s max_stalenesss r"_invalid_max_staleness_msgr)Ps Cm SSr$c|dk(ryt|tstt||dkrt t||S)zValidate max_staleness.r)rintrr)rr's r"_validate_max_stalenessr-UsG mS )2=ABB3MBCC r$c|yt|tstd|tjdt d|S)zValidate hedge.Nz hedge must be a dictionary, not The read preference 'hedge' option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for 'hedge' will be removed in PyMongo 5.0.r stacklevel)rdictrwarningswarnDeprecationWarning)hedges r"_validate_hedger7csF } eT ":5)DEE MM } Lr$ceZdZdZdZ d ddZeddZeddZeddZ eddZ edd Z edd Z edd Z edd Zdd ZddZddZddZddZddZy) _ServerModez$Base class for all read preferences.) __mongos_mode__mode __tag_sets__max_staleness__hedgeNct||_||_t||_t ||_t||_yN) _MONGOS_MODES_ServerMode__mongos_mode_ServerMode__moder#_ServerMode__tag_setsr-_ServerMode__max_stalenessr7_ServerMode__hedge)selfmoder r(r6s r"__init__z_ServerMode.__init__xs=+40 ,X66}E&u- r$c.|jjS)z!The name of this read preference.) __class____name__rGs r"namez_ServerMode.names~~&&&r$c|jS)z(The mongos mode of this read preference.)rBrMs r" mongos_modez_ServerMode.mongos_modes!!!r$cd|ji}|jdigfvr|j|d<|jdk7r|j|d<|jdifvr|j|d<|S)zRead preference as a document.rHNr!r+maxStalenessSecondsr6)rBrDrErF)rGdocs r"documentz_ServerMode.documentst &t'9'9: ??4", .//CK   2 %)-)=)=C% & <<bz )<`_ )rDrrMs r"r z_ServerMode.tag_setss ()-tDOO$AbTAr$c|jS)zThe maximum estimated length of time (in seconds) a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations, or -1 for no maximum. rErMs r"r(z_ServerMode.max_stalenesss ###r$cj|jtjdtd|jS)am**DEPRECATED** - The read preference 'hedge' option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for 'hedge' will be removed in PyMongo 5.0. The read preference ``hedge`` parameter. A dictionary that configures how the server will perform hedged reads. It consists of the following keys: - ``enabled``: Enables or disables hedged reads in sharded clusters. Hedged reads are automatically enabled in MongoDB 4.4+ when using a ``nearest`` read preference. To explicitly enable hedged reads, set the ``enabled`` key to ``true``:: >>> Nearest(hedge={'enabled': True}) To explicitly disable hedged reads, set the ``enabled`` key to ``False``:: >>> Nearest(hedge={'enabled': False}) .. versionadded:: 3.11 r/rr0)rFr3r4r5rMs r"r6z_ServerMode.hedges20 << # MME"  ||r$c(|jdk(rdSdS)aThe wire protocol version the server must support. Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5). All servers' maxWireVersion must be at least this read preference's `min_wire_version`, or the driver raises :exc:`~pymongo.errors.ConfigurationError`. r+rrXrMs r"min_wire_versionz_ServerMode.min_wire_versions((B.q5A5r$czdj|j|j|j|jS)Nz1{}(tag_sets={!r}, max_staleness={!r}, hedge={!r}))formatrNrDrErFrMs r"__repr__z_ServerMode.__repr__s3BII II OO  LL   r$ct|trj|j|jk(xrO|j|jk(xr4|j|jk(xr|j |j k(St Sr@)rr9rHr r(r6NotImplementedrGothers r"__eq__z_ServerMode.__eq__sn e[ ) UZZ'.MMU^^3.&&%*=*==.JJ%++-   r$c||k( Sr@r&rbs r"__ne__z_ServerMode.__ne__s5=  r$c`|j|j|j|jdS)zeReturn value of object for pickling. Needed explicitly because __slots__() defined. )rHr r(r6)rCrDrErFrMs r" __getstate__z_ServerMode.__getstate__s, KK!11\\   r$c|d|_t|j|_t|d|_t |d|_t|d|_y)zRestore from pickling.rHr r(r6N) rCrArBr#rDr-rEr7rF)rGvalues r" __setstate__z_ServerMode.__setstate__ sQFm *4;;7,U:->?6u_7MN&uW~6 r$c|Sr@r&rG selections r"__call__z_ServerMode.__call__sr$Nr+N) rHr,r Optional[_TagSets]r(r,r6Optional[_Hedge]returnNonersstr)rszdict[str, Any])rsr,)rs_TagSets)rsrrrcrrsbool)rjzMapping[str, Any]rsrtrnrrsr)rL __module__ __qualname____doc__ __slots__rIpropertyrNrPrTrHr r(r6r\r_rdrfrhrkror&r$r"r9r9ss .WI (,"& . .% . .  .  .''""  BB*$$> 6 6 !  7r$r9c@eZdZdZdZdfd ZddZd dZd dZxZ S) PrimaryaGPrimary read preference. * When directly connected to one mongod queries are allowed if the server is standalone or a replica set primary. * When connected to a mongos queries are sent to the primary of a shard. * When connected to a replica set queries are sent to the primary of the replica set. r&c,t|tyr@)superrI_PRIMARY)rGrKs r"rIzPrimary.__init__$s "r$c|jSz*Apply this read preference to a Selection.)primary_selectionrms r"rozPrimary.__call__'s***r$cy)Nz Primary()r&rMs r"r_zPrimary.__repr__+sr$cTt|tr|jtk(StSr@)rr9rHrrarbs r"rdzPrimary.__eq__.s! e[ )::) )r$rsrtrzrurx) rLr{r|r}r~rIror_rd __classcell__rKs@r"rrs#I#+r$rcFeZdZdZdZ d dfd ZddZxZS)PrimaryPreferredaPrimaryPreferred read preference. * When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries. * When connected to a mongos queries are sent to the primary of a shard if available, otherwise a shard secondary. * When connected to a replica set queries are sent to the primary if available, otherwise a secondary. .. note:: When a :class:`~pymongo.mongo_client.MongoClient` is first created reads will be routed to an available secondary until the primary of the replica set is discovered. :param tag_sets: The :attr:`~tag_sets` to use if the primary is not available. :param max_staleness: (integer, in seconds) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Default -1, meaning no maximum. If it is set, it must be at least 90 seconds. :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference. .. versionchanged:: 3.11 Added ``hedge`` parameter. r&c2t|t|||yr@)rrI_PRIMARY_PREFERREDrGr r(r6rKs r"rIzPrimaryPreferred.__init__Qs +X}eLr$c|jr |jSt|jt j |j |Sz(Apply this read preference to Selection.)rrr r r selectr(rms r"rozPrimaryPreferred.__call__YsB   .. .6 6==d>P>PR[\ r$rpr rqr(r,r6rrrsrtrzrLr{r|r}r~rIrorrs@r"rr4sO4I(,"& M$MM M  Mr$rcFeZdZdZdZ d dfd ZddZxZS) SecondaryaSecondary read preference. * When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries. * When connected to a mongos queries are distributed among shard secondaries. An error is raised if no secondaries are available. * When connected to a replica set queries are distributed among secondaries. An error is raised if no secondaries are available. :param tag_sets: The :attr:`~tag_sets` for this read preference. :param max_staleness: (integer, in seconds) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Default -1, meaning no maximum. If it is set, it must be at least 90 seconds. :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference. .. versionchanged:: 3.11 Added ``hedge`` parameter. r&c2t|t|||yr@)rrI _SECONDARYrs r"rIzSecondary.__init__{s X}eDr$cjt|jtj|j|Sr)r r r rr(rms r"rozSecondary.__call__s,2 MM299$:L:LiX  r$rprrzrrs@r"rrcsO*I(,"& E$EE E  E r$rcFeZdZdZdZ d dfd ZddZxZS)SecondaryPreferredaSecondaryPreferred read preference. * When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries. * When connected to a mongos queries are distributed among shard secondaries, or the shard primary if no secondary is available. * When connected to a replica set queries are distributed among secondaries, or the primary if no secondary is available. .. note:: When a :class:`~pymongo.mongo_client.MongoClient` is first created reads will be routed to the primary of the replica set until an available secondary is discovered. :param tag_sets: The :attr:`~tag_sets` for this read preference. :param max_staleness: (integer, in seconds) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Default -1, meaning no maximum. If it is set, it must be at least 90 seconds. :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference. .. versionchanged:: 3.11 Added ``hedge`` parameter. r&c2t|t|||yr@)rrI_SECONDARY_PREFERREDrs r"rIzSecondaryPreferred.__init__s -xNr$ct|jtj|j|}|r|S|j Sr)r r r rr(r)rGrn secondariess r"rozSecondaryPreferred.__call__s@9 MM299$:L:LiX   .. .r$rprrzrrs@r"rrsO2I(,"& O$OO O  O /r$rcFeZdZdZdZ d dfd ZddZxZS)NearestayNearest read preference. * When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries. * When connected to a mongos queries are distributed among all members of a shard. * When connected to a replica set queries are distributed among all members. :param tag_sets: The :attr:`~tag_sets` for this read preference. :param max_staleness: (integer, in seconds) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Default -1, meaning no maximum. If it is set, it must be at least 90 seconds. :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference. .. versionchanged:: 3.11 Added ``hedge`` parameter. r&c2t|t|||yr@)rrI_NEARESTrs r"rIzNearest.__init__s 8]EBr$cjt|jtj|j|Sr)r r r rr(rms r"rozNearest.__call__s,/ MM299$:L:LiX  r$rprrzrrs@r"rrsO*I(,"& C$CC C  C r$rc<eZdZdZdZd dZd dZd dZd dZd dZ y) _AggWritePrefzAgg $out/$merge write preference. * If there are readable servers and there is any pre-5.0 server, use primary read preference. * Otherwise use `pref` read preference. :param pref: The read preference to use on MongoDB 5.0+. )prefeffective_prefc<||_tj|_yr@)rReadPreferencePRIMARYr)rGrs r"rIz_AggWritePref.__init__s +9+A+Ar$c|j}|jtjr|r|dkrtj|_y|j |_y)N )common_wire_versionhas_readable_serverrPRIMARY_PREFERREDrrr)rGtopology_description common_wvs r"selection_hookz_AggWritePref.selection_hooksE(<< 4 4^5U5U VB"0"8"8D "&))D r$c$|j|Sr)rrms r"roz_AggWritePref.__call__s""9--r$c"d|jdS)Nz_AggWritePref(pref=))rrMs r"r_z_AggWritePref.__repr__s$TYYM33r$c.t|j|Sr@)getattrr)rGrNs r" __getattr__z_AggWritePref.__getattr__st**D11r$N)rr9)rrrsrtrzru)rNrvrsr) rLr{r|r}r~rIrror_rr&r$r"rrs(+IB ,.4 2r$rc|tk(r,|digfvr td|dk7r tdtSt|||S)Nz4Read preference primary cannot be combined with tagsr+zCRead preference primary cannot be combined with maxStalenessSeconds)rr r_ALL_READ_PREFERENCES)rHr r(s r"make_read_preferencer sZ x D2$< '$%[\ \ B $U y  &x ??r$)rr SECONDARYSECONDARY_PREFERREDNEARESTcVeZdZdZeZeZeZ e Z e Z y)raAn enum that defines some commonly used read preference modes. Apps can also create a custom read preference, for example:: Nearest(tag_sets=[{"node":"analytics"}]) See :doc:`/examples/high_availability` for code examples. A read preference is used in three cases: :class:`~pymongo.mongo_client.MongoClient` connected to a single mongod: - ``PRIMARY``: Queries are allowed if the server is standalone or a replica set primary. - All other modes allow queries to standalone servers, to a replica set primary, or to replica set secondaries. :class:`~pymongo.mongo_client.MongoClient` initialized with the ``replicaSet`` option: - ``PRIMARY``: Read from the primary. This is the default, and provides the strongest consistency. If no primary is available, raise :class:`~pymongo.errors.AutoReconnect`. - ``PRIMARY_PREFERRED``: Read from the primary if available, or if there is none, read from a secondary. - ``SECONDARY``: Read from a secondary. If no secondary is available, raise :class:`~pymongo.errors.AutoReconnect`. - ``SECONDARY_PREFERRED``: Read from a secondary if available, otherwise from the primary. - ``NEAREST``: Read from any member. :class:`~pymongo.mongo_client.MongoClient` connected to a mongos, with a sharded cluster of replica sets: - ``PRIMARY``: Read from the primary of the shard, or raise :class:`~pymongo.errors.OperationFailure` if there is none. This is the default. - ``PRIMARY_PREFERRED``: Read from the primary of the shard, or if there is none, read from a secondary of the shard. - ``SECONDARY``: Read from a secondary of the shard, or raise :class:`~pymongo.errors.OperationFailure` if there is none. - ``SECONDARY_PREFERRED``: Read from a secondary of the shard if available, otherwise from the shard primary. - ``NEAREST``: Read from any shard member. N)rLr{r|r}rrrrrrrrrrr&r$r"rr#s14liG(* I,.iGr$rc,tj|S)z2Get the read preference mode from mongos/uri name.)rAindex)rNs r"read_pref_mode_from_nameras   t $$r$c<eZdZUdZded<d dZd dZd dZd dZy) MovingAveragez0Tracks an exponentially-weighted moving average.Optional[float]averagecd|_yr@rrMs r"rIzMovingAverage.__init__k  r$c|dkrtd||j||_yd|jzd|zz|_y)Nrzduration cannot be negative g?g?)rr)rGsamples r" add_samplezMovingAverage.add_samplensH A:;F8DE E << !DL-f rsM #BB+- 2@      c  GCH% &.T   bbJk8,{,^$ $ N-/-/`$ k$ N%2%2P!"2I?QSZ[CE @  @+ @