-
Notifications
You must be signed in to change notification settings - Fork 412
/
SSTFilesToDTFilesOutputStream.cpp
233 lines (211 loc) · 7.73 KB
/
SSTFilesToDTFilesOutputStream.cpp
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
227
228
229
230
231
232
233
#include <Common/ProfileEvents.h>
#include <Common/TiFlashMetrics.h>
#include <Interpreters/Context.h>
#include <Poco/File.h>
#include <RaftStoreProxyFFI/ColumnFamily.h>
#include <Storages/DeltaMerge/DeltaMergeStore.h>
#include <Storages/DeltaMerge/File/DMFile.h>
#include <Storages/DeltaMerge/File/DMFileBlockOutputStream.h>
#include <Storages/DeltaMerge/SSTFilesToDTFilesOutputStream.h>
#include <Storages/StorageDeltaMerge.h>
#include <Storages/Transaction/PartitionStreams.h>
#include <Storages/Transaction/ProxyFFI.h>
#include <Storages/Transaction/Region.h>
#include <Storages/Transaction/SSTReader.h>
#include <Storages/Transaction/TMTContext.h>
#include <common/logger_useful.h>
namespace ProfileEvents
{
extern const Event DMWriteBytes;
}
namespace DB
{
namespace ErrorCodes
{
extern const int ILLFORMAT_RAFT_ROW;
} // namespace ErrorCodes
namespace DM
{
SSTFilesToDTFilesOutputStream::SSTFilesToDTFilesOutputStream( //
BoundedSSTFilesToBlockInputStreamPtr child_,
StorageDeltaMergePtr storage_,
DecodingStorageSchemaSnapshotConstPtr schema_snap_,
TiDB::SnapshotApplyMethod method_,
FileConvertJobType job_type_,
TMTContext & tmt_)
: child(std::move(child_))
, //
storage(std::move(storage_))
, schema_snap(std::move(schema_snap_))
, method(method_)
, job_type(job_type_)
, tmt(tmt_)
, log(&Poco::Logger::get("SSTFilesToDTFilesOutputStream"))
{
}
SSTFilesToDTFilesOutputStream::~SSTFilesToDTFilesOutputStream() = default;
void SSTFilesToDTFilesOutputStream::writePrefix()
{
child->readPrefix();
commit_rows = 0;
watch.start();
}
void SSTFilesToDTFilesOutputStream::writeSuffix()
{
child->readSuffix();
if (dt_stream != nullptr)
{
dt_stream->writeSuffix();
auto dt_file = dt_stream->getFile();
assert(!dt_file->canGC()); // The DTFile should not be able to gc until it is ingested.
// Add the DTFile to StoragePathPool so that we can restore it later
const auto bytes_written = dt_file->getBytesOnDisk();
storage->getStore()->preIngestFile(dt_file->parentPath(), dt_file->fileId(), bytes_written);
// Report DMWriteBytes for calculating write amplification
ProfileEvents::increment(ProfileEvents::DMWriteBytes, bytes_written);
dt_stream.reset();
}
const auto process_keys = child->getProcessKeys();
if (job_type == FileConvertJobType::ApplySnapshot)
{
GET_METRIC(tiflash_raft_command_duration_seconds, type_apply_snapshot_predecode).Observe(watch.elapsedSeconds());
// Note that number of keys in different cf will be aggregated into one metrics
GET_METRIC(tiflash_raft_process_keys, type_apply_snapshot).Increment(process_keys.total());
}
else
{
// Note that number of keys in different cf will be aggregated into one metrics
GET_METRIC(tiflash_raft_process_keys, type_ingest_sst).Increment(process_keys.total());
}
LOG_FMT_INFO(
log,
"Pre-handle snapshot {} to {} DTFiles, cost {}ms [rows={}] [write_cf_keys={}] [default_cf_keys={}] [lock_cf_keys={}]",
child->getRegion()->toString(true),
ingest_files.size(),
watch.elapsedMilliseconds(),
commit_rows,
process_keys.write_cf,
process_keys.default_cf,
process_keys.lock_cf);
}
bool SSTFilesToDTFilesOutputStream::newDTFileStream()
{
// Generate a DMFilePtr and its DMFileBlockOutputStream
DMFileBlockOutputStream::Flags flags;
switch (method)
{
case TiDB::SnapshotApplyMethod::DTFile_Directory:
flags.setSingleFile(false);
break;
case TiDB::SnapshotApplyMethod::DTFile_Single:
flags.setSingleFile(true);
break;
default:
break;
}
// The parent_path and file_id are generated by the storage.
auto [parent_path, file_id] = storage->getStore()->preAllocateIngestFile();
if (parent_path.empty())
{
// Can no allocate path and id for storing DTFiles (the storage may be dropped / shutdown)
return false;
}
auto dt_file = DMFile::create(file_id, parent_path, flags.isSingleFile(), storage->createChecksumConfig(flags.isSingleFile()));
LOG_FMT_INFO(
log,
"Create file for snapshot data {} [file={}] [single_file_mode={}]",
child->getRegion()->toString(true),
dt_file->path(),
flags.isSingleFile());
dt_stream = std::make_unique<DMFileBlockOutputStream>(tmt.getContext(), dt_file, *(schema_snap->column_defines), flags);
dt_stream->writePrefix();
ingest_files.emplace_back(dt_file);
return true;
}
void SSTFilesToDTFilesOutputStream::write()
{
size_t last_effective_num_rows = 0;
size_t last_not_clean_rows = 0;
size_t cur_effective_num_rows = 0;
size_t cur_not_clean_rows = 0;
while (true)
{
Block block = child->read();
if (!block)
break;
if (unlikely(block.rows() == 0))
continue;
if (dt_stream == nullptr)
{
// If can not create DTFile stream (the storage may be dropped / shutdown),
// break the writing loop.
if (bool ok = newDTFileStream(); !ok)
{
break;
}
}
{
// Check whether rows are sorted by handle & version in ascending order.
SortDescription sort;
sort.emplace_back(MutableSupport::tidb_pk_column_name, 1, 0);
sort.emplace_back(MutableSupport::version_column_name, 1, 0);
if (unlikely(block.rows() > 1 && !isAlreadySorted(block, sort)))
{
const String error_msg
= fmt::format("The block decoded from SSTFile is not sorted by primary key and version {}", child->getRegion()->toString(true));
LOG_ERROR(log, error_msg);
FieldVisitorToString visitor;
const size_t nrows = block.rows();
for (size_t i = 0; i < nrows; ++i)
{
const auto & pk_col = block.getByName(MutableSupport::tidb_pk_column_name);
const auto & ver_col = block.getByName(MutableSupport::version_column_name);
LOG_FMT_ERROR(
log,
"[Row={}/{}] [pk={}] [ver={}]",
i,
nrows,
applyVisitor(visitor, (*pk_col.column)[i]),
applyVisitor(visitor, (*ver_col.column)[i]));
}
throw Exception(error_msg);
}
}
// Write block to the output stream
DMFileBlockOutputStream::BlockProperty property;
std::tie(cur_effective_num_rows, cur_not_clean_rows, property.gc_hint_version) //
= child->getMvccStatistics();
property.effective_num_rows = cur_effective_num_rows - last_effective_num_rows;
property.not_clean_rows = cur_not_clean_rows - last_not_clean_rows;
dt_stream->write(block, property);
commit_rows += block.rows();
last_effective_num_rows = cur_effective_num_rows;
last_not_clean_rows = cur_not_clean_rows;
}
}
PageIds SSTFilesToDTFilesOutputStream::ingestIds() const
{
PageIds ids;
for (const auto & file : ingest_files)
{
ids.emplace_back(file->fileId());
}
return ids;
}
void SSTFilesToDTFilesOutputStream::cancel()
{
// Try a lightweight cleanup the file generated by this stream (marking them able to be GC-ed).
for (auto & file : ingest_files)
{
try
{
file->enableGC();
}
catch (...)
{
tryLogCurrentException(log, fmt::format("ignore exception while canceling SST files to DeltaTree files stream [file={}]", file->path()));
}
}
}
} // namespace DM
} // namespace DB