
    &`i              	          d dl Z d dl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mZmZmZmZmZmZ d dlZd dlmZ d dlmc mc mZ d dlmc mc mZ d dlmZm Z  d dl!m"Z" d dl#m$Z$m%Z%m&Z&m'Z' d d	l(m)Z) d d
l*m+Z+ d dl,m-Z- d dl.m/Z/  ej0        e1          Z2ej3        j4        ej3        j5        ej3        j6        ej3        j7        ej3        j8        fZ9dZ:dZ;dZ<g e"j=        de:fde:fde;fde<fddde;dz
  fdZ> e? ej@        dd                    ZAdZBdZC G d dejD                  ZE G d d ejF                  ZG G d! d"          ZH G d# d$eH          ZI G d% d&eH          ZJ G d' d(eH          ZK G d) d*eH          ZL G d+ d,          ZM G d- d.eM          ZN	 dHd0ejO        d/eeePef                  d1ePd2dfd3ZQd4ee	         d2edeEeeE         f         fd5ZR G d6 d7          ZS G d8 d9          ZTd/eeePef                  fd:ZUe G d; d<                      ZVd=ed2ePfd>ZWd?eXd=ed2eYfd@ZZdAe[dBe[d2eYfdCZ\ G dD dE          Z] G dF dG          Z^dS )I    N)OrderedDict)Future)	dataclass)AnyCallableDictListOptionalTupleUnion)extract_signatureget_signature)ray_constants)is_class_method	is_cythonis_function_or_methodis_static_method)check_oversized_function)ray)validate_options)	INT32_MAXii0u  i'	 zgrpc.max_send_message_lengthzgrpc.max_receive_message_lengthzgrpc.keepalive_time_mszgrpc.keepalive_timeout_ms)z#grpc.keepalive_permit_without_calls   )z!grpc.http2.max_pings_without_datar   z,grpc.http2.min_ping_interval_without_data_ms2   )zgrpc.http2.max_ping_strikesr   RAY_CLIENT_SERVER_MAX_THREADSd   i  P l        c                        e Zd Zdeeef         fdZd Z fdZ fdZ	 fdZ
d Z fdZed	             Zd
efdZdeegdf         d
dfdZ fdZddZ xZS )ClientObjectRefidc                 R   t          j                    | _        t          j                    j        | _        d | _        t          |t                    r| 
                    |           d S t          |t                    r	|| _        d S t          d                    |                    NzUnexpected type for id {})	threadingLock_mutexr   get_contextclient_worker_worker
_id_future
isinstancebytes_set_idr   	TypeErrorformatselfr   s     j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/client/common.py__init__zClientObjectRef.__init__Y   s    n&&((6b%   	DLLF## 	D DOOO7>>rBBCCC    c                    | j         || j                                         re	 |                                 s!| j                             | j                   d S d S # t
          $ r t                              d           Y d S w xY wd S d S )NzException in ObjectRef is ignored in destructor. To receive this exception in application code, call a method on the actor reference before its destructor is run.)r&   is_connectedis_nilcall_releaser   	Exceptionloggerinfor.   s    r/   __del__zClientObjectRef.__del__d   s    <#(A(A(C(C#	{{}} 7L--dg666667 7        	 $###s   3A $B Bc                 l    |                                   t                                                      S N_wait_for_idsuperbinaryr.   	__class__s    r/   r@   zClientObjectRef.binaryq   )    ww~~r1   c                 l    |                                   t                                                      S r<   r>   r?   hexrA   s    r/   rF   zClientObjectRef.hexu   '    ww{{}}r1   c                 l    |                                   t                                                      S r<   r>   r?   r4   rA   s    r/   r4   zClientObjectRef.is_nily   rC   r1   c                 R    |                                   t          | j                  S r<   r>   hashr   r9   s    r/   __hash__zClientObjectRef.__hash__}   "    DG}}r1   c                 l    |                                   t                                                      S r<   )r>   r?   task_idrA   s    r/   rP   zClientObjectRef.task_id   s)    ww   r1   c                 *    |                                  S r<   r@   r9   s    r/   r   zClientObjectRef.id       {{}}r1   returnc                 z    t                      dt          dd ffd}|                     |           | _        S )NdatarT   c                     t          | t                    r                    |            dS                     |            dS )zNSchedules a callback to set the exception or result
            in the Future.N)r(   r6   set_exception
set_result)rV   futs    r/   
set_futurez*ClientObjectRef.future.<locals>.set_future   sI     $	** %!!$'''''t$$$$$r1   )r   r   _on_completed
object_ref)r.   r[   rZ   s     @r/   futurezClientObjectRef.future   s\    hh	%S 	%T 	% 	% 	% 	% 	% 	% 	:&&& 
r1   py_callbackNc                     dt           t          j        t          f         ddffd}| j                            | |           dS )a  Register a callback that will be called after Object is ready.
        If the ObjectRef is already ready, the callback will be called soon.
        The callback should take the result as the only argument. The result
        can be an exception object in case of task error.
        resprT   Nc                    ddl m} t          | t                    r| }n\t          | t                    r ||           }n;| j        }d }|j        s || j        j                  }n || j        j                  } |           d S )Nr   )loads_from_server)	ray.util.client.client_picklerrc   r(   r6   	bytearraygetvaliderrorrV   )ra   rc   rV   objr_   s       r/   deserialize_objz6ClientObjectRef._on_completed.<locals>.deserialize_obj   s     IHHHHH$	** 
<D),, <((..hy <,,TX^<<DD,,TX];;DKr1   )r   ray_client_pb2DataResponser6   r&   register_callback)r.   r_   rj   s    ` r/   r\   zClientObjectRef._on_completed   s^    	3Y>?		 	 	 	 	 	& 	&&t_=====r1   c                 ~    t                                          |           | j                            |           d S r<   r?   r*   r&   call_retainr.   r   rB   s     r/   r*   zClientObjectRef._set_id   5      $$$$$r1   c                     | j         r^| j        5  | j         r5|                     | j                             |                     d | _         d d d            d S # 1 swxY w Y   d S d S N)timeoutr'   r#   r*   resultr.   ru   s     r/   r>   zClientObjectRef._wait_for_id       ? 	+ + +? +LL!7!7!7!H!HIII&*DO+ + + + + + + + + + + + + + + + + +	+ 	+   =AA Ar<   )__name__
__module____qualname__r   r)   r   r0   r:   r@   rF   r4   rM   rP   propertyr   r^   r   r   r\   r*   r>   __classcell__rB   s   @r/   r   r   X   sZ       	D5/ 	D 	D 	D 	D                          ! ! ! ! !   X    $>3%+)> >4 > > > >8% % % % %+ + + + + + + +r1   r   c                        e Zd Z	 ddeeef         dee         fdZd Z	 fdZ
 fdZ fdZd	 Zed
             Z fdZddZ xZS )ClientActorRefFr   weak_refc                 `   || _         t          j                    | _        t	          j                    j        | _        t          |t                    r| 
                    |           d | _        d S t          |t                    r	|| _        d S t          d                    |                    r    )	_weak_refr!   r"   r#   r   r$   r%   r&   r(   r)   r*   r'   r   r+   r,   )r.   r   r   s      r/   r0   zClientActorRef.__init__   s    
 "n&&((6b%   	DLL"DOOOF## 	D DOOO7>>rBBCCCr1   c                 "   | j         rd S | j        || j                                        re	 |                                 s!| j                            | j                   d S d S # t          $ r t                              d           Y d S w xY wd S d S )NzException from actor creation is ignored in destructor. To receive this exception in application code, call a method on the actor reference before its destructor is run.)	r   r&   r3   r4   r5   r   r6   r7   debugr9   s    r/   r:   zClientActorRef.__del__   s    > 	F<#(A(A(C(C#	{{}} 7L--dg666667 7        	 $###s   3A" "$B
	B
c                 l    |                                   t                                                      S r<   r=   rA   s    r/   r@   zClientActorRef.binary   rC   r1   c                 l    |                                   t                                                      S r<   rE   rA   s    r/   rF   zClientActorRef.hex   rG   r1   c                 l    |                                   t                                                      S r<   rI   rA   s    r/   r4   zClientActorRef.is_nil   rC   r1   c                 R    |                                   t          | j                  S r<   rK   r9   s    r/   rM   zClientActorRef.__hash__   rN   r1   c                 *    |                                  S r<   rR   r9   s    r/   r   zClientActorRef.id   rS   r1   c                 ~    t                                          |           | j                            |           d S r<   ro   rq   s     r/   r*   zClientActorRef._set_id   rr   r1   Nc                     | j         r^| j        5  | j         r5|                     | j                             |                     d | _         d d d            d S # 1 swxY w Y   d S d S rt   rv   rx   s     r/   r>   zClientActorRef._wait_for_id   ry   rz   )Fr<   )r{   r|   r}   r   r)   r   r
   boolr0   r:   r@   rF   r4   rM   r~   r   r*   r>   r   r   s   @r/   r   r      s        $)D D%- D 4.D D D D                               X% % % % %+ + + + + + + +r1   r   c                       e Zd ZdS )
ClientStubN)r{   r|   r}    r1   r/   r   r     s        Dr1   r   c                   b    e Zd ZdZddZd Zd Zd ZddZd Z	d	 Z
d
ej        fdZd
efdZdS )ClientRemoteFuncav  A stub created on the Ray Client to represent a remote
    function that can be exectued on the cluster.

    This class is allowed to be passed around between remote functions.

    Args:
        _func: The actual function to execute remotely
        _name: The original name of the function
        _ref: The ClientObjectRef of the pickled code of the function, _func
    Nc                     t          j                    | _        || _        |j        | _        t          |          | _        d | _        t          
                                | _        t          |          | _        d S r<   )r!   r"   _lock_funcr{   _namer   
_signature_refClientSideRefIDgenerate_id_client_side_refr   _options)r.   foptionss      r/   r0   zClientRemoteFunc.__init__  s^    ^%%

Z
'**	 / ; ; = =(11r1   c                 2    t          d| j         d          )Nz/Remote function cannot be called directly. Use z.remote method insteadr+   r   r.   argskwargss      r/   __call__zClientRemoteFunc.__call__  s-    6:6 6 6
 
 	
r1   c                 j     | j         j        |i | t          t          j        | g|R i |          S r<   r   bindreturn_refsr   call_remoter   s      r/   remotezClientRemoteFunc.remote#  sF     	d-f---3?4A$AAA&AABBBr1   c                 "    t          | |          S r<   OptionWrapperr.   r   s     r/   r   zClientRemoteFunc.options)      T6***r1   c                 B    |g }|i }  | j         di |j        |i |S Nr   r   r   r.   r   r   option_argss       r/   _remotezClientRemoteFunc._remote,  @    <D>F1|t|**k**14B6BBBr1   c                 (    d| j         d| j        dS )NzClientRemoteFunc(, )r   r   r9   s    r/   __repr__zClientRemoteFunc.__repr__3       -1ZZZCCr1   c                 P   | j         5  | j        ~t                      | _        t          j                            | j                  }t          || j        dd            t          j        	                    || j
        j                  | _        d d d            d S # 1 swxY w Y   d S )Nzremote functionclient_ref_id)r   r   InProgressSentinelr   worker_dumps_from_clientr   r   r   _put_pickledr   r   r.   rV   s     r/   _ensure_refzClientRemoteFunc._ensure_ref6  s    Z 	 	y  /00	z44TZ@@ )tz;LdSSSJ33(=(@ 4  		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   BBB"BrT   c                     |                                   t          j                    }t          j        j        |_        | j        |_        | j        j        |_	        t          || j        d           |S Nbaseline_options)r   rk   
ClientTaskFUNCTIONtyper   namer   r   
payload_idset_task_optionsr   r.   tasks     r/   _prepare_client_taskz%ClientRemoteFunc._prepare_client_taskJ  s]    (**"-6	J	),t}.@AAAr1   c                 H    | j         sd S | j                             d          S Nnum_returns)r   rf   r9   s    r/   _num_returnszClientRemoteFunc._num_returnsS  s'    } 	4}  ///r1   r<   NN)r{   r|   r}   __doc__r0   r   r   r   r   r   r   rk   r   r   intr   r   r1   r/   r   r     s        	 	2 2 2 2
 
 
C C C+ + +C C C CD D D  (n&?    0c 0 0 0 0 0 0r1   r   c                   z    e Zd ZdZddZd Zd ZddZd	 Zdd
Z	d Z
d Zdej        fdZedefd            ZdS )ClientActorClassa5  A stub created on the Ray Client to represent an actor class.

    It is wrapped by ray.remote and can be executed on the cluster.

    Args:
        actor_cls: The actual class to execute remotely
        _name: The original name of the class
        _ref: The ClientObjectRef of the pickled `actor_cls`
    Nc                 *   || _         t          j                    | _        |j        | _        t          j        t          |j	        d                    | _
        d | _        t                                          | _        t          |          | _        d S )NTignore_first
parameters)	actor_clsr!   r"   r   r{   r   inspect	Signaturer   r0   _init_signaturer   r   r   r   r   r   )r.   r   r   s      r/   r0   zClientActorClass.__init__d  s    "^%%
'
&0();$OOO 
  
  
 	 / ; ; = =(11r1   c                 2    t          d| j         d          )Nz2Remote actor cannot be instantiated directly. Use z.remote() insteadr   r   s      r/   r   zClientActorClass.__call__o  s-    1:1 1 1
 
 	
r1   c                 P   | j         5  | j        ~t                      | _        t          j                            | j                  }t          || j        dd            t          j        	                    || j
        j                  | _        d d d            d S # 1 swxY w Y   d S )Nactorr   )r   r   r   r   r   r   r   r   r   r   r   r   r   s     r/   r   zClientActorClass._ensure_refu  s    Z 	 	y  /00	z44T^DD )tz7DIIIJ33(=(@ 4  		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	r   rT   ClientActorHandlec                      | j         j        |i | t          j        | g|R i |}t	          |          dk    sJ t          t          |d                   |           S Nr   r   )actor_class)r   r   r   r   lenr   r   )r.   r   r   futuress       r/   r   zClientActorClass.remote  sr    !!426222/$8888887||q     
!;!;NNNNr1   c                 "    t          | |          S r<   )ActorOptionWrapperr   s     r/   r   zClientActorClass.options  s    !$///r1   c                 B    |g }|i }  | j         di |j        |i |S r   r   r   s       r/   r   zClientActorClass._remote  r   r1   c                 (    d| j         d| j        dS )NzClientActorClass(r   r   r   r9   s    r/   r   zClientActorClass.__repr__  r   r1   c                 P    || j         vrt          d          t          d          )NzNot a class attributezstatic methods)__dict__AttributeErrorNotImplementedErrorr.   keys     r/   __getattr__zClientActorClass.__getattr__  s-    dm## !8999!"2333r1   c                     |                                   t          j                    }t          j        j        |_        | j        |_        | j        j        |_	        t          || j        d           |S r   )r   rk   r   ACTORr   r   r   r   r   r   r   r   r   s     r/   r   z%ClientActorClass._prepare_client_task  s]    (**"-3	J	),t}.@AAAr1   c                      dS )Nr   r   r   r1   r/   r   zClientActorClass._num_returns  s    qr1   r<   )rT   r   r   )r{   r|   r}   r   r0   r   r   r   r   r   r   r   rk   r   r   staticmethodr   r   r   r1   r/   r   r   Y  s         	2 	2 	2 	2
 
 
  O O O O0 0 0C C C CD D D4 4 4
n&?     #    \  r1   r   c                        e Zd ZdZ	 ddedee         fdZdee	         f fdZ
edefd            Zdefd	Zdefd
Zd Zd Zd Z xZS )r   a  Client-side stub for instantiated actor.

    A stub created on the Ray Client to represent a remote actor that
    has been started on the cluster.  This class is allowed to be passed
    around between remote functions.

    Args:
        actor_ref: A reference to the running actor given to the client. This
          is a serialized version of the actual handle as an opaque token.
    N	actor_refr   c           
      |   || _         d | _        |i | _        i | _        t	          j        |j        t                    D ]m\  }}t          |dd           | j        |<   t	          j	        t          |t          |          pt          |j        |                               | j        |<   nd S d | _        d | _        d S )N__ray_num_returns__r   r   )r   _dir_method_num_returns_method_signaturesr   
getmembersr   r   getattrr   r   r   r   )r.   r   r   method_name
method_objs        r/   r0   zClientActorHandle.__init__  s    
 #)-	"')D$&(D#+2+=%'<, ,  'Z 9@ 5t9 9(5 8?7H0" !0
 ; ; !X#3K4I;#W#W     
8 
8 
8'44 $ (,D$&*D###r1   rT   c                    | j         | j                                         S t          j                    r-|                                  | j                                         S t                                                      S r<   )r  keysr   r3   _init_class_infor?   __dir__rA   s    r/   r  zClientActorHandle.__dir__  sm    #/+00222 	3!!###+00222ww   r1   c                     | j         S r<   )r   r9   s    r/   	_actor_idzClientActorHandle._actor_id  s
    ~r1   c                 *    t          | j                  S r<   )rL   r  r9   s    r/   rM   zClientActorHandle.__hash__  s    DN###r1   c                 B    t          |           t          |          k    S r<   )rL   )r.   _ClientActorHandle__values     r/   __eq__zClientActorHandle.__eq__  s    DzzT']]**r1   c                 &   |dk    rt          d| d          | j        |                                  || j        vrt          d| d          t	          | || j                            |          | j                            |                    S )Nr  z!ClientActorRef has no attribute '')r   r  r  r  ClientRemoteMethodrf   r   s     r/   r   zClientActorHandle.__getattr__  s    '''
 !!KS!K!K!KLLL#+!!###d--- !KS!K!K!KLLL!$((--#'',,	
 
 	
r1   c                 D    d| j         j                                        z  S )NzClientActorHandle(%s))r   r   rF   r9   s    r/   r   zClientActorHandle.__repr__  s    &$.*;*?*?*A*ABBr1   c                    t          j        d          d             }t          j        |                    |                     \  | _        }i | _        |                                D ]"\  }}t          j        |          | j        |<   #d S )Nr   )num_cpusc                     | j         | j        fS r<   )_ray_method_num_returns_ray_method_signatures)xs    r/   get_class_infoz:ClientActorHandle._init_class_info.<locals>.get_class_info  s    ,a.FFFr1   r   )r   r   rf   r  r  itemsr   r   )r.   r  method_parametersmethodr   s        r/   r  z"ClientActorHandle._init_class_info   s    	Q				G 	G 
 		G 7:g!!$''7
 7
3 "3 #%"3"9"9";"; 	W 	WFJ.5.?:.V.V.VD#F++	W 	Wr1   r<   )r{   r|   r}   r   r   r
   r   r0   r	   strr  r~   r  r   rM   r   r  r   r   r  r   r   s   @r/   r   r     s       	 	 37+ +!+ ./+ + + +>!c ! ! ! ! ! ! >    X$# $ $ $ $+ + + + +
 
 
&C C CW W W W W W Wr1   r   c                   v    e Zd ZdZdedededej        fdZ	d Z
d Zd	 Zd
 ZddZdej        fdZdefdZdS )r  a  A stub for a method on a remote actor.

    Can be annotated with execution options.

    Args:
        actor_handle: A reference to the ClientActorHandle that generated
          this method and will have this method called upon it.
        method_name: The name of this method
    actor_handler  r   	signaturec                 >    || _         || _        || _        || _        d S r<   )_actor_handle_method_namer  r   )r.   r$  r  r   r%  s        r/   r0   zClientRemoteMethod.__init__  s'     *'#. #r1   c                 B    t          d| j         d| j         d          )NzDActor methods cannot be called directly. Instead of running 'object.z()', try 'object.z.remote()'.)r+   r(  r   s      r/   r   zClientRemoteMethod.__call__&  s>    6"&"36 6(6 6 6
 
 	
r1   c                 j     | j         j        |i | t          t          j        | g|R i |          S r<   r   r   s      r/   r   zClientRemoteMethod.remote-  sD    d-f---3?4A$AAA&AABBBr1   c                 8    d| j         d| j        d| j        dS )NzClientRemoteMethod(r   r   )r(  r'  r  r9   s    r/   r   zClientRemoteMethod.__repr__1  s2     $$$
 	
r1   c                 "    t          | |          S r<   r   r   s     r/   r   zClientRemoteMethod.options8  r   r1   Nc                 B    |g }|i }  | j         di |j        |i |S r   r   r   s       r/   r   zClientRemoteMethod._remote;  r   r1   rT   c                     t          j                    }t           j        j        |_        | j        |_        | j        j        j        |_	        |S r<   )
rk   r   METHODr   r(  r   r'  r   r   r   r   s     r/   r   z'ClientRemoteMethod._prepare_client_taskB  s<    (**"-4	%	,69r1   c                     | j         S r<   )r  r9   s    r/   r   zClientRemoteMethod._num_returnsI  s    ''r1   r   )r{   r|   r}   r   r   r"  r   r   r   r0   r   r   r   r   r   rk   r   r   r   r   r1   r/   r  r    s         
$'
$ 
$ 	
$
 $
$ 
$ 
$ 
$
 
 
C C C
 
 
+ + +C C C Cn&?    (c ( ( ( ( ( (r1   r  c                   X    e Zd Zdedeeeef                  fdZd Z	d Z
d ZdefdZd	S )
r   stubr   c                 <    || _         t          |          | _        d S r<   )_remote_stubr   r   )r.   r2  r   s      r/   r0   zOptionWrapper.__init__N  s     (11r1   c                 t     | j         j        j        |i | t          t	          j        | g|R i |          S r<   )r4  r   r   r   r   r   r   s      r/   r   zOptionWrapper.remoteR  sH    )$)4:6:::3?4A$AAA&AABBBr1   c                 ,    t          | j        |          S r<   )r  r4  r   s     r/   r   zOptionWrapper.__getattr__V  s    t(#...r1   c                 b    | j                                         }t          || j                   |S r<   )r4  r   r   r   r   s     r/   r   z"OptionWrapper._prepare_client_taskY  s-     5577t}---r1   rT   c                 ~    | j         r| j                             d          }||S | j                                        S r   )r   rf   r4  r   )r.   nums     r/   r   zOptionWrapper._num_returns^  s@    = 	-##M22C
 --///r1   N)r{   r|   r}   r   r
   r   r"  r   r0   r   r   r   r   r   r   r1   r/   r   r   M  s        2Z 2(4S>2J 2 2 2 2C C C/ / /  
0c 0 0 0 0 0 0r1   r   c                       e Zd Zd ZdS )r   c                     | j         j        j        |i | t          j        | g|R i |}t          |          dk    sJ d }t          | j         t                    r| j         }t          t          |d                   |          S r   )
r4  r   r   r   r   r   r(   r   r   r   )r.   r   r   r   r   s        r/   r   zActorOptionWrapper.remoteg  s    .).????/$8888887||q    d')9:: 	,+K 
!;!;UUUUr1   N)r{   r|   r}   r   r   r1   r/   r   r   f  s(        V V V V Vr1   r   r   r   fieldrT   c                     ||                      |           d S t          j        |          t          | |          _        d S r<   )
ClearFieldpickledumpsr  pickled_options)r   r   r<  s      r/   r   r   q  sB    
 +1<+@+@GD%(((r1   r   c                 r    | sd S t          |           dk    rt          | d                   S d | D             S )Nr   r   c                 ,    g | ]}t          |          S r   )r   ).0rZ   s     r/   
<listcomp>zreturn_refs.<locals>.<listcomp>  s     444SOC  444r1   )r   r   )r   s    r/   r   r   }  sH      t
7||qwqz***44G4444r1   c                       e Zd ZdefdZdS )r   rT   c                     | j         j        S r<   )rB   r{   r9   s    r/   r   zInProgressSentinel.__repr__  s    ~&&r1   N)r{   r|   r}   r"  r   r   r1   r/   r   r     s/        '# ' ' ' ' ' 'r1   r   c                   6    e Zd ZdZdefdZedd            ZdS )r   zDAn ID generated by the client for objects not yet given an ObjectRefr   c                 >    t          |          dk    sJ || _        d S Nr   )r   r   r-   s     r/   r0   zClientSideRefID.__init__  s     2ww!||||r1   rT   c                  V    t          j                    } t          d| j        z             S )N   )uuiduuid4r   r)   )tids    r/   r   zClientSideRefID.generate_id  s"    jllw2333r1   N)rT   r   )r{   r|   r}   r   r)   r0   r   r   r   r1   r/   r   r     sU        NN5     4 4 4 \4 4 4r1   r   c                 "     dt           f fd}|S )NrT   c                     t          j        |           st          |           rt          |           S t          j        |           rt          |           S t          d          )Nr   zMThe @ray.remote decorator must be applied to either a function or to a class.)r   
isfunctionr   r   isclassr   r+   )function_or_classr   s    r/   	decoratorz#remote_decorator.<locals>.decorator  sy    /00 	I>O4P4P 	#$5wGGGG_.// 	#$5wGGGG3  r1   )r   )r   rV  s   ` r/   remote_decoratorrW    s0    	
 	 	 	 	 	 	 r1   c                   z    e Zd ZU dZej        ed<   ej        ed<   ej        ed<   e	j
        ed<   deddfd	Zd
 ZdS )ClientServerHandlezDHolds the handles to the registered gRPC servicers and their server.task_servicerdata_servicerlogs_servicergrpc_servergracerT   Nc                 v    | j                             |           | j        j                                         d S r<   )r]  stopr[  stoppedset)r.   r^  s     r/   r`  zClientServerHandle.stop  s9     	e$$$"&&(((((r1   c                 ,    t          | j        |          S r<   )r  r]  )r.   attrs     r/   r   zClientServerHandle.__getattr__  s    t'...r1   )r{   r|   r}   r   ray_client_pb2_grpcRayletDriverServicer__annotations__RayletDataStreamerServicerRayletLogStreamerServicergrpcServerr   r`  r   r   r1   r/   rY  rY    s         NN&;;;;&AAAA&@@@@)# )$ ) ) ) )/ / / / /r1   rY  contextc                     t          |                                           }|                    d          pd}|dk    r>t                              d           |                     t          j        j                   |S )z
    Get `client_id` from gRPC metadata. If the `client_id` is not present,
    this function logs an error and sets the status_code.
    	client_id z#Client connecting with no client_id)	dictinvocation_metadatarf   r7   rh   set_coderj  
