
    &`i\                        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
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 d dlmZmZmZmZ d d	lmZmZ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* d dl+m,Z, d dl-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3m4Z4mZ5mZ6 d dl7m8Z8 d dl9m:Z:m;Z;m<Z<m=Z=  ej>        e"          Z?de
de
fdZ@ G d d          ZAdS )    N)Sequencewraps)CallableDictListOptionalTupleUnion)ActorHandle)StatusOverview)BuiltApplication)DeploymentIDDeploymentStatusDeploymentStatusInfoRequestRoutingInfo)(CLIENT_CHECK_CREATION_POLLING_INTERVAL_SCLIENT_POLLING_INTERVAL_SHTTP_PROXY_TIMEOUTMAX_CACHED_HANDLESSERVE_DEFAULT_APP_NAMESERVE_LOGGER_NAME)ServeController)get_deploy_args)DeploymentInfo)ASGIAppReplicaWrapper)get_random_string)HTTPOptionsRayServeException)ApplicationArgsDeploymentArgsDeploymentRouter   r   )DeploymentHandle)ApplicationStatusLoggingConfigServeApplicationSchemaServeDeploySchemafreturnc                 <     t                      fd            }|S )Nc                 H    | j         rt          d           | g|R i |S )Nz"Client has already been shut down.)	_shutdownr    )selfargskwargsr)   s      m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/serve/_private/client.pycheckz _ensure_connected.<locals>.check6   s=    > 	J#$HIIIq'''''''    r   )r)   r2   s   ` r1   _ensure_connectedr4   5   s3    
1XX( ( ( ( X(
 Lr3   c                      e Zd ZdefdZed             Zed             Zd Zd Z	d Z
d2d	ed
dfdZd2d	ed
dfdZd3ded	efdZ	 d4deded	efdZ	 d3deded	efdZd3ded	efdZe	 d5ded
dfd            Zeddddee         deded
ee         fd            Ze	 d6deeef         d ed
dfd!            Zdee         d
dfd"Z ed5d#ee         d$efd%            Z!ed5d$efd&            Z"ededed
e#e$ef         fd'            Z%ee&fded
e'fd(            Z(ed
ee'         fd)            Z)ed
ee*         fd*            Z+ed
e,fd+            Z-ee&dfdede.e         d,ed
efd-            Z/ed.e0fd/            Z1ed0e2fd1            Z3dS )7ServeControllerClient
controllerc                 .   || _         d| _        t          j        |j                                                  | _        t          j        |j                                                  | _        t                      | _
        t                      | _        d S )NF)_controllerr-   raygetget_http_configremote_http_configget_root_url	_root_urldicthandle_cacheset_evicted_handle_keys)r.   r7   s     r1   __init__zServeControllerClient.__init__@   st     -7),1K1R1R1T1T)U)U!8!?!?!A!ABB !FF$'EE!!!r3   c                     | j         S N)r@   r.   s    r1   root_urlzServeControllerClient.root_urlM   s
    ~r3   c                     | j         S rG   )r>   rH   s    r1   http_configz!ServeControllerClient.http_configQ   s      r3   c                      t          d          )Nz&Ray Serve client cannot be serialized.r   rH   s    r1   
__reduce__z ServeControllerClient.__reduce__U   s    !IKKKr3   c                     t          | j                  D ])}| j        |                                          | j        |= *dS )zShuts down all cached handles.

        Remove the reference to the cached handles so that they can be
        garbage collected.
        N)listrB   shutdown)r.   	cache_keys     r1   shutdown_cached_handlesz-ServeControllerClient.shutdown_cached_handlesX   sP     d/00 	- 	-Ii(11333!),,	- 	-r3   c                 |    K    fdt          j        fdt           j                  D               d{V  dS )zShuts down all cached handles asynchronously.

        Remove the reference to the cached handles so that they can be
        garbage collected.
        c                 f   K   j         |                                           d {V  j         | = d S rG   )rB   shutdown_async)rQ   r.   s    r1   shutdown_taskzJServeControllerClient.shutdown_cached_handles_async.<locals>.shutdown_taski   sD      #I.==?????????!),,,r3   c                 &    g | ]} |          S  rX   ).0rQ   rV   s     r1   
