U
    /eV                     @  sj  d dl m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Zd dl	Z	d dl
Z
d dlZd dlmZ d dlmZmZmZ d dlmZmZ erd dlmZmZ eeZG dd dZG d	d
 d
ZG dd dZdddddZG dd deejZ G dd de Z!G dd de Z"G dd deZ#G dd deZ$G dd deZ%G dd dZ&G d d! d!eZ'dS )"    )annotationsN)	Awaitable)TYPE_CHECKINGAnyClassVar)funcnametmpfile)	SchedulerTaskStateStatec                   @  s   e Zd ZdZdddddZdddd	Zddd
dZddddddddZdddddZdddddddddZ	ddddddZ
ddddddZddddd d!Zddddd"d#Zdddd$d%d&Zd'S )(SchedulerPlugina  Interface to extend the Scheduler

    The scheduler operates by triggering and responding to events like
    ``task_finished``, ``update_graph``, ``task_erred``, etc..

    A plugin enables custom code to run at each of those same events.  The
    scheduler will run the analogous methods on this class when each event is
    triggered.  This runs user code within the scheduler thread that can
    perform arbitrary operations in synchrony with the scheduler itself.

    Plugins are often used for diagnostics and measurement, but have full
    access to the scheduler and could in principle affect core scheduling.

    To implement a plugin:

    1. subclass this class
    2. override some of its methods
    3. add the plugin to the scheduler with ``Scheduler.add_plugin(myplugin)``.

    Examples
    --------
    >>> class Counter(SchedulerPlugin):
    ...     def __init__(self):
    ...         self.counter = 0
    ...
    ...     def transition(self, key, start, finish, *args, **kwargs):
    ...         if start == 'processing' and finish == 'memory':
    ...             self.counter += 1
    ...
    ...     def restart(self, scheduler):
    ...         self.counter = 0

    >>> plugin = Counter()
    >>> scheduler.add_plugin(plugin)  # doctest: +SKIP
    r	   None)	schedulerreturnc                   s   dS )zhRun when the scheduler starts up

        This runs at the end of the Scheduler startup process
        N selfr   r   r   B/tmp/pip-unpacked-wheel-g426oqom/distributed/diagnostics/plugin.pystart=   s    zSchedulerPlugin.startr   c                   s   dS )z*Runs prior to any Scheduler shutdown logicNr   r   r   r   r   before_closeC   s    zSchedulerPlugin.before_closec                   s   dS )zRun when the scheduler closes down

        This runs at the beginning of the Scheduler shutdown process, but after
        workers have been asked to shut down gracefully
        Nr   r   r   r   r   closeF   s    zSchedulerPlugin.closezset[str]zdict[str, float]r   )r   keysrestrictionskwargsr   c                 K  s   dS )z0Run when a new graph / tasks enter the schedulerNr   )r   r   r   r   r   r   r   r   update_graphM   s    zSchedulerPlugin.update_graphc                 C  s   dS )z&Run when the scheduler restarts itselfNr   r   r   r   r   restartV   s    zSchedulerPlugin.restartstrr
   )keyr   finishargsr   r   c                 O  s   dS )a  Run whenever a task changes state

        Parameters
        ----------
        key : string
        start : string
            Start state of the transition.
            One of released, waiting, processing, memory, error.
        finish : string
            Final state of the transition.
        *args, **kwargs :
            More options passed when transitioning
            This may include worker ID, compute time, etc.
        Nr   )r   r   r   r   r    r   r   r   r   
