924060929 opened a new pull request, #70607:
URL: https://github.com/apache/doris/pull/70607
### What problem does this PR solve?
When a MySQL client stops consuming a large result, the FE can block writing
a result packet. `KILL QUERY` cancels the query's backend execution and result
fetching, but the original FE worker remains in the network write until
`net_write_timeout` expires. The query therefore remains visible even though
backend execution has already been cancelled or finished.
The normal flow is:
```text
BE result -> ResultReceiver -> StmtExecutor -> MysqlResultSender ->
MysqlChannel -> client
```
Cancellation reaches `StmtExecutor`, the coordinator and result receivers.
It does not interrupt an already-running `Channels.writeBlocking` or
`flushBlocking` in `MysqlChannel`. Adding only a cancellation check between
rows cannot release a worker already waiting inside that write.
**Fix**
- Bind a cancellation-reason supplier to the current MySQL command. It reads
the current executor's existing atomic cancellation state, including executor
changes within a multi-statement command, and is cleared in `finally`.
- Replace the long blocking write/flush with nonblocking attempts and
bounded `awaitWritable` waits. A stalled write checks cancellation at intervals
of at most 100 ms. The original network-write deadline is retained rather than
renewed on each check; the writable path introduces no waiting.
- On cancellation of a stalled stream, propagate `ConnectionException`
through the existing connection-cleanup path. Part of a MySQL packet may
already be on the wire, so appending an ERR packet cannot safely repair the
stream. This case closes the connection.
- Preserve ordinary `KILL QUERY` connection reuse: a cancellation error that
can be written normally is still sent without disconnecting the client.
- Check cancellation while sending rows and before a successful result-set
terminator. Only EOF is changed to an error; committed OK responses and NOOP
commands that already sent a response are preserved.
The bounded cancellation response applies to stalled MySQL writes. This PR
does not change coordinator-initialization cancellation races or promise the
same bound at every query execution stage.
### Release note
Cancelled queries now stop promptly when blocked sending MySQL results.
Ordinary query cancellation continues to allow connection reuse; cancelling a
stalled result stream closes that connection.
### Check List (For Author)
- Test
- [x] Regression test
- [x] Unit Test
- [x] Manual test (details below)
- [ ] No need to test or manual test. Explain why:
- Behavior changed:
- [ ] No.
- [x] Yes. Cancellation interrupts a stalled MySQL result write instead
of waiting for its network timeout; that stalled connection is closed.
- Does this need documentation?
- [x] No.
- [ ] Yes.
**Unit tests**
`MysqlChannelTest`, `MysqlResultSenderTest`, `MysqlWriteCancellationTest`
and `MysqlPacketGoldenTest` cover cancellation during write/flush waits,
partial writes, original timeout deadlines, writable cancellation responses,
command cleanup, EOF/OK/NOOP handling and the existing packet golden cases.
After applying the patch to master `0e8b56e0dc9`, the current PR head
`374a3977477` passed all **25 tests** in these four classes (0 failures, 0
errors), using `run-fe-ut.sh`. A separate current-head Checkstyle run reported
**zero violations**.
**JDBC regression**
Added `test_mysql_kill_query_reuse`. It cancels a long query twice on the
same JDBC connection, checks the server connection ID after each cancellation,
and then executes `SELECT 42`. The output was generated with
`run-regression-test.sh` and a subsequent comparison run passed: 1 suite, 0
failures.
**Local socket reproduction**
The following functional checks ran against a locally built FE before
rebasing onto current master; the BE used the existing local binary. They are
local samples, not throughput or percentile benchmarks.
1. Connect a MySQL client, set `net_write_timeout=20`, reduce its receive
buffer to 4 KiB, send the following query and stop reading the response:
```sql
SELECT repeat('x', 1048576) FROM numbers('number'='128');
```
2. Confirm the FE is waiting in `MysqlChannel.realNetSend` / `awaitWritable`
using a thread dump, then cancel from another connection.
3. Before the fix, the FE waited for the full 20-second write timeout (about
17 seconds remained after cancellation). After the fix, three `KILL QUERY`
samples removed the session in approximately 29 / 62 / 85 ms, and the client
observed disconnection. `KILL CONNECTION` controls completed in approximately 3
ms. Session-list sampling used a 20 ms interval.
4. For ordinary cancellation while fetching results, cancel three successive
queries on one TCP connection. Each cancellation returned an error, and
subsequent queries and PING succeeded with both the server connection ID and
client socket unchanged. Three additional early-cancellation checks also
retained their original connection, although one waited for the current
five-second BE operation to finish.
5. With normal client consumption, all 128 rows / 128 MiB of the result were
received.
The FE package was built with `build.sh --fe`, and Checkstyle reported zero
violations on the original tested tree. Remote current-head build and review
are requested separately.
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]