
    &`i}I                        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m	Z	m
Z
 d dlmZmZmZmZmZ d dlZd dlZd dlmc mc mZ d dlmc mc mZ d dlmZ d dlmZmZmZ d dlm Z  d d	l!m"Z" erd d
l#m$Z$  e j%        e&          Z'dZ(dede)fdZ*dej+        de)fdZ,deej+                 ddddfdZ- G d d          Z. G d dej/                  Z0dS )    N)defaultdict)Queue)EventLockThread)TYPE_CHECKINGAnyDictIteratorUnion)disable_client_hook)CLIENT_SERVER_MAX_THREADSOrderedResponseCache_propagate_error_in_context)loads_from_client)log_once)RayletServicer
   contextreturnc                     t          |                                           }|                    d          }||dvr t                              d| d           dS |dk    S )zE
    Get `reconnecting` from gRPC metadata, or False if missing.
    reconnectingN)TrueFalsez9Client connecting with invalid value for "reconnecting": zF, This may be because you have a mismatched client and server version.Fr   )dictinvocation_metadatagetloggererror)r   metadatavals      w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/client/server/dataservicer.py_get_reconnecting_from_contextr#      s{     G//1122H
,,~
&
&C
{c!222   	
 	
 	

 u&=    reqc                     |                      d          }|dk    r| j        j        rdS |dk    r| j        j        | j        j        dz
  k    S |dk    r| j        j        | j        j        dz
  k    S |dvS )a  
    Returns True if the response should to the given request should be cached,
    false otherwise. At the moment the only requests we do not cache are:
        - asynchronous gets: These arrive out of order. Skipping caching here
            is fine, since repeating an async get is idempotent
        - acks: Repeating acks is idempotent
        - clean up requests: Also idempotent, and client has likely already
             wrapped up the data connection by this point.
        - puts: We should only cache when we receive the final chunk, since
             any earlier chunks won't generate a response
        - tasks: We should only cache when we receive the final chunk,
             since any earlier chunks won't generate a response
    typer   Fput   task)acknowledgeconnection_cleanup)
WhichOneofr   asynchronousr(   chunk_idtotal_chunksr*   )r%   req_types     r"   _should_cacher2   /   s     ~~f%%H5SW1u5w37#7!#;;;6x CH$9A$===@@@r$   grpc_input_generatoroutput_queuezEQueue[Union[ray_client_pb2.DataRequest, ray_client_pb2.DataResponse]]c                    	 | D ]}|                     |           n9# t          j        $ r'}t                              d|            Y d}~nd}~ww xY w|                     d           dS # |                     d           w xY w)z<
    Pushes incoming requests to a shared output_queue.
    zHclosing dataservicer reader thread grpc error reading request_iterator: N)r(   grpcRpcErrorr   debug)r3   r4   r%   es       r"   
fill_queuer:   G   s    
' 	" 	"CS!!!!	"= 
 
 
8458 8	
 	
 	
 	
 	
 	
 	
 	

 	s+    A- AA	A- AA- -Bc                   \    e Zd ZdZd Zdej        deej        ej	        f         fdZ
d ZdS )ChunkCollectorzR
    Helper class for collecting chunks from PutObject or ClientTask messages
    c                 H    d | _         d| _        t                      | _        d S Ncurr_req_idlast_seen_chunk_id	bytearraydataselfs    r"   __init__zChunkCollector.__init__`   !    "$KK			r$   r%   chunkc                    | j         0| j         |j        k    r t          d| j          d|j         d          |j        | _         | j        dz   }|j        |k     rd S |j        |k    r t          d|j         d|j         d          |j        | j        dz   k    r+| j                            |j                   |j        | _        |j        dz   |j        k    S )Nz1Expected to receive a chunk from request with id z, but found z	 instead.r)   zA chunk z of request z was received out of order.)rA   req_idRuntimeErrorrB   r/   rD   extendr0   )rF   r%   rI   
next_chunks       r"   	add_chunkzChunkCollector.add_chunke   s   
 'D,<
