MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / workerFunction

Method workerFunction

src/Interpreters/CancellationChecker.cpp:125–194  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

123}
124
125void CancellationChecker::workerFunction()
126{
127 LOG_TRACE(log, "Started worker function");
128 std::vector<QueryToTrack> tasks_to_cancel;
129
130 std::unique_lock<std::mutex> lock(m);
131
132 while (!stop_thread)
133 {
134 UInt64 now_ms = 0;
135 if (!query_set.empty())
136 {
137 auto now = std::chrono::steady_clock::now();
138 now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
139
140 /// Batch all tasks that have reached their deadline.
141 /// Since deadlines are aligned to a grid, multiple tasks often expire together.
142 while (!query_set.empty())
143 {
144 auto next_task_it = query_set.begin();
145 if (next_task_it->endtime > now_ms || next_task_it->timeout == 0)
146 break;
147
148 LOG_DEBUG(
149 log,
150 "Cancelling the task because of the timeout: {} ms, query_id: {}",
151 next_task_it->timeout,
152 next_task_it->query->getClientInfo().current_query_id);
153
154 tasks_to_cancel.push_back(*next_task_it);
155 query_set.erase(next_task_it);
156 }
157 }
158
159 if (!tasks_to_cancel.empty())
160 {
161 lock.unlock();
162 std::ranges::for_each(tasks_to_cancel, cancelTask);
163 tasks_to_cancel.clear();
164 lock.lock();
165 continue;
166 }
167
168 /// if there are no queries,
169 /// wakeup on first query that was added so we can setup
170 /// proper timeout for waking up the thread
171 if (query_set.empty())
172 {
173 cond_var.wait(lock, [&] { return stop_thread || !query_set.empty(); });
174 }
175 else
176 {
177 chassert(!query_set.empty());
178 cond_var.wait_for(
179 lock,
180 std::chrono::milliseconds(query_set.begin()->endtime - now_ms),
181 [&] {
182 /// Use fresh time to avoid spinning when the predicate is re-evaluated after spurious wakeups.

Callers 2

mainMethod · 0.80
TESTFunction · 0.80

Calls 10

nowFunction · 0.50
emptyMethod · 0.45
countMethod · 0.45
beginMethod · 0.45
push_backMethod · 0.45
eraseMethod · 0.45
unlockMethod · 0.45
clearMethod · 0.45
lockMethod · 0.45
waitMethod · 0.45

Tested by 1

TESTFunction · 0.64