
    %`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Zd dlmZm	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 d dlmZ  ej        e          Zej        dk    Ze G d	 d
                      Ze G d d                      ZdS )    N)DictOptional)ray_constants)disable_client_hook)GcsClientOptions)FAKE_HEAD_NODE_ID)DeveloperAPIntc                   T    e Zd ZdZ	 ddededefdZ	 ddZdd	ee	         fd
Z
d ZdS )AutoscalingClusterzzCreate a local autoscaling cluster for testing.

    See test_autoscaler_fake_multinode.py for an end-to-end example.
    Fhead_resourcesworker_node_typesautoscaler_v2c                 N    || _          | j        ||fd|i|| _        || _        dS )zCreate the cluster.

        Args:
            head_resources: resources of the head node, including CPU.
            worker_node_types: autoscaler node types config for worker nodes.
        r   N)_head_resources_generate_config_config_autoscaler_v2)selfr   r   r   config_kwargss        e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/cluster_utils.py__init__zAutoscalingCluster.__init__    sQ      .,t,
 
 (
 	
 
 ,    c           	         t          j        t          t          j                            t          j                            t          j                  d                              }t          j
        |          }||d<   |i dd|d         d<   |rd|d         d<   t          |d         d	<   |                    |           |S )
Nz0autoscaler/_private/fake_multi_node/example.yamlavailable_node_typesr   )	resourcesnode_configmax_workerszray.head.defaultTproviderlaunch_multiplehead_node_id)yaml	safe_loadopenospathjoindirnameray__file__copydeepcopyr   update)r   r   r   r   r   base_configcustom_configs          r   r   z#AutoscalingCluster._generate_config6   s     nGOOCL11F  
 
 k220A,-'E
 E
,-.@A  	J;?M*%&788IM*%n5]+++r   Noverride_envc                    t          j        g d           t          j                    \  }}t	          |d          5 }|                    t          j        | j                             ddd           n# 1 swxY w Y   ddd	                    |          dg}d| j
        v r@|                    d		                    | j
                            d                               d
| j
        v r@|                    d	                    | j
                            d
                               d| j
        v r@|                    d	                    | j
                            d                               | j
        r?|                    d	                    t          j        | j
                                       |<|                    d	                    t          j        |d                               t          j                                        }|                    ddd           | j        r#|                    dt$          t$          d           |r|                    |           t          j        ||           dS )z{Start the cluster.

        After this call returns, you can connect to the cluster with
        ray.init("auto").
        r)   stopz--forcewNr)   startz--autoscaling-config={}z--headCPUz--num-cpus={}GPUz--num-gpus={}object_store_memoryz--object-store-memory={}z--resources='{}'z--system-config={}),:)
separators1)AUTOSCALER_UPDATE_INTERVAL_SRAY_FAKE_CLUSTER)RAY_enable_autoscaler_v2RAY_CLOUD_INSTANCE_ID RAY_OVERRIDE_NODE_ID_FOR_TESTING)env)
subprocess
check_calltempfilemkstempr$   writejsondumpsr   formatr   appendpopr%   environr+   r-   r   r   )r   _system_configr0   _fake_configfcmdrB   s           r   r5   zAutoscalingCluster.startP   s    	888999!)++;+s## 	.qGGDJt|,,---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. %,,[99	
 D(((JJ--d.B.F.Fu.M.MNNOOOD(((JJ--d.B.F.Fu.M.MNNOOO D$888JJ*11(,,-BCC   
  	TJJ)00D<P1Q1QRRSSS%JJ$++J~*EEE   
 joo

CSQQRRR 	JJ03->8I     	%JJ|$$$cs++++++s   -A66A:=A:c                 2    t          j        g d           dS )zTerminate the cluster.r2   N)rC   rD   r   s    r   shutdownzAutoscalingCluster.shutdown   s     88899999r   )F)NN)__name__
__module____qualname____doc__dictboolr   r   r   r   r5   rU    r   r   r   r      s          $	, ,,  , 	, , , ,. @E   4/, /,x~ /, /, /, /,b: : : : :r   r   c            	           e Zd Z	 	 	 	 ddedededefdZed	             Zed
             ZddZ	ddefdZ
ddZddefdZddefdZd Zd Zd ZdS )ClusterFNTinitialize_headconnecthead_node_argsshutdown_at_exitc                 v   t           rt                              d           d| _        t	                      | _        d| _        d| _        t          j	        j
                                        | _        || _        |s|rt          d          |r)|pi } | j        di | |r|                                  dS dS dS )aO  Initializes all services of a Ray cluster.

        Args:
            initialize_head: Automatically start a Ray cluster
                by initializing the head node. Defaults to False.
            connect: If `initialize_head=True` and `connect=True`,
                ray.init will be called with the address of this cluster
                passed in.
            head_node_args: Arguments to be passed into
                `start_ray_head` via `self.add_node`.
            shutdown_at_exit: If True, registers an exit hook
                for shutting down all started processes.
        zRay cluster mode is currently experimental and untested on Windows. If you are using it and running into issues please file a report at https://github.com/ray-project/ray/issues.NFz(Cannot connect to uninitialized cluster.r\   )cluster_not_supportedloggerwarning	head_nodesetworker_nodesredis_address	connectedr)   _privatestateGlobalStateglobal_state_shutdown_at_exitRuntimeErroradd_noder`   )r   r_   r`   ra   rb   s        r   r   zCluster.__init__   s    ( ! 	NNN  
 EE!L.::<<!1 	K7 	KIJJJ 	+1rNDM++N+++ 		 	 r   c                 ,    | j         d S | j         j        S N)rg   gcs_addressrT   s    r   ru   zCluster.gcs_address   s    >!4~))r   c                     | j         S rt   ru   rT   s    r   addresszCluster.address   s    r   c                     | j         J | j        rJ t          j        |d| j         | j        | j                  }t                              |           d| _        dS )z"Connect the driver to the cluster.NT)	namespaceignore_reinit_errorrx   _redis_username_redis_password)rx   rk   r)   initredis_usernameredis_passwordre   info)r   rz   output_infos      r   r`   zCluster.connect   sj    |'''>!!!h $L / /
 
 
 	K   r   waitc                 x   dddddd}t          j        j        j        di |} |j        di | t                      5  | j        t           j        j                            |d| j	        | j	                  }|| _        | j        j
        | _
        |                    dt          j                  | _        |                    d	t          j                  | _        | j        j        | _        t%          j        |j        ddd
          }| j                            |           t           j        j                            | j        j                   n|                    | j
                   |                    | j                   |                    d
           |                    d           d|vrd|_        t           j        j                            |d
| j	        | j	                  }| j                            |           |r|                     |           ddd           n# 1 swxY w Y   |S )a  Adds a node to the local Ray Cluster.

        All nodes are by default started with the following settings:
            cleanup=True,
            num_cpus=1,
            object_store_memory=150 * 1024 * 1024  # 150 MiB

        Args:
            wait: Whether to wait until the node is alive.
            node_args: Keyword arguments used in `start_ray_head` and
                `start_ray_node`. Overrides defaults.

        Returns:
            Node object of the added Ray node.
           r   i  `	)num_cpusnum_gpusr8   min_worker_portmax_worker_portNT)headrb   spawn_reaperr   r   F)allow_cluster_id_nilfetch_cluster_id_if_nil)rj   rw   )include_log_monitor)node_manager_portdashboard_agent_listen_portr\   )r)   rl   	parameter	RayParamsupdate_if_absentr   rg   nodeNoderp   rj   getr   REDIS_DEFAULT_USERNAMEr   REDIS_DEFAULT_PASSWORDr   	webui_urlr   createru   ro   _initialize_global_stateutilswrite_ray_addressr   ri   add_wait_for_node)r   r   	node_argsdefault_kwargs
ray_paramsr   gcs_optionss          r   rr   zCluster.add_node   s   " #4  
 
 \+5BB	BB
#
#55n555 "" 7	* 7	*~%|(--%)%;!%!7	 .   "&%)^%A"&/mm$m&J' '# '0mm$m&J' '# "&!9.5$)-,1	   !::;GGG "44T^5OPPPP++$:L+MMM++8H+III+++FFF++a+@@@0	AA >BJ:|(--%)%;!%!7	 .   !%%d+++ * ##D)))o7	* 7	* 7	* 7	* 7	* 7	* 7	* 7	* 7	* 7	* 7	* 7	* 7	* 7	* 7	*r s   G$H//H36H3c                    t           j        j        j        j        }|2|j        |j        k    r"t          j                     t          d          |                                 | j	        |k    r%| j	        
                    d|d           d| _	        n2|
                    d|d           | j                            |           |                                r
J d            dS )zKills all processes associated with worker node.

        Args:
            node: Worker node of which all associated processes
                will be removed.
        NzRemoving a node that is connected to this Ray client is not allowed because it will break the driver. You can use the get_other_node utility to avoid removing a node that the Ray client is connected.FT)check_aliveallow_gracefulr   z3There are zombie processes left over after killing.)r)   rl   workerglobal_workerr   _raylet_socket_namerU   
ValueErrordestroy_external_storagerg   kill_all_processesri   removeany_processes_alive)r   r   r   global_nodes       r   remove_nodezCluster.remove_node  s    l)7<"';+JJJ ?   	%%'''>T!! N--!.t .    "DNN
 ##!.t $    $$T*** ((**	A 	A@	A 	A*	A 	Ar      timeoutc                 f    t           j        j                            |j        |j        |           dS )a  Wait until this node has appeared in the client table.

        Args:
            node (ray._private.node.Node): The node to wait for.
            timeout: The amount of time in seconds to wait before raising an
                exception.

        Raises:
            TimeoutError: An exception is raised if the timeout expires before
                the node appears in the client table.
        N)r)   rl   serviceswait_for_noderu   plasma_store_socket_name)r   r   r   s      r   r   zCluster._wait_for_nodeE  s:     	++)	
 	
 	
 	
 	