transitionY   s    zSchedulerPlugin.transitionzNone | Awaitable[None])r   workerr   c                 C  s   dS )z(Run when a new worker enters the clusterNr   r   r   r"   r   r   r   
add_workerp   s    zSchedulerPlugin.add_workerc                 C  s   dS )z$Run when a worker leaves the clusterNr   r#   r   r   r   remove_workers   s    zSchedulerPlugin.remove_worker)r   clientr   c                 C  s   dS )zRun when a new client connectsNr   r   r   r&   r   r   r   
add_clientx   s    zSchedulerPlugin.add_clientc                 C  s   dS )zRun when a client disconnectsNr   r'   r   r   r   remove_client{   s    zSchedulerPlugin.remove_client)topicmsgr   c                 C  s   dS )zRun when an event is loggedNr   )r   r*   r+   r   r   r   	log_event~   s    zSchedulerPlugin.log_eventN)__name__
__module____qualname____doc__r   r   r   r   r   r!   r$   r%   r(   r)   r,   r   r   r   r   r      s   $	r   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	WorkerPlugina  Interface to extend the Worker

    A worker plugin enables custom code to run at different stages of the Workers'
    lifecycle: at setup, during task state transitions, when a task or dependency
    is released, and at teardown.

    A plugin enables custom code to run at each of step of a Workers's life. Whenever such
    an event happens, the corresponding method on this class will be called. Note that the
    user code always runs within the Worker's main thread.

    To implement a plugin implement some of the methods of this class and register
    the plugin to your client in order to have it attached to every existing and
    future workers with ``Client.register_worker_plugin``.

    Examples
    --------
    >>> class ErrorLogger(WorkerPlugin):
    ...     def __init__(self, logger):
    ...         self.logger = logger
    ...
    ...     def setup(self, worker):
    ...         self.worker = worker
    ...
    ...     def transition(self, key, start, finish, *args, **kwargs):
    ...         if finish == 'error':
    ...             ts = self.worker.tasks[key]
    ...             exc_info = (type(ts.exception), ts.exception, ts.traceback)
    ...             self.logger.error(
    ...                 "Error during computation of '%s'.", key,
    ...                 exc_info=exc_info
    ...             )

    >>> import logging
    >>> plugin = ErrorLogger(logging)
    >>> client.register_worker_plugin(plugin)  # doctest: +SKIP
    c                 C  s   dS )z
        Run when the plugin is attached to a worker. This happens when the plugin is registered
        and attached to existing workers, or when a worker is created after the plugin has been
        registered.
        Nr   r   r"   r   r   r   setup   s    zWorkerPlugin.setupc                 C  s   dS )z@Run when the worker to which the plugin is attached to is closedNr   r2   r   r   r   teardown   s    zWorkerPlugin.teardownc                 K  s   dS )a  
        Throughout the lifecycle of a task (see :doc:`Worker <worker>`), Workers are
        instructed by the scheduler to compute certain tasks, resulting in transitions
        in the state of each task. The Worker owning the task is then notified of this
        state transition.

        Whenever a task changes its state, this method will be called.

        Parameters
        ----------
        key : string
        start : string
            Start state of the transition.
            One of waiting, ready, executing, long-running, memory, error.
        finish : string
            Final state of the transition.
        kwargs : More options passed when transitioning
        Nr   )r   r   r   r   r   r   r   r   r!      s    zWorkerPlugin.transitionN)r-   r.   r/   r0   r3   r4   r!   r   r   r   r   r1      s   %r1   c                   @  s$   e Zd ZdZdZdd Zdd ZdS )NannyPlugina  Interface to extend the Nanny

    A worker plugin enables custom code to run at different stages of the Workers'
    lifecycle. A nanny plugin does the same thing, but benefits from being able
    to run code before the worker is started, or to restart the worker if
    necessary.

    To implement a plugin implement some of the methods of this class and register
    the plugin to your client in order to have it attached to every existing and
    future nanny by passing ``nanny=True`` to
    :meth:`Client.register_worker_plugin<distributed.Client.register_worker_plugin>`.

    The ``restart`` attribute is used to control whether or not a running ``Worker``
    needs to be restarted when registering the plugin.

    See Also
    --------
    WorkerPlugin
    SchedulerPlugin
    Fc                 C  s   dS )z
        Run when the plugin is attached to a nanny. This happens when the plugin is registered
        and attached to existing nannies, or when a nanny is created after the plugin has been
        registered.
        Nr   r   nannyr   r   r   r3      s    zNannyPlugin.setupc                 C  s   dS )z?Run when the nanny to which the plugin is attached to is closedNr   r6   r   r   r   r4      s    zNannyPlugin.teardownN)r-   r.   r/   r0   r   r3   r4   r   r   r   r   r5      s   r5   z,SchedulerPlugin | WorkerPlugin | NannyPluginr   )pluginr   c                 C  s0   t | dr| jS tt| d tt  S dS )zUReturn plugin name.

    If plugin has no name attribute a random name is used.

    name-N)hasattrr9   r   typer   uuiduuid4)r8   r   r   r   _get_plugin_name   s    
