In addition to the time since the most recent insert,
consider the elapsed time between the two recent queue
flushes when decreasing the timeout or processing an
entry synchronously.
Add AsynchronousInsertQueueSize and AsynchronousInsertQueueBytes
metrics to improve observability of asynchronous inserts.
The metrics do not account for tasks dispatched for immediate processing
(as opposed to, e.g., PendingAsyncInsert).
```
SELECT value
FROM system.metrics
WHERE metric IN ('AsynchronousInsertQueueSize', 'PendingAsyncInsert')
Query id: a711dd83-b48d-4ad5-8031-fa59b21a7c38
┌─value─┐
│ 18 │
│ 23 │
└───────┘
```
```
SELECT value
FROM system.metrics
WHERE metric IN ('AsynchronousInsertQueueSize', 'AsynchronousInsertQueueBytes')
Query id: b35a7ceb-2bb5-46ad-b301-e6cf03508699
┌─value─┐
│ 28 │
│ 1372 │
└───────┘
```
Implement the algorithm described in #56783 for adaptive asynchronous
insert timeouts.
- The adaptive async insert timeout can take values within
[async_insert_busy_timeout_min_ms, async_insert_busy_timeout_max_ms].
- The initial value is set to async_insert_busy_timeout_min_ms.
- If the elapsed time since the most recent queue insert was
greater than the maximum timeout, process the queue content immediately,
and reduce the timeout.
- If the elapsed time was long enough (longer than a would-be decreased
timeout), decrease the timeout.
- The adaptive timeout is changes exponentially based on the
async_insert_busy_timeout_{increase|decrease}_rate.
Fixes: https://github.com/ClickHouse/ClickHouse/issues/56783
It became flaky after we merged #39663. When external aggregation happens it seems like we get more than one part, and because we used `OPTIMIZE` without `FINAL` data wasn't properly merged.
`order by` on the line 12 is not needed by now, but let's add it just in case.