
    &`i                     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	 d dl
Z
d dlmZmZ d dl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mZmZ d dlmZ  ej         e!          Z" G d d          Z# e#            Z$	 d Z%d Z&eed                         Z'd Z(d Z)d Z*	 	 	 d"de	e+         de	e
j,                 de	e+         fdZ-eed#d                        Z.d#dZ/eed                         Z0eed                         Z1ed             Z2ed             Z3d Z4d  Z5d! Z6dS )$    N)defaultdict)Lock)DictOptional)HEAD_NODE_RESOURCE_NAMENODE_ID_PREFIX)binary_to_hexdecodehex_to_binary)client_mode_hook)message_to_dict)validate_actor_state_name)GlobalStateAccessor)autoscaler_pb2
common_pb2gcs_pb2)DeveloperAPIc                   <   e Zd ZdZd ZdefdZd Zd Z	 	 d9de	e
         d	e	ej                 d
e	e
         fdZd Zd Zd Zd Zd Zd Zd:dZd Zd Z ed ddddddddddddd           Zg d!Zd:d"Zd:d#Zd$ Zd% Zd& Zd' Zd( Z d) Z!d* Z"d+ Z#de$e
e$e
e%f         f         fd,Z&d- Z'd. Z(d/ Z)d0e
fd1Z*de$e
e%f         fd2Z+de,j-        fd3Z.e/d4e	e,j-                 d5e
de	e%         fd6            Z0de	e$e
e%f                  fd7Z1dej2        de	e
         fd8Z3dS );GlobalStatezA class used to interface with the Ray control state.

    Attributes:
        global_state_accessor: The client used to query gcs table from gcs
            server.
    c                 H    d| _         d| _        t                      | _        dS )zCreate a GlobalState object.N)gcs_options_global_state_accessorr   
_init_lockselfs    f/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/state.py__init__zGlobalState.__init__   s#      &*#&&    returnc                    | j         5  | j        | j        cddd           S | j        t          j                            d          t          | j                  | _        | j                                        }|s&d| _        t          j                            d          | j        cddd           S # 1 swxY w Y   dS )a9  
        This lazily initializes clients needed for state accessors and returns a connected global state accessor.

        Returns:
            GlobalStateAccessor: A connected global state accessor.
        Raises:
            RuntimeError: An exception is raised if ray.init() has not been called yet.
        NzXRay has not been started yet. Trying to use state API before ray.init() has been called.zrFailed to connect to GCS. Please check if the GCS server is running and if this node can connect to the head node.)r   r   r   ray
exceptionsRaySystemErrorr   connect)r   	connecteds     r   _connect_and_get_accessorz%GlobalState._connect_and_get_accessor&   s0    _ 	/ 	/*62	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 'n33n   +>d>N*O*OD'3;;==I .2+n33E   .#	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s   B6BB66B:=B:c                 p    | j         5  d| _        | j        d| _        ddd           dS # 1 swxY w Y   dS )z!Disconnect global state from GCS.N)r   r   r   r   s    r   
disconnectzGlobalState.disconnectB   s    _ 	3 	3#D*6.2+	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s   +//c                 T    | j         5  || _        ddd           dS # 1 swxY w Y   dS )aR  Set args for lazily initialization of the GlobalState object.

        It's possible that certain keys in gcs kv may not have been fully
        populated yet. In this case, we will retry this method until they have
        been populated or we exceed a timeout.

        Args:
            gcs_options: The client options for gcs
        N)r   r   )r   r   s     r   _initialize_global_statez$GlobalState._initialize_global_stateI   sx     _ 	+ 	+*D	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   !!Nactor_idjob_idactor_state_namec                 8   |                                  }|nt          j        t          |                    }|                    |          }|i S t
          j                            |          }|                     |          S t          |           |
                    ||          }i }t          t          |                    D ]Q}	t
          j                            ||	                   }|                     |          |t          |j                  <   R|S )a/  Fetch and parse the actor table information for a single actor ID.

        Args:
            actor_id: A hex string of the actor ID to fetch information about.
                If this is None, then the actor table is fetched.
                If this is not None, `job_id` and `actor_state_name`
                will not take effect.
            job_id: To filter actors by job_id, which is of type `ray.JobID`.
                You can use the `ray.get_runtime_context().job_id` function
                to get the current job ID
            actor_state_name: To filter actors based on actor state,
                which can be one of the following: "DEPENDENCIES_UNREADY",
                "PENDING_CREATION", "ALIVE", "RESTARTING", or "DEAD".
        Returns:
            Information from the actor table.
        )r&   r!   ActorIDr   get_actor_infor   ActorTableData
FromString_gen_actor_infor   get_actor_tablerangelenr	   r+   )
r   r+   r,   r-   accessor
actor_infoactor_table_dataactor_tableresultsis
             r   r:   zGlobalState.actor_tableY   s   , 1133{=#:#:;;H!00::J!	#*#9#D#DZ#P#P ++,<===%&6777"226;KLLKG3{++,, ; ;#*#9#D#D[QR^#T#T  (()9:: !"2";<<  Nr   c                    t          |j                  |j        |j        |j        t          |j                  |j        j        |j        j        t          |j        j	                  d|j
        j        |j
        j        t          |j
        j	                  dt          j        j        j        j        |j                 j        |j        |j        |j        |j        |j        |j        d}|S )z\Parse actor table data.

        Returns:
            Information from actor table.
        )	IPAddressPortNodeID)r/   ActorClassName
IsDetachedNameJobIDAddressOwnerAddressStateNumRestarts	Timestamp	StartTimeEndTime
DeathCausePid)r	   r+   
class_nameis_detachednamer,   address
ip_addressportnode_idowner_addressr   r1   
ActorState
DESCRIPTORvalues_by_numberstatenum_restarts	timestamp
start_timeend_timedeath_causepid)r   r9   r8   s      r   r3   zGlobalState._gen_actor_info   s     %%5%>??.9*6$)"#3#:;;-5@(05'(8(@(HII  .;F(6;'(8(F(NOO 
 +6AR &+8)3)4'0*6#'1
 

4 r   c                 R    |                                  }|                                S )zzFetch and parse the Gcs node info table.

        Returns:
            Information about the node in the cluster.
        )r&   get_node_tabler   r7   s     r   
node_tablezGlobalState.node_table   s'     1133&&(((r   c                    |                                  }|                    dd          }g }t          t          |                    D ]}t          j                            ||                   }i }|j                                        |d<   |j	        j
        |d<   |j        |d<   |j        |d<   |j        |d<   |j        |d<   |j        |d	<   |j        |d
<   |                    |           |S )a  Fetch and parse the gcs job table.

        Returns:
            Information about the Ray jobs in the cluster,
            namely a list of dicts with keys:
            - "JobID" (identifier for the job),
            - "DriverIPAddress" (IP address of the driver for this job),
            - "DriverPid" (process ID of the driver for this job),
            - "StartTime" (UNIX timestamp of the start time of this job),
            - "StopTime" (UNIX timestamp of the stop time of this job, if any)
        T)skip_submission_job_info_fieldskip_is_running_tasks_fieldrD   DriverIPAddress	DriverPidrI   rJ   rK   IsDead
Entrypoint)r&   get_job_tabler5   r6   r   JobTableDatar2   r,   hexdriver_addressrR   
driver_pidr[   r\   r]   is_dead
entrypointappend)r   r7   	job_tabler;   r<   entryjob_infos          r   rs   zGlobalState.job_table   s    1133**+/T + 
 
	 s9~~&& 	% 	%A(33IaLAAEH % 0 0 2 2HW*/*>*IH&'$)$4H[!$)OH[!$)$4H[!"'.HY!&HX%*%5H\"NN8$$$$r   c                     |                                  }t          j                            |                                          S )z\Get next job id from GCS.

        Returns:
            Next job id in the cluster.
        )r&   r!   rD   from_intget_next_job_idrb   s     r   next_job_idzGlobalState.next_job_id   s7     1133y!!(":":"<"<===r   c           
      N   |                                  }t          t                    }|                                }t	          t          |                    D ]}t          j                            ||                   }|j	        }|s1|j
        }t          |j                  }|j        }	|j        D ]c}	 t          j        |j                  }
n# t$          $ r i }
Y nw xY w|j        ||	||j        |j        |
d}||                             |           dt/          |          S )a  Retrieve and return task profiling events from GCS.

        Return:
            Profiling events by component id (e.g. worker id).
            {
                <component_id>: [
                    {
                        event_type: <event name> ,
                        component_id: <i.e. worker id>,
                        node_ip_address: <on which node profiling was done>,
                        component_type: <i.e. worker/driver>,
                        start_time: <unix timestamp in seconds>,
                        end_time: <unix timestamp in seconds>,
                        extra_data: <e.g. stack trace when error raised>,
                    }
                ]
            }
        )
event_typecomponent_idnode_ip_addresscomponent_typer\   r]   
extra_data)r&   r   listget_task_eventsr5   r6   r   
TaskEventsr2   profile_eventsr~   r	   r|   r}   eventsjsonloadsr   
ValueError
event_namer\   r]   rr   dict)r   r7   resulttask_eventsr<   eventprofiler~   r|   r}   r   profile_events               r   r   zGlobalState.profile_events   sI   & 1133T""..00s;''(( 	; 	;A&11+a.AAE*G $3N()=>>L%5O  ; ;$!%E,<!=!=JJ! $ $ $!#JJJ$ #("2$0'6&4"'"2 %",! ! |$++M::::;" F||s   6CCCc                     |                                  }|                    ||          }|d S t          j                            |          }|                     |          S N)r&   get_placement_group_by_namer   PlacementGroupTableDatar2   _gen_placement_group_info)r   placement_group_nameray_namespacer7   placement_group_infoplacement_group_table_datas         r   r   z'GlobalState.get_placement_group_by_name  sn    1133'CC - 
  
  '4)0)H)S)S$* *& 112LMMMr   c                    |                                  }|t          j        t          |                                                    }|                    |          }|i S t          j                            |          }| 	                    |          S |
                                }i }|D ]M}t          j                            |          }t          |j                  }| 	                    |          ||<   N|S r   )r&   r!   PlacementGroupIDr   rm   get_placement_group_infor   r   r2   r   get_placement_group_tabler	   placement_group_id)r   r   r7   r   placement_group_tabler;   r   s          r   r   z!GlobalState.placement_group_table  s   1133)!$!50446677" " $,#D#DEW#X#X #+	'.'F'Q'Q(( ($ 556JKKK$,$F$F$H$H!G(= 	 	$-4-L-W-W(. .* &3.A& &" /3.L.L./ /*++ Nr   c                 ~   ddl m d }fd}|j        }|J t          |j                  |j        d |j        D             d |j        D              ||j                   ||j                  |j	        dz  |j
        dz  |j        |j        t          j        j        j        j        |j                 j        dd	S )
Nr   )PlacementStrategyc                     | t           j        j        k    rdS | t           j        j        k    rdS | t           j        j        k    rdS | t           j        j        k    rdS dS )NPENDINGPREPAREDCREATEDRESCHEDULINGREMOVED)r   r   r   r   r   r   )rY   s    r   	get_statez8GlobalState._gen_placement_group_info.<locals>.get_stateB  s_    7??? y'9BBB!z'9AAA y'9FFF%~ yr   c                     | j         k    rdS | j        k    rdS | j        k    rdS | j        k    rdS t	          d           )NPACKSTRICT_PACKSTRICT_SPREADSPREADzInvalid strategy returned: )r   r   r   r   r   )strategyr   s    r   get_strategyz;GlobalState._gen_placement_group_info.<locals>.get_strategyN  sk    ,111v.:::$}.<<<&.555x !R?P!R!RSSSr   c                 N    i | ]"}|j         j        t          |          d          #S )unitResources)	bundle_idbundle_indexr   .0bundles     r   
<dictcomp>z9GlobalState._gen_placement_group_info.<locals>.<dictcomp>a  s?          -v/F/F/W  r   c                 L    i | ]!}|j         j        t          |j                  "S  )r   r   r	   rT   r   s     r   r   z9GlobalState._gen_placement_group_info.<locals>.<dictcomp>g  s:     # # #  -}V^/L/L# # #r   g     @@)end_to_end_creation_latency_msscheduling_latency_msscheduling_attempthighest_retry_delay_msscheduling_state)r   rP   bundlesbundles_to_node_idr   rY   stats)ray.core.generated.common_pb2r   r   r	   r   rP   r   r   rY   end_to_end_creation_latency_usscheduling_latency_usr   r   r   PlacementGroupStatsSchedulingStaterW   rX   r   )r   r   r   r   r   r   s        @r   r   z%GlobalState._gen_placement_group_info>  s1   CCCCCC
	! 
	! 
	!
	T 
	T 
	T 
	T 
	T %*#///"/$7# # )-  3:	  # #2:# # # %%9%BCCY39:: 86A*/*E*N&+&>*/*F$+$?$O$Z$k*%
 
#
 
 	
r   c                     |dz  }|S )z=A helper function for converting nanoseconds to microseconds.i  r   )r   time_in_nanosecondstime_in_microsecondss      r   _nanoseconds_to_microsecondsz(GlobalState._nanoseconds_to_microsecondsz  s    2T9##r   c                      dS )Ngeneric_workr   r   r   r   <lambda>zGlobalState.<lambda>  s     r   cq_build_abandonedrail_response	rail_loadrail_animation	rail_idledetailed_memory_dumpgoodterriblevsync_highlight_colorbackground_memory_dump)worker_idletaskztask:deserialize_argumentsztask:executeztask:store_outputswait_for_functionzray.getzray.putzray.waitsubmit_taskfetch_and_run_functionregister_remote_function)thread_state_uninterruptiblethread_state_iowaitthread_state_runningthread_state_runnablethread_state_sleepingthread_state_unknownr   light_memory_dumpr   r   r   r   badr   yellowoliver   r   r   r   startupheap_dump_stack_frameheap_dump_object_typeheap_dump_child_node_arrowcq_build_runningcq_build_passedcq_build_failedr   cq_build_attempt_runnigcq_build_attempt_passedcq_build_attempt_failedc                 F   |                                   ddl}|                    d           |                                 }g }|                                D ]\  }}|d         d         }|dvr|D ]}|d         |d         |d         |d         dz   |d	         z   |                     |d
                   |                     |d         |d
         z
            d| j        |d                  |d         d	}	d|d         v r|d         d         |	d<   d|d         v r|d         d         |	d<   |                    |	           Ќ|st          	                    d           |@t          |d          5 }
t          j        ||
           ddd           dS # 1 swxY w Y   dS |S )a  Return a list of profiling events that can viewed as a timeline.

        To view this information as a timeline, simply dump it as a json file
        by passing in "filename" or using using json.dump, and then load go to
        chrome://tracing in the Chrome web browser and load the dumped file.
        Make sure to enable "Flow events" in the "View Options" menu.

        Args:
            filename: If a filename is provided, the timeline is dumped to that
                file.

        Returns:
            If filename is not provided, this returns a list of profiling
                events. Each profile event is a dictionary.
        r   N   r~   )workerdriverr{   r}   :r|   r\   r]   Xr   	catrP   r_   tidtsdurphcnameargsr   rP   zNo profiling events found. Ray profiling must be enabled by setting RAY_PROFILING=1, and make sure RAY_task_events_report_interval_ms=0.w)r&   timesleepr   itemsr   _default_color_mappingrr   loggerwarningopenr   dump)r   filenamer   r   
all_eventscomponent_id_hexcomponent_eventsr~   r   	new_eventoutfiles              r   chrome_tracing_dumpzGlobalState.chrome_tracing_dump  sO   * 	&&(((
 	

1,,..
2@2F2F2H2H &	- &	-..-a01ABN%999)  -  - !.!,/ !!23 !12S85;PP;;E,<OPP<<j)E,,??  !8|9LM!,/+ 	4 eL111).|)<W)EIg&U<000(-l(;F(CIf%!!),,,,A -D  	NN8   h$$ /	*g.../ / / / / / / / / / / / / / / / / / s   1FFFc                    |                                   i }|                                 D ]-}d                    |d         |d                   ||d         <   .g }|                                                                 D ]\  }}|d         d         dk    r|D ]}|d         d	k    r|d
         \  }}	}
}
n;|d         dk    r|d
         \  }}	}
n"|d         dk    r|d
         \  }}	n
J d            t          |dd         d          }| j        |t          | j                  z           }|d         |d         ||         ||	         |                     |d                   |                     |d         |d         z
            d||d
         d	}|	                    |           |d         d	k    r0|
                                }d|d<   |	                    |           H|d         dk    r0|
                                }d|d<   |	                    |           |@t          |d          5 }t          j        ||           ddd           dS # 1 swxY w Y   dS |S )a  Return a list of transfer events that can viewed as a timeline.

        To view this information as a timeline, simply dump it as a json file
        by passing in "filename" or using json.dump, and then load go to
        chrome://tracing in the Chrome web browser and load the dumped file.
        Make sure to enable "Flow events" in the "View Options" menu.

        Args:
            filename: If a filename is provided, the timeline is dumped to that
                file.

        Returns:
            If filename is not provided, this returns a list of profiling
                events. Each profile event is a dictionary.
        z{}:{}NodeManagerAddressObjectManagerPortr@   r   r~   object_managerr{   transfer_sendr   transfer_receivereceive_pull_requestFzThis should be unreachable.N      r\   r]   r   r   blackr   greyr   )r&   rc   formatr   r  int_chrome_tracing_colorsr6   r   rr   copyr  r   r  )r   r  node_id_to_address	node_infor  keyr  r   
object_refremote_node_id_object_ref_intcolorr  additional_eventr  s                   r   #chrome_tracing_object_transfer_dumpz/GlobalState.chrome_tracing_object_transfer_dump  s-     	&&(((** 	 	I6=nn./;N1O7 7y233 
--//5577 =	 =	JCQx()-=== 8 8&/997<\7J4J11<(,>>>49,4G1J<(,BBB16|1D.J @"???5 "%Z^R!8!83"S)D%E%EE !.!,/ .c2-n=;;E,<OPP<<j)E,,??  "!,/+ 	. !!),,, &/99'0~~'7'7$07$W-%%&67777<(,>>>'0~~'7'7$06$W-%%&67777q8t h$$ /	*g.../ / / / / / / / / / / / / / / / / / s   6II Ic                 >   |                                  }|                                }i }t          t          |                    D ]}t          j                            ||                   }|j        r|j        t          j
        k    rt          |j        j                  }|j        }t          |d                   t          |d                   d||<   d|v rt          |d                   ||         d<   d|v rt          |d                   ||         d<   |S )z9Get a dictionary mapping worker ID to worker information.s   node_ip_addresss   plasma_store_socket)r}   plasma_store_sockets   stderr_filestderr_files   stdout_filestdout_file)r&   get_worker_tabler5   r6   r   WorkerTableDatar2   is_aliveworker_typer   WORKERr	   worker_address	worker_idworker_infor
   )r   r7   worker_tableworkers_datar<   worker_table_datar1  r2  s           r   workerszGlobalState.workersn  s8   1133  0022s<(()) 	 	A ' 7 B B<PQ? S S!*%1Z5FFF)*;*J*TUU	/; (.k:L.M'N'N+1+>T2U+V+V+ +Y' "[00=C#N3> >L+M: "[00=C#N3> >L+M: r   c                 8   |                                  }t          j                    }d|_        ||j        _        ||_        |                                D ]\  }}t          |d          |j	        |<   |
                    |                                          S )aV  Add a worker to the cluster.

        Args:
            worker_id: ID of this worker. Type is bytes.
            worker_type: Type of this worker. Value is common_pb2.DRIVER or
                common_pb2.WORKER.
            worker_info: Info of this worker. Type is dict{str: str}.

        Returns:
             Is operation success
        Tzutf-8)encoding)r&   r   r,  r-  r0  r1  r.  r  bytesr2  add_worker_infoSerializeToString)r   r1  r.  r2  r7   worker_datakvs           r   
add_workerzGlobalState.add_worker  s     1133-//#/8","-%%'' 	D 	DDAq).q7)C)C)CK#A&&''(E(E(G(GHHHr   c                     |                                  }|
J d            ||dk    s
J d            |                    ||          S )zUpdate the debugger port of a worker.

        Args:
            worker_id: ID of this worker. Type is bytes.
            debugger_port: Port of the debugger. Type is int.

        Returns:
             Is operation success
        Nworker_id is not validr   zdebugger_port is not valid)r&   update_worker_debugger_port)r   r1  debugger_portr7   s       r   rB  z'GlobalState.update_worker_debugger_port  sc     1133$$&>$$$%-!*;*;*;' +<*;; 33I}MMMr   c                 l    |                                  }|
J d            |                    |          S )zGet the debugger port of a worker.

        Args:
            worker_id: ID of this worker. Type is bytes.

        Returns:
             Debugger port of the worker.
        NrA  )r&   get_worker_debugger_port)r   r1  r7   s      r   rE  z$GlobalState.get_worker_debugger_port  s>     1133$$&>$$$00;;;r   c                     |                                  }|
J d            |
J d            |                    ||          S )a  Updates the number of paused threads of a worker.

        Args:
            worker_id: ID of this worker. Type is bytes.
            num_paused_threads_delta: The delta of the number of paused threads.

        Returns:
             Is operation success
        NrA  )r&    update_worker_num_paused_threads)r   r1  num_paused_threads_deltar7   s       r   rG  z,GlobalState.update_worker_num_paused_threads  s]     1133$$&>$$$'335M33388/
 
 	
r   c                    |                                   t          t                    }|                                                                 D ],}|                                D ]\  }}||xx         |z  cc<   -t          |          S )a'  Get the current total cluster resources.

        Note that this information can grow stale as nodes are added to or
        removed from the cluster.

        Returns:
            A dictionary mapping resource name to the total quantity of that
                resource in the cluster.
        )r&   r   r  total_resources_per_nodevaluesr  r   )r   total_resourcesnode_total_resourcesresource_idvalues        r   cluster_resourceszGlobalState.cluster_resources  s     	&&((( &c**$($A$A$C$C$J$J$L$L 	6 	6 &:&@&@&B&B 6 6"U,,,5,,,,6 O$$$r   c                 h    t          |                                                                           S )z=Returns a set of node IDs corresponding to nodes still alive.)setrJ  keysr   s    r   _live_node_idszGlobalState._live_node_ids  s(    400227799:::r   c                 J   |                                  }i }|                                }|D ]u}t          j                            |          }i }|j                                        D ]
\  }}|||<   t          j        j	        
                    |j                  }	|||	<   v|S )z<Returns a dictionary mapping node id to available resources.)r&   get_all_available_resourcesr   AvailableResourcesr2   resources_availabler  r!   _commonutilsr	   rT   )
r   r7   available_resources_by_idall_available_resourcesavailable_resourcemessagedynamic_resourcesrN  capacityrT   s
             r   available_resources_per_nodez(GlobalState.available_resources_per_node  s    1133$&!"*"F"F"H"H"9 	C 	C0;;<NOOG ")0)D)J)J)L)L : :%X19!+..k'55goFFG1B%g..((r   c                 J   |                                  }i }|                                }|D ]u}t          j                            |          }i }|j                                        D ]
\  }}|||<   t          j        j	        
                    |j                  }	|||	<   v|S r   )r&   get_all_total_resourcesr   TotalResourcesr2   resources_totalr  r!   rY  rZ  r	   rT   )
r   r7   total_resources_by_nodeall_total_resourcesrM  r^  node_resourcesrN  r`  rT   s
             r   rJ  z$GlobalState.total_resources_per_node  s    1133"$&>>@@$7 	> 	> ,778LMMGN)0)@)F)F)H)H 7 7%X.6{++k'55goFFG/=#G,,&&r   c                    |                                   |                                 }t          t                    }|                                D ],}|                                D ]\  }}||xx         |z  cc<   -t          |          S )a<  Get the current available cluster resources.

        This is different from `cluster_resources` in that this will return
        idle (available) resources rather than total resources.

        Note that this information can grow stale as tasks start and finish.

        Returns:
            A dictionary mapping resource name to the total quantity of that
                resource in the cluster. Note that if a resource (e.g., "CPU")
                is currently not available (i.e., quantity is 0), it will not
                be included in this dictionary.
        )r&   ra  r   r  rK  r  r   )r   r[  total_available_resourcesavailable_resourcesrN  num_availables         r   rk  zGlobalState.available_resources  s     	&&((($($E$E$G$G! %0$4$4!#<#C#C#E#E 	H 	H.A.G.G.I.I H H*])+666-G6666H -...r   c                 v    |                                  }t          j        |                                          S )z%Get the system config of the cluster.)r&   r   r   get_system_configrb   s     r   rn  zGlobalState.get_system_config-  s/    1133z(4466777r   c                 T    |                                  }|                    |          S )z)Get the node to connect for a Ray driver.)r&   get_node_to_connect_for_driver)r   r}   r7   s      r   rp  z*GlobalState.get_node_to_connect_for_driver2  s'    113366GGGr   rT   c                 T    |                                  }|                    |          S )z'Get the node information for a node id.)r&   get_node)r   rT   r7   s      r   rr  zGlobalState.get_node7  s'    1133  )))r   c                 R    |                                  }|                                S )zGet all the hex ids of nodes that are being drained
        and the corresponding draining deadline timestamps in ms.

        There is no deadline if the timestamp is 0.
        )r&   get_draining_nodesrb   s     r   rt  zGlobalState.get_draining_nodes<  s'     1133**,,,r   c                     |                                  }|                    t          j        j                                        t          j        j                                                  }|rt          j        	                    |          S dS )z.Get the cluster config of the current cluster.N)
r&   get_internal_kvr!   _rayletGCS_AUTOSCALER_STATE_NAMESPACEencode!GCS_AUTOSCALER_CLUSTER_CONFIG_KEYr   ClusterConfigr2   )r   r7   serialized_cluster_configs      r   get_cluster_configzGlobalState.get_cluster_configE  sy    1133$,$<$<K6==??K9@@BB%
 %
! % 	V!/::;TUUUtr   cluster_configr  c                 B   | dS d}| j         D ]T}|j                            |d          }|j        }|dk    s|dk    r2|dk    s|dk    rt          j        c S |||z  z  }U|dk    rdS | j                            |t          j                  }t          ||          S )zCalculate the maximum available resources for a given resource type from cluster config.
        If the resource type is not available, return None.
        Nr   )default)node_group_configs	resourcesget	max_countsysmaxsizemax_resourcesmin)r~  r  	max_valuenode_group_confignum_resources	num_nodesmax_value_limits          r   +_calculate_max_resource_from_cluster_configz7GlobalState._calculate_max_resource_from_cluster_configP  s     !4	!/!B 	3 	3-7;;C;KKM)3IA~~!!3!3B-2"5"5{"""]22II>>4(6::3:TT9o...r   c                 B   t                      }|                                 }|dS |j        r6|j        D ].}|                    |j                                                   /t          |          dk    rdS i }|D ]!}|                     ||          }||nd||<   "|S )a  Get the maximum available resources for all resource types from cluster config.

        Returns:
            A dictionary mapping resource name to the maximum quantity of that
            resource that could be available in the cluster based on the cluster config.
            Returns None if the config is not available.
            Values in the dictionary default to 0 if there is no such resource.
        Nr   )rR  r}  r  updater  rS  r6   r  )r   all_resource_keysconfigr  r   r  r  s          r   %get_max_resources_from_cluster_configz1GlobalState.get_max_resources_from_cluster_configh  s      EE((**>4$ 	M%+%> M M!!(():)D)I)I)K)KLLLL !!Q&&4$ 	D 	DCHHQTUUI'0'<))!F3KKr   c                 T    |                                  }|                    |          S )z"Get the actor info for a actor id.)r&   r0   )r   r+   r7   s      r   r0   zGlobalState.get_actor_info  s'    1133&&x000r   )NNr   )4__name__
__module____qualname____doc__r   r   r&   r(   r*   r   strr!   rD   r:   r3   rc   rs   ry   r   r   r   r   r   r   r  r  r  r&  r6  r?  rB  rE  rG  rP  rT  ra  r   r  rJ  rk  rn  rp  rr  rt  r   r{  r}  staticmethodr  r  r/   r0   r   r   r   r   r      s        ! ! !/+> / / / /83 3 3+ + +& '+*.	* *3-* #* #3-	* * * *X     D) ) )     D> > >2 2 2hN N N   >:
 :
 :
x$ $ $ )[/#*5,"-!7!/3&<(>	
 	
 &# # #JT T T Tl] ] ] ]~  <I I I,N N N&< < <
 
 
&% % %(; ; ;) ) )&'$sDcN/B*C ' ' ' '"/ / /48 8 8
H H H
* * * * *
-DcN - - - -	N$@ 	 	 	 	 / !=>/EH/	#/ / / \/.xS#X7O    81s{ 1x} 1 1 1 1 1 1r   r   c                  4    t                                           S )a  Get a list of the jobs in the cluster (for debugging only).

    Returns:
        Information from the job table, namely a list of dicts with keys:
        - "JobID" (identifier for the job),
        - "DriverIPAddress" (IP address of the driver for this job),
        - "DriverPid" (process ID of the driver for this job),
        - "StartTime" (UNIX timestamp of the start time of this job),
        - "StopTime" (UNIX timestamp of the stop time of this job, if any)
    )rY   rs   r   r   r   jobsr    s     ??r   c                  4    t                                           S )zjGet next job id from GCS.

    Returns:
        Next job id in integer representation in the cluster.
    )rY   ry   r   r   r   ry   ry     s     r   c                  4    t                                           S )zGet a list of the nodes in the cluster (for debugging only).

    Returns:
        Information about the Ray clients in the cluster.
    )rY   rc   r   r   r   nodesr    s     r   c                  4    t                                           S )zvGet a list of the workers in the cluster.

    Returns:
        Information about the Ray workers in the cluster.
    )rY   r6  r   r   r   r6  r6    s     ==??r   c                  N    t           t          j                                        z   S )a  Return the node id of the current node.

    For example, "node:172.10.5.34". This can be used as a custom resource,
    e.g., {node_id: 1} to reserve the whole node, or {node_id: 0.001} to
    just force placement on the node.

    Returns:
        Id of the current node.
    )r   r!   utilget_node_ip_addressr   r   r   current_node_idr    s     CH88::::r   c                     g } t                                                                           D ]S}|                                D ]<}|                    t
                    r |t          k    r|                     |           =T| S )a6  Get a list of the node ids in the cluster.

    For example, ["node:172.10.5.34", "node:172.42.3.77"]. These can be used
    as custom resources, e.g., {node_id: 1} to reserve the whole node, or
    {node_id: 0.001} to just force placement on the node.

    Returns:
        List of the node resource ids.
    )rY   rJ  rK  rS  
startswithr   r   rr   )node_idsrM  rN  s      r   r  r    s     H % > > @ @ G G I I - -/4466 	- 	-K&&~66-#:::,,,	- Or   r+   r,   r-   c                 <    t                               | ||          S )a  Fetch actor info for one or more actor IDs (for debugging only).

    Args:
        actor_id: A hex string of the actor ID to fetch information about. If
            this is None, then all actor information is fetched.
            If this is not None, `job_id` and `actor_state_name`
            will not take effect.
        job_id: To filter actors by job_id, which is of type `ray.JobID`.
            You can use the `ray.get_runtime_context().job_id` function
            to get the current job ID
        actor_state_name: To filter actors based on actor state,
            which can be one of the following: "DEPENDENCIES_UNREADY",
            "PENDING_CREATION", "ALIVE", "RESTARTING", or "DEAD".
    Returns:
        Information about the actors.
    r+   r,   r-   )rY   r:   r  s      r   actorsr    s)    * &;K    r   c                 8    t                               |           S )a  Return a list of profiling events that can viewed as a timeline.

    Ray profiling must be enabled by setting the RAY_PROFILING=1 environment
    variable prior to starting Ray, and set RAY_task_events_report_interval_ms=0

    To view this information as a timeline, simply dump it as a json file by
    passing in "filename" or using json.dump, and then load go to
    chrome://tracing in the Chrome web browser and load the dumped file.

    Args:
        filename: If a filename is provided, the timeline is dumped to that
            file.

    Returns:
        If filename is not provided, this returns a list of profiling events.
            Each profile event is a dictionary.
    r  )rY   r  r  s    r   timeliner    s    ( $$h$777r   c                 8    t                               |           S )aT  Return a list of transfer events that can viewed as a timeline.

    To view this information as a timeline, simply dump it as a json file by
    passing in "filename" or using json.dump, and then load go to
    chrome://tracing in the Chrome web browser and load the dumped file. Make
    sure to enable "Flow events" in the "View Options" menu.

    Args:
        filename: If a filename is provided, the timeline is dumped to that
            file.

    Returns:
        If filename is not provided, this returns a list of profiling events.
            Each profile event is a dictionary.
    r  )rY   r&  r  s    r   object_transfer_timeliner    s      44h4GGGr   c                  4    t                                           S )a  Get the current total cluster resources.

    Note that this information can grow stale as nodes are added to or removed
    from the cluster.

    Returns:
        A dictionary mapping resource name to the total quantity of that
            resource in the cluster.
    )rY   rP  r   r   r   rP  rP    s     ""$$$r   c                  4    t                                           S )a  Get the current available cluster resources.

    This is different from `cluster_resources` in that this will return idle
    (available) resources rather than total resources.

    Note that this information can grow stale as tasks start and finish.

    Returns:
        A dictionary mapping resource name to the total quantity of that
            resource in the cluster. Note that if a resource (e.g., "CPU")
            is currently not available (i.e., quantity is 0), it will not
            be included in this dictionary.
    )rY   rk  r   r   r   rk  rk  .  s      $$&&&r   c                  4    t                                           S )zGet the current available resources of each live node.

    Note that this information can grow stale as tasks start and finish.

    Returns:
        A dictionary mapping node hex id to available resources dictionary.
    )rY   ra  r   r   r   ra  ra  A  s     --///r   c                  4    t                                           S )zGet the current total resources of each live node.

    Note that this information can grow stale as tasks start and finish.

    Returns:
        A dictionary mapping node hex id to total resources dictionary.
    )rY   rJ  r   r   r   rJ  rJ  N  s     ))+++r   c                 8    t                               | |          S )zUpdate the debugger port of a worker.

    Args:
        worker_id: ID of this worker. Type is bytes.
        debugger_port: Port of the debugger. Type is int.

    Returns:
         Is operation success
    )rY   rB  )r1  rC  s     r   rB  rB  [  s     ,,YFFFr   c                 8    t                               | |          S )zUpdate the number of paused threads of a worker.

    Args:
        worker_id: ID of this worker. Type is bytes.
        num_paused_threads_delta: The delta of the number of paused threads.

    Returns:
         Is operation success
    )rY   rG  )r1  rH  s     r   rG  rG  h  s     11)=UVVVr   c                 6    t                               |           S )zGet the debugger port of a worker.

    Args:
        worker_id: ID of this worker. Type is bytes.

    Returns:
         Debugger port of the worker.
    )rY   rE  )r1  s    r   rE  rE  u  s     )))444r   )NNNr   )7r   loggingr  collectionsr   	threadingr   typingr   r   r!   ray._common.constantsr   r   ray._common.utilsr	   r
   r   ray._private.client_mode_hookr   ray._private.protobuf_compatr   ray._private.utilsr   ray._rayletr   ray.core.generatedr   r   r   ray.util.annotationsr   	getLoggerr  r  r   rY   r  ry   r  r6  r  r  r  rD   r  r  r  rP  rk  ra  rJ  rB  rG  rE  r   r   r   <module>r     s4     



 # # # # # #       ! ! ! ! ! ! ! ! 



 I I I I I I I I B B B B B B B B B B : : : : : : 8 8 8 8 8 8      , + + + + + B B B B B B B B B B - - - - - -		8	$	$p1 p1 p1 p1 p1 p1 p1 p1f 	 @          
; 
; 
;  , #"&&* smSY sm   4 8 8 8  8*H H H H& 
% 
%  
% ' '  '" 	0 	0 	0 	, 	, 	,
G 
G 
G
W 
W 
W	5 	5 	5 	5 	5r   