To create a Dask cluster from the extension. Go to the dask-labextension tab and click "+ NEW"
%% Cell type:markdown id:f32e926e tags:

%% Cell type:markdown id:5cdebde9 tags:
You can see the cluster information and have some shortcuts
* "<>" insert a cell with the necessary lines to create a Dask client. Some environment variables have been set in order to be able to use a "regular" Dask Client
* "SCALE" increase/decrease the number of workers
* "SHUTDOWN" shutdown the cluster
%% Cell type:markdown id:7c2597ed tags:

%% Cell type:code id:61fb5ed7 tags:
``` python
fromdask.distributedimportClient
client=Client("tcp://192.168.101.82:34781'")
client
```
%% Cell type:code id:5aa4058d tags:
``` python
client.cluster
```
%% Cell type:markdown id:e2e751cf tags:
The encryption of the traffic between the different Dask components is enforced through environment variables
Once you have started a Dask cluster, the dasboard can be accessed through the Dashboard URL, typically the port 8787 and the IP of the host where it is running, e.g.: `https://192.168.102.43:8787/status`. This URL is not directly accessible from outside PIC's internal network.
However, PIC's jupyter service comes with the `jupyter-server-proxy` package which redirects the traffic from `https://jupyter.pic.es/user/{username}/proxy/{PORT}/{PATH}` to the `PORT` of host where the user's jupyterlab server is running.
However, PIC's jupyter service comes with the `jupyter-server-proxy` package which redirects the traffic from `https://jupyter.pic.es/user/{username}/proxy/{PORT}/{PATH}` to the `PORT` of the host where the user's jupyterlab server is running.
So it is possible to connect to the Dask dashboard from outside PIC through the URL: `https://jupyter.pic.es/user/{username}/proxy/{PORT}/status`, e.g. `https://jupyter.pic.es/user/torradeflot/proxy/8787/status`.
So it is possible to connect to the Dask dashboard from outside PIC through the URL: `https://jupyter.pic.es/user/{username}/proxy/{PORT}/status`, e.g. `https://jupyter.pic.es/user/torradeflot/proxy/8787/status`
%% Cell type:markdown id:b762046d tags:

