
    &`iu                     $   d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	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Z d dlmZmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZm Z  d dl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z, d dl-m.Z. d dl/m0Z0m1Z1 d dl2m3Z3m4Z4m5Z5 d dl6m7Z7m8Z8m9Z9m:Z: d dl;m<Z<  ej=        e*          Z> G d de          Z? G d de?          Z@ G d d          ZA G d d          ZBde jC        deDfdZEd!dedeeF         de jC        fd ZGdS )"    N)ABCabstractmethod)deepcopy)DictListOptionalSetTupleType)	ObjectRef)build_address)Timer	TimerBase)ActorHandle)GetTimeoutErrorRayActorError)ClusterNodeInfoCache)NodeIdRequestProtocol)ASYNC_CONCURRENCYPROXY_DRAIN_CHECK_PERIOD_SPROXY_HEALTH_CHECK_PERIOD_SPROXY_HEALTH_CHECK_TIMEOUT_S&PROXY_HEALTH_CHECK_UNHEALTHY_THRESHOLDPROXY_READY_CHECK_TIMEOUT_S'RAY_SERVE_ALWAYS_RUN_PROXY_ON_HEAD_NODERAY_SERVE_ENABLE_TASK_EVENTSSERVE_LOGGER_NAMESERVE_NAMESPACESERVE_PROXY_NAME)
ProxyActor)format_actor_nameis_grpc_enabled)DeploymentModeHTTPOptionsgRPCOptions)LoggingConfigProxyDetailsProxyStatusTarget)NodeAffinitySchedulingStrategyc                      e Zd Zeedefd                        Zededee	         fd            Z
ededee	         fd            Zededee	         fd            Zed             Zede	fd	            Zed
             ZdS )ProxyWrapperreturnc                     t           z'Return the actor id of the proxy actor.NotImplementedErrorselfs    r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/serve/_private/proxy_state.pyactor_idzProxyWrapper.actor_id0   s
     "!    	timeout_sc                     t           )a  Return whether proxy is ready to be serving requests.

        Since actual readiness check is asynchronous, this method could return
        any of the following statuses:
            - None: Readiness check is pending
            - True: Readiness check completed successfully (proxy is ready)
            - False: Readiness check completed with failure (either timing out
            or failing)
        r1   r4   r8   s     r5   is_readyzProxyWrapper.is_ready6   s
     "!r7   c                     t           )ay  Return whether the proxy actor is healthy.

        Since actual health-check is asynchronous, this method could return
        either of the following statuses:
            - None: Health-check is pending
            - True: Health-check completed successfully (proxy is healthy)
            - False: Health-check completed with failure (either timing out or failing)
        r1   r:   s     r5   
is_healthyzProxyWrapper.is_healthyC   
     "!r7   c                     t           )af  Return whether the proxy actor is drained.

        Since actual check whether proxy is drained is asynchronous, this method could
        return either of the following statuses:
            - None: Drain-check is pending
            - True: Drain-check completed, node *is drained*
            - False: Drain-check completed, node is *NOT* drained
        r1   r:   s     r5   
is_drainedzProxyWrapper.is_drainedO   r>   r7   c                     t           )z+Return whether the proxy actor is shutdown.r1   r3   s    r5   is_shutdownzProxyWrapper.is_shutdown[   
     "!r7   drainingc                     t           ).Update the draining status of the proxy actor.r1   )r4   rD   s     r5   update_drainingzProxyWrapper.update_draining`   rC   r7   c                     t           )Kill the proxy actor.r1   r3   s    r5   killzProxyWrapper.kille   rC   r7   N)__name__
__module____qualname__propertyr   strr6   floatr   boolr;   r=   r@   rB   rG   rJ    r7   r5   r-   r-   /   s6       "# " " " ^ X" 
"% 
"HTN 
" 
" 
" ^
" 	"E 	"htn 	" 	" 	" ^	" 	"E 	"htn 	" 	" 	" ^	" " " ^" " " " " ^" " " ^" " "r7   r-   c                      e Zd Zdddddddefdedee         dee         dee         dee	         dee	         dee	         d	ee
         d
