==========================
Adding MLflow to Workbench
==========================

You can install MLflow as an optional component of Data Science & AI Workbench.

Prerequisites
=============

You must have managed persistence enabled. 

Set environment variables
=========================

Setting environment variables allows MLflow to be accessible from all sessions, deployments, and schedules. This also sets the deployment-wide values for the MLflow tracking server endpoint.

#. Connect to your instance of Workbench.
#. Edit the ``anaconda-enterprise-env-var-config`` config map by following the process for :ref:`setting global config variables <global_config>`.
#. Include the following lines:


    .. code-block:: none

        # Replace <WORKBENCH_FQDN> with your Workbench fully qualified domain name
        MLFLOW_DISABLE_ENV_MANAGER_CONDA_WARNING: TRUE
        MLFLOW_TRACKING_URI: https://mlflow.<WORKBENCH_FQDN>
        MLFLOW_REGISTRY_URI: https://mlflow.<WORKBENCH_FQDN>

    .. note::
        
        If your ``ENV_VAR_PLACEHOLDER: foo`` entry still exists, replace it now. 

    Here is an example of what your file might look like when complete:

    .. thumbnail:: ../../images/ae5_mlflow_env_var_config_file.png
        :align: center

Download MLflow
===============

#. `Download the latest MLflow release <https://airgap.svc.anaconda.com/?prefix=ae5/>`_. 
#. Extract all files from the tarball you just downloaded. 

.. tip:: 
    
    Keep these somewhere that is easy for you to locate. 

Install MLflow
==============

#. Open a browser and navigate to Workbench. 
#. Log in as an administrator account with managed persistence permissions. 
#. Click **Create +**, then select **Upload Project** from the dropdown menu. 
#. Click **Browse**.
#. Locate the extracted files from your download and select the ``MLflowTracking ServerProject-<VERSION>.tar.gz`` file. 
#. Provide a name for your MLflow Server project.
#. Click **Upload**.
#. Open a session for your new MLflow Server project.
#. Upload the ``migrate-<VERSION>.py`` and ``anaconda-platform-mlflow-runtime-<VERSION>.tar.gz`` files to the root of the project. 

    .. caution::

        Do not commit changes to the project until instructed. If you attempt to commit too early, you will receive an error due to the size of the runtime file.

#. Open a terminal in your session, and run the following command:

    .. code-block:: none 

        # Replace <VERSION> with your release version
        python migrate-<VERSION>.py

#. Activate your new environment by running the following command:
   
    .. code-block:: none 

        conda activate /tools/mlflow/mlflow_env/


#. Verify your installation was successful by running the following command:
   
    .. code-block:: none 

        mlflow

    .. tip:: 
        
        If your install was successful, your command returns a list of available MLflow arguments.

#. Open the ``anaconda-project.yml`` file in the project with your preferred file editor.
#. Update the ``MLFLOW_TRACKING_GC_TTL`` value to something that makes sense for your use case. 

    .. thumbnail:: ../../images/ae5_mlflow_set_gc_ttl.png
        :align: center

    .. note:: 

        The ``MLFLOW_TRACKING_GC_TTL`` variable instructs MLflow to perform garbage collection on deleted artifacts that have reached a specified age.

#. Commit the changes you’ve made to the project.

    .. note:: 

        It is not necessary to commit the ``migrate-<VERSION>.py`` file to the project. Once installation is complete, you can safely delete this file. 

#. Stop the project session.

Deploy MLflow
=============

#. Select |chart-network| **Deployments** from the left-hand navigation. 
#. Click **Deploy**.
#. Enter a name for the deployment, set the static url to the same value used in your ``anaconda-enterprise-env-var-config`` file (``https://mlflow.<AE5_FQDN>``), and then click **Deploy**.

Provide Access
==============

#. Select the deployment you just created. 
#. Select |share-nodes| **Share** from the left-hand navigation menu.
#. Enter the names of users or groups to provide with permissions to access MLflow, then click **Add**. 

    .. note::

        This list populates from Keycloak. 

#. Select |gears| **Settings** from the left-hand menu.
#. Click **Generate** to create a token for this deployment.

    .. note:: 
        
        Every user who needs API access to MLflow also requires this token. You must share this token manually.
        
        The administrator of the MLFlow Tracking Server *must* generate and provide the access token *each time* the server is restarted.