StatusCodeFAILED_PRECONDITION)rl  metadatarn  s      r/   _get_client_id_from_contextrv    sn    
 G//1122H[))/RIB:;;;<===r1   ec                    	 t          | t          j                  ri|                    |                                            |                    |                                            |                                 t          vS n# t          $ r Y nw xY w|                    t          j	        j
                   |                    t          |                      dS )z
    Encode an error into the context of an RPC response. Returns True
    if the error can be recovered from, false otherwise
    F)r(   rj  RpcErrorrr  codeset_detailsdetailsGRPC_UNRECOVERABLE_ERRORSr6   rs  rt  r"  )rw  rl  s     r/   _propagate_error_in_contextr~    s    
	a'' 	=QVVXX&&&		,,,6688#<<<		=
     	 T_8999A5s   BB 
BBid1id2c                 Z    t          || z
            }|t          dz  k    r| |k     S | |k    S )a  
    We should only replace cache entries with the responses for newer IDs.
    Most of the time newer IDs will be the ones with higher value, except when
    the req_id counter rolls over. We check for this case by checking the
    distance between the two IDs. If the distance is significant, then it's
    likely that the req_id counter rolled over, and the smaller id should
    still be used to replace the one in cache.
       )absr   )r  r  diffs      r/   _id_is_newerr    s6     sSy>>DyA~Sy9r1   c                   P    e Zd ZdZd Zdededee         fdZdedededdfd	Z	dS )