ee         fdZeefdedede	de	de	d	e
ded
ee         defd            Zede	fd            Zedefd            Zdedee         fdZdedee         fdZdedee         fdZdefdZdefdZd ZdS )ActorProxyWrapperNlogging_configactor_handlehttp_optionsgrpc_optionsnamenode_idnode_ip_addressportproxy_actor_classc
           
          |p|                      |||||||	|          | _        d | _        d | _        d | _        d | _        || _        d | _        d | _        d S )N)rW   rX   rY   rZ   r[   r\   r]   rU   )	_get_or_create_proxy_actor_actor_handle_ready_check_future_health_check_future_drained_check_future_update_draining_obj_ref_node_id	worker_idlog_file_path)
r4   rU   rV   rW   rX   rY   rZ   r[   r\   r]   s
             r5   __init__zActorProxyWrapper.__init__l   s     * 	
T-L-L%%+/) .M 	.
 	.
 $( $(!%)"(,%!r7   r.   c                    d}	 t          j        |t                    }nO# t          $ rB t	          | j        | j                  }	t                              d| d|	 dddi           Y nw xY w|pW|	                    | j
        |t          d	t          d
t          |d          t                                        | ||||          S )zHelper to start or reuse existing proxy.

        Takes the name of the proxy, the node id, and the node ip address, and look up
        or creates a new ProxyActor actor handle for the proxy.
        N)	namespacezStarting proxy on node 'z' listening on ''.log_to_stderrF)extradetachedr   )soft)num_cpusrY   rj   lifetimemax_concurrencymax_restartsscheduling_strategyenable_task_events)rX   rZ   r[   rU   )ray	get_actorr   
ValueErrorr   hostr\   loggerinfooptionsrp   r   r+   r   remote)
rW   rX   rY   rZ   r[   r\   rU   r]   proxyaddrs
             r5   r_   z,ActorProxyWrapper._get_or_create_proxy_actor   s     	M$/BBBEE 	 	 	 !2L4EFFDKKL7LLDLLL&.      	  
)11!*%- >wU S S S; 2 	
 	
 &%+)  
 
	