Set up garbage collection
=========================

When a client deletes a resource, MLflow transitions the resource into a ``deleted`` lifecycle state and hides it in the UI, but does not purge it from the system. Deleted resources will block creation of new resources with the same name until the garbage collection process has purged it. 

The garbage collection process works in tandem with the ``MLFLOW_TRACKING_GC_TTL`` variable that is set in the ``anaconda-platform.yml`` project file. When a resource reaches the age specified by the ``MLFLOW_TRACKING_GC_TTL`` variable AND the garbage collection process runs, it will be purged.

#. Create a schedule within the MLflow Server project.
#. Name the schedule MLflow Garbage Collection.
#. Open the **Deployment Command** dropdown menu and select **gc**.
#. Schedule an interval to run the garbage collection. Custom schedules utilize `cron expressions <https://crontab.guru/>`_.
#. Click **Schedule**.

Upgrading MLflow
================

#. `Download the latest MLflow release <https://airgap.svc.anaconda.com/?prefix=ae5/>`_. 
#. Open a browser and navigate to Workbench. 
#. Log in as an administrator account with managed persistence permissions. 
#. Open your MLflow Server project.
#. Select |chart-network| **Deployments** from the left-hand navigation.
#. Terminate your current deployment. 
#. Select |calendar-light| **Schedules** from the left-hand navigation. 
#. Pause all scheduled runs. 
#. Start a new session in your MLflow Server project. 
#. Upload your newly obtained ``migrate-<VERSION>.py`` and ``anaconda-platform-mlflow-runtime-<VERSION>.tar.gz`` files to the root of the project.
#. Open a terminal in your project and run the following command:

    .. code-block:: none

        # Replace <VERSION> with your release version
        python migrate-<VERSION>.py

#. Redeploy your MLflow Server project. 
#. Generate a new token to share your deployment. 

    .. note:: 

        Remember, you *must* generate an access token and provide it to users *each time* the server is restarted!

#. Restart all scheduled runs.