r   c                     t          j                     }t          j                     |z
  |k     r| j                                        }t          |                                           }t          |          |k    rt
                              d           dS t
                              t          |           d|            t          j        d           t          j                     |z
  |k     t          d          )a  Waits for correct number of nodes to be registered.

        This will wait until the number of live nodes in the client table
        exactly matches the number of "add_node" calls minus the number of
        "remove_node" calls that have been made on this cluster. This means
        that if a node dies without "remove_node" having been called, this will
        raise an exception.

        Args:
            timeout: The number of seconds to wait for nodes to join
                before failing.

        Raises:
            TimeoutError: An exception is raised if we time out while waiting
                for nodes to join.
        z!All nodes registered as expected.Nz6 nodes are currently registered, but we are expecting g?z*Timed out while waiting for nodes to join.)	timero   _live_node_idslenlist_all_nodesre   debugsleepTimeoutError)r   r   
start_timelive_clientsexpecteds        r   wait_for_nodeszCluster.wait_for_nodesW  s    " Y[[
ikkJ&00,;;==L4..0011H<  H,,@AAA<(( 7 7,47 7   
3 ikkJ&00 GHHHr   c                 R    t          | j                  }| j        r| j        g|z   }|S )zLists all nodes.

        TODO(rliaw): What is the desired behavior if a head node
        dies before worker nodes die?

        Returns:
            List of all nodes, including the head node.
        )listri   rg   )r   nodess     r   r   zCluster.list_all_nodesx  s2     T&''> 	-^$u,Er   c                 X    t          d |                                 D                       S )a  Returns a bool indicating whether all processes are alive or not.

        Note that this ignores processes that have been explicitly killed,
        e.g., via a command like node.kill_raylet().

        Returns:
            True if all processes are alive and false otherwise.
        c              3   >   K   | ]}|                                 V  d S rt   )remaining_processes_alive).0r   s     r   	<genexpr>z4Cluster.remaining_processes_alive.<locals>.<genexpr>  s.      VV41133VVVVVVr   )allr   rT   s    r   r   z!Cluster.remaining_processes_alive  s-     VV@S@S@U@UVVVVVVr   c                 0   t          | j                  }|D ]}|                     |           | j        |                     | j                   t          j        j                                         t          j        j	        
                                 dS )zRemoves all nodes.N)r   ri   r   rg   r)   experimentalinternal_kv_internal_kv_reset_commonr   reset_ray_address)r   	all_nodesr   s      r   rU   zCluster.shutdown  s    
 *++	 	# 	#DT"""">%T^,,,$77999++-----r   )FFNTrt   )T)r   )rV   rW   rX   r[   rZ   r   propertyru   rx   r`   rr   r   floatr   r   r   r   rU   r\   r   r   r^   r^      sc        !&#!%( (( ( 	(
 ( ( ( (T * * X*
     X    R RT R R R Rh%A %A %A %AN
 
E 
 
 
 
$I Ie I I I IB  	W 	W 	W. . . . .r   r^   )r+   rH   loggingr%   rC   rE   r   typingr   r   r"   r)   ray._private.servicesray._privater   ray._private.client_mode_hookr   ray._rayletr   5ray.autoscaler._private.fake_multi_node.node_providerr   ray.util.annotationsr	   	getLoggerrV   re   namerd   r   r^   r\   r   r   <module>r      sz      				       ! ! ! ! ! ! ! !  



     & & & & & & = = = = = = ( ( ( ( ( ( S S S S S S - - - - - -		8	$	$4  i: i: i: i: i: i: i: i:X X. X. X. X. X. X. X. X. X. X.r   