o
    }oi                     @  s8  U d dl mZ d dlmZ d dlmZ d dlmZ d dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ ernd dlZn
d dlmZ edZg dZded< ddgZ dZ!ed ddZ"G dd deZ#d!ddZ$dS )"    )annotations)ThreadPoolExecutor)AbstractContextManager)contextmanagerN)TracebackType)Any)	Generator)IO)TYPE_CHECKING)BaseStorage)GrpcStorageProxy)JournalFileBackend)NamedTemporaryFilePool)_LazyImportgrpc)inmemorysqlitecached_sqlitejournaljournal_redisgrpc_rdbgrpc_journal_filez	list[Any]STORAGE_MODESr   r   i,  returnGenerator[None, None, None]c                  c  s    t jdkrtjtjdddd} nd} tjtj| dd t	| d	}t jdkrGd
d l
}|| |jd d V  || |jd nd
d l}|||j d V  |||j |  d S )Nwin32PROGRAMDATAzC:\ProgramDataoptunazoptuna_find_free_port.lockz/tmp/optuna_find_free_port.lockT)exist_okwr      )sysplatformospathjoinenvirongetmakedirsdirnameopenmsvcrtlockingfilenoLK_LOCKLK_UNLCKfcntlflockLOCK_EXLOCK_UNclose)	lock_pathlockfiler+   r0    r7   K/home/ubuntu/.local/lib/python3.10/site-packages/optuna/testing/storages.py_lock_to_search_for_free_port4   s(   


r9   c                   @  s8   e Zd ZdddZdd
dZ	ddddZd ddZdS )!StorageSupplierstorage_specifierstrkwargsr   r   Nonec                 K  s4   || _ || _d | _d | _d | _d | _d | _d | _d S N)r;   
extra_argstempfileserverthreadproxystoragebackend_storage)selfr;   r=   r7   r7   r8   __init__R   s   
zStorageSupplier.__init__optuna.storages.InMemoryStorage | optuna.storages._CachedStorage | optuna.storages.RDBStorage | optuna.storages.JournalStorage | optuna.storages.GrpcStorageProxyc                 C  s
  | j dkrt| jdkrtdtj | _| jS d| j v rMt 	 | _	d
| j	j}tjj|fdddtiii| j}d	| j v rGtj|n|| _| jS | j d
krmtjjd}| jdt |_tj|| _| jS | j dkr| jdt 	 | _	| j	d usJ tjtjj| j	j}| j|| jdd| _| jS d| j v r| jdt 	 | _	| j	d usJ t| j	j}tj|| _| jS | j dkrt 	 | _	d
| j	j}tj|| _| | j| _| jS | j dkrd| jv sJ | | jd | _| jS J )Nr   r   z.InMemoryStorage does not accept any arguments!r   zsqlite:///{}engine_kwargsconnect_argstimeoutcachedr   zredis://localhostredisr   filethread_poolrP   r   r   
grpc_proxybase_storage)r;   lenr@   
ValueErrorr   storagesInMemoryStoragerE   r   rA   formatname
RDBStorageSQLITE3_TIMEOUT_CachedStorager   JournalRedisBackendr'   	fakeredisFakeStrictRedis_redisJournalStorager   _create_proxyrF   )rG   urlrdb_storagejournal_redis_storagerE   file_storager7   r7   r8   	__enter__\   sp   
	/


&



zStorageSupplier.__enter__NrE   r   rP   ThreadPoolExecutor | Noner   c                 C  s   t  7 t }tjjjj|d||d| _tj| jj	d| _
| j
	  td|d| _| jjdd | jW  d    S 1 s=w   Y  d S )N	localhostrQ   )target)hostport<   )rL   )r9   _find_free_portr   rV   _grpcrB   make_server	threadingThreadstartrC   r   rD   wait_server_ready)rG   rE   rP   rl   r7   r7   r8   rb      s   

$zStorageSupplier._create_proxyexc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonec                 C  s   t | jtjjr| jj  n%t | jtjjr| jjj  n| j	dkr4t | j
tjjs.J | j
j  | jr<| j  | jrG| j  d | _| jrf| jd usQJ | jd  | j  d | _d | _d S d S )Nr      )
isinstancerE   r   rV   rZ   enginedisposer\   _backendr;   rF   rA   r4   rD   rB   rC   stopwaitr%   )rG   ru   rw   ry   r7   r7   r8   __exit__   s&   




zStorageSupplier.__exit__)r;   r<   r=   r   r   r>   )r   rI   r?   )rE   r   rP   rh   r   r   )ru   rv   rw   rx   ry   rz   r   r>   )__name__
__module____qualname__rH   rg   rb   r   r7   r7   r7   r8   r:   Q   s    


>r:   intc               	   C  sR   t  t jt j} tddD ]}z| d|f |W   S  ty$   Y qw J d)Ni2  i,3  ri   Fzmust not reach here)socketAF_INETSOCK_STREAMrangebindOSError)sockrl   r7   r7   r8   rn      s   
rn   )r   r   )r   r   )%
__future__r   concurrent.futuresr   
contextlibr   r   r#   r   r!   rq   typesr   typingr   r   r	   r
   r^   r   optuna.storagesr   r   optuna.storages.journalr   optuna.testing.tempfile_poolr   r   optuna._importsr   r   __annotations__STORAGE_MODES_HEARTBEATr[   r9   r:   rn   r7   r7   r7   r8   <module>   s@    
u