forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_context.cpp
More file actions
226 lines (209 loc) · 10.3 KB
/
Copy paththread_context.cpp
File metadata and controls
226 lines (209 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// 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.
#include "runtime/thread_context.h"
#include "common/signal_handler.h"
#include "runtime/query_context.h"
#include "runtime/runtime_state.h"
namespace doris {
class MemTracker;
void AttachTask::init(const std::shared_ptr<ResourceContext>& rc) {
// Validate the ResourceContext chain before mutating any thread-local
// or signal state. If any link is null we throw immediately, so the
// caller's stack-unwind sees a clean state (no thread-local handle is
// acquired and no signal task id is set). Without these the previous
// code would silently propagate a null mem_tracker into
// ThreadMemTrackerMgr and crash much later inside the allocator.
if (UNLIKELY(rc == nullptr)) {
throw Exception(
Status::FatalError("AttachTask::init: rc is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
if (UNLIKELY(rc->memory_context() == nullptr)) {
throw Exception(Status::FatalError(
"AttachTask::init: rc->memory_context() is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
if (UNLIKELY(rc->memory_context()->mem_tracker() == nullptr)) {
throw Exception(Status::FatalError(
"AttachTask::init: rc->memory_context()->mem_tracker() is null. "
"ResourceContext was created but set_mem_tracker has not been called yet "
"(likely a half-initialized QueryContext used before _init_query_mem_tracker). "
"signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
if (UNLIKELY(rc->task_controller() == nullptr)) {
throw Exception(Status::FatalError(
"AttachTask::init: rc->task_controller() is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
ThreadLocalHandle::create_thread_local_if_not_exits();
signal::set_signal_task_id(rc->task_controller()->task_id());
thread_context()->attach_task(rc);
}
AttachTask::AttachTask(const std::shared_ptr<ResourceContext>& rc) {
init(rc);
}
AttachTask::AttachTask(const std::shared_ptr<MemTrackerLimiter>& mem_tracker) {
// if parameter is `orphan_mem_tracker`, if you do not switch thraed mem tracker afterwards,
// alloc or free memory from Allocator will fail DCHECK. unless you know for sure that
// the thread will not alloc or free memory from Allocator later.
//
// Validate before constructing the ResourceContext: MemoryContext::set_mem_tracker()
// immediately dereferences mem_tracker->limit(), so a null shared_ptr would
// crash there before reaching init()'s diagnostics.
if (UNLIKELY(mem_tracker == nullptr)) {
throw Exception(Status::FatalError(
"AttachTask(MemTrackerLimiter): mem_tracker is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
std::shared_ptr<ResourceContext> rc = ResourceContext::create_shared();
rc->memory_context()->set_mem_tracker(mem_tracker);
init(rc);
}
AttachTask::AttachTask(RuntimeState* runtime_state) {
// Walk the chain `runtime_state -> get_query_ctx() -> resource_ctx()`
// step by step so that an unexpected null pinpoints exactly which link
// failed instead of crashing with a generic NPE inside attach_task() or
// even later inside the allocator.
if (UNLIKELY(runtime_state == nullptr)) {
throw Exception(Status::FatalError(
"AttachTask(RuntimeState*): runtime_state is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
if (UNLIKELY(runtime_state->get_query_ctx() == nullptr)) {
throw Exception(Status::FatalError(
"AttachTask(RuntimeState*): runtime_state->get_query_ctx() is null. "
"signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
if (UNLIKELY(runtime_state->get_query_ctx()->resource_ctx() == nullptr)) {
throw Exception(
Status::FatalError("AttachTask(RuntimeState*): query_ctx->resource_ctx() is null. "
"signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
signal::set_signal_is_nereids(runtime_state->is_nereids());
init(runtime_state->get_query_ctx()->resource_ctx());
}
AttachTask::AttachTask(QueryContext* query_ctx) {
if (UNLIKELY(query_ctx == nullptr)) {
throw Exception(Status::FatalError(
"AttachTask(QueryContext*): query_ctx is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
init(query_ctx->resource_ctx());
}
AttachTask::~AttachTask() {
signal::set_signal_task_id(TUniqueId());
thread_context()->detach_task();
ThreadLocalHandle::del_thread_local_if_count_is_zero();
}
SwitchResourceContext::SwitchResourceContext(const std::shared_ptr<ResourceContext>& rc) {
DCHECK(rc != nullptr);
// Validate the chain before mutating any thread-local or signal state,
// symmetric to AttachTask::init(). Throwing after the thread-local
// handle was acquired or the signal task id was set would skip this
// object's destructor (because construction failed) and leak the
// handle / leave a stale signal task id behind.
if (UNLIKELY(rc == nullptr)) {
throw Exception(
Status::FatalError("SwitchResourceContext: rc is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
if (UNLIKELY(rc->memory_context() == nullptr)) {
throw Exception(Status::FatalError(
"SwitchResourceContext: rc->memory_context() is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
if (UNLIKELY(rc->memory_context()->mem_tracker() == nullptr)) {
throw Exception(Status::FatalError(
"SwitchResourceContext: rc->memory_context()->mem_tracker() is null. "
"ResourceContext was switched in before _init_query_mem_tracker ran. "
"signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
if (UNLIKELY(rc->task_controller() == nullptr)) {
throw Exception(Status::FatalError(
"SwitchResourceContext: rc->task_controller() is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
doris::ThreadLocalHandle::create_thread_local_if_not_exits();
DCHECK(thread_context()->is_attach_task());
old_resource_ctx_ = thread_context()->resource_ctx();
if (rc != old_resource_ctx_) {
signal::set_signal_task_id(rc->task_controller()->task_id());
thread_context()->resource_ctx_ = rc;
thread_context()->thread_mem_tracker_mgr->attach_limiter_tracker(
rc->memory_context()->mem_tracker(), rc->workload_group());
}
}
SwitchResourceContext::~SwitchResourceContext() {
if (old_resource_ctx_ != thread_context()->resource_ctx()) {
DCHECK(old_resource_ctx_ != nullptr);
signal::set_signal_task_id(old_resource_ctx_->task_controller()->task_id());
thread_context()->resource_ctx_ = old_resource_ctx_;
thread_context()->thread_mem_tracker_mgr->detach_limiter_tracker();
}
doris::ThreadLocalHandle::del_thread_local_if_count_is_zero();
}
SwitchThreadMemTrackerLimiter::SwitchThreadMemTrackerLimiter(
const std::shared_ptr<doris::MemTrackerLimiter>& mem_tracker) {
DCHECK(mem_tracker);
// Third entry point that calls attach_limiter_tracker(). Without this
// null guard a null mem_tracker silently propagates and the next
// allocation on this thread would NPE deep inside the allocator. Throw
// before acquiring the thread-local handle / doing any side effect so
// the destructor (which is noexcept) never runs in a dirty state.
if (UNLIKELY(mem_tracker == nullptr)) {
throw Exception(Status::FatalError(
"SwitchThreadMemTrackerLimiter: mem_tracker is null. signal_query_id={:x}-{:x}",
signal::query_id_hi, signal::query_id_lo));
}
doris::ThreadLocalHandle::create_thread_local_if_not_exits();
if (mem_tracker != thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker_sptr()) {
thread_context()->thread_mem_tracker_mgr->attach_limiter_tracker(mem_tracker);
is_switched_ = true;
}
}
SwitchThreadMemTrackerLimiter::~SwitchThreadMemTrackerLimiter() {
if (is_switched_) {
thread_context()->thread_mem_tracker_mgr->detach_limiter_tracker();
}
doris::ThreadLocalHandle::del_thread_local_if_count_is_zero();
}
AddThreadMemTrackerConsumer::AddThreadMemTrackerConsumer(MemTracker* mem_tracker) {
ThreadLocalHandle::create_thread_local_if_not_exits();
if (mem_tracker) {
_need_pop = thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(mem_tracker);
}
}
AddThreadMemTrackerConsumer::AddThreadMemTrackerConsumer(
const std::shared_ptr<MemTracker>& mem_tracker)
: _mem_tracker(mem_tracker) {
ThreadLocalHandle::create_thread_local_if_not_exits();
if (_mem_tracker) {
_need_pop =
thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(_mem_tracker.get());
}
}
AddThreadMemTrackerConsumer::~AddThreadMemTrackerConsumer() {
if (_need_pop) {
thread_context()->thread_mem_tracker_mgr->pop_consumer_tracker();
}
ThreadLocalHandle::del_thread_local_if_count_is_zero();
}
} // namespace doris