<listcomp>zGServeControllerClient.shutdown_cached_handles_async.<locals>.<listcomp>n   s#    PPP9mmI&&PPPr3   N)asynciogatherrO   rB   )r.   rV   s   `@r1   shutdown_cached_handles_asyncz3ServeControllerClient.shutdown_cached_handles_asyncb   s|      	- 	- 	- 	- 	- nPPPPT=N8O8OPPP
 	
 	
 	
 	
 	
 	
 	
 	
 	
r3         >@	timeout_sr*   Nc                 f   |                                   t          j                    r| j        s	 t          j        | j        j                                        |           nC# t          j        j	        $ r Y n-t          $ r! t                              d| d           Y nw xY wd| _        dS dS dS )Completely shut down the connected Serve instance.

        Shuts down all processes and deletes all state associated with the
        instance.
        timeout&Controller failed to shut down within *s. Check controller logs for more details.TN)rR   r:   is_initializedr-   r;   r9   graceful_shutdownr=   
exceptionsRayActorErrorTimeoutErrorloggerwarningr.   r_   s     r1   rP   zServeControllerClient.shutdownq   s     	$$&&& 	" 	"	(:AACCYWWWWW>/      >Y > > >    
 "DNNN	" 	" 	" 	"s   2A# #B#9'B#"B#c                   K   |                                   d{V  t          j                    r| j        s	 t	          j        | j        j                                        |           d{V  nC# t          j	        j
        $ r Y n-t          $ r! t                              d| d           Y nw xY wd| _        dS dS dS )ra   Nrb   rd   re   T)r]   r:   rf   r-   r[   wait_forr9   rg   r=   rh   ri   rj   rk   rl   rm   s     r1   rU   z$ServeControllerClient.shutdown_async   s/      00222222222 	" 	"&$6==??           >/      >Y > > >    
 "DNNN	" 	" 	" 	"s   8A1 1B1'B10B1namec                    t          j                     }t          j                     |z
  |k     s|dk     r*t          j        | j        j                            |                    }|t          d| d          t          j        t          j
        |                    }|j        t          j        k    rdS |j        t          j        k    rt          d| d|j                   |j        t          j        k    sJ t"                              d| d|j         d	           t          j        t(                     t          j                     |z
  |k     #|dk     *t+          d| d
| d          )zWaits for the named deployment to enter "HEALTHY" status.

        Raises RuntimeError if the deployment enters the "UNHEALTHY" status
        instead.

        Raises TimeoutError if this doesn't happen before timeout_s.
        r   NzWaiting for deployment z- to be HEALTHY, but deployment doesn't exist.Deployment z is UNHEALTHY: Waiting for z  to be healthy, current status: .z did not become HEALTHY after s.)timer:   r;   r9   get_deployment_statusr=   RuntimeErrorr   