r?   c                   @  s   e Zd ZU dZded< ded< ded< ded	< ddd
ddZdd ZejddddZ	dd Z
dd Zdd Zdd Zdd Zdd ZdS ) PackageInstalla1  Abstract parent class for a worker plugin to install a set of packages

    This accepts a set of packages to install on all workers.
    You can also optionally ask for the worker to restart itself after
    performing this installation.

    .. note::

       This will increase the time it takes to start up
       each worker. If possible, we recommend including the
       libraries in the worker environment or image. This is
       primarily intended for experimentation and debugging.

    Parameters
    ----------
    packages
        A list of packages (with optional versions) to install
    restart
        Whether or not to restart the worker after installing the packages
        Only functions if the worker has an attached nanny process

    See Also
    --------
    CondaInstall
    PipInstall
    zClassVar[str]	INSTALLERr   r9   	list[str]packagesboolr   )rC   r   c                 C  s&   || _ || _| j dt  | _d S )Nz	-install-)rC   r   rA   r=   r>   r9   )r   rC   r   r   r   r   __init__  s    zPackageInstall.__init__c              
     s   ddl m} |dt ddI d H 4 I d H  | |I d H sjtd| j| j | 	|I d H  | 
  ntd| j | jr|jr| |I d H std | |I d H  |jj|jd| j d	d
 W 5 Q I d H R X d S )Nr   )	Semaphore   T)Z
max_leasesr9   registerz(%s installing the following packages: %sz6The following packages have already been installed: %sz)Restarting worker to refresh interpreter.z-setup)r   reason)Zdistributed.semaphorerF   socketgethostname_is_installedloggerinforA   rC   _set_installedinstallr   r7   _is_restarted_set_restartedZloopZadd_callbackZclose_gracefullyr9   )r   r"   rF   r   r   r   r3   !  s.    

  
zPackageInstall.setupr   r   c                 C  s   dS )zInstall the requested packagesNr   r   r   r   r   rP   <  s    zPackageInstall.installc                   s   |j j|  ddI d H S NF)default)r&   get_metadata_compose_installed_keyr2   r   r   r   rL   @  s     zPackageInstall._is_installedc                   s   |j |  dI d H  d S NT)r&   set_metadatarV   r2   r   r   r   rO   E  s    zPackageInstall._set_installedc                 C  s   | j dt gS )N	installed)r9   rJ   rK   r   r   r   r   rV   K  s    z%PackageInstall._compose_installed_keyc                   s   |j j| |ddI d H S rS   )r&   rU   _compose_restarted_keyr2   r   r   r   rQ   R  s    zPackageInstall._is_restartedc                   s   |j | |dI d H  d S rW   )r&   rX   rZ   r2   r   r   r   rR   X  s    zPackageInstall._set_restartedc                 C  s   | j d|jgS )NZ	restarted)r9   r7   r2   r   r   r   rZ   ^  s    z%PackageInstall._compose_restarted_keyN)r-   r.   r/   r0   __annotations__rE   r3   abcabstractmethodrP   rL   rO   rV   rQ   rR   rZ   r   r   r   r   r@      s   
	r@   c                      sH   e Zd ZU dZdZded< ddddd	 fd
dZddddZ  ZS )CondaInstallam  A Worker Plugin to conda install a set of packages

    This accepts a set of packages to install on all workers as well as
    options to use when installing.
    You can also optionally ask for the worker to restart itself after
    performing this installation.

    .. note::

       This will increase the time it takes to start up
       each worker. If possible, we recommend including the
       libraries in the worker environment or image. This is
       primarily intended for experimentation and debugging.

    Parameters
    ----------
    packages
        A list of packages (with optional versions) to install using conda
    conda_options
        Additional options to pass to conda
    restart
        Whether or not to restart the worker after installing the packages
        Only functions if the worker has an attached nanny process

    Examples
    --------
    >>> from dask.distributed import CondaInstall
    >>> plugin = CondaInstall(packages=["scikit-learn"], conda_options=["--update-deps"])

    >>> client.register_worker_plugin(plugin)

    See Also
    --------
    PackageInstall
    PipInstall
    ZcondarB   conda_optionsNFlist[str] | NonerD   )rC   r_   r   c                   s   t  j||d |pg | _d S N)r   )superrE   r_   )r   rC   r_   r   	__class__r   r   rE     s    zCondaInstall.__init__r   r   c              
   C  s   zddl m}m} W n: tk
