github-actions[bot] commented on code in PR #70607:
URL: https://github.com/apache/doris/pull/70607#discussion_r4597921612
##########
fe/fe-core/src/main/java/org/apache/doris/mysql/MysqlChannel.java:
##########
@@ -432,17 +447,40 @@ protected void realNetSend(ByteBuffer buffer) throws
IOException {
buffer = encryptData(buffer);
long bufLen = buffer.remaining();
long start = System.currentTimeMillis();
- long writeLen = Channels.writeBlocking(conn.getSinkChannel(), buffer,
context.getNetWriteTimeout(),
- TimeUnit.SECONDS);
- if (bufLen != writeLen) {
- long duration = System.currentTimeMillis() - start;
- throw new ConnectionException("Write mysql packet failed.[write="
+ writeLen
- + ", needToWrite=" + bufLen + "], duration: " + duration +
" ms");
- }
- Channels.flushBlocking(conn.getSinkChannel(),
context.getNetWriteTimeout(), TimeUnit.SECONDS);
+ StreamSinkChannel sink = conn.getSinkChannel();
+ long timeoutNanos =
TimeUnit.SECONDS.toNanos(context.getNetWriteTimeout());
+ long deadline = System.nanoTime() + timeoutNanos;
+ while (buffer.hasRemaining()) {
+ if (sink.write(buffer) == 0 && !awaitWritable(sink, deadline)) {
+ long duration = System.currentTimeMillis() - start;
+ throw new ConnectionException("Write mysql packet
failed.[write=" + (bufLen - buffer.remaining())
+ + ", needToWrite=" + bufLen + "], duration: " +
duration + " ms");
+ }
+ }
+ deadline = System.nanoTime() + timeoutNanos;
+ while (!sink.flush()) {
+ if (!awaitWritable(sink, deadline)) {
+ throw new ConnectionException("Flush mysql packet timed out");
+ }
+ }
isSend = true;
}
+ private boolean awaitWritable(StreamSinkChannel sink, long deadline)
throws IOException {
+ Status cancelReason = queryCancelReason.get();
+ if (cancelReason != null) {
+ // The peer is not consuming results and a packet may already be
partially written.
+ // An ERR packet cannot repair that stream. Let the command's
connection-error path close it.
+ throw new ConnectionException("Mysql result write cancelled: " +
cancelReason.getErrorMsg());
Review Comment:
[P2] Make cancelled partial writes terminal across command handlers
Throwing `ConnectionException` here does not always reach connection
cleanup. For a large `COM_STMT_EXECUTE` result, `ExecuteCommand.run` invokes
the query inside `StmtExecutor`'s Command branch; that branch wraps the
exception in `NereidsException`, and `execute()` converts it to ERR and
returns. `handleExecute` also catches all `Throwable`. If the client resumes
reading before `finalizeCommand`, its ERR write can succeed without calling
`awaitWritable`, so the connection stays open with ERR bytes appended to the
truncated row packet (`flush` has already cleared the unsent bytes). Preserve
the fatal exception through these boundaries or mark/close the channel before
another response; cover a prepared partial write whose sink becomes writable
again.
##########
fe/fe-core/src/main/java/org/apache/doris/mysql/protocol/MysqlProtocolAdapter.java:
##########
@@ -261,6 +263,12 @@ public boolean finishStatement(ConnectContext ctx,
StmtExecutor executor, int st
*/
public void finishCommand(ConnectContext ctx, StmtExecutor executor)
throws IOException {
LOG.debug("Finalize command for query {}",
DebugUtil.printId(ctx.queryId()));
+ // The last batch may carry EOS, so cancellation need not pass through
getNext again.
+ // Keep the connection when the cancellation error can be written
normally.
+ Status cancelReason = executor == null ? null :
executor.getPendingCancelReason();
+ if (cancelReason != null && ctx.getState().getStateType() ==
MysqlStateType.EOF) {
+ ctx.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR,
cancelReason.getErrorMsg());
Review Comment:
[P2] Send cancellation ERR only once for a multi-statement command
With `CLIENT_MULTI_STATEMENTS`, cancellation after the final batch of a
non-last SELECT reaches `finishStatement` while the state is EOF. It calls
`finishCommand`, which now changes the state to ERR and sends it.
`executeQuery` then breaks on ERR, but `processOnce` still calls
`finalizeCommand` and sends the same ERR again. The second terminal packet
remains on the connection and desynchronizes the next query. Perform this
EOF-to-ERR conversion before deciding whether to send an intermediate response,
or explicitly suppress the command's second response.
--
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]