forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoryArchive.cpp
387 lines (349 loc) · 9.23 KB
/
HistoryArchive.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright 2014 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
// ASIO is somewhat particular about when it gets included -- it wants to be the
// first to include <windows.h> -- so we try to include it before everything
// else.
#include "util/asio.h"
#include "history/HistoryArchive.h"
#include "bucket/Bucket.h"
#include "bucket/BucketList.h"
#include "crypto/Hex.h"
#include "crypto/SHA.h"
#include "history/HistoryManager.h"
#include "lib/util/format.h"
#include "main/Application.h"
#include "main/StellarCoreVersion.h"
#include "process/ProcessManager.h"
#include "util/Fs.h"
#include "util/Logging.h"
#include <cereal/archives/json.hpp>
#include <cereal/cereal.hpp>
#include <cereal/types/vector.hpp>
#include <chrono>
#include <fstream>
#include <future>
#include <iostream>
#include <medida/meter.h>
#include <medida/metrics_registry.h>
#include <set>
#include <sstream>
namespace stellar
{
unsigned const HistoryArchiveState::HISTORY_ARCHIVE_STATE_VERSION = 1;
template <typename... Tokens>
std::string
formatString(std::string const& templateString, Tokens const&... tokens)
{
try
{
return fmt::format(templateString, tokens...);
}
catch (fmt::FormatError const& ex)
{
CLOG(ERROR, "History") << "Failed to format string \"" << templateString
<< "\":" << ex.what();
CLOG(ERROR, "History")
<< "Check your HISTORY entry in configuration file";
throw std::runtime_error("failed to format command string");
}
}
bool
HistoryArchiveState::futuresAllReady() const
{
for (auto const& level : currentBuckets)
{
if (level.next.isMerging())
{
if (!level.next.mergeComplete())
{
return false;
}
}
}
return true;
}
bool
HistoryArchiveState::futuresAllResolved() const
{
for (auto const& level : currentBuckets)
{
if (level.next.isMerging())
{
return false;
}
}
return true;
}
void
HistoryArchiveState::resolveAllFutures()
{
for (auto& level : currentBuckets)
{
if (level.next.isMerging())
{
level.next.resolve();
}
}
}
void
HistoryArchiveState::resolveAnyReadyFutures()
{
for (auto& level : currentBuckets)
{
if (level.next.isMerging() && level.next.mergeComplete())
{
level.next.resolve();
}
}
}
void
HistoryArchiveState::save(std::string const& outFile) const
{
assert(futuresAllResolved());
std::ofstream out(outFile);
cereal::JSONOutputArchive ar(out);
serialize(ar);
}
std::string
HistoryArchiveState::toString() const
{
std::ostringstream out;
{
cereal::JSONOutputArchive ar(out);
serialize(ar);
}
return out.str();
}
void
HistoryArchiveState::load(std::string const& inFile)
{
std::ifstream in(inFile);
cereal::JSONInputArchive ar(in);
serialize(ar);
if (version != HISTORY_ARCHIVE_STATE_VERSION)
{
CLOG(ERROR, "History")
<< "Unexpected history archive state version: " << version;
throw std::runtime_error("unexpected history archive state version");
}
assert(futuresAllResolved());
}
void
HistoryArchiveState::fromString(std::string const& str)
{
std::istringstream in(str);
cereal::JSONInputArchive ar(in);
serialize(ar);
assert(futuresAllResolved());
}
std::string
HistoryArchiveState::baseName()
{
return std::string("stellar-history.json");
}
std::string
HistoryArchiveState::wellKnownRemoteDir()
{
// The RFC 5785 dir
return std::string(".well-known");
}
std::string
HistoryArchiveState::wellKnownRemoteName()
{
return wellKnownRemoteDir() + "/" + baseName();
}
std::string
HistoryArchiveState::remoteDir(uint32_t snapshotNumber)
{
return fs::remoteDir("history", fs::hexStr(snapshotNumber));
}
std::string
HistoryArchiveState::remoteName(uint32_t snapshotNumber)
{
return fs::remoteName("history", fs::hexStr(snapshotNumber), "json");
}
std::string
HistoryArchiveState::localName(Application& app, std::string const& archiveName)
{
return app.getHistoryManager().localFilename(archiveName + "-" +
baseName());
}
Hash
HistoryArchiveState::getBucketListHash()
{
// NB: This hash algorithm has to match "what the BucketList does" to
// calculate its BucketList hash exactly. It's not a particularly complex
// algorithm -- just hash all the hashes of all the bucket levels, in order,
// with each level-hash being the hash of its curr bucket then its snap
// bucket -- but we duplicate the logic here because it honestly seems like
// it'd be less readable to try to abstract the code between the two
// relatively-different representations. Everything will explode if there is
// any difference in these algorithms anyways, so..
auto totalHash = SHA256::create();
for (auto const& level : currentBuckets)
{
auto levelHash = SHA256::create();
levelHash->add(hexToBin(level.curr));
levelHash->add(hexToBin(level.snap));
totalHash->add(levelHash->finish());
}
return totalHash->finish();
}
std::vector<std::string>
HistoryArchiveState::differingBuckets(HistoryArchiveState const& other) const
{
assert(futuresAllResolved());
std::set<std::string> inhibit;
uint256 zero;
inhibit.insert(binToHex(zero));
for (auto b : other.currentBuckets)
{
inhibit.insert(b.curr);
if (b.next.isLive())
{
b.next.resolve();
}
if (b.next.hasOutputHash())
{
inhibit.insert(b.next.getOutputHash());
}
inhibit.insert(b.snap);
}
std::vector<std::string> ret;
for (size_t i = BucketList::kNumLevels; i != 0; --i)
{
auto s = currentBuckets[i - 1].snap;
auto n = s;
if (currentBuckets[i - 1].next.hasOutputHash())
{
n = currentBuckets[i - 1].next.getOutputHash();
}
auto c = currentBuckets[i - 1].curr;
auto bs = {s, n, c};
for (auto const& j : bs)
{
if (inhibit.find(j) == inhibit.end())
{
ret.push_back(j);
inhibit.insert(j);
}
}
}
return ret;
}
std::vector<std::string>
HistoryArchiveState::allBuckets() const
{
std::set<std::string> buckets;
for (auto const& level : currentBuckets)
{
buckets.insert(level.curr);
buckets.insert(level.snap);
auto nh = level.next.getHashes();
buckets.insert(nh.begin(), nh.end());
}
return std::vector<std::string>(buckets.begin(), buckets.end());
}
HistoryArchiveState::HistoryArchiveState() : server(STELLAR_CORE_VERSION)
{
uint256 u;
std::string s = binToHex(u);
HistoryStateBucket b;
b.curr = s;
b.snap = s;
while (currentBuckets.size() < BucketList::kNumLevels)
{
currentBuckets.push_back(b);
}
}
HistoryArchiveState::HistoryArchiveState(uint32_t ledgerSeq,
BucketList const& buckets)
: server(STELLAR_CORE_VERSION), currentLedger(ledgerSeq)
{
for (uint32_t i = 0; i < BucketList::kNumLevels; ++i)
{
HistoryStateBucket b;
auto& level = buckets.getLevel(i);
b.curr = binToHex(level.getCurr()->getHash());
b.next = level.getNext();
b.snap = binToHex(level.getSnap()->getHash());
currentBuckets.push_back(b);
}
}
HistoryArchive::HistoryArchive(Application& app,
HistoryArchiveConfiguration const& config)
: mConfig(config)
, mSuccessMeter(app.getMetrics().NewMeter(
{"history-archive", config.mName, "success"}, "event"))
, mFailureMeter(app.getMetrics().NewMeter(
{"history-archive", config.mName, "failure"}, "event"))
{
}
HistoryArchive::~HistoryArchive()
{
}
bool
HistoryArchive::hasGetCmd() const
{
return !mConfig.mGetCmd.empty();
}
bool
HistoryArchive::hasPutCmd() const
{
return !mConfig.mPutCmd.empty();
}
bool
HistoryArchive::hasMkdirCmd() const
{
return !mConfig.mMkdirCmd.empty();
}
std::string const&
HistoryArchive::getName() const
{
return mConfig.mName;
}
std::string
HistoryArchive::getFileCmd(std::string const& remote,
std::string const& local) const
{
if (mConfig.mGetCmd.empty())
return "";
return formatString(mConfig.mGetCmd, remote, local);
}
std::string
HistoryArchive::putFileCmd(std::string const& local,
std::string const& remote) const
{
if (mConfig.mPutCmd.empty())
return "";
return formatString(mConfig.mPutCmd, local, remote);
}
std::string
HistoryArchive::mkdirCmd(std::string const& remoteDir) const
{
if (mConfig.mMkdirCmd.empty())
return "";
return formatString(mConfig.mMkdirCmd, remoteDir);
}
void
HistoryArchive::markSuccess()
{
mSuccessMeter.Mark();
}
void
HistoryArchive::markFailure()
{
mFailureMeter.Mark();
}
uint64_t
HistoryArchive::getSuccessCount() const
{
return mSuccessMeter.count();
}
uint64_t
HistoryArchive::getFailureCount() const
{
return mFailureMeter.count();
}
}