rN } zd}t| t||W 5 d }~X Y nX z||j| j| j	 \}}}W n: t
k
r } zd}t| t||W 5 d }~X Y nX |dkrd|   d}t| t|d S )Nr   )Commandsrun_commandz`conda install failed because conda could not be found. Please make sure that conda is installed.zconda install failedzconda install failed with '')Zconda.cli.python_apire   rf   ModuleNotFoundErrorrM   errorRuntimeErrorINSTALLr_   rC   	Exceptiondecodestrip)r   re   rf   er+   _stderr
returncoder   r   r   rP     s(    
 


zCondaInstall.install)NF	r-   r.   r/   r0   rA   r[   rE   rP   __classcell__r   r   rc   r   r^   b  s   
%  	r^   c                      sH   e Zd ZU dZdZded< ddddd	 fd
dZddddZ  ZS )
PipInstalla]  A Worker Plugin to pip install a set of packages

    This accepts a set of packages to install on all workers as well as
    options to use when installing.
    You can also optionally ask for the worker to restart itself after
    performing this installation.

    .. note::

       This will increase the time it takes to start up
       each worker. If possible, we recommend including the
       libraries in the worker environment or image. This is
       primarily intended for experimentation and debugging.

    Parameters
    ----------
    packages
        A list of packages (with optional versions) to install using pip
    pip_options
        Additional options to pass to pip
    restart
        Whether or not to restart the worker after installing the packages
        Only functions if the worker has an attached nanny process

    Examples
    --------
    >>> from dask.distributed import PipInstall
    >>> plugin = PipInstall(packages=["scikit-learn"], pip_options=["--upgrade"])

    >>> client.register_worker_plugin(plugin)

    See Also
    --------
    PackageInstall
    CondaInstall
    piprB   pip_optionsNFr`   rD   )rC   rw   r   c                   s   t  j||d |pg | _d S ra   )rb   rE   rw   )r   rC   rw   r   rc   r   r   rE     s    zPipInstall.__init__r   r   c                 C  sp   t jtjdddg| j | j t jt jd}| \}}| }|dkrld|	 
  d}t| t|d S )Nz-mrv   rP   )stdoutrq   r   zpip install failed with 'rg   )
subprocessPopensys
executablerw   rC   PIPEcommunicatewaitrm   rn   rM   ri   rj   )r   procrp   rq   rr   r+   r   r   r   rP     s    
zPipInstall.install)NFrs   r   r   rc   r   ru     s   
%  	ru   c                   @  s$   e Zd ZdZdZdd Zdd ZdS )
UploadFileaQ  A WorkerPlugin to upload a local file to workers.

    Parameters
    ----------
    filepath: str
        A path to the file (.py, egg, or zip) to upload

    Examples
    --------
    >>> from distributed.diagnostics.plugin import UploadFile

    >>> client.register_worker_plugin(UploadFile("/path/to/file.py"))  # doctest: +SKIP
    upload_filec              	   C  s2   t j|| _t|d}| | _W 5 Q R X dS )S
        Initialize the plugin by reading in the data from the given file.
        rbN)ospathbasenamefilenameopenreaddata)r   filepathfr   r   r   rE     s    zUploadFile.__init__c                   s4   |j | j| jddI d H }t| j|d ks0td S )NT)r   r   loadnbytes)r   r   r   lenAssertionError)r   r"   responser   r   r   r3   	  s      zUploadFile.setupN)r-   r.   r/   r0   r9   rE   r3   r   r   r   r   r     s   r   c                   @  s(   e Zd ZdZd	ddddZdd ZdS )