ResponseCachea  
    Cache for blocking method calls. Needed to prevent retried requests from
    being applied multiple times on the server, for example when the client
    disconnects. This is used to cache requests/responses sent through
    unary-unary RPCs to the RayletServicer.

    Note that no clean up logic is used, the last response for each thread
    will always be remembered, so at most the cache will hold N entries,
    where N is the number of threads on the client side. This relies on the
    assumption that a thread will not make a new blocking request until it has
    received a response for a previous one, at which point it's safe to
    overwrite the old response.

    The high level logic is:

    1. Before making a call, check the cache for the current thread.
    2. If present in the cache, check the request id of the cached
        response.
        a. If it matches the current request_id, then the request has been
            received before and we shouldn't re-attempt the logic. Wait for
            the response to become available in the cache, and then return it
        b. If it doesn't match, then this is a new request and we can
            proceed with calling the real stub. While the response is still
            being generated, temporarily keep (req_id, None) in the cache.
            Once the call is finished, update the cache entry with the
            new (req_id, response) pair. Notify other threads that may
            have been waiting for the response to be prepared.
    c                 D    t          j                    | _        i | _        d S r<   )r!   	Conditioncvcacher9   s    r/   r0   zResponseCache.__init__  s    %''13


r1   	thread_id
request_idrT   c                    | j         5  || j        v r| j        |         \  }}||k    rW|G| j                                          | j        |         \  }}||k    rt          d| d| d          |G|cddd           S t	          ||          st          d| d|           |df| j        |<   ddd           n# 1 swxY w Y   dS )a  
        Check the cache for a given thread, and see if the entry in the cache
        matches the current request_id. Returns None if the request_id has
        not been seen yet, otherwise returns the cached result.

        Throws an error if the placeholder in the cache doesn't match the
        request_id -- this means that a new request evicted the old value in
        the cache, and that the RPC for `request_id` is redundant and the
        result can be discarded, i.e.:

        1. Request A is sent (A1)
        2. Channel disconnects
        3. Request A is resent (A2)
        4. A1 is received
        5. A2 is received, waits for A1 to finish
        6. A1 finishes and is sent back to client
        7. Request B is sent
        8. Request B overwrites cache entry
        9. A2 wakes up extremely late, but cache is now invalid

        In practice this is VERY unlikely to happen, but the error can at
        least serve as a sanity check or catch invalid request id's.
        NzCached response doesn't match the id of the original request. This might happen if this request was received out of order. The result of the caller is no longer needed. ( != r   zAttempting to replace newer cache entry with older one. This might happen if this request was received out of order. The result of the caller is no longer needed. ()r  r  waitRuntimeErrorr  )r.   r  r  cached_request_idcached_resps        r/   check_cachezResponseCache.check_cache  s   0 W 	7 	7DJ&&15I1F.!;$
