
    &`ix)                         d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ  e j        e          Ze G d d                      ZdS )	    N)
ModuleType)AnyDictListOptionalDockerCommandRunnerSSHCommandRunner)CommandRunnerInterface)DeveloperAPIc                      e Zd ZdZdeeef         deddfdZdefdZ	deeef         de
e         fd	Zd
edefdZd
edefdZd
edeeef         fdZd
edefdZd
edefdZd+dededefdZdeeef         deeef         dedeeeef                  fdZdeeef         deeef         dedeeef         deeef         deeeef                  fdZd
edeeef         ddfdZd
edeeeef                  fdZde
e         deeeef                  fdZedee         fd            Zed eeef         deeef         fd!            Z	 d,d"ed
ed#eeef         ded$eded%eeeef                  defd&Z d eeef         deeef         fd'Z!ed eeef         deeef         fd(            Z"defd)Z#d-d*Z$dS ).NodeProvidera  Interface for getting and returning nodes from a Cloud.

    **Important**: This is an INTERNAL API that is only exposed for the purpose
    of implementing custom node providers. It is not allowed to call into
    NodeProvider methods from any Ray package outside the autoscaler, only to
    define new implementations of NodeProvider for use with the "external" node
    provider option.

    NodeProviders are namespaced by the `cluster_name` parameter; they only
    operate on nodes within that namespace.

    Nodes may be in one of three states: {pending, running, terminated}. Nodes
    appear immediately once started by `create_node`, and transition
    immediately to terminated when `terminate_node` is called.

    Threading and concurrency:
    - The autoscaler calls the following methods from multiple threads
      (NodeLauncher, NodeUpdaterThread, autoscaler main loop, and
       NodeProviderAdapter executors).
    - These methods MUST be thread-safe:
      non_terminated_nodes, is_running, is_terminated, node_tags, internal_ip,
      external_ip, get_node_id, create_node/create_node_with_resources_and_labels,
      set_node_tags, terminate_node/terminate_nodes.

    TODO (rueian): make sure all the existing implementations are thread-safe.
    provider_configcluster_namereturnNc                 >    || _         || _        i | _        i | _        d S N)r   r   _internal_ip_cache_external_ip_cache)selfr   r   s      p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/node_provider.py__init__zNodeProvider.__init__)   s(    .(2424    c                     dS )zReturns whether this provider is readonly.

        Readonly node providers do not allow nodes to be created or terminated.
        F r   s    r   is_readonlyzNodeProvider.is_readonly/   s	    
 ur   tag_filtersc                     t           )a  Return a list of node ids filtered by the specified tags dict.

        This list must not include terminated nodes. For performance reasons,
        providers are allowed to cache the result of a call to
        non_terminated_nodes() to serve single-node queries
        (e.g. is_running(node_id)). This means that non_terminate_nodes() must
        be called again to refresh results.

        Examples:
            >>> from ray.autoscaler.node_provider import NodeProvider
            >>> from ray.autoscaler.tags import TAG_RAY_NODE_KIND
            >>> provider = NodeProvider(...) # doctest: +SKIP
            >>> provider.non_terminated_nodes( # doctest: +SKIP
            ...     {TAG_RAY_NODE_KIND: "worker"})
            ["node-1", "node-2"]

        NotImplementedError)r   r   s     r   non_terminated_nodesz!NodeProvider.non_terminated_nodes6   s
    $ "!r   node_idc                     t           )z-Return whether the specified node is running.r    r   r#   s     r   
is_runningzNodeProvider.is_runningJ       !!r   c                     t           )z0Return whether the specified node is terminated.r    r%   s     r   is_terminatedzNodeProvider.is_terminatedN   r'   r   c                     t           )z1Returns the tags of the given node (string dict).r    r%   s     r   	node_tagszNodeProvider.node_tagsR   r'   r   c                     t           )z*Returns the external ip of the given node.r    r%   s     r   external_ipzNodeProvider.external_ipV   r'   r   c                     t           )z3Returns the internal ip (Ray ip) of the given node.r    r%   s     r   internal_ipzNodeProvider.internal_ipZ   r'   r   F
ip_addressuse_internal_ipc                 p     fd} |            sH                      i           }r j        n j        }r j        n j        }|D ]}|| ||          <    |            sGrdt           j                   }ndt           j                   }t          d d|z              |            S )a*  Returns the node_id given an IP address.

        Assumes ip-address is unique per node.

        Args:
            ip_address: Address of node.
            use_internal_ip: Whether the ip address is
                public or private.

        Raises:
            ValueError: If not found.
        c                  p    rj                                        S j                                       S r   )r   getr   )r0   r   r1   s   r   find_node_idz.NodeProvider.get_node_id.<locals>.find_node_idl   s9     ?.22:>>>.22:>>>r   zWorker internal IPs: zWorker external IP: zip z not found. )r"   r/   r-   r   r   list
ValueError)	r   r0   r1   r5   	all_nodesip_funcip_cacher#   	known_msgs	   ```      r   get_node_idzNodeProvider.get_node_id^   s   	? 	? 	? 	? 	? 	? 	? |~~ 	511"55I*9Od&&t?OG+:W''@W  % 5 5-4))**|~~ 	I SSD9P4Q4QSS		R48O3P3PRR	;:;;;iGHHH|~~r   node_configtagscountc                     t           )aP  Creates a number of nodes within the namespace.

        Optionally returns a mapping from created node ids to node metadata.

        Optionally may throw a
        ray.autoscaler.node_launch_exception.NodeLaunchException which the
        autoscaler may use to provide additional functionality such as
        observability.

        r    )r   r=   r>   r?   s       r   create_nodezNodeProvider.create_node   s
     "!r   	resourceslabelsc                 0    |                      |||          S )aS  Create nodes with a given resource and label config.

        This is the method actually called by the autoscaler. Prefer to
        implement this when possible directly, otherwise it delegates to the
        create_node() implementation.

        Optionally may throw a ray.autoscaler.node_launch_exception.NodeLaunchException.
        )rA   )r   r=   r>   r?   rB   rC   s         r   %create_node_with_resources_and_labelsz2NodeProvider.create_node_with_resources_and_labels   s      T5999r   c                     t           )z9Sets the tag values (string dict) for the specified node.r    )r   r#   r>   s      r   set_node_tagszNodeProvider.set_node_tags   r'   r   c                     t           )z|Terminates the specified node.

        Optionally return a mapping from deleted node ids to node
        metadata.
        r    r%   s     r   terminate_nodezNodeProvider.terminate_node   s
     "!r   node_idsc                     |D ]D}t                               d                    |                     |                     |           EdS )zTerminates a set of nodes.

        May be overridden with a batch method, which optionally may return a
        mapping from deleted node ids to node metadata.
        z"NodeProvider: {}: Terminating nodeN)loggerinfoformatrI   )r   rJ   r#   s      r   terminate_nodeszNodeProvider.terminate_nodes   sP       	) 	)GKK<CCGLLMMM((((tr   c                     dS )a>  The maximum number of nodes which can be terminated in one single
        API request. By default, this is "None", which means that the node
        provider's underlying API allows infinite requests to be terminated
        with one request.

        For example, AWS only allows 1000 nodes to be terminated
        at once; to terminate more, we must issue multiple separate API
        requests. If the limit is infinity, then simply set this to None.

        This may be overridden. The value may be useful when overriding the
        "terminate_nodes" method.
        Nr   r   s    r   max_terminate_nodesz NodeProvider.max_terminate_nodes   s	     tr   cluster_configc                     | S )z?Bootstraps the cluster config by adding env defaults if needed.r   rR   s    r   bootstrap_configzNodeProvider.bootstrap_config   s
     r   