s     A	A,+A,c                 >    | j         j                                        S r0   )r`   	_actor_idhexr3   s    r5   r6   zActorProxyWrapper.actor_id   s     !+//111r7   c                     | j         S )zReturn the actor handle of the proxy actor.

        This is used in _start_controller() in _private/controller.py to check whether
        the proxies exist. It is also used in some tests to access proxy's actor handle.
        )r`   r3   s    r5   rV   zActorProxyWrapper.actor_handle   s     !!r7   r8   c                 *   | j         2t          | j        j                                        |          | _         | j                                         sd S 	 t          j        | j                                                   \  }}|| _	        || _
        	 d | _         dS # t          $ r) t                              d| j         d| d           Y n1t          $ r% t                              d| j                    Y nw xY wd | _         n# d | _         w xY wdS )Nr8   Tz)Proxy actor readiness check for proxy on z didn't complete in s.z7Unexpected error invoking readiness check for proxy on F)ra   wrap_as_futurer`   readyr}   donejsonloadsresultrf   rg   TimeoutErrorrz   warningre   	Exception	exception)r4   r8   rf   rg   s       r5   r;   zActorProxyWrapper.is_ready   ss   #+'5"(//11Y( ( (D$ ',,.. 	4	,'+z$2J2Q2Q2S2S'T'T$I}&DN!.D (,D$$$  	 	 	NN5DM 5 5'05 5 5      	 	 	'}' '    	 (,D$$tD$++++us0   <B 0C<D +C<9D ;C<<D 	Dc                    | j         2t          | j        j                                        |          | _         | j                                         sd S 	 | j                                         d | _         S # t          $ r) t          	                    d| j
         d| d           Y n1t          $ r% t                              d| j
                    Y nw xY wd | _         n# d | _         w xY wdS )Nr   z2Didn't receive health check response for proxy on  after r   z4Unexpected error invoking health check for proxy on F)rb   r   r`   check_healthr}   r   r   r   rz   r   re   r   r   r:   s     r5   r=   zActorProxyWrapper.is_healthy   sN   $,(6"/6688I) ) )D% (--// 	4	-,3355 )-D%%  	 	 	NN;}; ;-6; ; ;      	 	 	&m& &    	 )-D%%D%,,,,us0   A6 60C&C! (+CC! CC! !	C*c                    | j         2t          | j        j                                        |          | _         | j                                         sd S 	 | j                                         }|d | _         S # t          $ r) t          	                    d| j
         d| d           Y n1t          $ r% t                              d| j
                    Y nw xY wd | _         n# d | _         w xY wdS )Nr   z1Didn't receive drain check response for proxy on r   r   z3Unexpected error invoking drain-check for proxy on F)rc   r   r`   r@   r}   r   r   r   rz   r   re   r   r   )r4   r8   r@   s      r5   r@   zActorProxyWrapper.is_drained   sT   %-)7"-4466#* * *D&
 )..00 	4	.3::<<J *.D&&  	 	 	NN;}; ;-6; ; ;      	 	 	&m& &    	 *.D&&D&----us0   A8 80C(C# *+CC# CC# #	C,c                     	 t          j        | j        j                                        d           n# t
          $ r Y dS t          $ r Y nw xY wdS )zReturn whether the proxy actor is shutdown.

        If the actor is dead, the health check will return RayActorError.
        r   )timeoutTF)rv   getr`   r   r}   r   r   r3   s    r5   rB   zActorProxyWrapper.is_shutdown  sq    
	GD&3::<<aHHHHH 	 	 	44 	 	 	D	 us   25 
A	AArD   c                     | j         j                            || j                  | _        |s&| j        }d| _        |r|                                 dS dS dS )rF   )_afterN)r`   rG   r}   rd   rc   cancel)r4   rD   futures      r5   rG   z!ActorProxyWrapper.update_draining*  sw     )-(:(J(Q(QT: )R )
 )
%
  	 /F)-D&  		  	    r7   c                 <    t          j        | j        d           dS )rI   T)
