ClickHouse resource queues
This topic describes the resource queue, an enhanced feature of ApsaraDB for ClickHouse.
Background information
The open source version of ClickHouse does not include a resource queue feature. For more information about resource limits in ApsaraDB for ClickHouse Community-Compatible Edition clusters of V20.8 and later, see Restrictions on Query Complexity.
The resource queue is an enhanced feature of ApsaraDB for ClickHouse. It is available only for ApsaraDB for ClickHouse Community-Compatible Edition clusters of V20.3. The open source version of ClickHouse provides a user-level memory fencing mechanism. Because of this, the resource queue feature is disabled by default when you purchase a new cluster.
Limits
The resource queue feature is not available for the cloud-native edition of ApsaraDB for ClickHouse.
Resource queue definition
The following is the syntax for creating a resource queue:
-- Create a resource queue.
CREATE RESOURCE QUEUE [IF NOT EXISTS | OR REPLACE] name [ON CLUSTER cluster]
* {[SET] MEMORY = {number}
* [, CONCURRENCY = {number}]
* [, PRIORITY = { LOWEST | LOW | NORMAL | HIGH | HIGHEST }]
* [, ISOLATE = {number}]
* }
* [TO {role [,...] | ALL | ALL EXCEPT role [,...]}]
-- Modify a resource queue.
ALTER RESOURCE QUEUE [IF NOT EXISTS | OR REPLACE] name [ON CLUSTER cluster]
* {[SET] MEMORY = {number}
* [, CONCURRENCY = {number}]
* [, PRIORITY = { LOWEST | LOW | NORMAL | HIGH | HIGHEST }]
* [, ISOLATE = {number}]
* }
* [TO {role [,...] | ALL | ALL EXCEPT role [,...]}]
-- View the definition of a resource queue.
SHOW CREATE resource queue name
-- View the definition of the current resource queue.
SHOW CREATE resource queue current
-- Delete a resource queue.
DROP resource queue if exists name- memory: The size of the memory pool for the resource queue. The creation fails if all memory on a node is already allocated to existing resource queues.
- concurrency: The maximum number of concurrent queries allowed in the resource queue. The default value is 20. If the number of user-initiated queries reaches this limit, subsequent queries are blocked and queued. System-initiated subqueries are not blocked by this limit, but they are counted towards the concurrency total. For example, if the concurrency limit is 20, up to 25 system subqueries can run simultaneously. However, all user-initiated queries are blocked until the number of system subqueries drops below 20.
- priority: The priority of the resource queue. This priority affects both CPU scheduling and memory preemption.
- isolate: The memory isolation level of the resource queue.
- The default value is 0. This value indicates no isolation. If a resource queue has available memory, a higher-priority resource queue can preempt it when needed.
- A value of 1 indicates soft isolation. The memory of this resource queue cannot be preempted by higher-priority queues.
- A value of 2 indicates absolute isolation. The memory of this resource queue cannot be preempted, and this queue cannot preempt the memory of lower-priority queues.
- role: You can use
To role [,...]to attach the resource queue to one or more roles. Queries from the specified roles are routed to this resource queue by default. If multiple resource queues are attached to the same role, the system randomly selects one of the queues with the highest priority to execute queries.
- target_resource_queue: Forces a query to be routed to a specific resource queue. You can also configure this setting in a user's profile file to force routing for all of that user's queries.
- resource_queue_max_wait_ms: The maximum time that a query can wait in a queue because of concurrency limits. The default value is 10 s.
Example:
CREATE RESOURCE QUEUE IF NOT EXISTS test_queue ON CLUSTER cluster SET
MEMORY = 1073741824, CONCURRENCY = 20, ISOLATE = 0, PRIORITY = NORMAL
TO default;
CREATE RESOURCE QUEUE IF NOT EXISTS anonymous_queue ON CLUSTER cluster SET
MEMORY = 1073741824, CONCURRENCY = 20, ISOLATE = 1, PRIORITY = LOW;
SHOW CREATE resource queue test_queue;
SHOW CREATE resource queue current;
SELECT count (distinct intDiv(number, 10)) FROM numbers(100000) settings target_resource_queue='anonymous_queue';
DROP resource queue if exists test_queue;
DROP resource queue if exists anonymous_queue;Query resource queue status
show resource queues;The following table describes the returned parameters.
| Name | Type | Description |
| name | String | The name of the resource queue. |
| concurrency | UInt32 | The concurrency limit of the resource queue. |
| memory | UInt64 | The size of the memory pool for the resource queue. Unit: bytes. |
| isolate | UInt8 | The isolation level of the resource queue. |
| priority | Enum8 (enumeration) | The priority level of the resource queue. |
| roles | Array<String> | All users who are attached to the resource queue. |
show resource queue stat [CURRENT | ALL];The following table describes the returned parameters.
| Name | Type | Description |
| name | String | The name of the resource queue. |
| running_query | UInt32 | The number of queries that are running in the resource queue. |
| waiting_query | UInt32 | The number of queries that are waiting to be executed in the resource queue. |
| grabbing_query | UInt32 | The number of queries that are not in this resource queue but are preempting its memory pool. |
| allocated_memory | UInt64 | The size of memory that is allocated from the memory pool for regular requests. Unit: bytes. |
| grabbed_memory | UInt64 | The size of memory that is preempted from the memory pool. Unit: bytes. |
| free_memory | UInt64 | The amount of available memory in the memory pool. Unit: bytes. |
Resource queue-related query exceptions
Query times out because of concurrency limits
- Error code: 13005
- Solution: Reduce the number of concurrent queries from the client.
A query uses more memory than is available in the resource queue pool
- Error code: 241
- Solution: Optimize the query plan or increase the memory pool size of the resource queue.
The specified resource queue for forced routing does not exist
- Error code: 13006
- Solution: Check the creation status of the resource queue.
A query is killed when it attempts to preempt a lower-priority resource queue
- Error code: 394
- Solution: Reduce the concurrency of the resource queue and adjust the queue priority.