rs_dpr_service/dask/dask_cluster_handler.md
Module to handle connection to Dask cluster.
DaskClusterHandler
Class to handle connection to Dask cluster.
NOTE: a new instance of this class is called for every endpoint call.
Source code in docs/rs-dpr-service/rs_dpr_service/dask/dask_cluster_handler.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
setup_dask_connection()
Connects a dask cluster scheduler Establishes a connection to a Dask cluster, either in a local environment or via a Dask Gateway in a Kubernetes cluster. This method checks if the cluster is already created (for local mode) or connects to a Dask Gateway to find or create a cluster scheduler (for Kubernetes mode, see RSPY_LOCAL_MODE env var).
-
Local Mode: - If
self.clusteralready exists, it assumes the Dask cluster was created when the application started, and proceeds without creating a new cluster. -
Kubernetes Mode: - If
self.clusteris not already defined, the method attempts to connect to a Dask Gateway (using environment variablesDASK_GATEWAY_ADDRESSandDASK_GATEWAY__AUTH__TYPE) to retrieve a list of existing clusters. - If no clusters are available, it attempts to create a new cluster scheduler.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Raised if the cluster name is None, required environment variables are missing, cluster creation fails or authentication errors occur. |
KeyError
|
Raised if the necessary Dask Gateway environment variables ( |
IndexError
|
Raised if no clusters are found in the Dask Gateway and new cluster creation is attempted. |
GatewayServerError
|
Raised when there is a server-side error in Dask Gateway. |
AuthenticationError
|
Raised if authentication to the Dask Gateway fails. |
ClusterLimitExceeded
|
Raised if the limit on the number of clusters is exceeded. |
Behavior: 1. Cluster Creation and Connection: - In Kubernetes mode, the method tries to connect to an existing cluster or creates a new one if none exists. - Error handling includes catching issues like missing environment variables, authentication failures, cluster creation timeouts, or exceeding cluster limits.
-
Logging:
- Logs the list of available clusters if connected via the Dask Gateway.
- Logs the success of the connection or any errors encountered during the process.
- Logs the Dask dashboard URL and the number of active workers.
-
Client Initialization:
- Once connected to the Dask cluster, the method creates a Dask
Clientobject for managing tasks and logs the number of running workers. - If no workers are found, it scales the cluster to 1 worker.
- Once connected to the Dask cluster, the method creates a Dask
-
Error Handling:
- Handles various exceptions during the connection and creation process, including:
- Missing environment variables.
- Failures during cluster creation.
- Issues related to cluster scaling, worker retrieval, or client creation.
- If an error occurs, the method logs the error and attempts to gracefully handle failure.
Returns:
| Type | Description |
|---|---|
Client
|
Dask client |
Source code in docs/rs-dpr-service/rs_dpr_service/dask/dask_cluster_handler.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |