924060929 commented on code in PR #70607:
URL: https://github.com/apache/doris/pull/70607#discussion_r4598638788


##########
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:
   Fixed in acad76189b6. `finishStatement` now resolves EOF cancellation before 
deciding whether to emit an intermediate response. Packet emission does not 
re-evaluate that decision, so command finalization alone sends the cancellation 
ERR. The new regression invokes both boundaries, asserts exactly one ERR, and 
then verifies the next response is OK on the same channel.



##########
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:
   Fixed in acad76189b6. Deliberate write/flush cancellation or timeout now 
latches a terminal transport failure, marks the session killed, and closes the 
channel on the writer thread. Later writes rethrow the original failure even if 
a Command/prepared handler consumed it, the peer resumed reading, or 
buffers/cancellation suppliers were reset. The prepared-handler regression 
wraps a partial-write exception in `NereidsException`, makes the sink writable 
again, and verifies that finalization and another send cannot append bytes. All 
35 focused FE tests, the FE package build, and Checkstyle passed.



-- 
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]

Reply via email to