22%- 9=I9N6);,
::".!I %/	!I !I 5F	!I !I !I# #  &- '%	7 	7 	7 	7 	7 	7 	7 	7& $J0ABB &O ,6O O <MO O   &0$6DJy!5	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	76 ts   A*B<?1B<<C C responseNc                     | j         5  | j        |         \  }}||k    s|t          d| d| d          ||f| j        |<   | j                                          ddd           dS # 1 swxY w Y   dS )E
        Inserts `response` into the cache for `request_id`.
        NzAttempting to update the cache, but placeholder's do not match the current request_id. This might happen if this request was received out of order. The result of the caller is no longer needed. (r  r   )r  r  r  
notify_all)r.   r  r  r  r  r  s         r/   update_cachezResponseCache.update_cacheG  s     W 	! 	!-1Z	-B*{ J..+2I #, <F, , )	, , ,   &0$:DJy!G   !	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s   AA))A-0A-)
r{   r|   r}   r   r0   r   r
   r   r  r  r   r1   r/   r  r    s         :4 4 43S 3c 3hsm 3 3 3 3j!c !s !c !d ! ! ! ! ! !r1   r  c                   h    e Zd ZdZd Zdedee         fdZdededdfdZ	d	e
defd
ZdeddfdZdS )OrderedResponseCachez
    Cache for streaming RPCs, i.e. the DataServicer. Relies on explicit
    ack's from the client to determine when it can clean up cache entries.
    c                 j    d| _         t          j                    | _        t	                      | _        d S rJ  )last_receivedr!   r  r  r   r  r9   s    r/   r0   zOrderedResponseCache.__init__d  s)    %''%0]]