,J,JG#G G14G G G   :,q0
>J&&F>J&&)5> ) )sz ) ) )   ^t6:::IUZ(((&+nD#~!U%777r$   c                 H    d | _         d| _        t                      | _        d S r>   r@   rE   s    r"   resetzChunkCollector.reset~   rH   r$   N)__name__
__module____qualname____doc__rG   ray_client_pb2DataRequestr   
PutRequest
ClientTaskrO   rQ    r$   r"   r<   r<   [   su              
8'8 ^.0IIJ8 8 8 82         r$   r<   c                   6    e Zd ZddZd ZdededefdZd	 Z	d
S )DataServicerbasic_servicer   c                    || _         t                      | _        d| _        i | _        i | _        t          t                    | _        t                      | _
        t                      | _        t                      | _        d S )Nr   )r]   r   clients_locknum_clientsclient_last_seenreconnect_grace_periodsr   r   response_cachesr   stoppedr<   put_request_chunk_collectorclient_task_chunk_collector)rF   r]   s     r"   rG   zDataServicer.__init__   sx    * FF249;$@K A
 A
 ww ,:+;+;( ,:+;+;(((r$   c           	   #      K   t          j                     }d}t          |                                          }|                    d          }|t                              d           d S t                              d| d           |                     |||          }| j        |         }d}	|sd S 	 t                      }
t          t          d||
f          }|                                 	 t          |
j        d           D ]I}t          |t          j                  r|V  "t          |t          j                  sJ t%          |          r:|	r8|                    |j                  }t          |t*                    r|||V  d }|                    d          }|d	k    r{| j                            |j                  }t          j        |
          }| j        5  |j        j        | j        |<   |j        j        dk    rd}	d d d            n# 1 swxY w Y   n|dk    ro|j        j        r,| j                            |j        ||j        |
          }|\n | j                            |j        |          }t          j        |          }ny|dk    r| j         !                    ||j"                  s| j        #                    | j         j$        |j"        j%        ||j"        j&                  }| j         '                                 t          j        |          }n|dk    rkg }|j(        j)        D ]2}| j        (                    ||          }|*                    |           3t          j        t          j+        |                    }nu|dk    r)t          j        | ,                                          }nF|dk    rU| j        5  | j        -                    |j.                  }t          j        |          }d d d            n# 1 swxY w Y   n|dk    r,d}t          j/                    }t          j        |          }n|dk    r!|0                    |j1        j                   {|dk    r| j        5  |j2        }| j3        !                    ||          s	 d d d            ti          | j3        j$        | j                  \  }}| j3        '                                 | j        5                    |j2        |||          }t          j        |          }~~d d d            n# 1 swxY w Y   n|dk    rU| j        5  | j        6                    |j7        |          }t          j        |          }d d d            n# 1 swxY w Y   nm|dk    rT| j        5  | j        8                    |j9                  }t          j        |          }d d d            n# 1 swxY w Y   nt+          d | d!          |j        |_        t%          |          r|	r|:                    |j        |           |V  Kn# t*          $ rs}t          ;                    d"           ty          ||          }|=                    |          }|r|r&|>                    t~          j@        jA                   d}Y d }~nd }~ww xY wt                              d#|            |B                    t                     |D                                r2t                              d$E                    t                               | j                            |          } |s<| :t                              d%|  d&           | jF        G                    | '           nt                              d(           | j        5  || jH        vr(t                              d)           	 d d d            d S | jH        |         }!|!|k    r(t                              d*           	 d d d            d S | j        I                    |           | jH        |= || j        v r| j        |= || j        v r| j        |= | xjJ        d+z  c_J        t                              d,| d-| jJ                    t                      5  | jJ        dk    r-t                              d.           t          jM                     d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S # t                              d#|            |B                    t                     |D                                r2t                              d$E                    t                               | j                            |          } |s<| :t                              d%|  d&           | jF        G                    | '           nt                              d(           | j        5  || jH        vr*t                              d)           	 d d d             Y d S | jH        |         }!|!|k    r*t                              d*           	 d d d             Y d S | j        I                    |           | jH        |= || j        v r| j        |= || j        v r| j        |= | xjJ        d+z  c_J        t                              d,| d-| jJ                    t                      5  | jJ        dk    r-t                              d.           t          jM                     d d d            n# 1 swxY w Y   d d d            w # 1 swxY w Y   w xY w)/NF	client_idz#Client connecting with no client_idz New data connection from client z: T)targetdaemonargsr'   init)rl   r   r   )r   r(   )r(   release)ok)rm   connection_info)ro   prep_runtime_env)rp   r,   )r,   r+   r*   )task_ticket	terminate)rr   list_named_actors)rs   zUnreachable code: Request type z not handled in DatapathzError in data channel:zStream is broken with client z5Queue filler thread failed to join before timeout: {}z-Cleanup wasn't requested, delaying cleanup byz	 seconds.)timeoutz/Cleanup was requested, cleaning up immediately.zConnection already cleaned up.z$Client reconnected, skipping cleanupr)   zRemoved client z, remaining=zShutting down ray.)Ntimer   r   r   r   r   r8   _initrc   r   r   r:   startiter