no_restartN)rv   rJ   r`   r3   s    r5   rJ   zActorProxyWrapper.kill9  s     #555555r7   )rK   rL   rM   r!   r'   r   r   r%   r&   rO   intr   rh   staticmethodr-   r_   rN   r6   rV   rP   rQ   r;   r=   r@   rB   rG   rJ   rR   r7   r5   rT   rT   k   sO        /3.2.2"!%)-".8 "  "% " {+ " {+	 "
 {+ " sm " # " "# " sm "  
+ "  "  "  "D  /9(
 (
!(
!(
 (
 	(

 (
 (
 &(
  
+(
 
(
 (
 (
 \(
T 2# 2 2 2 X2 "k " " " X"% HTN    :E htn    4E htn    8T              6 6 6 6 6r7   rT   c                   @   e Zd Zd e            fdededededededefd	Ze	d
e
fd            Ze	d
efd            Ze	d
efd            Ze	d
efd            Ze	d
efd            Ze	d
efd            Zded
dfdZdefdZddZddefdZdefdZd Zd
efdZdS )
ProxyStater   actor_proxy_wrapper
actor_namerZ   node_ipnode_instance_idproxy_restart_counttimerc                    || _         || _        || _        t          j        | _        || _        d| _        d| _        || _	        d | _
        d | _        t          |||| j         j        | j        | j                  | _        d S )NFr   )rZ   r   r   r6   r   status)_actor_proxy_wrapper_actor_namere   r)   STARTING_status_timer_shutting_down"_consecutive_health_check_failures_proxy_restart_count_last_health_check_time_last_drain_check_timer(   r6   _actor_details)r4   r   r   rZ   r   r   r   r   s           r5   rh   zProxyState.__init__?  s     %8!%"+#78/$7!8<$7;#*-.7'<
 
 
r7   r.   c                     | j         j        S N)r   rV   r3   s    r5   rV   zProxyState.actor_handle]  s    (55r7   c                     | j         S r   )r   r3   s    r5   r   zProxyState.actor_namea  s    r7   c                     | j         j        S r   )r   r6   r3   s    r5   r6   zProxyState.actor_ide  s    (11r7   c                     | j         S r   )r   r3   s    r5   r   zProxyState.statusi  s
    |r7   c                     | j         S r   )r   r3   s    r5   actor_detailszProxyState.actor_detailsm  s    ""r7   c                     | j         S r   )r   r3   s    r5   r   zProxyState.proxy_restart_countq  s    ((r7   r   Nc                 J    || _         |                     | j                    dS )zSets _status and updates _actor_details with the new status.

        NOTE: This method should not be used directly, instead please
              use `try_update_status` method
        r   N)r   update_actor_detailsr4   r   s     r5   _set_statuszProxyState._set_statusu  s*     !!!66666r7   c                     |t           j        k    rN| xj        dz  c_        | j        t          k     rdS t                              d| j         d| j         d           nd| _        |                     |           dS )ay  Try update with the new status and only update when the conditions are met.

        Status will only set to UNHEALTHY after PROXY_HEALTH_CHECK_UNHEALTHY_THRESHOLD
        consecutive failures. A warning will be logged when the status is set to
        UNHEALTHY. Also, when status is set to HEALTHY, we will reset
        self._consecutive_health_check_failures to 0.
           NzProxy z failed the health check z& times in a row, marking it unhealthy.r   r   )r)   	UNHEALTHYr   r   rz   r   r   r   r   s     r5   try_update_statuszProxyState.try_update_status~  s     [***33q833 789 9  &T- & &>& & &    78D3'''''r7   c                     | j                                         }|                    |           t          di || _         dS )z-Updates _actor_details with passed in kwargs.NrR   )r   dictupdater(   )r4   kwargsdetails_kwargss      r5   r   zProxyState.update_actor_details  sF    ,1133f%%%*<<^<<r7   FrD   c                     	 |                      |           d S # t          $ rN}|                     t          j                   t
                              d| j         |           Y d }~d S d }~ww xY w)NzAUnexpected error occurred when reconciling stae of proxy on node )exc_info)_reconcile_internalr   r   r)   r   rz   errorre   )r4   rD   es      r5   	reconcilezProxyState.reconcile  s    	$$X..... 	 	 	"";#8999LL1!%1 1          	s    
A1AA,,A1c                 &   | j         s*| j        t          j        k    s| j        t          j        k    rdS | j        dz   t          z  }| j        t          j        k    r| j        	                    |          }||rR| 
                    t          j                   |                     | j        j        | j        j        | j                   dS | 
                    t          j                   t                              d| j                    dS dS | j        t          j        t          j        hv sJ | j        du p)| j                                        | j        z
  t,          k    }|r{| j                            t0                    }|Z|r| 
                    | j                   n| 
                    t          j                   | j                                        | _        | j        t          j        k    rdS | j        t          j        k    rl|rht                              d| j         d           | j        J | j                            d           | 
                    t          j                   dS dS | j        t          j        k    r|sft                              d	| j         d           d| _        | j                            d
           | 
                    t          j                   dS | j        du p)| j                                        | j        z
  t8          k    }|rd| j                            t                    }|E|r| 
                    t          j                   | j                                        | _        dS dS dS dS )aJ  Update the status of the current proxy.

        The state machine is:
        STARTING -> HEALTHY or UNHEALTHY
        HEALTHY -> DRAINING or UNHEALTHY
        DRAINING -> HEALTHY or UNHEALTHY or DRAINED

        UNHEALTHY is a terminal state upon reaching which, Proxy is going to be
        restarted by the controller
        Nr   )rf   rg   r   z'Proxy actor reported not ready on node zDraining proxy on node 'rk   T)rD   z"No longer draining proxy on node 'F)r   r   r)   DRAINEDr   r   r   r   r   r;   r   HEALTHYr   rf   rg   rz   r   re   DRAININGr   r   timer   r=   r   r{   r   rG   r   r@   )r4   rD   ready_check_timeoutis_ready_responseshould_check_healthis_healthy_responseshould_check_drainis_drained_responses           r5   r   zProxyState._reconcile_internal  s    	|{222|{444F $q('( <;/// $ 9 B BCV W W ,$ **;+>???--"&";"E&*&?&M#| .      **;+@AAANNQ$-QQ     -, <K$79M#NNNNN"&">$"F #  ""T%AA./  
 # F&*&?&J&J0' '# '2* F ..t|<<<<..{/DEEE37;3C3C3E3ED0 |{444!444 AKK L4= L L LMMM6>>>-==t=LLL**;+?@@@@@A A !555 MKK VT] V V VWWW26D/-==u=MMM**;+>?????)-)D)L *((**T-HH56 ' * 	M.2.G.R.R7/ /+ /:2 L $ 6 6{7J K K K:>+:J:J:L:LD777+ 65	M 	M
 ;:r7   c                 F    d| _         | j                                         d S )NT)r   r   rJ   r3   s    r5   shutdownzProxyState.shutdown  s%    "!&&(((((r7   c                 F    | j         sdS | j                                        S )zReturn whether the proxy actor is shutdown.

        For a proxy actor to be considered shutdown, it must be marked as
        _shutting_down and the actor must be shut down.
        F)r   r   rB   r3   s    r5   is_ready_for_shutdownz ProxyState.is_ready_for_shutdown  s)     " 	5(44666r7   r.   N)F)rK   rL   rM   r   r-   rO   r   r   rh   rN   r   rV   r   r6   r)   r   r(   r   r   r   r   r   rQ   r   r   r   r   rR   r7   r5   r   r   >  s+        $% 577
 
)
 
 	

 
 
 !
 
 
 
 
< 6k 6 6 6 X6  C       X  2# 2 2 2 X2     X #| # # # X# )S ) ) ) X)7+ 7$ 7 7 7 7( ( ( ( (B= = = =	 	$ 	 	 	 	\MD \M \M \M \M|) ) )	7t 	7 	7 	7 	7 	7 	7r7   r   c                      e Zd ZdZdee e            fdedede	de
dee         dee         d	ee         d
efdZde
fdZd"dZdefdZdefdZdefdZdeeef         fdZdeeef         fdZdeeef         fdZdedee          fdZ!de"e         fdZ#d#de"e         de"e         fdZ$dee%eeef                  fdZ&dedefdZ'dedededefdZ(d"d Z)defd!Z*dS )$ProxyStateManagerzManages all state for proxies in the system.

    This class is *not* thread safe, so any state-modifying methods should be
    called with a lock held.
    NrW   head_node_idcluster_node_info_cacherU   rX   r]   actor_proxy_wrapper_classr   c	                 (   || _         |pt                      | _        |pt                      | _        t                      | _        t                      | _        || _        || _	        || _
        || _        || _        t          |t                    sJ d S r   )rU   r%   _http_optionsr&   _grpc_optionsr   _proxy_states_proxy_restart_counts_head_node_id_proxy_actor_class_actor_proxy_wrapper_classr   _cluster_node_info_cache
isinstancerO   )	r4   rW   r   r   rU   rX   r]   r   r   s	            r5   rh   zProxyStateManager.__init__%  s     -):[]]):[]]7;vv8<""."3*C'(?%,,,,,,,,r7   c                     || _         d S r   )rU   )r4   rU   s     r5   reconfigure_logging_configz,ProxyStateManager.reconfigure_logging_config>  s    ,r7   r.   c                 f    | j                                         D ]}|                                 d S r   )r   valuesr   )r4   proxy_states     r5   r   zProxyStateManager.shutdownA  s?    -4466 	# 	#K  """"	# 	#r7   c                 b    t          d | j                                        D                       S )zReturn whether all proxies are shutdown.

        Iterate through all proxy states and check if all their proxy actors
        are shutdown.
        c              3   >   K   | ]}|                                 V  d S r   )r   ).0r   s     r5   	<genexpr>z:ProxyStateManager.is_ready_for_shutdown.<locals>.<genexpr>K  sB       
 
 --//
 
 
 
 
 
r7   )allr   r   r3   s    r5   r   z'ProxyStateManager.is_ready_for_shutdownE  sA      
 
#188::
 
 
 
 
 	
r7   c                     | j         S r   )r   r3   s    r5   
get_configzProxyStateManager.get_configP      !!r7   c                     | j         S r   )r   r3   s    r5   get_grpc_configz!ProxyStateManager.get_grpc_configS  r   r7   c                 H    d | j                                         D             S )Nc                 $    i | ]\  }}||j         S rR   )rV   r   rZ   states      r5   
<dictcomp>z7ProxyStateManager.get_proxy_handles.<locals>.<dictcomp>W  s.     
 
 
,:GUGU'
 
 
r7   r   itemsr3   s    r5   get_proxy_handlesz#ProxyStateManager.get_proxy_handlesV  s2    
 
>B>P>V>V>X>X
 
 
 	
r7   c                 H    d | j                                         D             S )Nc                 $    i | ]\  }}||j         S rR   )r   r  s      r5   r  z5ProxyStateManager.get_proxy_names.<locals>.<dictcomp>\  s.     
 
 
*8'5GU%
 
 
r7   r  r3   s    r5   get_proxy_namesz!ProxyStateManager.get_proxy_names[  s2    
 
<@<N<T<T<V<V
 
 
 	
r7   c                 H    d | j                                         D             S )Nc                 $    i | ]\  }}||j         S rR   )r   r  s      r5   r  z7ProxyStateManager.get_proxy_details.<locals>.<dictcomp>a  s1     
 
 
 U(
 
 
r7   r  r3   s    r5   get_proxy_detailsz#ProxyStateManager.get_proxy_details`  s2    
 