r1   req_idrT   c                    | j         5  t          | j        |          s| j        |k    rt          d| d| j         d          || j        v r]| j        |         }|@| j                                          || j        vrt          d          | j        |         }|@|cddd           S d| j        |<   ddd           n# 1 swxY w Y   dS )z
        Check the cache for a given thread, and see if the entry in the cache
        matches the current request_id. Returns None if the request_id has
        not been seen yet, otherwise returns the cached result.
        zAttempting to accesss a cache entry that has already cleaned up. The client has already acknowledged receiving this response. (r   r   Nz\Cache entry was removed. This likely means that the result of this call is no longer needed.)r  r  r  r  r  r  )r.   r  r  s      r/   r  z OrderedResponseCache.check_cachei  sp    W 	& 	&D.77 4;MQW;W;W #-17- - )- - -   ##"j0!) GLLNNNTZ//*K   #'*V"4K ") #/	& 	& 	& 	& 	& 	& 	& 	&0 "&DJv1	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&2 ts   BC *
C  CCra   Nc                     | j         5  | j                                          || j        vrt          d| d          || j        |<   ddd           dS # 1 swxY w Y   dS )r  ztAttempting to update the cache, but placeholder is missing. This might happen on a redundant call to update_cache. (r   N)r  r  r  r  )r.   r  ra   s      r/   r  z!OrderedResponseCache.update_cache  s     W 	& 	&G   TZ''"0&,0 0 0  
 "&DJv	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   A AAArw  c                     | j         5  d}| j        D ];}| j        |         