from_protoDeploymentStatusInfoProto
FromStringstatusr   HEALTHY	UNHEALTHYmessageUPDATINGrk   debugsleepr   rj   r.   rq   r_   startstatus_bytesr}   s         r1   _wait_for_deployment_healthyz2ServeControllerClient._wait_for_deployment_healthy   s    	ikkE!I--Q74#3#I#P#PQU#V#VWWL#"4d 4 4 4  
 *4)4\BB F } 0 888"2"<<<"K$KK6>KK  
 }(8(AAAAALL$t $ $=$ $ $   J01117 ikkE!I--Q: OdOO)OOO  r3   <   app_namec                    t          j                     }t          j                     |z
  |k     rt          j        | j        j                            |                    }|dS t          j        t          j	        |                    }t                              d| d| d           t          j        t                     t          j                     |z
  |k     t          d| d| d          )zWaits for the named deployment to be shut down and deleted.

        Raises TimeoutError if this doesn't happen before timeout_s.
        Nrt   z  to be deleted, current status: ru   rs   z wasn't deleted after rv   )rw   r:   r;   r9   rx   r=   r   rz   r{   r|   rk   r   r   r   rj   )r.   rq   r   r_   r   curr_status_bytescurr_statuss          r1   _wait_for_deployment_deletedz2ServeControllerClient._wait_for_deployment_deleted   s     	ikkE!I-- # 6==dCC! ! !(.9)45FGG K LLStSS[SSS   J0111 ikkE!I-- VTVVVVVWWWr3   deployment_namec           	         t          j                     }t          j                     |z
  |k     s|dk     rt          j        | j        j                            ||                    }|dS t                              d| d| d           t          j        t                     t          j                     |z
  |k     |dk     t          d| d| d| d          )	ag  Waits for the named deployment to be created.

        A deployment being created simply means that its been registered
        with the deployment state manager. The deployment state manager
        will then continue to reconcile the deployment towards its
        target state.

        Raises TimeoutError if this doesn't happen before timeout_s.
        r   NzWaiting for deployment 'z' in application 'z' to be created.zDeployment 'z' did not become HEALTHY after rv   )rw   r:   r;   r9   rx   r=   rk   r   r   r   rj   )r.   r   r   r_   r   r   s         r1   _wait_for_deployment_createdz2ServeControllerClient._wait_for_deployment_created   s    	ikkE!I--Q7 6==oxXX L 'LL/? / // / /   J?@@@ ikkE!I--Q > > >( > >09> > >  r3   c                    t          j                     }t          j                     |z
  |k     s|dk     r't          j        | j        j                            |                    }|t          d| d          t          j        t          j
        |                    }|j        j        t          j        k    rdS |j        j        t          j        k    rt          d| d|j        j                   t"                              d| d|j        j         d	           t          j        t(                     t          j                     |z
  |k      |dk     't+          d
| d| d          )zWaits for the named application to enter "RUNNING" status.

        Raises:
            RuntimeError: if the application enters the "DEPLOY_FAILED" status instead.
            TimeoutError: if this doesn't happen before timeout_s.
        r   NzWaiting for application z. to be RUNNING, but application doesn't exist.zDeploying application z	 failed: rt   z  to be RUNNING, current status: ru   zApplication z did not become RUNNING after rv   )rw   r:   r;   r9   get_serve_statusr=   ry   r   rz   StatusOverviewProtor|   
app_statusr}   r%   RUNNINGDEPLOY_FAILEDr   rk   r   r   r   rj   r   s         r1   _wait_for_application_runningz3ServeControllerClient._wait_for_application_running   s    	ikkE!I--Q74#3#D#K#KD#Q#QRRL#"5t 5 5 5  
 $.#.|<< F  '+<+DDD")->-LLL"WTWWF<M<UWW   LL/t / /$+/ / /   J0111/ ikkE!I--Q2 PtPP9PPP  r3   Twait_for_applications_runningc                    t          j        | j        j                                                  }fd|                                D             }t          j        |t          t          |                    \  }}t          |          dk    rt          dt           d          |D ]T}	 t          j        |d           # t           j
        j        $ r Y 0t          $ r t          dt           d          w xY wdS )	z3Wait for the proxies to be ready to serve requests.c                 F    g | ]}|j                                        S )r   )servingr=   )rY   handler   s     r1   rZ   zBServeControllerClient.wait_for_proxies_serving.<locals>.<listcomp>+  sE     
 
 
  N!!.K "  
 
 
