Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,53 @@ export function calculateAge(birthDateStr) {
}
$$;
```

## Worker Management for UDFs

In Databend Cloud, each UDF has an associated **Worker** that manages its execution environment in the sandbox. After creating a UDF, you may need to manage its worker for optimal performance and resource utilization.

### Creating a Worker for Your UDF

```sql
-- Create a worker for your UDF (worker name should match UDF name)
CREATE WORKER calculate_age_js WITH
size='small',
auto_suspend='300',
auto_resume='true';
```

### Managing Worker Resources

```sql
-- View all workers
SHOW WORKERS;

-- Adjust worker settings
ALTER WORKER calculate_age_js SET size='medium', auto_suspend='600';

-- Add tags for organization
ALTER WORKER calculate_age_js SET TAG
environment='production',
team='analytics',
purpose='age-calculation';
```

### Worker Lifecycle

```sql
-- Suspend worker when not in use
ALTER WORKER calculate_age_js SUSPEND;

-- Resume worker when needed
ALTER WORKER calculate_age_js RESUME;

-- Remove worker when UDF is no longer needed
DROP WORKER calculate_age_js;
```

### Environment Variables

For security reasons, environment variables for UDFs are managed separately in the cloud console. After creating a UDF and its worker, configure any required environment variables through the Databend Cloud interface.

For more information, see [Worker Management](../20-worker/index.md).
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Worker",
"position": 20,
"link": {
"type": "generated-index",
"description": "Worker management commands for UDF execution environments in Databend Cloud sandbox."
}
}
103 changes: 103 additions & 0 deletions docs/en/sql-reference/10-sql-commands/00-ddl/20-worker/alter-worker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: ALTER WORKER
sidebar_position: 3
---

import FunctionDescription from '@site/src/components/FunctionDescription';

<FunctionDescription description="Introduced: v1.3.0"/>

Modifies the settings, tags, or state of an existing worker.

## Syntax

```sql
-- Modify worker options
ALTER WORKER <worker_name> SET <option_name> = '<value>' [, <option_name> = '<value>' ...]
ALTER WORKER <worker_name> UNSET <option_name> [, <option_name> ...]

-- Modify worker tags
ALTER WORKER <worker_name> SET TAG <tag_name> = '<tag_value>' [, <tag_name> = '<tag_value>' ...]
ALTER WORKER <worker_name> UNSET TAG <tag_name> [, <tag_name> ...]

