diff --git a/src/main/java/com/actiontech/dble/server/NonBlockingSession.java b/src/main/java/com/actiontech/dble/server/NonBlockingSession.java index 99cddb8d6b..7dd2f108d1 100644 --- a/src/main/java/com/actiontech/dble/server/NonBlockingSession.java +++ b/src/main/java/com/actiontech/dble/server/NonBlockingSession.java @@ -180,6 +180,13 @@ public void startProcess() { provider.startProcess(shardingService.getConnection().getId()); } + public void endParseTCL() { + if (traceEnable || SlowQueryLog.getInstance().isEnableSlowLog()) { + traceResult.ready(); + traceResult.setTCL(); + } + } + public void endParse() { sessionStage = SessionStage.Route_Calculation; if (traceEnable || SlowQueryLog.getInstance().isEnableSlowLog()) { diff --git a/src/main/java/com/actiontech/dble/server/handler/BeginHandler.java b/src/main/java/com/actiontech/dble/server/handler/BeginHandler.java index 78a313c315..5fbe0ad857 100644 --- a/src/main/java/com/actiontech/dble/server/handler/BeginHandler.java +++ b/src/main/java/com/actiontech/dble/server/handler/BeginHandler.java @@ -14,6 +14,7 @@ private BeginHandler() { } public static void handle(String stmt, ShardingService service) { + service.getSession2().endParseTCL(); if (service.isTxStart() || !service.isAutocommit()) { service.beginInTx(stmt); } else { @@ -21,6 +22,7 @@ public static void handle(String stmt, ShardingService service) { StatisticListener.getInstance().record(service, r -> r.onTxStart(service)); TxnLogHelper.putTxnLog(service, stmt); service.writeOkPacket(); + service.getSession2().setResponseTime(true); } } } diff --git a/src/main/java/com/actiontech/dble/server/handler/CommitHandler.java b/src/main/java/com/actiontech/dble/server/handler/CommitHandler.java index 0cec0dc09e..9abb8cbdab 100644 --- a/src/main/java/com/actiontech/dble/server/handler/CommitHandler.java +++ b/src/main/java/com/actiontech/dble/server/handler/CommitHandler.java @@ -12,6 +12,7 @@ private CommitHandler() { } public static void handle(String stmt, ShardingService service) { + service.getSession2().endParseTCL(); service.transactionsCount(); service.commit(stmt); } diff --git a/src/main/java/com/actiontech/dble/server/handler/RollBackHandler.java b/src/main/java/com/actiontech/dble/server/handler/RollBackHandler.java index 3b8af07f20..c1c411ff55 100644 --- a/src/main/java/com/actiontech/dble/server/handler/RollBackHandler.java +++ b/src/main/java/com/actiontech/dble/server/handler/RollBackHandler.java @@ -14,6 +14,7 @@ private RollBackHandler() { } public static void handle(String stmt, ShardingService service) { + service.getSession2().endParseTCL(); if (service.isTxStart() || !service.isAutocommit()) { StatisticListener.getInstance().record(service, r -> r.onTxEnd()); TxnLogHelper.putTxnLog(service, stmt); diff --git a/src/main/java/com/actiontech/dble/server/trace/TraceResult.java b/src/main/java/com/actiontech/dble/server/trace/TraceResult.java index 7c3ec95474..8ec1b4ae22 100644 --- a/src/main/java/com/actiontech/dble/server/trace/TraceResult.java +++ b/src/main/java/com/actiontech/dble/server/trace/TraceResult.java @@ -24,7 +24,7 @@ public class TraceResult implements Cloneable { public enum SqlTraceType { - SINGLE_NODE_QUERY, MULTI_NODE_QUERY, MULTI_NODE_GROUP, COMPLEX_QUERY; + SINGLE_NODE_QUERY, MULTI_NODE_QUERY, MULTI_NODE_GROUP, COMPLEX_QUERY, SIMPLE_QUERY; } private static final Logger LOGGER = LoggerFactory.getLogger(TraceResult.class); @@ -53,11 +53,17 @@ public enum SqlTraceType { private long veryEnd; private SqlTraceType type; private boolean subQuery = false; + private volatile boolean isTCL = false; public void setVeryStartPrepare(long veryStartPrepare) { prepareFinished = false; this.veryStartPrepare = veryStartPrepare; this.requestStartPrepare = new TraceRecord(veryStartPrepare); + this.isTCL = false; + } + + public void setTCL() { + this.isTCL = true; } public void setRouteStart(TraceRecord routeStart) { @@ -260,6 +266,7 @@ public List genTraceResult() { if (LOGGER.isDebugEnabled()) { LOGGER.debug("start genTraceResult"); } + List lst = new ArrayList<>(); if (!isCompleted()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("collect info not in pairs,veryEnd:" + veryEnd + ",connFlagMap.size:" + connFlagMap.size() + @@ -267,19 +274,23 @@ public List genTraceResult() { ",recordStartMap.size:" + recordStartMap.size() + ",recordEndMap.size:" + recordEndMap.size()); } return null; - } - List lst = new ArrayList<>(); - lst.add(genTraceRecord("Read_SQL", requestStart.getTimestamp(), parseStart.getTimestamp())); - lst.add(genTraceRecord("Parse_SQL", parseStart.getTimestamp(), routeStart.getTimestamp())); - if (simpleHandler != null) { - if (genSimpleResults(lst)) return null; - } else if (builder != null) { - if (genComplexQueryResults(lst)) return null; + } else if (isTCL) { + lst.add(genTraceRecord("Read_SQL", requestStart.getTimestamp(), parseStart.getTimestamp())); + lst.add(genTraceRecord("Parse_SQL", parseStart.getTimestamp(), routeStart.getTimestamp())); + lst.add(genTraceRecord("Write_to_Client", routeStart.getTimestamp(), veryEnd)); } else { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("not support trace this query"); + lst.add(genTraceRecord("Read_SQL", requestStart.getTimestamp(), parseStart.getTimestamp())); + lst.add(genTraceRecord("Parse_SQL", parseStart.getTimestamp(), routeStart.getTimestamp())); + if (simpleHandler != null) { + if (genSimpleResults(lst)) return null; + } else if (builder != null) { + if (genComplexQueryResults(lst)) return null; + } else { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("not support trace this query"); + } + return null; } - return null; } lst.add(genTraceRecord("Over_All", veryStart, veryEnd)); if (LOGGER.isDebugEnabled()) { @@ -572,10 +583,13 @@ private String nanoToMilliSecond(long nano) { } public boolean isCompleted() { - return veryStart != 0 && veryEnd != 0 && connFlagMap.size() != 0 && connReceivedMap.size() == connFinishedMap.size() && recordStartMap.size() == recordEndMap.size(); + return veryStart != 0 && veryEnd != 0 && (isTCL) || (connFlagMap.size() != 0 && connReceivedMap.size() == connFinishedMap.size() && recordStartMap.size() == recordEndMap.size()); } public SqlTraceType getType() { + if (this.type == null) { + return SqlTraceType.SIMPLE_QUERY; + } return this.type; } @@ -590,6 +604,12 @@ public List genLogResult() { ",recordStartMap.size:" + connReceivedMap.size() + ",recordEndMap.size:" + connFinishedMap.size()); } return null; + } else if (isTCL) { + List lst = new ArrayList<>(); + lst.add(genLogRecord("Read_SQL", requestStart.getTimestamp(), parseStart.getTimestamp())); + lst.add(genLogRecord("Inner_Execute", parseStart.getTimestamp(), veryEnd)); + lst.add(genLogRecord("Write_Client", veryEnd, veryEnd)); + return lst; } List lst = new ArrayList<>(); lst.add(genLogRecord("Read_SQL", requestStart.getTimestamp(), parseStart.getTimestamp()));