o
    }oi                     @  s   U d dl mZ d dlZd dlZd dlmZ d dlmZ ee	Z
er.d dlmZ d dlmZ G dd dZerId d	lmZ eed
Zded< dS dS )    )annotationsN)TYPE_CHECKING)ArtifactNotFound)BinaryIO)ArtifactStorec                   @  sN   e Zd ZdZdddddd!ddZd"ddZd#ddZd$ddZd%ddZd S )&Backoffa  An artifact store's middleware for exponential backoff.

    Example:
       .. code-block:: python

           import optuna
           from optuna.artifacts import upload_artifact
           from optuna.artifacts import Boto3ArtifactStore
           from optuna.artifacts import Backoff


           artifact_store = Backoff(Boto3ArtifactStore("my-bucket"))


           def objective(trial: optuna.Trial) -> float:
               ... = trial.suggest_float("x", -10, 10)
               file_path = generate_example(...)
               upload_artifact(
                   artifact_store=artifact_store,
                   file_path=file_path,
                   study_or_trial=trial,
               )
               return ...
    
      g?   )max_retries
multiplier	min_delay	max_delaybackendr   r   intr   floatr   r   returnNonec                C  sR   || _ |dks	J |dksJ |dksJ ||ksJ || _|| _|| _|| _d S )Nr   )_backend_max_retries_multiplier
_min_delay
_max_delay)selfr   r   r   r   r    r   M/home/ubuntu/.local/lib/python3.10/site-packages/optuna/artifacts/_backoff.py__init__-   s   
zBackoff.__init__n_retryc                 C  s   t | j| j|  | jS )N)minr   r   r   )r   r   r   r   r   _get_sleep_secsB   s   zBackoff._get_sleep_secsartifact_idstrr   c                 C  s   t | jD ]B}z	| j|W   S  ty     ty> } z|| jd kr' tjd| d| |d W Y d }~nd }~ww t	| 
| qJ d)N   Failed to open artifact=	 n_retry=exc_infoFzmust not reach here)ranger   r   open_readerr   	Exception_loggererrortimesleepr   r   r    ier   r   r   r(   E   s   &zBackoff.open_readercontent_bodyc                 C  s   t | jD ]I}z| j|| W  d S  ty     ty@ } z|| jd kr) tjd| d| |d W Y d }~nd }~ww |d t	
| | qd S )Nr"   r#   r$   r%   r   )r'   r   r   writer   r)   r*   r+   seekr,   r-   r   )r   r    r1   r/   r0   r   r   r   r2   S   s   &
zBackoff.writec                 C  s   t | jD ]>}z| j| W n+ ty     ty: } z|| jd kr& tjd| |d W Y d }~nd }~ww t	| 
| qd S )Nr"   zFailed to delete artifact=r%   )r'   r   r   remover   r)   r*   r+   r,   r-   r   r.   r   r   r   r4   b   s    zBackoff.removeN)r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )r    r!   r   r   )r    r!   r1   r   r   r   )r    r!   r   r   )	__name__
__module____qualname____doc__r   r   r(   r2   r4   r   r   r   r   r      s    


r   )FileSystemArtifactStore.r   _)
__future__r   loggingr,   typingr   optuna.artifacts.exceptionsr   	getLoggerr5   r*   r   optuna.artifacts._protocolr   r   optuna.artifactsr9   r;   __annotations__r   r   r   r   <module>   s    
]