"&"4":":"<"<
 
 
 	
r7   protocolc                    g }|t           j        k    r| j        j        nE|t           j        k    r#t          | j                  sg S | j        j        nt          d|           fd| j        	                                D             }|S )a  In Ray Serve, every proxy is responsible for routing requests to the
        correct application. Here we curate a list of targets for the given protocol.
        Where each target represents how to reach a proxy.

        Args:
            protocol: Either "http" or "grpc"
        zInvalid protocol: c                     g | ]K\  }}|j         j        t          j        k    t	          |j         j        |j         j        |j                   LS ))ipr\   instance_idrY   )r   r   r)   r   r*   r   r   r   )r   _r  r\   s      r5   
<listcomp>z1ProxyStateManager.get_targets.<locals>.<listcomp>x  sj     	
 	
 	
 5")[-@@@ &.!/@%	   A@@r7   )
r   HTTPr   r\   GRPCr#   r   rx   r   r  )r4   r  targetsr\   s      @r5   get_targetszProxyStateManager.get_targetsf  s     +++%*DD---"4#566 	%*DD<(<<===	
 	
 	
 	
 !.4466	
 	
 	
 r7   c                 H    d | j                                         D             S )Nc                     h | ]	}|j         
S rR   )r6   )r   r  s     r5   	<setcomp>z>ProxyStateManager.get_alive_proxy_actor_ids.<locals>.<setcomp>  s    HHH5HHHr7   )r   r   r3   s    r5   get_alive_proxy_actor_idsz+ProxyStateManager.get_alive_proxy_actor_ids  s%    HHD,>,E,E,G,GHHHHr7   proxy_nodesc                 l   |t                      }t          r|                    | j                   |                     |          }d |D             }| j                                        D ]\  }}||v}|                    |           |                                  | 	                    |           dS )zUpdate the state of all proxies.

        Start proxies on all nodes if not already exist and stop the proxies on nodes
        that are no longer exist. Update all proxy states. Kill and restart
        unhealthy proxies.
        Nc                     h | ]\  }}}|	S rR   rR   )r   rZ   r  s      r5   r  z+ProxyStateManager.update.<locals>.<setcomp>  s    EEE}w17EEEr7   )