.. |gears| raw:: html

   <svg class="inline" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path d="M146.2 40.7l-6.9 25.1c-1.4 5.2-5.4 9.4-10.6 11c-14.6 4.6-27.9 12.4-39 22.5c-4 3.7-9.6 5.1-14.9 3.7L49.7 96.4c-2.2 3.1-4.3 6.3-6.3 9.6l-2.4 4.2c-1.8 3.3-3.5 6.6-5 10l18.3 18.5c3.8 3.9 5.4 9.4 4.2 14.7C56.8 160.7 56 168.2 56 176s.8 15.3 2.4 22.5c1.2 5.3-.4 10.8-4.2 14.7L35.9 231.8c1.5 3.4 3.2 6.7 5 10l2.5 4.3c2 3.3 4.1 6.5 6.3 9.6L74.9 249c5.3-1.4 10.9 0 14.9 3.7c11.1 10.2 24.3 17.9 39 22.5c5.2 1.6 9.2 5.8 10.6 11l6.9 25.1c4.5 .5 9.1 .7 13.8 .7s9.2-.2 13.8-.7l6.9-25.1c1.4-5.2 5.4-9.4 10.6-11c14.6-4.6 27.9-12.4 39-22.5c4-3.7 9.6-5.1 14.9-3.7l25.2 6.6c2.2-3 4.2-6.1 6.1-9.3l2.8-4.8c1.7-3.2 3.4-6.4 4.8-9.7l-18.3-18.5c-3.8-3.9-5.4-9.4-4.2-14.7c1.6-7.2 2.4-14.8 2.4-22.6s-.8-15.3-2.4-22.6c-1.2-5.3 .4-10.8 4.2-14.7l18.3-18.5c-1.5-3.3-3.1-6.5-4.8-9.7l-2.8-4.8c-1.9-3.2-4-6.3-6.1-9.3L245.1 103c-5.3 1.4-10.9 0-14.9-3.7c-11.1-10.2-24.3-17.9-39-22.5c-5.2-1.6-9.2-5.8-10.6-11l-6.9-25.1c-4.5-.5-9.1-.7-13.8-.7s-9.2 .2-13.8 .7zM130.8 10.5C140.3 8.9 150.1 8 160 8s19.7 .9 29.1 2.5c6.1 1.1 11 5.6 12.7 11.5l7.5 27.2c13.1 5.1 25.1 12.1 35.9 20.7l27.3-7.1c6-1.6 12.3 .5 16.3 5.2c5.6 6.7 10.7 13.8 15.2 21.3l.2 .3 0 0 2.9 5.1 .2 .3 0 0c4.1 7.5 7.7 15.4 10.7 23.6c2.1 5.8 .7 12.3-3.6 16.7l-19.8 20.1c1 6.8 1.6 13.7 1.6 20.7s-.5 13.9-1.6 20.7l19.8 20.1c4.3 4.4 5.8 10.9 3.6 16.7c-3 8.2-6.6 16-10.7 23.6l-.2 .3 0 0-3 5.1-.2 .3 0 0c-4.5 7.5-9.6 14.6-15.2 21.3c-4 4.7-10.3 6.8-16.3 5.2l-27.3-7.1c-10.7 8.6-22.8 15.6-35.8 20.7L201.8 330c-1.6 6-6.6 10.5-12.7 11.5c-9.5 1.7-19.2 2.5-29.1 2.5s-19.7-.9-29.2-2.5c-6.1-1.1-11-5.6-12.7-11.5l-7.5-27.2c-13.1-5.1-25.1-12.1-35.8-20.7l-27.3 7.1c-6 1.6-12.3-.5-16.3-5.2c-5.7-6.8-10.9-14-15.4-21.6l-.1-.2 0 0-2.6-4.5-.1-.2 0 0c-4.2-7.6-7.9-15.6-10.9-23.9c-2.1-5.8-.7-12.3 3.6-16.7l19.8-20.1C24.5 189.9 24 183 24 176s.5-13.9 1.6-20.7L5.7 135.2c-4.3-4.4-5.8-10.9-3.6-16.7c3-8.3 6.7-16.3 10.9-23.9l.1-.2 0 0 2.6-4.5 .1-.2 0 0C20.4 82 25.6 74.8 31.3 68c4-4.7 10.3-6.8 16.3-5.2l27.3 7.1c10.7-8.6 22.8-15.6 35.8-20.7L118.2 22c1.6-6 6.6-10.5 12.7-11.5zM104 176a56 56 0 1 1 112 0 56 56 0 1 1 -112 0zm56 24a24 24 0 1 0 0-48 24 24 0 1 0 0 48zM328.7 365.8l25.1 6.9c5.2 1.4 9.4 5.4 11 10.6c4.6 14.6 12.4 27.9 22.5 39c3.7 4 5.1 9.6 3.7 14.9l-6.6 25.2c3.1 2.2 6.3 4.3 9.6 6.3l4.2 2.4c3.3 1.8 6.6 3.5 10 5l18.5-18.3c3.9-3.8 9.4-5.4 14.7-4.2c7.2 1.6 14.8 2.4 22.5 2.4s15.3-.8 22.5-2.4c5.3-1.2 10.8 .4 14.7 4.2l18.5 18.3c3.4-1.5 6.7-3.2 10-5l4.3-2.5c3.3-2 6.5-4.1 9.6-6.3L537 437.1c-1.4-5.3 0-10.9 3.7-14.9c10.2-11.1 17.9-24.3 22.5-39c1.6-5.2 5.8-9.2 11-10.6l25.1-6.9c.5-4.5 .7-9.1 .7-13.8s-.2-9.2-.7-13.8l-25.1-6.9c-5.2-1.4-9.4-5.4-11-10.6c-4.6-14.7-12.4-27.9-22.5-39c-3.7-4-5.1-9.6-3.7-14.9l6.6-25.2c-3-2.2-6.1-4.2-9.3-6.1l-4.8-2.8c-3.2-1.7-6.4-3.4-9.7-4.8l-18.5 18.3c-3.9 3.8-9.4 5.4-14.7 4.2c-7.2-1.6-14.8-2.4-22.6-2.4s-15.3 .8-22.6 2.4c-5.3 1.2-10.8-.4-14.7-4.2l-18.5-18.3c-3.3 1.5-6.5 3.1-9.7 4.8l-4.8 2.8c-3.2 1.9-6.3 4-9.3 6.1l6.6 25.2c1.4 5.3 0 10.9-3.7 14.9c-10.2 11.1-17.9 24.3-22.5 39c-1.6 5.2-5.8 9.2-11 10.6l-25.1 6.9c-.5 4.5-.7 9.1-.7 13.8s.2 9.2 .7 13.8zm-30.2 15.4c-1.7-9.5-2.5-19.2-2.5-29.2s.9-19.7 2.5-29.1c1.1-6.1 5.6-11 11.5-12.7l27.2-7.5c5.1-13.1 12.1-25.1 20.7-35.9l-7.1-27.3c-1.6-6 .5-12.3 5.2-16.3c6.7-5.6 13.8-10.7 21.3-15.2l.3-.2 0 0 5.1-2.9 .3-.2 0 0c7.5-4.1 15.4-7.7 23.6-10.7c5.8-2.1 12.3-.7 16.7 3.6l20.1 19.8c6.8-1 13.7-1.6 20.7-1.6s13.9 .5 20.7 1.6l20.1-19.8c4.4-4.3 10.9-5.8 16.7-3.6c8.2 3 16 6.6 23.6 10.7l.3 .2 0 0 5.1 3 .3 .2 0 0c7.5 4.5 14.6 9.6 21.3 15.2c4.7 4 6.7 10.3 5.2 16.3l-7.1 27.3c8.6 10.7 15.6 22.8 20.7 35.8l27.2 7.5c6 1.6 10.5 6.6 11.5 12.7c1.7 9.5 2.5 19.2 2.5 29.1s-.9 19.7-2.5 29.2c-1.1 6.1-5.6 11-11.5 12.7l-27.2 7.5c-5.1 13.1-12.1 25.1-20.7 35.8l7.1 27.3c1.6 6-.5 12.3-5.2 16.3c-6.8 5.7-14 10.9-21.6 15.4l-.2 .1 0 0-4.5 2.6-.2 .1 0 0c-7.6 4.2-15.6 7.9-23.9 10.9c-5.8 2.1-12.3 .7-16.7-3.6l-20.1-19.8c-6.8 1-13.7 1.6-20.7 1.6s-13.9-.5-20.7-1.6l-20.1 19.8c-4.4 4.3-10.9 5.8-16.7 3.6c-8.3-3-16.3-6.7-23.9-10.9l-.2-.1 0 0-4.5-2.6-.2-.1 0 0c-7.6-4.6-14.9-9.8-21.6-15.4c-4.7-4-6.8-10.3-5.2-16.3l7.1-27.3c-8.6-10.7-15.6-22.8-20.7-35.8L310 393.8c-6-1.6-10.5-6.6-11.5-12.7zM464 408a56 56 0 1 1 0-112 56 56 0 1 1 0 112zm24-56a24 24 0 1 0 -48 0 24 24 0 1 0 48 0z"/></svg>

