forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckpointBuilder.cpp
More file actions
346 lines (310 loc) · 11.6 KB
/
CheckpointBuilder.cpp
File metadata and controls
346 lines (310 loc) · 11.6 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "history/CheckpointBuilder.h"
#include "history/HistoryArchiveManager.h"
#include "history/HistoryManager.h"
#include "ledger/LedgerManager.h"
#include "main/Application.h"
#include "main/ErrorMessages.h"
#include "util/XDRStream.h"
namespace stellar
{
void
CheckpointBuilder::ensureOpen(uint32_t ledgerSeq)
{
ZoneScoped;
releaseAssert(mApp.getHistoryArchiveManager().publishEnabled());
mSkipFirstCheckpointSinceItIsIncomplete = false;
if (!mOpen)
{
releaseAssert(!mTxResults);
releaseAssert(!mTxs);
releaseAssert(!mLedgerHeaders);
auto checkpoint = HistoryManager::checkpointContainingLedger(
ledgerSeq, mApp.getConfig());
auto res = FileTransferInfo(FileType::HISTORY_FILE_TYPE_RESULTS,
checkpoint, mApp.getConfig());
auto txs = FileTransferInfo(FileType::HISTORY_FILE_TYPE_TRANSACTIONS,
checkpoint, mApp.getConfig());
auto ledger = FileTransferInfo(FileType::HISTORY_FILE_TYPE_LEDGER,
checkpoint, mApp.getConfig());
// Open files in append mode
mTxResults = std::make_unique<XDROutputFileStream>(
mApp.getClock().getIOContext(), /* fsync*/ true);
mTxResults->open(res.localPath_nogz_dirty());
mTxs = std::make_unique<XDROutputFileStream>(
mApp.getClock().getIOContext(), /* fsync*/ true);
mTxs->open(txs.localPath_nogz_dirty());
mLedgerHeaders = std::make_unique<XDROutputFileStream>(
mApp.getClock().getIOContext(), /* fsync*/ true);
mLedgerHeaders->open(ledger.localPath_nogz_dirty());
mOpen = true;
}
}
void
CheckpointBuilder::checkpointComplete(uint32_t checkpoint)
{
ZoneScoped;
releaseAssert(mApp.getHistoryArchiveManager().publishEnabled());
releaseAssert(
HistoryManager::isLastLedgerInCheckpoint(checkpoint, mApp.getConfig()));
// This will close and reset the streams
mLedgerHeaders.reset();
mTxs.reset();
mTxResults.reset();
mOpen = false;
auto maybeRename = [&](FileTransferInfo const& ft) {
if (fs::exists(ft.localPath_nogz()))
{
CLOG_INFO(History, "File {} already exists, skipping rename",
ft.localPath_nogz());
}
else if (fs::exists(ft.localPath_nogz_dirty()) &&
!fs::durableRename(
ft.localPath_nogz_dirty(), ft.localPath_nogz(),
getPublishHistoryDir(ft.getType(), mApp.getConfig())
.string()))
{
throw std::runtime_error(
fmt::format("Failed to rename checkpoint file {}",
ft.localPath_nogz_dirty()));
}
};
auto res = FileTransferInfo(FileType::HISTORY_FILE_TYPE_RESULTS, checkpoint,
mApp.getConfig());
auto txs = FileTransferInfo(FileType::HISTORY_FILE_TYPE_TRANSACTIONS,
checkpoint, mApp.getConfig());
auto ledger = FileTransferInfo(FileType::HISTORY_FILE_TYPE_LEDGER,
checkpoint, mApp.getConfig());
maybeRename(res);
maybeRename(txs);
maybeRename(ledger);
}
CheckpointBuilder::CheckpointBuilder(Application& app) : mApp(app)
{
}
void
CheckpointBuilder::appendTransactionSet(uint32_t ledgerSeq,
TxSetXDRFrameConstPtr const& txSet,
TransactionResultSet const& resultSet,
bool skipStartupCheck)
{
ZoneScoped;
TransactionHistoryEntry txs;
txs.ledgerSeq = ledgerSeq;
if (txSet->isGeneralizedTxSet())
{
txs.ext.v(1);
txSet->toXDR(txs.ext.generalizedTxSet());
}
else
{
txSet->toXDR(txs.txSet);
}
appendTransactionSet(ledgerSeq, txs, resultSet);
}
void
CheckpointBuilder::appendTransactionSet(uint32_t ledgerSeq,
TransactionHistoryEntry const& txSet,
TransactionResultSet const& resultSet,
bool skipStartupCheck)
{
ZoneScoped;
if (!mStartupValidationComplete &&
ledgerSeq != LedgerManager::GENESIS_LEDGER_SEQ && !skipStartupCheck)
{
throw std::runtime_error("Startup validation not performed");
}
// Skip incomplete checkpoint if we're not on the checkpoint boundary
if (skipIncompleteFirstCheckpointSinceRestart() &&
!HistoryManager::isFirstLedgerInCheckpoint(ledgerSeq, mApp.getConfig()))
{
return;
}
ensureOpen(ledgerSeq);
if (!resultSet.results.empty())
{
TransactionHistoryResultEntry results;
results.ledgerSeq = ledgerSeq;
results.txResultSet = resultSet;
mTxResults->durableWriteOne(results);
mTxs->durableWriteOne(txSet);
}
}
void
CheckpointBuilder::appendLedgerHeader(LedgerHeader const& header,
bool skipStartupCheck)
{
ZoneScoped;
if (!mStartupValidationComplete &&
header.ledgerSeq != LedgerManager::GENESIS_LEDGER_SEQ &&
!skipStartupCheck)
{
throw std::runtime_error("Startup validation not performed");
}
// Skip incomplete checkpoint if we're not on the checkpoint boundary
if (skipIncompleteFirstCheckpointSinceRestart() &&
!HistoryManager::isFirstLedgerInCheckpoint(header.ledgerSeq,
mApp.getConfig()))
{
return;
}
ensureOpen(header.ledgerSeq);
LedgerHeaderHistoryEntry lhe;
lhe.header = header;
lhe.hash = xdrSha256(header);
mLedgerHeaders->durableWriteOne(lhe);
}
namespace
{
uint32_t
getLedgerSeq(TransactionHistoryEntry const& entry)
{
return entry.ledgerSeq;
}
uint32_t
getLedgerSeq(TransactionHistoryResultEntry const& entry)
{
return entry.ledgerSeq;
}
uint32_t
getLedgerSeq(LedgerHeaderHistoryEntry const& entry)
{
return entry.header.ledgerSeq;
}
} // namespace
void
CheckpointBuilder::cleanup(uint32_t lcl)
{
if (mStartupValidationComplete)
{
return;
}
mTxResults.reset();
mTxs.reset();
mLedgerHeaders.reset();
mOpen = false;
auto const& cfg = mApp.getConfig();
auto checkpoint = HistoryManager::checkpointContainingLedger(lcl, cfg);
auto res =
FileTransferInfo(FileType::HISTORY_FILE_TYPE_RESULTS, checkpoint, cfg);
auto txs = FileTransferInfo(FileType::HISTORY_FILE_TYPE_TRANSACTIONS,
checkpoint, cfg);
auto ledger =
FileTransferInfo(FileType::HISTORY_FILE_TYPE_LEDGER, checkpoint, cfg);
auto tmpDir =
mApp.getBucketManager().getTmpDirManager().tmpDir("truncated");
auto recover = [&](FileTransferInfo const& ft, auto entry,
uint32_t enforceLCL) {
if (fs::exists(ft.localPath_nogz()))
{
// Make sure any new checkpoints are deleted
auto next = FileTransferInfo(
ft.getType(),
HistoryManager::checkpointContainingLedger(checkpoint + 1, cfg),
cfg);
CLOG_INFO(History, "Deleting next checkpoint files {}",
next.localPath_nogz_dirty());
// This may fail if no files were created before core restarted
// (which is a valid behavior)
if (std::remove(next.localPath_nogz_dirty().c_str()) &&
errno != ENOENT)
{
throw std::runtime_error(
fmt::format("Failed to delete next checkpoint file {}",
next.localPath_nogz_dirty()));
}
return true;
}
if (!fs::exists(ft.localPath_nogz_dirty()))
{
CLOG_INFO(History,
"Skipping recovery of file {}, does not exist. This can "
"occur if publish was previously disabled.",
ft.localPath_nogz_dirty());
return false;
}
// Find a tmp file; any potentially invalid files _must_ be tmp files,
// because checkpoint files are finalized (renamed) only after ledger is
// committed.
std::filesystem::path dir = tmpDir.getName();
std::filesystem::path tmpFile = dir / ft.baseName_nogz();
{
auto out = XDROutputFileStream(mApp.getClock().getIOContext(),
/* fsync*/ true);
out.open(tmpFile.string());
XDRInputFileStream in;
in.open(ft.localPath_nogz_dirty());
uint32_t lastWrittenLedgerSeq = 0;
while (in)
{
try
{
if (!in.readOne(entry))
{
break;
}
auto seq = getLedgerSeq(entry);
if (seq > lcl)
{
CLOG_INFO(History, "Truncating {} at ledger {}",
ft.localPath_nogz_dirty(), lcl);
break;
}
out.durableWriteOne(entry);
lastWrittenLedgerSeq = seq;
}
catch (xdr::xdr_runtime_error const& e)
{
// If we can't read the entry, we likely encountered a
// partial write
CLOG_INFO(History,
"Encountered partial write in {}, truncating",
ft.localPath_nogz_dirty());
break;
}
}
// Once we finished truncating, check that we ended on LCL.
// If file doesn't end on LCL, it's corrupt
if (enforceLCL && lastWrittenLedgerSeq != lcl)
{
throw std::runtime_error(fmt::format(
"Corrupt checkpoint file {}, ends "
"on ledger {}, LCL is {}",
ft.localPath_nogz_dirty(), lastWrittenLedgerSeq, lcl));
}
}
if (!fs::durableRename(
tmpFile.string(), ft.localPath_nogz_dirty(),
getPublishHistoryDir(ft.getType(), mApp.getConfig()).string()))
{
throw std::runtime_error("Failed to rename checkpoint file");
}
return true;
};
// We can only require ledger header to be at LCL; transactions and results
// can have gaps (if there were empty ledgers)
auto resExists =
recover(res, TransactionHistoryResultEntry{}, /* enforceLCL */ false);
auto txExists =
recover(txs, TransactionHistoryEntry{}, /* enforceLCL */ false);
auto headerExists =
recover(ledger, LedgerHeaderHistoryEntry{}, /* enforceLCL */ true);
if (!resExists && !txExists && !headerExists)
{
mSkipFirstCheckpointSinceItIsIncomplete = true;
CLOG_INFO(History, "No checkpoint files found during recovery, likely "
"publish was previously disabled");
}
else if (!(resExists && txExists && headerExists))
{
std::string errMsg =
fmt::format("Some checkpoint files were not found during recovery, "
"results={}, transactions={}, headers={}.",
resExists, txExists, headerExists);
CLOG_ERROR(History, "{}", errMsg);
CLOG_ERROR(History, "Delete incomplete checkpoint files and restart.");
CLOG_ERROR(History, "{}", REPORT_INTERNAL_BUG);
throw std::runtime_error(errMsg);
}
mStartupValidationComplete = true;
}
}