
    -jiXA                       S SK Jr  S SKJr  S SKJr  S SKrS SKrS SKrS SKJ	r	  S SKJ
r
  S SKrS SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJ r   \
(       a  S SK!r!S SK"J#r$  S SK%J&r&  S SK%J'r'  O \" S5      r&\" S5      r'\" S5      r!\" S5      r$S"S jr(\" S5       " S S\5      5       r) " S S5      r* " S  S!5      r+g)#    )annotations)	Container)SequenceN)Any)TYPE_CHECKING)experimental_class)_LazyImport)BaseDistribution)distribution_to_json)DuplicatedStudyError)UpdateFinishedTrialError)BaseStorage)DEFAULT_STUDY_NAME_PREFIX)FrozenStudy)StudyDirection)FrozenTrial)
TrialState)servicer)api_pb2)api_pb2_grpcz,optuna.storages._grpc.auto_generated.api_pb2z1optuna.storages._grpc.auto_generated.api_pb2_grpcgrpczoptuna.storages._grpc.servicerc                8    [         R                  " U  SU 3S/S9$ )N:)zgrpc.max_receive_message_length)options)r   insecure_channelhostports     V/home/james-whalen/.local/lib/python3.13/site-packages/optuna/storages/_grpc/client.pycreate_insecure_channelr!   '   s)      &$#J"K     z4.2.0c                     \ rS rSrSrSSS.S!S jjrS"S jrS#S$S	 jjrS"S
 jrS%S jr	S&S jr
 S#     S'S jjrS(S jrS)S jrS)S jrS*S jrS+S jrS,S jrS-S jrS-S jrS.S jrS#S/S jjr          S0S jr S#       S1S jjr        S2S jrS3S jrS3S jrS4S jrS5S jr  S6       S7S jjrS rg)8GrpcStorageProxy-   a4  gRPC client for :func:`~optuna.storages.run_grpc_proxy_server`.

Example:

    This is a simple example of using :class:`~optuna.storages.GrpcStorageProxy` with
    :func:`~optuna.storages.run_grpc_proxy_server`.

    .. code::

        import optuna
        from optuna.storages import GrpcStorageProxy

        storage = GrpcStorageProxy(host="localhost", port=13000)
        study = optuna.create_study(storage=storage)

    Please refer to the example in :func:`~optuna.storages.run_grpc_proxy_server` for the
    server side code.

Args:
    host: The hostname of the gRPC server.
    port: The port of the gRPC server.

.. warning::

    Currently, gRPC storage proxy in combination with an SQLite3 database may cause unexpected
    behaviors when calling :func:`optuna.delete_study` due to non-invalidated cache.
	localhosti2  r   c               <    Xl         X l        U R                  5         g N)_host_port_setup)selfr   r   s      r    __init__GrpcStorageProxy.__init__K   s    

r"   c                    [        U R                  U R                  5      U l        [        R
                  " U R                  5      U l        [        U R                  5      U l        g)z!Set up the gRPC channel and stub.N)	r!   r)   r*   _channelr   StorageServiceStub_stubGrpcClientCache_cacher,   s    r    r+   GrpcStorageProxy._setupP   s>    /