log_prefixauth_configprocess_runnerdocker_configc                 d    ||| ||||d}|r|d         dk    rt          |fi |S t          di |S )a  Returns the CommandRunner class used to perform SSH commands.

        Args:
            log_prefix: stores "NodeUpdater: {}: ".format(<node_id>). Used
                to print progress in the CommandRunner.
            node_id: the node ID.
            auth_config: the authentication configs from the autoscaler
                yaml file.
            cluster_name: the name of the cluster.
            process_runner: the module to use to run the commands
                in the CommandRunner. E.g., subprocess.
            use_internal_ip: whether the node_id belongs to an internal ip
                or external ip.
            docker_config: If set, the docker information of the docker
                container that commands should be run on.
        )rV   r#   providerrW   r   rX   r1   container_name Nr   r   )	r   rV   r#   rW   r   rX   r1   rY   common_argss	            r   get_command_runnerzNodeProvider.get_command_runner   si    6 %&(,.
 
  	3]+;<BB&}DDDDD#22k222r   c                     |S )z?Returns a new cluster config with custom configs for head node.r   )r   rR   s     r   prepare_for_head_nodez"NodeProvider.prepare_for_head_node   s    r   c                     | S )z=Fills out missing "resources" field for available_node_types.r   rT   s    r   &fillout_available_node_types_resourcesz3NodeProvider.fillout_available_node_types_resources   s
    
 r   c                     dS )aQ  Optional condition to determine if it's safe to proceed with an autoscaling
        update. Can be used to wait for convergence of state managed by an external
        cluster manager.

        Called by the autoscaler immediately after non_terminated_nodes().
        If False is returned, the autoscaler will abort the update.
        Tr   r   s    r   safe_to_scalezNodeProvider.safe_to_scale  s	     tr   c                     dS )z]This optional method is executed at the end of
        StandardAutoscaler._update().
        Nr   r   s    r   post_processzNodeProvider.post_process  s	     	r   )Fr   )r   N)%__name__
__module____qualname____doc__r   strr   r   boolr   r   r"   r&   r)   r+   r-   r/   r<   intr   rA   floatrE   rG   rI   rO   propertyrQ   staticmethodrU   r   r   r_   ra   rc   re   rg   r   r   r   r   r      sf        65S#X 5c 5d 5 5 5 5T    "S#X "49 " " " "("# "$ " " " ""S "T " " " "" "c3h " " " ""3 "3 " " " ""3 "3 " " " "$ $c $D $S $ $ $ $L"S>"15c3h"HK"	$sCx.	!" " " ":#s(^: 38n: 	:
 U
#: S#X: 
$sCx.	!: : : :$"S "S#X "4 " " " ""c "htCH~.F " " " "	S	 	htCH~6N 	 	 	 	 Xc]    X c3h DcN    \ 37&3 &3&3 &3 #s(^	&3
 &3 #&3 &3  S#X/&3 
 &3 &3 &3 &3PDcN tCQTH~     S#X	c3h   \t         r   r   )loggingtypesr   typingr   r   r   r   &ray.autoscaler._private.command_runnerr	   r
   ray.autoscaler.command_runnerr   ray.util.annotationsr   	getLoggerrh   rL   r   r   r   r   <module>ry      s           , , , , , , , , , , , , X X X X X X X X @ @ @ @ @ @ - - - - - -		8	$	$ E E E E E E E E E Er   