setr   addr   _get_target_nodesr   r  r   _stop_proxies_if_needed_start_proxies_if_needed)r4   r  target_nodestarget_node_idsrZ   r   rD   s          r5   r   zProxyStateManager.update  s     %%K 3 	0OOD.///--k::EEEEE$($6$<$<$>$> 	, 	, G[o5H!!(++++$$&&&%%l33333r7   c                 "     j         j        }|t          j        k    rg S fd j                                        D             }|t          j        k    r9 fd|D             }t          |          dk    sJ d j         d| d            |S |S )zTReturn the list of (node_id, ip_address) to deploy HTTP and gRPC servers
        on.c                 *    g | ]\  }}}|v 
|||fS rR   rR   )r   rZ   
ip_addressr  r  s       r5   r  z7ProxyStateManager._get_target_nodes.<locals>.<listcomp>  s;     
 
 
0[+%% j+.%%%r7   c                 8    g | ]\  }}}|j         k    |||fS rR   )r   )r   rZ   r(  r  r4   s       r5   r  z7ProxyStateManager._get_target_nodes.<locals>.<listcomp>  s>       4GZd000 *k2000r7   r   z#Head node not found! Head node id: z, all nodes: .)	r   locationr$   NoServerr   get_alive_nodesHeadOnlylenr   )r4   r  r+  r$  nodess   ``   r5   r!  z#ProxyStateManager._get_target_nodes  s     %.~...I
 
 
 
484Q4a4a4c4c
 
 
 ~...   8D  E
 u::???.d6H . .*. . . #?? Lr7   rZ   c                 ,    t          t          |          S r   )r"   r    )r4   rZ   s     r5   _generate_actor_namez&ProxyStateManager._generate_actor_name  s     !17;;;r7   rY   r[   c           
      t   | j         }| j        }|| j        k    rgt          j        d          St
                              d| d           t          |          }t          t          j        d                    |_	        || j        k    rt          j        d          tt
                              d| dt          t          j        d                                t          |          }t          t          j        d                    |_	        | 
                    | j        |||||| j                  S )aX  Helper to start or reuse existing proxy and wrap in the proxy actor wrapper.

        Compute the HTTP port based on `TEST_WORKER_NODE_HTTP_PORT` env var and gRPC
        port based on `TEST_WORKER_NODE_GRPC_PORT` env var. Passed all the required
        variables into the proxy actor wrapper class and return the proxy actor wrapper.
        TEST_WORKER_NODE_HTTP_PORTNzF`TEST_WORKER_NODE_HTTP_PORT` env var is set. Using it for worker node r*  TEST_WORKER_NODE_GRPC_PORTzF`TEST_WORKER_NODE_GRPC_PORT` env var is set. Using it for worker node )rU   rW   rX   rY   rZ   r[   r]   )r   r   r   osgetenvrz   r   r   r   r\   r   rU   r   )r4   rY   rZ   r[   rW   rX   s         r5   _start_proxyzProxyStateManager._start_proxy  s\    )) t)))	677CNN7,37 7 7   $L11L #BI.J$K$K L LL t)))	677CNNB,3B Bry!=>>??B B  
 $L11L #BI.J$K$K L LL...%%+"5 / 
 
 	