r3   )rc   num_returnsr   zProxies not available after rv      rb   N)r:   r;   r9   get_proxiesr=   valueswaitr   lenrj   rh   ri   	Exception)r.   r   proxy_handlesserving_refsdonependingrefs    `     r1   wait_for_proxies_servingz.ServeControllerClient.wait_for_proxies_serving%  sG   
  0 < C C E EFF
 
 
 
 (..00	
 
 
 &L))
 
 
g w<<!T>PTTTUUU  	 	CQ'''''>/      "I3EIII  	 	s   .CC="C=)$wait_for_ingress_deployment_creationr   
built_appsr   c          
         i }i }|D ]U}g }|j         D ]}|j        "|j        r|                    |j                  }|j        |j        k    }	t          |j        |	|j        |j        |j        pt                      |	r|j
        nd           }
t                      }|
d         |_        |
d         |_        |
d         |_        |
d         |_        |
d         r|
d         |_
        |
d         |_        |                    |                                           	t'                      }|j        |_        |||j        <   |                                ||j        <   W|                     |           t-          j        | j        j                            ||                     g }|D ]}|r |                     |j        |j                   |r\|                     |j                   |j
        d	| j        z   |j
        z   }nd
}t<                              d|j         d| d           |                    |                      |j        |j        d                     |S )N)logging_config)ingressreplica_configdeployment_configversionroute_prefixr   deployment_config_proto_bytesreplica_config_proto_bytesdeployer_job_idr   r   z at  zApplication 'z
' is readyru   F)check_exists)!deploymentsr   optionsrq   ingress_deployment_namer   _replica_config_deployment_config_versionr   r   r"   r   r   r   r   r   appendSerializeToStringr!   external_scaler_enabled_check_ingress_deploymentsr:   r;   r9   deploy_applicationsr=   r   r   r@   rk   info
get_handle)r.   r   r   r   name_to_deployment_args_listname_to_application_argsappdeployment_args_list
deployment
is_ingressdeployment_argsdeployment_args_protoapplication_args_protohandlesurl_parts                  r1   r   z)ServeControllerClient.deploy_applicationsF  s    (*$#%  )	; )	;C#% !o W W
,49K4!+!3!33CU!3!V!VJ'_0KK
"1O&#-#=&0&C&/F3D3F3F5?!I!1!1T# # # )7(8(8%8G%9%5 ;J3;%7 8G08%4 9H%9%5 #>2 Y9H9X)60?	0J%-$++,A,S,S,U,UVVVV%4%6%6"=@=X":5I(2 '88:: % 
 	''
333077,.F 	
 	
 	
  	 	C 4 Y11#2MsxXXX, M2238<<<#/%69IIHH!HKCHKKKKKLLLNN/        r3   Fconfig	_blockingc                    t          j        | j        j                            |                     |r/d}t          |t                    rd |j        D             n|j        ht          j	                    }t          j	                    |z
  |k     r| 
                                }fd|D             }t                    t          |          k    r1t          |                                          t          j        hk    rnFt          j        t"                     t          j	                    |z
  |k     t%          d| d          |                     d           dS dS )	a  Starts a task on the controller that deploys application(s) from a config.

        Args:
            config: A single-application config (ServeApplicationSchema) or a
                multi-application config (ServeDeploySchema)
            _blocking: Whether to block until the application is running.

        Raises:
            RayTaskError: If the deploy task on the controller fails. This can be
                because a single-app config was deployed after deploying a multi-app
                config, or vice versa.
        r   c                     h | ]	}|j         
S rX   )rq   )rY   r   s     r1   	<setcomp>z4ServeControllerClient.deploy_apps.<locals>.<setcomp>  s    EEE#SXEEEr3   c                 F    i | ]}|j         v |j         |j        j        S rX   )rq   r   r}   )rY   r}   	app_namess     r1   
<dictcomp>z5ServeControllerClient.deploy_apps.<locals>.<dictcomp>  s:     ! ! !{i// K!2!9///r3   z&Serve application isn't running after rv   Tr   N)r:   r;   r9   apply_configr=   
isinstancer(   applicationsrq   rw   list_serve_statusesr   rC   r   r%   r   r   r   rj   r   )r.   r   r   r_   r   statusesapp_to_statusr   s          @r1   deploy_appsz!ServeControllerClient.deploy_apps  s   $ 	 -44V<<=== 	NI&"344 *EE1DEEE		#[M	IKKE)++%	113355! ! ! !"*! ! !
 y>>S%7%777C!((**= ='/0=1 =1 
4555 )++%	11 #JYJJJ   )))MMMMM7	N 	Nr3   c                     |D ]d}d}|j         D ]:}t          j        |j                  rt	          |j        t
                    r|dz  };|dk    rt          d|j         d          edS )zCheck @serve.ingress of deployments across applications.

        Raises: RayServeException if more than one @serve.ingress
            is found among deployments in any single application.
        r   r   z3Found multiple FastAPI deployments in application "za".Please only include one deployment with @serve.ingress in your application to avoid this issue.N)r   inspectisclassfunc_or_class
issubclassr   r    rq   )r.   r   r   num_ingress_deploymentsr   s        r1   r   z0ServeControllerClient._check_ingress_deployments  s      	 	C&'#!o 1 1
?:#;<< 1,.CB B 1 ,q0+&**'?#( ? ? ?   +	 	r3   namesblockingc                 j   |sd S t                               d|            | j        j                            |           |rt          j                    }t          j                    |z
  dk     rt          j        | j        j                            |                    }d}|D ]D}t          j
        t          j        |                    }|j        j        t          j        k    rd}E|rd S t          j        t$                     t          j                    |z
  dk     t'          d|           d S )NzDeleting app r   TFz6Some of these applications weren't deleted after 60s: )rk   r   r9   delete_appsr=   rw   r:   r;   get_serve_statusesr   rz   r   r|   r   r}   r%   NOT_STARTEDr   r   rj   )r.   r   r   r   curr_statuses_bytesall_deletedcur_status_bytes
cur_statuss           r1   r   z!ServeControllerClient.delete_apps  sJ    	F+E++,,,$++E222 	IKKE)++%**&)g$7>>uEE' '# #(; , ,$!/!:+67GHH" "J ",37H7TTT&+ F
4555 )++%** #TUTT  #	 	r3   c                     g }t          j        | j        j                                                  D ]D}t          j        |          }t          j        |          }|	                    |j
                   E|                     ||           dS )zDelete all applicationsN)r:   r;   r9   r   r=   r   r|   r   rz   r   rq   r   )r.   r   all_appsr   protor}   s         r1   delete_all_appsz%ServeControllerClient.delete_all_apps   s     GD$4$H$O$O$Q$QRR 	) 	)L'2<@@E#.u55FOOFK((((8,,,,,r3   c                     t          j        t          j        | j        j                            ||                              }t          j        |j	                  |j
        dk    r|j
        nd fS )Nr   )r#   r|   r:   r;   r9   get_deployment_infor=   r   rz   deployment_inforoute)r.   rq   r   deployment_routes       r1   r   z)ServeControllerClient.get_deployment_info
  sq     +5GD$8??hOOPP
 
 %&6&FGG&6&<&B&B""
 	
r3   c                     t          j        t          j        | j        j                            |                              }t          j        |          S rG   )	r   r|   r:   r;   r9   r   r=   r   rz   )r.   rq   r   s      r1   r   z&ServeControllerClient.get_serve_status  sH    #.GD$5<<TBBCC
 
 (///r3   c                 z    t          j        | j        j                                                  }d |D             S )Nc                 Z    g | ](}t          j        t          j        |                    )S rX   )r   rz   r   r|   rY   r   s     r1   rZ   z=ServeControllerClient.list_serve_statuses.<locals>.<listcomp>   s?     
 
 
 %&9&D\&R&RSS
 
 
r3   )r:   r;   r9   r   r=   r.   statuses_bytess     r1   r   z)ServeControllerClient.list_serve_statuses  sD    !1!E!L!L!N!NOO
 
 .
 
 
 	
r3   c                 z    t          j        | j        j                                                  }d |D             S )Nc                 Z    g | ](}t          j        t          j        |                    )S rX   )r   rz   r{   r|   r   s     r1   rZ   zEServeControllerClient.get_all_deployment_statuses.<locals>.<listcomp>(  sE     
 
 
  !+)4\BB 
 
 
r3   )r:   r;   r9   get_all_deployment_statusesr=   r   s     r1   r  z1ServeControllerClient.get_all_deployment_statuses%  sF    !1!M!T!T!V!VWW
 
 !/	
 
 
 	
r3   c                 b    t          j        | j        j                                                  S rG   )r:   r;   r9   get_serve_instance_detailsr=   rH   s    r1   get_serve_detailsz'ServeControllerClient.get_serve_details/  s$    wt'BIIKKLLLr3   r   c                 z   t          ||          }|||f}|| j        v r| j        |         S |rFt          j        | j        j                                                  }||vrt          | d          t          ||          }|| j        |<   || j	        v rt                              d           t          | j                  t          k    rlt          j        t!          | j                                                            }| j	                            |           | j                            |           |S )aG  Construct a handle for the specified deployment.

        Args:
            deployment_name: Deployment name.
            app_name: Application name.
            check_exists: If False, then Serve won't check the deployment
                is registered. True by default.

        Returns:
            DeploymentHandle
        )rq   r   z does not exist.a4  You just got a ServeHandle that was evicted from internal cache. This means you are getting too many ServeHandles in the same process, this will bring down Serve's performance. Please post a github issue at https://github.com/ray-project/ray/issues to let the Serve team to find workaround for your use case.)r   rB   r:   r;   r9   list_deployment_idsr=   KeyErrorr$   rD   rk   rl   r   r   randomchoicerO   keysaddpop)	r.   r   r   r   deployment_idrQ   all_deploymentsr   	evict_keys	            r1   r   z ServeControllerClient.get_handle3  sC   $ %/HMMM$h=	)))$Y// 	C!gd&6&J&Q&Q&S&STTOO33-AAABBB!/8<<'-)$111NN=   t !!$666 d4+<+A+A+C+C&D&DEEI%)))444!!),,,r3   r   c                 D    | j         j                            |           dS )zRecord replica routing information for a replica.

        Args:
            info: RequestRoutingInfo including deployment name, replica tag,
                multiplex model ids, and routing stats.
        N)r9   record_request_routing_infor=   )r.   r   s     r1   r  z1ServeControllerClient.record_request_routing_infoe  s$     	4;;DAAAAAr3   r   c                 D    | j         j                            |           dS )z<Reconfigure the logging config for the controller & proxies.N)r9   !reconfigure_global_logging_configr=   )r.   r   s     r1   update_global_logging_configz2ServeControllerClient.update_global_logging_configo  s$     	:AA.QQQQQr3   )r^   )rp   )r   )T)F)4__name__
__module____qualname__r   rE   propertyrI   rK   rM   rR   r]   floatrP   rU   strintr   r   r   r   r4   boolr   r   r   r   r$   r   r   r'   r(   r   r   r   r   r
   r   r   r   r   r   r   r   r  r   r  r	   r   r   r  r&   r  rX   r3   r1   r6   r6   ?   s       ** * * *   X ! ! X!L L L- - -
 
 
" "% "4 " " " "*" "e "t " " " ".( ( ( ( ( ( (V :<X XX#&X36X X X X2 EG ".1>A   B# ## ## # # # #J 48 -1	   @ 
 6:.2S S S-.S /3	S
 (,S 
	S S S Sj   .N .N,.??@.N .N 
	.N .N .N .N`"#34	   .  c d    6 - - - - - - 	
	
