forked from pingcap/tiflash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentReader.cpp
More file actions
218 lines (194 loc) · 6.66 KB
/
SegmentReader.cpp
File metadata and controls
218 lines (194 loc) · 6.66 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
// Copyright 2023 PingCAP, Inc.
//
// Licensed 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 <Common/Logger.h>
#include <Common/setThreadName.h>
#include <Storages/DeltaMerge/ReadThread/CPU.h>
#include <Storages/DeltaMerge/ReadThread/SegmentReadTaskScheduler.h>
#include <Storages/DeltaMerge/ReadThread/SegmentReader.h>
#include <Storages/DeltaMerge/SegmentReadTaskPool.h>
#include <ext/scope_guard.h>
namespace DB::DM
{
class SegmentReader
{
inline static const std::string name{"SegmentReader"};
public:
SegmentReader(WorkQueue<MergedTaskPtr> & task_queue_, const std::vector<int> & cpus_)
: task_queue(task_queue_)
, stop(false)
, log(Logger::get())
, cpus(cpus_)
{
t = std::thread(&SegmentReader::run, this);
}
void setStop() { stop.store(true, std::memory_order_relaxed); }
~SegmentReader()
{
t.join();
LOG_DEBUG(log, "Stopped");
}
std::thread::id getId() const { return t.get_id(); }
private:
bool isStop() { return stop.load(std::memory_order_relaxed); }
void readSegments()
{
MergedTaskPtr merged_task;
try
{
if (!task_queue.pop(merged_task))
{
LOG_INFO(log, "Pop fail, stop={}", isStop());
return;
}
int read_count = 0;
while (!merged_task->allStreamsFinished() && !isStop())
{
auto c = merged_task->readBlock();
read_count += c;
if (c <= 0)
{
break;
}
}
if (read_count <= 0)
{
LOG_DEBUG(
log,
"All finished, pool_ids={} segment_id={} read_count={}",
merged_task->getPoolIds(),
merged_task->getSegmentId(),
read_count);
}
// If `merged_task` is pushed back to `MergedTaskPool`, it can be accessed by another read thread if it is scheduled.
// So do not push back to `MergedTaskPool` when exception happened since current read thread can still access to this `merged_task` object and set exception message to it.
// If exception happens, `merged_task` will be released by `shared_ptr` automatically.
if (!merged_task->allStreamsFinished())
{
SegmentReadTaskScheduler::instance().pushMergedTask(merged_task);
}
}
catch (DB::Exception & e)
{
LOG_ERROR(log, "ErrMsg: {} StackTrace {}", e.message(), e.getStackTrace().toString());
if (merged_task != nullptr)
{
merged_task->setException(e);
}
}
catch (std::exception & e)
{
LOG_ERROR(log, "ErrMsg: {}", e.what());
if (merged_task != nullptr)
{
merged_task->setException(DB::Exception(e.what()));
}
}
catch (...)
{
tryLogCurrentException("exception thrown in SegmentReader");
if (merged_task != nullptr)
{
merged_task->setException(DB::Exception("unknown exception thrown in SegmentReader"));
}
}
}
void run()
{
setCPUAffinity(cpus, log);
setThreadName(name.c_str());
while (!isStop())
{
readSegments();
}
}
WorkQueue<MergedTaskPtr> & task_queue;
std::atomic<bool> stop;
LoggerPtr log;
std::thread t;
std::vector<int> cpus;
};
// ===== SegmentReaderPool ===== //
void SegmentReaderPool::addTask(MergedTaskPtr && task)
{
if (!task_queue.push(std::forward<MergedTaskPtr>(task), nullptr))
{
throw Exception("addTask fail");
}
}
SegmentReaderPool::SegmentReaderPool(int thread_count, const std::vector<int> & cpus)
: log(Logger::get())
{
LOG_INFO(log, "Create start, thread_count={} cpus={}", thread_count, cpus);
for (int i = 0; i < thread_count; i++)
{
readers.push_back(std::make_unique<SegmentReader>(task_queue, cpus));
}
LOG_INFO(log, "Create end, thread_count={} cpus={}", thread_count, cpus);
}
SegmentReaderPool::~SegmentReaderPool()
{
for (auto & reader : readers)
{
reader->setStop();
}
task_queue.finish();
}
std::vector<std::thread::id> SegmentReaderPool::getReaderIds() const
{
std::vector<std::thread::id> ids;
for (const auto & r : readers)
{
ids.push_back(r->getId());
}
return ids;
}
// ===== SegmentReaderPoolManager ===== //
SegmentReaderPoolManager::SegmentReaderPoolManager()
: log(Logger::get())
{}
SegmentReaderPoolManager::~SegmentReaderPoolManager() = default;
void SegmentReaderPoolManager::init(UInt32 logical_cpu_cores, double read_thread_count_scale)
{
double total_thread_count = logical_cpu_cores * read_thread_count_scale;
auto numa_nodes = getNumaNodes(log);
RUNTIME_CHECK(!numa_nodes.empty());
UInt32 thread_count_per_node = std::ceil(total_thread_count / numa_nodes.size());
for (const auto & node : numa_nodes)
{
reader_pools.push_back(std::make_unique<SegmentReaderPool>(thread_count_per_node, node));
auto ids = reader_pools.back()->getReaderIds();
reader_ids.insert(ids.begin(), ids.end());
}
LOG_INFO(log, "numa_nodes={} number_of_readers={}", numa_nodes, reader_ids.size());
}
void SegmentReaderPoolManager::addTask(MergedTaskPtr && task)
{
static std::hash<uint64_t> hash_func;
auto idx = hash_func(task->getSegmentId()) % reader_pools.size();
reader_pools[idx]->addTask(std::move(task));
}
// `isSegmentReader` checks whether this thread is a `SegmentReader`.
// Use this function in DMFileBlockInputSteam to check whether enable read thread of this read request,
// Maybe we can pass the argument from DeltaMerge -> SegmentReadTaskPool -> ... -> DMFileBlockInputSteam.
// But this is a long code path and can affect a lot a code.
bool SegmentReaderPoolManager::isSegmentReader() const
{
return reader_ids.find(std::this_thread::get_id()) != reader_ids.end();
}
void SegmentReaderPoolManager::stop()
{
reader_pools.clear();
reader_ids.clear();
}
} // namespace DB::DM