-- Change worker state
ALTER WORKER <worker_name> SUSPEND
ALTER WORKER <worker_name> RESUME
```

| Parameter | Description |
| ------------ | --------------------------------------------------------------------------- |
| worker_name | The name of the worker to modify |
| option_name | One of: `size`, `auto_suspend`, `auto_resume`, `max_cluster_count`, `min_cluster_count` |
| value | The new value for the option (as a string) |
| tag_name | The name of the tag to set or unset |
| tag_value | The value for the tag |

## Options

The same options available in `CREATE WORKER` can be modified using `ALTER WORKER`:

| Option | Description |
| --------------------- | --------------------------------------------------------------------------- |
| `size` | Compute size of the worker (e.g., 'small', 'medium') |
| `auto_suspend` | Idle timeout before automatic suspend (seconds) |
| `auto_resume` | Whether auto-resume is enabled ('true' or 'false') |
| `max_cluster_count` | Upper bound for auto-scaling clusters |
| `min_cluster_count` | Lower bound for auto-scaling clusters |

## Examples

### Modify Worker Options

Change the size and auto-suspend settings of a worker:

```sql
ALTER WORKER read_env SET size='medium', auto_suspend='600';
```

Reset specific options to their default values:

```sql
ALTER WORKER read_env UNSET size, auto_suspend;
```

### Manage Worker Tags

Add or update tags on a worker:

```sql
ALTER WORKER read_env SET TAG purpose='sandbox', owner='ci';
```

Remove tags from a worker:

```sql
ALTER WORKER read_env UNSET TAG purpose, owner;
```

### Control Worker State

Suspend a worker (stop its execution environment):

```sql
ALTER WORKER read_env SUSPEND;
```

Resume a suspended worker:

```sql
ALTER WORKER read_env RESUME;
```

## Notes

1. **Atomic Operations**: Multiple options can be modified in a single `ALTER WORKER` statement.
2. **State Changes**: `SUSPEND` and `RESUME` are mutually exclusive with option modifications.
3. **Tag Management**: Tags are useful for categorizing and organizing workers. They can be used for cost allocation, environment identification, or team ownership.
4. **Validation**: Option values are validated according to the same rules as `CREATE WORKER`.

## Related Topics

- [CREATE WORKER](create-worker.md) - Create a new worker
- [SHOW WORKERS](show-workers.md) - List workers and their current settings
- [DROP WORKER](drop-worker.md) - Remove a worker
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: CREATE WORKER
sidebar_position: 1
---

import FunctionDescription from '@site/src/components/FunctionDescription';

<FunctionDescription description="Introduced: v1.3.0"/>

Creates a new worker for UDF execution in the sandbox environment.

## Syntax

```sql
CREATE WORKER [ IF NOT EXISTS ] <worker_name>
[ WITH size = '<size_value>']
[ WITH auto_suspend = '<suspend_seconds>']
[ WITH auto_resume = '<true|false>']
[ WITH max_cluster_count = '<count>']
[ WITH min_cluster_count = '<count>']
```

| Parameter | Description |
| --------------- | --------------------------------------------------------------------------------------------- |
| `IF NOT EXISTS` | Optional. If specified, the command succeeds without changes if the worker already exists. |
| worker_name | The name of the worker to create. Corresponds to a UDF name. |

## Options

| Option | Type / Values | Default | Description |
| --------------------- | -------------------------------------- | ------------- | --------------------------------------------------------------------------- |
| `size` | String (e.g., 'small', 'medium') | Platform default | Controls the compute size of the worker |
| `auto_suspend` | String (seconds) | Platform default | Idle timeout before automatic suspend |
| `auto_resume` | String ('true' or 'false') | Platform default | Controls whether incoming requests wake the worker automatically |
| `max_cluster_count` | String (count) | Platform default | Upper bound for auto-scaling clusters |
| `min_cluster_count` | String (count) | Platform default | Lower bound for auto-scaling clusters |

- Options are specified as key-value pairs using the `WITH` keyword
- All option values are passed as strings and must be enclosed in single quotes
- Options may appear in any order
- Option names are case-insensitive but shown here in lowercase for consistency

## Examples

Create a basic worker for a UDF named `read_env`:

```sql
CREATE WORKER read_env;
```

Create a worker with `IF NOT EXISTS` to avoid errors if it already exists:

```sql
CREATE WORKER IF NOT EXISTS read_env;
```

Create a worker with custom configuration:

```sql
CREATE WORKER read_env WITH size='small', auto_suspend='300', auto_resume='true', max_cluster_count='3', min_cluster_count='1';
```

## Notes

1. **UDF Association**: Each worker corresponds to a single UDF. The worker name should match the UDF name.
2. **Environment Variables**: Environment variables for UDFs are managed separately in the cloud console for security reasons.
3. **Resource Management**: Workers manage the execution environment and resources for their associated UDFs.
4. **Cloud Integration**: Workers are integrated with Databend Cloud's control plane for lifecycle management.

## Related Topics

- [ALTER WORKER](alter-worker.md) - Modify worker settings
- [SHOW WORKERS](show-workers.md) - List available workers
- [DROP WORKER](drop-worker.md) - Remove a worker
- [User-Defined Functions](../10-udf/index.md) - Learn about UDFs in Databend
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: DROP WORKER
sidebar_position: 4
---

import FunctionDescription from '@site/src/components/FunctionDescription';

<FunctionDescription description="Introduced: v1.3.0"/>

Removes a worker from the system.

## Syntax

```sql
DROP WORKER [ IF EXISTS ] <worker_name>
```

| Parameter | Description |
| --------------- | --------------------------------------------------------------------------------------------- |
| `IF EXISTS` | Optional. If specified, the command succeeds without errors if the worker does not exist. |
| worker_name | The name of the worker to remove. |

## Examples

Remove a worker:

```sql
DROP WORKER read_env;
```

Remove a worker only if it exists (avoids errors if the worker doesn't exist):

```sql
DROP WORKER IF EXISTS read_env;
```

## Behavior

1. **Resource Cleanup**: When a worker is dropped, all associated resources are released.
2. **UDF Association**: Dropping a worker does not automatically drop the associated UDF. The UDF can still exist but will not have an execution environment.
3. **Irreversible Operation**: Dropping a worker cannot be undone. The worker must be recreated if needed.
4. **Dependencies**: Ensure no active executions are using the worker before dropping it.

## Error Conditions

- `UnknownWorker`: If the worker does not exist and `IF EXISTS` is not specified.
- `WorkerInUse`: If the worker is currently executing UDFs or has pending operations.

## Notes

1. **Safety First**: Use `IF EXISTS` to make your scripts idempotent and avoid unnecessary errors.
2. **Check State**: Consider checking the worker's state with `SHOW WORKERS` before dropping it.
3. **Cleanup Order**: If you're cleaning up a UDF and its worker, drop the worker first, then the UDF if desired.
4. **Cloud Integration**: Worker deletion is coordinated with Databend Cloud's control plane to ensure proper resource cleanup.

## Related Topics

- [CREATE WORKER](create-worker.md) - Create a new worker
- [SHOW WORKERS](show-workers.md) - List available workers
- [ALTER WORKER](alter-worker.md) - Modify worker settings
- [User-Defined Functions](../10-udf/index.md) - Learn about UDFs in Databend
Loading
Loading