.. |share-nodes| raw:: html

   <svg class="inline" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M448 112c0 44.2-35.8 80-80 80c-22.9 0-43.6-9.6-58.1-25l-151 75.5c.8 4.4 1.1 8.9 1.1 13.5s-.4 9.1-1.1 13.5l151 75.5c14.6-15.4 35.2-25 58.1-25c44.2 0 80 35.8 80 80s-35.8 80-80 80s-80-35.8-80-80c0-9.7 1.7-19 4.9-27.7L147.2 299.5c-14.3 22-39 36.5-67.2 36.5c-44.2 0-80-35.8-80-80s35.8-80 80-80c28.2 0 52.9 14.5 67.2 36.5l145.7-72.9c-3.2-8.6-4.9-17.9-4.9-27.7c0-44.2 35.8-80 80-80s80 35.8 80 80zM80 304a48 48 0 1 0 0-96 48 48 0 1 0 0 96zM416 112a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zM368 448a48 48 0 1 0 0-96 48 48 0 1 0 0 96z"/></svg>

.. |chart-network| raw:: html
   
   <i class="fa-light fa-chart-network"></i>

.. |calendar-light| raw:: html 

   <svg class="inline" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M112 0c8.8 0 16 7.2 16 16V64H320V16c0-8.8 7.2-16 16-16s16 7.2 16 16V64h32c35.3 0 64 28.7 64 64v32 32V448c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V192 160 128C0 92.7 28.7 64 64 64H96V16c0-8.8 7.2-16 16-16zM416 192H32V448c0 17.7 14.3 32 32 32H384c17.7 0 32-14.3 32-32V192zM384 96H64c-17.7 0-32 14.3-32 32v32H416V128c0-17.7-14.3-32-32-32z"/></svg>