%% Cell type:markdown id:86bfa20e tags:
## From python
### Using the default environment
This could be a notebook or a script submitted to HTCondor
job_extra_directives={'getenv':'True'})# needed to propagate the security
```
%% Output
WARNING:bokeh.server.util:Host wildcard '*' will allow connections originating from multiple (or possibly all) hostnames or IPs. Use non-wildcard values to restrict access explicitly
%% Cell type:code id:e3a6efad tags:
``` python
cluster
```
%% Output
%% Cell type:code id:e0c0f659 tags:
``` python
cluster.scale(2)
```
%% Cell type:code id:66cf3e55 tags:
``` python
c=Client(cluster)
```
%% Cell type:markdown id:a101a9d8 tags:
Security is inherited from environment variables
%% Cell type:code id:8b92c694 tags:
``` python
cluster.security
```
%% Cell type:markdown id:b0ffb263 tags:
## Connect to existing cluster
%% Cell type:markdown id:3570b4ab tags:
Meaning a cluster that was launched from outside you jupyterlab instance, e.g. an independent HTCondor job or somebody else's cluster.
To assign a GPU to your workers, include the `+RequestGpus=1` argument in the worker setup. This can be done by adding it to the `job_extra_directives` attribute when you instantiate the `HTCondorCluster` class.
**It is of uttermost importance to enable security when lauching a Dask cluster**, otherwise a malicious user can impersonate you by connecting to your cluster and submitting jobs to it. The attacker would then have access to your personal (e.g. SSH keys and private files in your home) and shared data.
## in jupyter.pic.es
In order to enforce security in the communication with Dask clusters, the notebooks started through jupyter.pic.es are populated with the environment variables:
See the [official documentation](https://distributed.dask.org/en/stable/tls.html#tls-ssl) to understand the function of each file.
When starting a Dask cluster through the jupyterlab extension or using the `pic_jupyterhub.dask_condor.SecureHTCondor` module in the standard environment these files will be created at runtime.
There might be issues when trying to communicate with a running Dask cluster if the security is not properly configured. See the [Troubleshooting section](#DaskSecurityTroubleshooting) for details
## in a custom cluster
When launching a cluster from a cell or a job, there are two way of providing encryption.
### Temporary security
Using temporary information stored in memory, the cluster won't be accessible from outside the process that spawned it. For this solution, the `cryptography` packages is needed. You can install it with `conda install cryptography`.
/data/jupyter/software/envs/jupyter_8440a3ce4a306902/lib/python3.12/site-packages/dask_jobqueue/core.py:745: UserWarning: Using a temporary security object without explicitly setting a shared_temp_directory: writing temp files to current working directory (/nfs/pic.es/user/t/torradeflot/services-code-samples/computing/dask) instead. You can set this value by using dask for e.g. `dask.config.set({'jobqueue.pbs.shared_temp_directory': '~'})`or by setting this value in the config file found in `~/.config/dask/jobqueue.yaml`
warnings.warn(
/data/jupyter/software/envs/jupyter_8440a3ce4a306902/lib/python3.12/site-packages/distributed/node.py:182: UserWarning: Port 8787 is already in use.
### Encryption using files generated with SecureHTCondor
In a similar fashion as in the previous section, if you ever started a cluster using the `SecureHTCondor` module availalbe in jupyter's base environment ([how to](#Creating-a-cluster)), a set of files that can be used for SSL encryption will already be available in the folder `${HOME}/.config/dask/security` and can be used as follows.
There are different ways to configure Dask and the related libraries, check the [official documentation](https://docs.dask.org/en/stable/configuration.html)
## Configuration files
You can add YAML files to `~/.config/dask/` to customize Dask's configuration. Specifics for each of the libraries in the Dask stack:
*[distributed](https://distributed.dask.org/en/stable/): framework for distributed computing
*[jobqueue](https://jobqueue.dask.org/en/latest/clusters-configuration.html): library to scale up Dask clusters to a batch system (e.g. HTCondor)
*[dask-labextension](https://github.com/dask/dask-labextension): jupyterlab extension to be able to manage/monitor Dask Clusters through a GUI in jupyterlab
## Programatic access to configuration
Configuration can be managed through the `dask.config` module. In particular, you can:
*`dask.config.get` or `dask.config.set` specific parameters
* list current configuration: `dask.config.config`
%% Cell type:markdown id:3809e604 tags:
# Troubleshooting
## Compatibility issues
If you try to connect a notebook to a Dask cluster, and the notebook's environment is different from the one used to launch de cluster, you may encounter compatibility issues.
You will tipycally receive a "Mismatched versions found" warning like this:
```
/data/astro/scratch2/torradeflot/envs/dask/lib/python3.11/site-packages/distributed/client.py:1388: VersionMismatchWarning: Mismatched versions found
Some mismatches might be blocking, it is recommended to match the major and minor versions. A mismatch in the patch version shouldn't be a problem.
%% Cell type:markdown id:8b2a984a tags:
<aname="DaskSecurityTroubleshooting"></a>
## Security
If you are trying to connect to a running Dask cluster and security is no properly configured you may encounter an error such as:
```
Cluster Start Error
Cluster
failed to start: TLS certificate does not match. Check your security
settings. More info at https://distributed.dask.org/en/latest/tls.html
```
There are different reasons why you can see this type of errors
### SSL files don't exist
If you launch a Dask cluster from a notebook in `jupyter.pic.es` but you have never launched a cluster from the dask-labextension or using the `pic_jupyterhub` module, you may encounter a problem because encryption is enforced but the certificates do not exist.
If this is the case, you can launch a cluster using one of these options as shown above. This will generate the certificate files and the subsequent creation of a Dask cluster from a notebook should succeed.
### SSL files got corrupted or expired
In some cases, the files used for the encryption can get corrupted. These files can be found in `${HOME}/.config/dask/security`. If you think this is the case, remove all the files in this folder and start a new Dask cluster from the jupyterlab extension or the `pic_jupyterhub` module in the main environment so that they can be regenerated.
### SSL configuration mismatch between client and cluster
Make sure that the encripytion configuration is consistent. Check Dask environment variables, configuration files and direct arguments used on both ends.
## Starting Dask Cluster through the extension fails
### Got multiple values for keyword argument asynchronous
There's currently a bug in the extension and the second time you try to start a cluster from it you will see this error. You can still spawn newe clusters from a notebook, but not from the extension.
The only workaraound as of now is to close the jupyterlab server and request a new one.
It can be difficult to debug a problem when using the parallell computing paradigm. For debugging purposes the scheduler can be configured to be single-threaded and synchronous ([doc](https://docs.dask.org/en/stable/scheduling.html#single-thread) ). With this configuration the processing will run serially in the same thread as the notebook, so the log/print messages will be displayed in the notebook.
**WARNING** Using this approach will dramaticallly reduce speed
# Once you have started a Dask cluster, the dasboard can be accessed through the Dashboard URL, typically the port 8787 and the IP of the host where it is running, e.g.: `https://192.168.102.43:8787/status`. This URL is not directly accessible from outside PIC's internal network.
#
# However, PIC's jupyter service comes with the `jupyter-server-proxy` package which redirects the traffic from `https://jupyter.pic.es/user/{username}/proxy/{PORT}/{PATH}` to the `PORT` of host where the user's jupyterlab server is running.
# However, PIC's jupyter service comes with the `jupyter-server-proxy` package which redirects the traffic from `https://jupyter.pic.es/user/{username}/proxy/{PORT}/{PATH}` to the `PORT` of the host where the user's jupyterlab server is running.
#
# So it is possible to connect to the Dask dashboard from outside PIC through the URL: `https://jupyter.pic.es/user/{username}/proxy/{PORT}/status`, e.g. `https://jupyter.pic.es/user/torradeflot/proxy/8787/status`.
# So it is possible to connect to the Dask dashboard from outside PIC through the URL: `https://jupyter.pic.es/user/{username}/proxy/{PORT}/status`, e.g. `https://jupyter.pic.es/user/torradeflot/proxy/8787/status`
This notebook contains instruction on how to use the Hadoop cluster from the command line
# Setup
## Authentication
The Hadoop cluster uses Kerberos for authentication. You need to initialize your credentials with Kerberos to be able to connect to the Hadoop platform.
### Initializing credentials
You have to run the `kinit` command in a regular terminal.
This can't be run in a notebook because it will ask for your password.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hadoop-3.2.3/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/tez-0.10.1/lib/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hive-3.1.2/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2024-11-25 10:45:19,596 WARN erasurecode.ErasureCodeNative: ISA-L support is not available in your platform... using builtin-java codec where applicable
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hadoop-3.2.3/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/tez-0.10.1/lib/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hive-3.1.2/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2024-11-25 10:39:25,751 WARN erasurecode.ErasureCodeNative: ISA-L support is not available in your platform... using builtin-java codec where applicable
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hadoop-3.2.3/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/tez-0.10.1/lib/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hive-3.1.2/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2024-11-25 10:46:33,439 INFO fs.TrashPolicyDefault: Moved: 'hdfs://hsrv04.pic.es:9000/user/torradeflot/test_1M' to trash at: hdfs://hsrv04.pic.es:9000/user/torradeflot/.Trash/Current/user/torradeflot/test_1M1732527993433
Beeline is an interactive CLI to explore Hive. Since it is interactive it is a bit tricky to run it within python cells and get the stderr and stdout printed out.
This is how it would look if you run it in a terminal
### Open beeline
[torradeflot@td835 hadoop-3.2.3]$ beeline
...
Connecting to jdbc:hive2://hsrv01.pic.es:10000/default;principal=hive/_HOST@PIC.ES