DJJG!44T]]C
%djj1r"   Nc                    [        U R                  U R                  5       n[        R                  " U5      R                  US9  SSS5        g! , (       d  f       g= f! [        R                   a  n[        S5      UeSnAff = f)zWait until the gRPC server is ready.

Args:
    timeout: The maximum time to wait in seconds. If :obj:`None`, wait indefinitely.
)timeoutNzGRPC connection timeout)r!   r)   r*   r   channel_ready_futureresultFutureTimeoutErrorConnectionError)r,   r8   channeles       r    wait_server_ready"GrpcStorageProxy.wait_server_readyV   sj    	D(TZZ@G))'299'9J A@@&& 	D!";<!C	Ds4    A  $AA  
AA  A   B4B  Bc                8    U R                   R                  5         g)zClose the gRPC channel.N)r0   closer5   s    r    rB   GrpcStorageProxy.closeb   s    r"   c                L    U R                   R                  5       nUS	 US	 US	 U$ )Nr0   r2   r4   )__dict__copyr,   states     r    __getstate__GrpcStorageProxy.__getstate__f   s/    ""$*'N(Or"   c                Z    U R                   R                  U5        U R                  5         g r(   )rE   updater+   rG   s     r    __setstate__GrpcStorageProxy.__setstate__m   s    U#r"   c                   [         R                  " U Vs/ s H7  nU[        R                  :X  a  [         R                  O[         R                  PM9     snU=(       d%    [
        [        [        R                  " 5       5      -   S9n U R                  R                  U5      nUR"                  $ s  snf ! [        R                   a9  nUR                  5       [        R                  R                  :X  a  [         Uee S nAff = f)N)
directions
study_name)r   CreateNewStudyRequestr   MINIMIZEMAXIMIZEr   struuiduuid4r2   CreateNewStudyr   RpcErrorcode
StatusCodeALREADY_EXISTSr   study_id)r,   rP   rQ   drequestresponser>   s          r    create_new_study!GrpcStorageProxy.create_new_studyq   s     // $#A %&)@)@$@  gFVFVV# "R%>TZZ\AR%R
	zz009H
     }} 	vvx4??999*1	s   >B-B2 2C?4C::C?c                :   [         R                  " US9n U R                  R                  U5        U R                  R                  U5        g ! [        R
                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = fN)r]   )r   DeleteStudyRequestr2   DeleteStudyr   rY   rZ   r[   	NOT_FOUNDKeyErrorr4   delete_study_cache)r,   r]   r_   r>   s       r    delete_studyGrpcStorageProxy.delete_study   sv    ,,h?	JJ""7+ 	&&x0 }} 	vvx4??444A%	s   A B!4BBc                .   [         R                  " X[        R                  " U5      S9n U R                  R                  U5        g ! [        R                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = fN)r]   keyvalue)r   SetStudyUserAttributeRequestjsondumpsr2   SetStudyUserAttributer   rY   rZ   r[   rg   rh   r,   r]   rn   ro   r_   r>   s         r    set_study_user_attr$GrpcStorageProxy.set_study_user_attr   sn    66djj.?
	JJ,,W5}} 	vvx4??444A%	   A B4BBc                .   [         R                  " X[        R                  " U5      S9n U R                  R                  U5        g ! [        R                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = frm   )r   SetStudySystemAttributeRequestrq   rr   r2   SetStudySystemAttributer   rY   rZ   r[   rg   rh   rt   s         r    set_study_system_attr&GrpcStorageProxy.set_study_system_attr   sn    88djj.?
	JJ..w7}} 	vvx4??444A%	rw   c                   [         R                  " US9n U R                  R                  U5      nUR                  $ ! [        R
                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = f)N)rQ   )r   GetStudyIdFromNameRequestr2   GetStudyIdFromNamer   rY   rZ   r[   rg   rh   r]   )r,   rQ   r_   r`   r>   s        r    get_study_id_from_name'GrpcStorageProxy.get_study_id_from_name   sr    33zJ	zz44W=H
    	 }} 	vvx4??444A%	   = B
4BB
c                   [         R                  " US9n U R                  R                  U5      nUR                  $ ! [        R
                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = frd   )r   GetStudyNameFromIdRequestr2   GetStudyNameFromIdr   rY   rZ   r[   rg   rh   rQ   )r,   r]   r_   r`   r>   s        r    get_study_name_from_id'GrpcStorageProxy.get_study_name_from_id   sr    33XF	zz44W=H
 """	 }} 	vvx4??444A%	r   c                   [         R                  " US9n U R                  R                  U5      nUR                   Vs/ s H7  nU[         R                  :X  a  [        R                  O[        R                  PM9     sn$ ! [        R
                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = fs  snf rd   )r   GetStudyDirectionsRequestr2   GetStudyDirectionsr   rY   rZ   r[   rg   rh   rP   rS   r   rT   )r,   r]   r_   r`   r>   r^   s         r    get_study_directions%GrpcStorageProxy.get_study_directions   s    33XF	zz44W=H ((
( ()G,<,<'<N##.BYBYY(
 	
	 }} 	vvx4??444A%	
s   B  >C C4CCc                   [         R                  " US9n U R                  R                  U5      nUR                  R                  5        VVs0 s H  u  pVU[        R                  " U5      _M     snn$ ! [        R
                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = fs  snnf rd   )r   GetStudyUserAttributesRequestr2   GetStudyUserAttributesr   rY   rZ   r[   rg   rh   user_attributesitemsrq   loadsr,   r]   r_   r`   r>   rn   ro   s          r    get_study_user_attrs%GrpcStorageProxy.get_study_user_attrs   s    77J	zz88AH
 :B9Q9Q9W9W9YZ9Y:3TZZ&&9YZZ	 }} 	vvx4??444A%	 [   A5 #C5C	4B==Cc                   [         R                  " US9n U R                  R                  U5      nUR                  R                  5        VVs0 s H  u  pVU[        R                  " U5      _M     snn$ ! [        R
                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = fs  snnf rd   )r   GetStudySystemAttributesRequestr2   GetStudySystemAttributesr   rY   rZ   r[   rg   rh   system_attributesr   rq   r   r   s          r    get_study_system_attrs'GrpcStorageProxy.get_study_system_attrs   s    998L	zz::7CH
 :B9S9S9Y9Y9[\9[:3TZZ&&9[\\	 }} 	vvx4??444A%	 ]r   c                   [         R                  " 5       nU R                  R                  U5      nUR                   VVVVs/ s H  n[        UR                  UR                  S UR                   Vs/ s H7  nU[         R                  :X  a  [        R                  O[        R                  PM9     snUR                  R                  5        VVs0 s H  u  pVU[        R                  " U5      _M     snnUR                   R                  5        VVs0 s H  u  pVU[        R                  " U5      _M     snnS9PM     snnnn$ s  snf s  snnf s  snnf s  snnnnf )N)r]   rQ   	directionrP   
user_attrssystem_attrs)r   GetAllStudiesRequestr2   GetAllStudiesstudiesr   r]   rQ   rP   rS   r   rT   r   r   rq   r   r   )r,   r_   r`   studyr^   rn   ro   s          r    get_all_studies GrpcStorageProxy.get_all_studies   s1   ..0::++G4" "))!
  *  ++ #--- 01G4D4D/DN++.JaJaa-
 >C=R=R=X=X=Z=ZzsCE**=Z >C=T=T=Z=Z=\=\zsCE**=\ *!
 	


s0   0E
2>E0E
#E
2 E
#E 5
E
E
c                x   Uc  [         R                  " USS9nO*[         R                  " U[        R                  " U5      SS9n U R                  R                  U5      nUR                  $ ! [        R                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = f)NT)r]   template_trial_is_noneF)r]   template_trialr   )r   CreateNewTrialRequestgrpc_servicer_to_proto_trialr2   CreateNewTrialr   rY   rZ   r[   rg   rh   trial_id)r,   r]   r   r_   r`   r>   s         r    create_new_trial!GrpcStorageProxy.create_new_trial   s    !33X^bcG33!,<<^L',G
	zz009H
    	 }} 	vvx4??444A%	s   A, ,B9 4B44B9c                   [         R                  " UUU[        U5      S9n U R                  R	                  U5        g ! [
        R                   a  nUR                  5       [
        R                  R                  :X  a  [        UeUR                  5       [
        R                  R                  :X  a  [        UeUR                  5       [
        R                  R                  :X  a  [        Uee S nAff = f)N)r   
param_nameparam_value_internaldistribution)r   SetTrialParameterRequestr   r2   SetTrialParameterr   rY   rZ   r[   rg   rh   FAILED_PRECONDITIONr   INVALID_ARGUMENT
ValueError)r,   r   r   r   r   r_   r>   s          r    set_trial_param GrpcStorageProxy.set_trial_param  s     22!!5-l;	

	JJ((1}} 	vvx4??444A%T__@@@.A5T__=== a'	s   > C1BC,,C1c                   [         R                  " U[        R                  " U5      US9n U R                  R                  U5      nUR                  $ ! [        R                   al  nUR                  5       [        R                  R                  :X  a  [        UeUR                  5       [        R                  R                  :X  a  [        Uee S nAff = f)N)r   rH   values)r   SetTrialStateValuesRequestr   _to_proto_trial_stater2   SetTrialStateValuesr   rY   rZ   r[   rg   rh   r   r   trial_updated)r,   r   rH   r   r_   r`   r>   s          r    set_trial_state_values'GrpcStorageProxy.set_trial_state_values  s     4455e<

	zz55g>H %%% }} 	vvx4??444A%T__@@@.A5	s   A C'A'CCc                l   [         R                  " XUS9n U R                  R                  U5        g ! [        R
                   al  nUR                  5       [        R                  R                  :X  a  [        UeUR                  5       [        R                  R                  :X  a  [        Uee S nAff = f)N)r   stepintermediate_value)r    SetTrialIntermediateValueRequestr2   SetTrialIntermediateValuer   rY   rZ   r[   rg   rh   r   r   )r,   r   r   r   r_   r>   s         r    set_trial_intermediate_value-GrpcStorageProxy.set_trial_intermediate_value.  s     ::=O
	JJ009}} 	vvx4??444A%T__@@@.A5	s   3 B3A'B..B3c                   [         R                  " X[        R                  " U5      S9n U R                  R                  U5        g ! [        R                   al  nUR                  5       [        R                  R                  :X  a  [        UeUR                  5       [        R                  R                  :X  a  [        Uee S nAff = fN)r   rn   ro   )r   SetTrialUserAttributeRequestrq   rr   r2   SetTrialUserAttributer   rY   rZ   r[   rg   rh   r   r   r,   r   rn   ro   r_   r>   s         r    set_trial_user_attr$GrpcStorageProxy.set_trial_user_attr>  s    66djj.?
	JJ,,W5}} 	vvx4??444A%T__@@@.A5	   A CA'CCc                   [         R                  " X[        R                  " U5      S9n U R                  R                  U5        g ! [        R                   al  nUR                  5       [        R                  R                  :X  a  [        UeUR                  5       [        R                  R                  :X  a  [        Uee S nAff = fr   )r   SetTrialSystemAttributeRequestrq   rr   r2   SetTrialSystemAttributer   rY   rZ   r[   rg   rh   r   r   r   s         r    set_trial_system_attr&GrpcStorageProxy.set_trial_system_attrL  s    88djj.?
	JJ..w7}} 	vvx4??444A%T__@@@.A5	r   c                   [         R                  " XS9n U R                  R                  U5      nUR                  $ ! [        R
                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = f)N)r]   trial_number)r   'GetTrialIdFromStudyIdTrialNumberRequestr2    GetTrialIdFromStudyIdTrialNumberr   rY   rZ   r[   rg   rh   r   )r,   r]   r   r_   r`   r>   s         r    'get_trial_id_from_study_id_trial_number8GrpcStorageProxy.get_trial_id_from_study_id_trial_numberZ  su    AA
	zzBB7KH
    	 }} 	vvx4??444A%	r   c                B   [         R                  " US9n U R                  R                  U5      n[        R                  " UR                  5      $ ! [        R
                   a9  nUR                  5       [        R                  R                  :X  a  [        Uee S nAff = f)N)r   )r   GetTrialRequestr2   GetTrialr   rY   rZ   r[   rg   rh   r   _from_proto_trialtrial)r,   r   r_   r`   r>   s        r    	get_trialGrpcStorageProxy.get_trialf  s{    ))8<	zz**73H
 ..x~~>>	 }} 	vvx4??444A%	s   A B%4BBc                v    U R                   R                  X5      nU(       a  [        R                  " U5      $ U$ r(   )r4   get_all_trialsrF   deepcopy)r,   r]   r   statestrialss        r    r   GrpcStorageProxy.get_all_trialsp  s.     ++H=(0t}}V$<f<r"   )r4   r0   r)   r*   r2   )r   rU   r   intreturnNoner   r   r(   )r8   zfloat | Noner   r   )r   dict[Any, Any])rH   r   r   r   )rP   zSequence[StudyDirection]rQ   z
str | Noner   r   r]   r   r   r   )r]   r   rn   rU   ro   r   r   r   )rQ   rU   r   r   )r]   r   r   rU   )r]   r   r   zlist[StudyDirection])r]   r   r   zdict[str, Any])r   zlist[FrozenStudy])r]   r   r   zFrozenTrial | Noner   r   )
r   r   r   rU   r   floatr   r
   r   r   )r   r   rH   r   r   zSequence[float] | Noner   bool)r   r   r   r   r   r   r   r   )r   r   rn   rU   ro   r   r   r   )r]   r   r   r   r   r   )r   r   r   r   )TN)r]   r   r   r   r   Container[TrialState] | Noner   list[FrozenTrial])__name__
__module____qualname____firstlineno____doc__r-   r+   r?   rB   rI   rM   ra   rj   ru   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__ r"   r    r$   r$   -   sW   8 '2u 
2
D
 NR!2!@J!	!$
1		!#
[]
,!"  $	
 ' 
4 RV&&$.&8N&	&(#&<A	 
!? /3	== = -	=
 
= =r"   r$   c                  R    \ rS rSrS	S jrS
S jr      SS jrS
S jrSS jrSr	g)r3   iz  c                R    0 U l         Xl        [        R                  " 5       U l        g r(   )r   grpc_client	threadingLocklock)r,   r   s     r    r-   GrpcClientCache.__init__{  s    8:&NN$	r"   c                    U R                      U R                  R                  US 5        S S S 5        g ! , (       d  f       g = fr(   )r  r   pop)r,   r]   s     r    ri   "GrpcClientCache.delete_study_cache  s'    YYLLXt, YYs	   3
Ac                   U R                      U R                  U5        U R                  U   nUbA  UR                  R	                  5        VVs0 s H  u  pEUR
                  U;   d  M  XE_M     nnnOUR                  n[        [        UR                  5       S S95      nUsS S S 5        $ s  snnf ! , (       d  f       g = f)Nc                    U R                   $ r(   )number)ts    r    <lambda>0GrpcClientCache.get_all_trials.<locals>.<lambda>  s    r"   )rn   )	r   _read_trials_from_remote_storager   r   r   rH   listsortedr   )r,   r]   r   r   r  r	  r   s          r    r   GrpcClientCache.get_all_trials  s     YY11(;LL*E!5:\\5G5G5I_5I	QWWX^M^)&)5I_&6HIJF Y
 ` Ys$   AB1B+(B+.3B1+B11
B?c                j   XR                   ;  a  [        5       U R                   U'   U R                   U   n[        R                  " UUR                  UR
                  S9n U R                  R                  U5      nUR                  (       d  g UR                   H*  n[         R"                  " U5      nU R%                  X5        M,     g ! [        R                   aU  nUR                  5       [        R                  R                  :X  a#  U R                   R                  US 5        [        Uee S nAff = f)N)r]   included_trial_idstrial_id_greater_than)r   GrpcClientCacheEntryr   GetTrialsRequestunfinished_trial_idslast_finished_trial_idr   	GetTrialsr   rY   rZ   r[   rg   r  rh   r   r   r   _add_trial_to_cache)r,   r]   r   reqresr>   trial_protor   s           r    r  0GrpcClientCache._read_trials_from_remote_storage  s    <<'%9%;DLL"X&&&$99"'">">

	"",,S1C zz::K!33K@E$$X5 & }} 	vvx4??444  40A%		s   !C	 	D2AD--D2c                p   U R                   U   nX#R                  UR                  '   UR                  R	                  5       (       d&  UR
                  R                  UR                  5        g [        UR                  UR                  5      Ul	        UR
                  R                  UR                  5        g r(   )r   r   r  rH   is_finishedr  add	_trial_idmaxr  discard)r,   r]   r   r   s       r    r  #GrpcClientCache._add_trial_to_cache  s    X&%*U\\"{{&&((&&**5??;'*5+G+G'Y$""**5??;r"   )r   r  r   N)r   zapi_pb2_grpc.StorageServiceStubr   r   r   )r]   r   r   r   r   r   )r]   r   r   r   r   r   )
r   r   r   r   r-   ri   r   r  r  r   r   r"   r    r3   r3   z  s3    %
-%A	60	<r"   r3   c                      \ rS rSrSS jrSrg)r  i  c                >    0 U l         [        5       U l        SU l        g )Nr   )r   setr  r  r5   s    r    r-   GrpcClientCacheEntry.__init__  s    .0.1e!+-#r"   )r  r   r  Nr   )r   r   r   r   r-   r   r   r"   r    r  r    s    .r"   r  )r   rU   r   r   r   zgrpc.Channel),
__future__r   collections.abcr   r   rF   rq   r   typingr   r   rV   optuna._experimentalr   optuna._importsr	   optuna.distributionsr
   r   optuna.exceptionsr   r   optuna.storages._baser   r   optuna.study._frozenr   optuna.study._study_directionr   optuna.trial._frozenr   optuna.trial._stater   r   optuna.storages._grpcr   r   $optuna.storages._grpc.auto_generatedr   r   r!   r$   r3   r  r   r"   r    <module>r6     s    " % $        3 ' 1 5 2 6 - ; , 8 , * ?<AHIGRSLvD @AM GI={ I= I=X
9< 9<x. .r"   