isinstancerV   DataResponserW   r2   check_cacherK   	Exceptionr-   r]   Initrl   r_   reconnect_grace_periodrb   r.   _async_get_object_get_objectre   rO   r(   _put_objectrD   client_ref_idowner_idrQ   rm   idsappendReleaseResponse_build_connection_responsePrepRuntimeEnvrp   ConnectionCleanupResponsecleanupr+   r*   rf   r   Schedule	Terminaterr   ListNamedActorsrs   update_cache	exceptionr   
invalidateset_coder6   
StatusCodeFAILED_PRECONDITIONjoinQUEUE_JOIN_SECONDSis_aliveformatrd   waitra   release_allr`   r   rayshutdown)"rF   request_iteratorr   
start_timecleanup_requestedr    rh   accepted_connectionresponse_cachereconnect_enabledrequest_queuequeue_filler_threadr%   cached_resprespr1   	resp_initget_respput_respreleasedrel_idrel	resp_prepcleanup_respr*   arglistkwargsresp_ticketresponser9   recoverableinvalid_cachecleanup_delay	last_seens"                                     r"   DatapathzDataServicer.Datapath   s]     Y[[
!335566LL--	LL>???FE	EEEFFF"jjGZHH-i8 " 	F	'!GGM"(!$6F5V# # #  %%'''
 M-t44 t tc>#>?? III!#~'ABBBBB %% !*; !"0"<"<SZ"H"HK!+y99 *))".)))) >>&11v%% $ 2 7 7 A AI)6&  D * 6 6  H; 4% 8:a??05-6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 &&w+ V#'#5#G#GGY
M$ $ $+
 % , $(#5#A#A#'9#U#U)68DDDDD&&;EEc37SS ! #1==8=-!(	   H 4::<<<)68DDDDD**!H"%+/ - -"088FKK ,,,,)6 . >( K K K  DD !222)6(,(G(G(I(I  DD !333* W W$($6$E$E0% %	  .:IVVV	W W W W W W W W W W W W W W W
 !555(,%#1#K#M#ML)6,WWWDD.."**3?+ABBB''* # #"x#?II#tTT %$	# # # # # # #
 +< <A4CU+ + 8>>@@@&*&8&A&AHgvw' '  .:{SSS#"# # # # # # # # # # # # # # # ,,* O O#'#5#?#?w#W#W-:XNNNO O O O O O O O O O O O O O O !444* W W#'#5#E#E1$ $  .:XVVV	W W W W W W W W W W W W W W W $>#> > >   "j %% B*; B"//
DAAA



itj  	) 	) 	)56665aAAK*55a88M )- )  !DEEE$(!	) LLDDDEEE$$%7888"++-- KRR*   
 !8<<YGGM$ 
P)B0$0 0 0   !!-!8888NOOO" "' "'D$999LL!ABBB "' "' "' "' "' "' "' "' !1)<	z)) LL!GHHH"' "' "' "' "' "' "' "'  "..y999))4 <<<4Y? 444,Y7  A%  SiSSAQSS   )** ' ''1,,%9:::' ' ' ' ' ' ' ' ' ' ' ' ' ' '?"' "' "' "' "' "' "' "' "' "' "' "' "' "' "' "' "' "') LLDDDEEE$$%7888"++-- KRR*   
 !8<<YGGM$ 
P)B0$0 0 0   !!-!8888NOOO" "' "'D$999LL!ABBB "' "' "' "' "' "' "' "' "' "' !1)<	z)) LL!GHHH"' "' "' "' "' "' "' "' "' "'  "..y999))4 <<<4Y? 444,Y7  A%  SiSSAQSS   )** ' ''1,,%9:::' ' ' ' ' ' ' ' ' ' ' ' ' ' '?"' "' "' "' "' "' "' "' "' "' "' "' "' "' "' "'s  5D0Y %'HY H	Y H	 F:Y 5PY P	Y "P	#A+Y $T?2Y ?A4T?3Y ?U	Y U	Y 6VY V	Y !V	"Y 35W4(Y 4W8	8Y ;W8	<AY d& 
[A)[
d& 
[d& %d:.d5Bd<9d5dd	dd		dd d&C7m=%m1m=.m1?m=Bm19m	m1mm1!m"m1%m=1m55m=8m59m=rh   r   r   c           
      6   | j         5  t          |          }t          t          dz            }| j        |k    rt
                              d| j         d| d| d           t          d          r#t
                              dt           d           |                    t          j
        j                   	 d	d	d	           d
S |rP|| j        vrG|                    t          j
        j                   |                    d           	 d	d	d	           d
S || j        v rt
                              d| d           n5| xj        dz  c_        t
                              d| d| j                    || j        |<   	 d	d	d	           dS # 1 swxY w Y   d	S )z
        Checks if resources allow for another client.
        Returns a boolean indicating if initialization was successful.
           z[Data Servicer]: Num clients z has reached the threshold z. Rejecting client: z. client_thresholdzyYou can configure the client connection threshold by setting the RAY_CLIENT_SERVER_MAX_THREADS env var (currently set to z).NFzEAttempted to reconnect to a session that has already been cleaned up.zClient z has reconnected.r)   zAccepted data connection from z. Total clients: T)r_   r#   intr   r`   r   warningr   r   r6   r   RESOURCE_EXHAUSTEDra   	NOT_FOUNDset_detailsr8   )rF   rh   r   r   r   	thresholds         r"   rv   zDataServicer._initk  s   
  $	 $	9'BBL59::I9,,7D4D 7 71:7 7)27 7 7  
 .// NNK .GK K K     !CDDD#$	 $	 $	 $	 $	 $	 $	 $	$  	1F F F   !:;;;##'   5$	 $	 $	 $	 $	 $	 $	 $	6 D111CyCCCDDDD  A%  9Y 9 9&*&69 9   0:D!),I$	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	s    B2FAFA(FFFc           	      0   | j         5  | j        }d d d            n# 1 swxY w Y   t          j        |d                    t
          j        d         t
          j        d         t
          j        d                   t          j        t          j	                  S )Nz{}.{}.{}r   r)   r   )r`   python_versionray_version
ray_commit)
r_   r`   rV   ConnectionInfoResponser   sysversion_infor   __version__
__commit__)rF   cur_num_clientss     r"   r   z'DataServicer._build_connection_response  s     	/ 	/".O	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/4'%,, #S%5a%8#:J1:M  ~
 
 
 	
s     N)r]   r   )
rR   rS   rT   rG   r   strr	   floatrv   r   rZ   r$   r"   r\   r\      st        < < < <*O' O' O'b)s )S )e ) ) ) )V

 

 

 

 

r$   r\   )1loggingr   ru   collectionsr   queuer   	threadingr   r   r   typingr   r	   r
   r   r   r6   r   !ray.core.generated.ray_client_pb2core	generatedrV   &ray.core.generated.ray_client_pb2_grpcray_client_pb2_grpcray._private.client_mode_hookr   ray.util.client.commonr   r   r   %ray.util.client.server.server_picklerr   ray.util.debugr   ray.util.client.server.serverr   	getLoggerrR   r   r   boolr#   rW   r2   r:   r<   RayletDataStreamerServicerr\   rZ   r$   r"   <module>r      sq    



  # # # # # #       ) ) ) ) ) ) ) ) ) ) < < < < < < < < < < < < < <  



 : : : : : : : : : : : : D D D D D D D D D D D D = = = = = =         
 D C C C C C # # # # # # =<<<<<<		8	$	$ C D     A~1 Ad A A A A0">#=>Y 
   (&  &  &  &  &  &  &  & R\
 \
 \
 \
 \
&A \
 \
 \
 \
 \
r$   