From daed9c209eb6c5bfb2a2da83202c876010d7e483 Mon Sep 17 00:00:00 2001 From: wenyh <44251917+wenyh1@users.noreply.github.com> Date: Fri, 25 Aug 2023 17:29:52 +0800 Subject: [PATCH] [inner-2340] fix: explain' lost connection.... (cherry picked from commit 2b87829dc786e4644c4ee67bf842a94a887ec04f) --- .../dble/server/handler/ExplainHandler.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/actiontech/dble/server/handler/ExplainHandler.java b/src/main/java/com/actiontech/dble/server/handler/ExplainHandler.java index 44b0bd2cc0..1f64dd3a86 100644 --- a/src/main/java/com/actiontech/dble/server/handler/ExplainHandler.java +++ b/src/main/java/com/actiontech/dble/server/handler/ExplainHandler.java @@ -52,6 +52,7 @@ import java.sql.SQLException; import java.sql.SQLNonTransientException; import java.sql.SQLSyntaxErrorException; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -270,6 +271,7 @@ private static RouteResultsetNode getTryRouteSingleNode(BaseHandlerBuilder build } public static void writeOutHeadAndEof(ShardingService service, RouteResultset rrs) { + final List rows = getRows(service, rrs); ByteBuffer buffer = service.allocate(); // writeDirectly header ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT); @@ -287,20 +289,29 @@ public static void writeOutHeadAndEof(ShardingService service, RouteResultset rr eof.setPacketId(service.nextPacketId()); buffer = eof.write(buffer, service, true); + // writeDirectly rows + for (RowDataPacket row : rows) { + row.setPacketId(service.nextPacketId()); + buffer = row.write(buffer, service, true); + } + + // writeDirectly last eof + EOFRowPacket lastEof = new EOFRowPacket(); + lastEof.setPacketId(service.nextPacketId()); + lastEof.write(buffer, service); + } + + private static List getRows(ShardingService service, RouteResultset rrs) { + LinkedList rows = new LinkedList<>(); if (!rrs.isNeedOptimizer()) { - // writeDirectly rows for (RouteResultsetNode node : rrs.getNodes()) { - RowDataPacket row = getRow(node, service.getCharset().getResults()); - row.setPacketId(service.nextPacketId()); - buffer = row.write(buffer, service, true); + rows.add(getRow(node, service.getCharset().getResults())); } } else { BaseHandlerBuilder builder = buildNodes(rrs, service); RouteResultsetNode routeSingleNode = getTryRouteSingleNode(builder, rrs); if (routeSingleNode != null) { - RowDataPacket row = getRow(routeSingleNode, service.getCharset().getResults()); - row.setPacketId(service.nextPacketId()); - buffer = row.write(buffer, service, true); + rows.add(getRow(routeSingleNode, service.getCharset().getResults())); } else { List results = ComplexQueryPlanUtil.getComplexQueryResult(builder); for (ReferenceHandlerInfo result : results) { @@ -308,17 +319,14 @@ public static void writeOutHeadAndEof(ShardingService service, RouteResultset rr row.add(StringUtil.encode(getRowStr(result.getName(), result.isIndentation()), service.getCharset().getResults())); row.add(StringUtil.encode(getRowStr(result.getType(), result.isIndentation()), service.getCharset().getResults())); row.add(StringUtil.encode(getRowStr(result.getRefOrSQL(), result.isIndentation()), service.getCharset().getResults())); - row.setPacketId(service.nextPacketId()); - buffer = row.write(buffer, service, true); + rows.add(row); } } } - // writeDirectly last eof - EOFRowPacket lastEof = new EOFRowPacket(); - lastEof.setPacketId(service.nextPacketId()); - lastEof.write(buffer, service); + return rows; } + private static String getRowStr(String content, boolean indentation) { String indentationStr = "------ "; return indentation ? indentationStr + content : content;