#&	
	~s"	#	
 	
 	
 	
 +A 0 0S 0n 0 0 0 0 
T.%9 
 
 
 
 
T2F-G 
 
 
 
 M4 M M M M  #9!	/ // 3-/ 	/
 
/ / / /b B0B B B B B R= R R R R R Rr3   r6   )Br[   r   loggingr  rw   collections.abcr   	functoolsr   typingr   r   r   r	   r
   r   r:   	ray.actorr   $ray.serve._private.application_stater   ray.serve._private.build_appr   ray.serve._private.commonr   r   r   r   ray.serve._private.constantsr   r   r   r   r   r   ray.serve._private.controllerr   ray.serve._private.deploy_utilsr   "ray.serve._private.deployment_infor   ray.serve._private.http_utilr   ray.serve._private.utilsr   ray.serve.configr   ray.serve.exceptionsr    ray.serve.generated.serve_pb2r!   r"   r#   r{   r   ray.serve.handler$   ray.serve.schemar%   r&   r'   r(   	getLoggerrk   r4   r6   rX   r3   r1   <module>r1     s        $ $ $ $ $ $       ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 



 ! ! ! ! ! ! ? ? ? ? ? ? 9 9 9 9 9 9                           : 9 9 9 9 9 ; ; ; ; ; ; = = = = = = > > > > > > 6 6 6 6 6 6 ( ( ( ( ( ( 2 2 2 2 2 2              . - - - - -            
	,	-	- h    sR sR sR sR sR sR sR sR sR sRr3   