r7   c                     |D ]z\  }}}|| j         v r|                     |          }|                     |||          }t          |||||| j                            |d          | j                  | j         |<   {dS )z8Start a proxy on every node if it doesn't already exist.)rZ   )rY   rZ   r[   r   )r   r   rZ   r   r   r   r   N)r   r2  r8  r   r   r   r   )r4   r$  rZ   r[   r   rY   r   s          r5   r#  z*ProxyStateManager._start_proxies_if_needed  s     ;G 	 	6G_&6$,,,,,W,==D"&"3"3 / #4 # # +5$7'!1$($>$B$B7A$N$Nk+ + +Dw''	 	r7   c                    | j                                         }g }| j                                        D ]\  }}||vr4t                              d| d           |                    |           =|j        t          j	        k    r4t                              d| d           |                    |           |j        t          j
        k    r3t                              d| d           |                    |           |D ]B}| j                            |          }|j        dz   | j        |<   |                                 CdS )ztRemoves proxy actors.

        Removes proxy actors from any nodes that no longer exist or unhealthy proxy.
        z Removing proxy on removed node 'rk   zProxy on node 'zI' is unhealthy. Shutting down the unhealthy proxy and starting a new one.z Removing drained proxy on node 'r   N)r   get_alive_node_idsr   r  rz   r{   appendr   r)   r   r   popr   r   r   )r4   alive_node_idsto_stoprZ   r   s        r5   r"  z)ProxyStateManager._stop_proxies_if_needed  sr   
 6IIKK$($6$<$<$>$> 	( 	( G[n,,JwJJJKKKw''''#{'<<<Bg B B B   w''''#{':::JwJJJKKKw''' 	# 	#G,0099K2=2QTU2UD&w/  """"	# 	#r7   r   r   )+rK   rL   rM   __doc__r!   rT   r   r%   rO   r   r'   r   r&   r   r-   r   rh   r   r   rQ   r   r   r   r   r   r   r  r	  r(   r  r   r   r*   r  r	   r  r   r
   r!  r2  r8  r#  r"  rR   r7   r5   r   r     s         /3.88I 577- -!- - "6	-
 &- {+-  
+- $(#5- - - - -2- - - - -# # # #	
t 	
 	
 	
 	
"K " " " "" " " " "
4(;#< 
 
 
 


fck!2 
 
 
 


4(<#= 
 
 
 
O V    <I3s8 I I I I4 4#f+ 4S 4 4 4 40U3S=5I0J    8<C <C < < < <.
.
 .
 	.

 
.
 .
 .
 .
`   0# # # # # # #r7   r   futr   c                 \    |                                  s|                     |           d S d S r   )r   set_exception)rA  r   s     r5   _try_set_exceptionrD  !  s6    88:: ! r7   refr8   r.   c           
      F   t          j                    }t          j        |                                           }|d|dk    s
J d            |                    t          |d          t          |t          d| d                    |                    fd           |S )Nr   z$Timeout value should be non-negativezFuture cancelled after timeout sc                 ,                                     S r   )r   )r  timeout_handlers    r5   <lambda>z wrap_as_future.<locals>.<lambda>5  s    O,B,B,D,D r7   )	asyncioget_running_loopwrap_futurer   
call_latermaxrD  r   add_done_callback)rE  r8   loopaio_futrI  s       @r5   r   r   &  s    #%%D!#**,,//GA~~~E~~~//	1G9GGGHH	
 
 	!!"D"D"D"DEEENr7   r   )HrK  r   loggingr6  abcr   r   copyr   typingr   r   r   r	   r
   r   rv   r   ray._common.network_utilsr   ray._common.utilsr   r   	ray.actorr   ray.exceptionsr   r   *ray.serve._private.cluster_node_info_cacher   ray.serve._private.commonr   r   ray.serve._private.constantsr   r   r   r   r   r   r   r   r   r   r    ray.serve._private.proxyr!   ray.serve._private.utilsr"   r#   ray.serve.configr$   r%   r&   ray.serve.schemar'   r(   r)   r*   ray.util.scheduling_strategiesr+   	getLoggerrz   r-   rT   r   r   Futurer   rD  rP   r   rR   r7   r5   <module>re     s      				 # # # # # # # #       9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 



       3 3 3 3 3 3 . . . . . . . . ! ! ! ! ! ! 9 9 9 9 9 9 9 9 K K K K K K = = = = = = = =                          0 / / / / /        F E E E E E E E E E            J I I I I I		,	-	-9" 9" 9" 9" 9"3 9" 9" 9"xP6 P6 P6 P6 P6 P6 P6 P6f]7 ]7 ]7 ]7 ]7 ]7 ]7 ]7@@# @# @# @# @# @# @# @#FGN y    
 	 huo       r7   