EnvironTNzdict | None)environc                 C  s    |pi }dd |  D | _d S )Nc                 S  s   i | ]\}}|t |qS r   )r   ).0kvr   r   r   
<dictcomp>  s      z$Environ.__init__.<locals>.<dictcomp>)itemsr   )r   r   r   r   r   rE     s    zEnviron.__init__c                   s   |j | j d S N)envupdater   r6   r   r   r   r3     s    zEnviron.setup)N)r-   r.   r/   r   rE   r3   r   r   r   r   r     s   r   c                   @  s0   e Zd ZdZddddd ffddZdd	 Zd
S )UploadDirectoryaU  A NannyPlugin to upload a local file to workers.

    Parameters
    ----------
    path: str
        A path to the directory to upload

    Examples
    --------
    >>> from distributed.diagnostics.plugin import UploadDirectory
    >>> client.register_worker_plugin(UploadDirectory("/path/to/directory"), nanny=True)  # doctest: +SKIP
    F)z.gitz.githubz.pytest_cachetestsZdocsc                 C  s   t j| d dkS )NrG   z.pyc)r   r   splitext)fnr   r   r   <lambda>/      zUploadDirectory.<lambda>c                   s.  t j|}t j|d | _|| _|| _dt j|d  | _tdd}t	|dtj
}t |D ]\} }	|	D ]|}
t j||
tfdd|D rqvt j t fdd|D rqvt jt j||
t j|d	}|| qvqhW 5 Q R X t|d
}| | _W 5 Q R X W 5 Q R X dS )r   zupload-directory-zip)	extensionwc                 3  s   | ]}| V  qd S r   r   )r   	predicate)r   r   r   	<genexpr>@  s     z+UploadDirectory.__init__.<locals>.<genexpr>c                 3  s   | ]}| kV  qd S r   r   )r   word)dirsr   r   r   C  s     z..r   N)r   r   
expandusersplitr   update_pathr9   r   zipfileZipFileZIP_DEFLATEDwalkjoinanyseprelpathwriter   r   r   )r   r   r   r   Z
skip_wordsskipr   zrootfilesfilearchive_namer   r   )r   r   r   rE   )  s,     zUploadDirectory.__init__c              	     s   t j|jdt  d}t|d}|| j W 5 Q R X dd l	}|
|}|j|jd W 5 Q R X | jrt j|j| j}|tjkrtjd| t | d S )Nztmp-z.zipwbr   )r   )r   r   r   Zlocal_directoryr=   r>   r   r   r   r   r   
extractallr   r{   insertremove)r   r7   r   r   r   r   r   r   r   r   r3   N  s    
zUploadDirectory.setupN)r-   r.   r/   r0   rE   r3   r   r   r   r   r     s   
%r   c                   @  sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )forward_streamc                 C  sd   || _ i | _tt|| _|dkr(d| _n |dkr8d| _ntd| d|dkrTdnd| _g | _d S )Nrx   rG   rq      z1Expected stream to be 'stdout' or 'stderr'; got 'rg   )_worker_original_methodsgetattrr{   _stream_file
ValueError_buffer)r   streamr"   r   r   r   rE   a  s    
zforward_stream.__init__c                 C  s   |  | || d S r   )_forward)r   Zwrite_fnr   r   r   r   _writeq  s    
zforward_stream._writec                 C  s(   | j | d|ksd|kr$|   d S )N
)r   append_send)r   r   r   r   r   r   u  s    zforward_stream._forwardc                 C  s*   | j | jddd}| jd| g | _ d S )N )r    r   r   endprint)r   r   r   r,   )r   r+   r   r   r   r   {  s    zforward_stream._sendc                 C  s   |    |  d S r   r   )r   Zflush_fnr   r   r   _flush  s    zforward_stream._flushc                 C  s   |    |  d S r   r   )r   Zclose_fnr   r   r   _close  s    zforward_stream._closec                 C  s0   t | j|}|| j|< t| j|t|| d S r   )r   r   r   setattr	functoolspartial)r   method_nameZinterceptorZoriginal_methodr   r   r   
_intercept  s    
  
zforward_stream._interceptc                 C  s0   |  d| j |  d| j |  d| j | jS )Nr   flushr   )r   r   r   r   r   r   r   r   r   	__enter__  s    zforward_stream.__enter__c                 C  s6   | j   | j D ]\}}t| j || qi | _d S r   )r   r   r   r   r   )r   exc_type	exc_value	tracebackattroriginalr   r   r   __exit__  s    
zforward_stream.__exit__N)r-   r.   r/   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   `  s   r   c                   @  s    e Zd ZdZdd Zdd ZdS )ForwardOutputa#  A Worker Plugin that forwards ``stdout`` and ``stderr`` from workers to clients

    This plugin forwards all output sent to ``stdout`` and ``stderr` on all workers
    to all clients where it is written to the respective streams. Analogous to the
    terminal, this plugin uses line buffering. To ensure that an output is written
    without a newline, make sure to flush the stream.

    .. warning::

        Using this plugin will forward **all** output in ``stdout`` and ``stderr`` from
        every worker to every client. If the output is very chatty, this will add
        significant strain on the scheduler. Proceed with caution!

    Examples
    --------
    >>> from dask.distributed import ForwardOutput
    >>> plugin = ForwardOutput()

    >>> client.register_worker_plugin(plugin)
    c                 C  s6   t  | _| jtd|d | jtd|d d S )Nrx   )r"   rq   )
contextlib	ExitStack_exit_stackenter_contextr   r2   r   r   r   r3     s    
zForwardOutput.setupc                 C  s   | j   d S r   )r   r   r2   r   r   r   r4     s    zForwardOutput.teardownN)r-   r.   r/   r0   r3   r4   r   r   r   r   r     s   r   )(
__future__r   r\   r   r   loggingr   rJ   ry   r{   r=   r   collections.abcr   typingr   r   r   Z
dask.utilsr   r   Zdistributed.schedulerr	   r
   	getLoggerr-   rM   r   r1   r5   r?   ABCr@   r^   ru   r   r   r   r   r   r   r   r   r   <module>   s8   
jE#lLB E<