|| j        |<   t          | j        |         t                    rd}<| j                                          ddd           n# 1 swxY w Y   |S )a  
        Invalidate any partially populated cache entries, replacing their
        placeholders with the passed in exception. Useful to prevent a thread
        from waiting indefinitely on a failed call.

        Returns True if the cache contains an error, False otherwise
        FNT)r  r  r(   r6   r  )r.   rw  invalidr  s       r/   
invalidatezOrderedResponseCache.invalidate  s     W 	! 	!G* # #:f%-)*DJv&dj0)<< #"GG   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! s   AA33A7:A7r  c                 :   | j         5  t          || j                  r|| _        g }| j        D ].}t          ||          s||k    r|                    |           . |D ]
}| j        |= | j                                          ddd           dS # 1 swxY w Y   dS )z
        Cleanup all of the cached requests up to last_received. Assumes that
        the cache entries were inserted in ascending order.
        N)r  r  r  r  appendr  )r.   r  	to_remover  s       r/   cleanupzOrderedResponseCache.cleanup  s	   
 W 	! 	!M4+=>> 3%2"I*  v66 -6:Q:Q$$V,,,,# ' 'Jv&&G   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s   A;BBB)r{   r|   r}   r   r0   r   r
   r   r  r  r6   r   r  r  r   r1   r/   r  r  ^  s         
