3
OÉ6bæ  ã               @   sH   d Z ddlmZ ddlmZ dZdZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dS )aò  Criteria to select ServerDescriptions based on maxStalenessSeconds.

The Max Staleness Spec says: When there is a known primary P,
a secondary S's staleness is estimated with this formula:

  (S.lastUpdateTime - S.lastWriteDate) - (P.lastUpdateTime - P.lastWriteDate)
  + heartbeatFrequencyMS

When there is no known primary, a secondary S's staleness is estimated with:

  SMax.lastWriteDate - S.lastWriteDate + heartbeatFrequencyMS

where "SMax" is the secondary with the greatest lastWriteDate.
é    )ÚConfigurationError)ÚSERVER_TYPEé
   éZ   c             C   s>   | |t  k r"tdt | |d f ƒ‚| tk r:tdt| f ƒ‚d S )NzŒmaxStalenessSeconds must be at least heartbeatFrequencyMS + %d seconds. maxStalenessSeconds is set to %d, heartbeatFrequencyMS is set to %d.iè  zJmaxStalenessSeconds must be at least %d. maxStalenessSeconds is set to %d.)ÚIDLE_WRITE_PERIODr   ÚSMALLEST_MAX_STALENESS)Úmax_stalenessÚheartbeat_frequency© r
   ú^/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/pymongo/max_staleness_selectors.pyÚ_validate_max_staleness(   s    r   c             C   sl   |j }g }xV|jD ]L}|jtjkrT|j|j |j|j  |j }|| kr^|j|ƒ q|j|ƒ qW |j	|ƒS )zEApply max_staleness, in seconds, to a Selection with a known primary.)
ÚprimaryÚserver_descriptionsÚserver_typer   ÚRSSecondaryZlast_update_timeÚlast_write_dater	   ÚappendÚwith_server_descriptions)r   Ú	selectionr   ÚsdsÚsÚ	stalenessr
   r
   r   Ú_with_primary9   s    r   c             C   sp   |j ƒ }|s|jg ƒS g }xJ|jD ]@}|jtjkrX|j|j |j }|| krb|j|ƒ q"|j|ƒ q"W |j|ƒS )zFApply max_staleness, in seconds, to a Selection with no known primary.)	Z"secondary_with_max_last_write_dater   r   r   r   r   r   r	   r   )r   r   Zsmaxr   r   r   r
   r
   r   Ú_no_primaryN   s    

r   c             C   s6   | dkr|S t | |jƒ |jr(t| |ƒS t| |ƒS dS )z0Apply max_staleness, in seconds, to a Selection.é   Néÿÿÿÿ)r   r	   r   r   r   )r   r   r
   r
   r   Úselectg   s    
r   N)Ú__doc__Zpymongo.errorsr   Zpymongo.server_typer   r   r   r   r   r   r   r
   r
   r
   r   Ú<module>   s   