wenzhenghu commented on code in PR #68694:
URL: https://github.com/apache/doris/pull/68694#discussion_r4267297505
##########
fe/fe-core/src/main/java/org/apache/doris/arrowflight/DorisFlightSqlProducer.java:
##########
@@ -220,6 +224,8 @@ private FlightInfo executeQueryStatement(String
peerIdentity, ConnectContext con
final FlightDescriptor descriptor) {
try {
Preconditions.checkState(null != connectContext);
+ // A new request has no query identity until statement execution
assigns one.
+ connectContext.resetQueryId();
Review Comment:
I kept the idempotent resets at the three actual preparation boundaries. The
producer must clear the prior identity before `beginRequest()` cleanup; the
Flight processor must clear it before `prepare()` and workload-group
resolution; the shared processor must clear it before SQL parsing, including
ordinary MySQL calls.
Keeping only the innermost reset would leave Flight preparation/error logs
under the previous query. `resetQueryId()` updates `lastQueryId` only for a
non-null current ID, so repeated clearing does not overwrite it. Consolidating
these separate request boundaries was not included in this attribution update.
##########
fe/fe-core/src/main/java/org/apache/doris/common/QueryLogContext.java:
##########
@@ -0,0 +1,145 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.common;
+
+import org.apache.doris.common.util.DebugUtil;
+import org.apache.doris.proto.Types.PUniqueId;
+import org.apache.doris.thrift.TUniqueId;
+
+import org.apache.logging.log4j.ThreadContext;
+
+import java.util.concurrent.Executor;
+import java.util.function.Supplier;
+
+/** Query identity for runtime LogEvents, independent of connection and query
object lifetimes. */
+public final class QueryLogContext implements AutoCloseable {
+ public static final String QUERY_ID = "query_id";
+ private static final QueryLogContext NOOP = new QueryLogContext();
+ private static final ThreadLocal<String> PLANNING_QUERY_ID = new
ThreadLocal<>();
+
+ private final String previousQueryId;
+ private final boolean installed;
+
+ private QueryLogContext() {
+ previousQueryId = null;
+ installed = false;
+ }
+
+ private QueryLogContext(String queryId) {
+ previousQueryId = ThreadContext.get(QUERY_ID);
+ installed = true;
+ set(queryId);
+ }
+
+ public static QueryLogContext open(TUniqueId queryId) {
+ return Config.sys_log_enable_query_id ? new
QueryLogContext(format(queryId)) : NOOP;
+ }
+
+ /** Internal planning connections without an ID inherit the enclosing
planning identity. */
+ public static <T> T withPlanningContext(TUniqueId queryId, Supplier<T>
action) {
+ if (!Config.sys_log_enable_query_id) {
+ return action.get();
+ }
+ String previous = PLANNING_QUERY_ID.get();
+ String current = queryId == null ? previous : format(queryId);
+ PLANNING_QUERY_ID.set(current);
+ try (QueryLogContext ignored = new QueryLogContext(current)) {
+ return action.get();
+ } finally {
+ if (previous == null) {
+ PLANNING_QUERY_ID.remove();
+ } else {
+ PLANNING_QUERY_ID.set(previous);
+ }
+ }
+ }
+
+ public static void setQueryId(TUniqueId queryId) {
+ if (Config.sys_log_enable_query_id) {
+ set(format(queryId));
+ }
+ }
+
+ public static void clear() {
+ if (Config.sys_log_enable_query_id) {
+ ThreadContext.remove(QUERY_ID);
+ }
+ }
+
+ /**
+ * Keep the query ID in a runtime message only when the prefix does not
already contain it.
+ * This preserves attribution when logging is disabled or the caller has
no query context.
+ * Audit/protocol fields and IDs relating different queries must remain
explicit.
+ */
+ public static String queryIdSuffix(TUniqueId queryId) {
+ return suffix(format(queryId));
+ }
+
+ public static String queryIdSuffix(PUniqueId queryId) {
Review Comment:
Partially adopted in
[e8c944b9cb66](https://github.com/apache/doris/commit/e8c944b9cb668c580b1854979a0225a04d80ef76):
removed the unused `FlightSqlChannel` overloads and two-argument cache-entry
constructor, and migrated tests to the explicit query-ID entry points.
The protobuf `queryIdSuffix(PUniqueId)` overload is still used in production
by `ResultReceiver` cancellation/interruption logs (`this.queryId` is a
`Types.PUniqueId`). I retained that overload and its tests. Flight/result-cache
tests passed in the independent FE run.
--
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]