be/src/common/query_log_context.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <cstdint> |
21 | | #include <iosfwd> |
22 | | #include <string> |
23 | | |
24 | | namespace doris { |
25 | | |
26 | | class TUniqueId; |
27 | | class PUniqueId; |
28 | | |
29 | | // A value snapshot. It never retains a query, a resource context, or an RPC request. |
30 | | struct QueryLogIdentity { |
31 | | uint64_t query_hi = 0; |
32 | | uint64_t query_lo = 0; |
33 | | uint64_t instance_hi = 0; |
34 | | uint64_t instance_lo = 0; |
35 | | |
36 | 9.80M | QueryLogIdentity() = default; |
37 | | explicit QueryLogIdentity(const TUniqueId& query_id); |
38 | | explicit QueryLogIdentity(const PUniqueId& query_id); |
39 | | QueryLogIdentity(const TUniqueId& query_id, const TUniqueId& instance_id); |
40 | | QueryLogIdentity(const PUniqueId& query_id, const PUniqueId& instance_id); |
41 | | }; |
42 | | |
43 | | // Called during logging initialization, before worker threads start. The bthread key |
44 | | // has process lifetime. Reading the identity never creates thread-local resources. |
45 | | void init_query_log_context(); |
46 | | QueryLogIdentity current_query_log_identity(); |
47 | | void append_query_log_identity(std::ostream& stream); |
48 | | |
49 | | // A message suffix for an ID not already present in the runtime log prefix. Keeps |
50 | | // query attribution when prefix logging is disabled or the caller has no context. |
51 | | // Do not use for audit/protocol fields or IDs describing relationships between queries. |
52 | | std::string query_id_log_suffix(const TUniqueId& query_id); |
53 | | |
54 | | // The installed pointer refers to this stack object and is restored before it dies. |
55 | | // bthreads use their own key, so yielding or migrating cannot expose a pthread's ID. |
56 | | class ScopedQueryLogContext { |
57 | | public: |
58 | 1.22k | ScopedQueryLogContext() = default; |
59 | 144k | explicit ScopedQueryLogContext(QueryLogIdentity identity) { reset(identity); } |
60 | | ~ScopedQueryLogContext(); |
61 | | |
62 | | ScopedQueryLogContext(const ScopedQueryLogContext&) = delete; |
63 | | ScopedQueryLogContext& operator=(const ScopedQueryLogContext&) = delete; |
64 | | ScopedQueryLogContext(ScopedQueryLogContext&&) = delete; |
65 | | ScopedQueryLogContext& operator=(ScopedQueryLogContext&&) = delete; |
66 | | |
67 | | // Replacing the current identity does not replace the saved outer scope. |
68 | | void reset(QueryLogIdentity identity); |
69 | | |
70 | | private: |
71 | | QueryLogIdentity _identity; |
72 | | QueryLogIdentity* _previous = nullptr; |
73 | | bool _installed = false; |
74 | | }; |
75 | | |
76 | | } // namespace doris |