3 3 3
# (3-    B&3 &c &d & & & &I $    $!S !T ! ! ! ! ! !r1   r  rR  )_r   loggingosr?  r!   rM  collectionsr   concurrent.futuresr   dataclassesr   typingr   r   r   r	   r
   r   r   rj  ray._raylet_rayletraylet!ray.core.generated.ray_client_pb2core	generatedrk   &ray.core.generated.ray_client_pb2_grpcre  ray._common.signaturer   r   ray._privater   ray._private.inspect_utilr   r   r   r   ray._private.utilsr   ray.util.clientr   ray.util.client.optionsr   ray.util.commonr   	getLoggerr{   r7   rs  RESOURCE_EXHAUSTEDINVALID_ARGUMENT	NOT_FOUNDrt  ABORTEDr}  GRPC_MAX_MESSAGE_SIZEGRPC_KEEPALIVE_TIME_MSGRPC_KEEPALIVE_TIMEOUT_MSGLOBAL_GRPC_OPTIONSGRPC_OPTIONSfloatgetenvCLIENT_SERVER_MAX_THREADSOBJECT_TRANSFER_CHUNK_SIZEOBJECT_TRANSFER_WARNING_SIZE	ObjectRefr   ActorIDr   r   r   r   r   r  r   r   r   r"  r   r   r   r   rW  rY  rv  r6   r   r~  r   r  r  r  r   r1   r/   <module>r     s     				       # # # # # # % % % % % % ! ! ! ! ! ! D D D D D D D D D D D D D D D D D D        : : : : : : : : : : : : D D D D D D D D D D D D B B B B B B B B & & & & & &            8 7 7 7 7 7       4 4 4 4 4 4 % % % % % %		8	$	$ 	O&O$OO'O   5  #  ' &#%:; '(=> 56	
 !";< / - 45Kb5PQ ' "E)"),KS"Q"QRR  '   ) h+ h+ h+ h+ h+f& h+ h+ h+V>+ >+ >+ >+ >+V^ >+ >+ >+B	 	 	 	 	 	 	 	N0 N0 N0 N0 N0z N0 N0 N0bO O O O Oz O O OdaW aW aW aW aW
 aW aW aWH;( ;( ;( ;( ;( ;( ;( ;(|0 0 0 0 0 0 0 02V V V V V V V V 	A 	A

#	Ad38n%	A 	A 
		A 	A 	A 	A5&\5
4$"7785 5 5 5' ' ' ' ' ' ' '

4 
4 
4 
4 
4 
4 
4 
4htCH~6     / / / / / / / /*
 
 
 
 
 
9 s t    (c      "k! k! k! k! k! k! k! k!\\! \! \! \! \! \! \! \! \! \!r1   