forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoryArchiveManager.cpp
258 lines (229 loc) · 7.61 KB
/
HistoryArchiveManager.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
// Copyright 2018 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
#include "history/HistoryArchiveManager.h"
#include "history/HistoryArchive.h"
#include "historywork/GetHistoryArchiveStateWork.h"
#include "historywork/PutHistoryArchiveStateWork.h"
#include "main/Application.h"
#include "main/Config.h"
#include "util/Logging.h"
#include "util/Math.h"
#include "work/WorkScheduler.h"
#include <vector>
namespace stellar
{
HistoryArchiveManager::HistoryArchiveManager(Application& app) : mApp{app}
{
for (auto const& archiveConfiguration : mApp.getConfig().HISTORY)
mArchives.push_back(
std::make_shared<HistoryArchive>(app, archiveConfiguration.second));
}
bool
HistoryArchiveManager::checkSensibleConfig() const
{
// Check reasonable-ness of history archive definitions
std::vector<std::string> readOnlyArchives;
std::vector<std::string> readWriteArchives;
std::vector<std::string> writeOnlyArchives;
std::vector<std::string> inertArchives;
for (auto const& archive : mArchives)
{
if (archive->hasGetCmd())
{
if (archive->hasPutCmd())
{
readWriteArchives.push_back(archive->getName());
}
else
{
readOnlyArchives.push_back(archive->getName());
}
}
else
{
if (archive->hasPutCmd())
{
writeOnlyArchives.push_back(archive->getName());
}
else
{
inertArchives.push_back(archive->getName());
}
}
}
bool badArchives = false;
for (auto const& a : inertArchives)
{
CLOG(FATAL, "History")
<< "Archive '" << a
<< "' has no 'get' or 'put' command, will not function";
badArchives = true;
}
for (auto const& a : writeOnlyArchives)
{
CLOG(FATAL, "History")
<< "Archive '" << a
<< "' has 'put' but no 'get' command, will be unwritable";
badArchives = true;
}
for (auto const& a : readWriteArchives)
{
CLOG(INFO, "History")
<< "Archive '" << a
<< "' has 'put' and 'get' commands, will be read and written";
}
for (auto const& a : readOnlyArchives)
{
CLOG(INFO, "History")
<< "Archive '" << a
<< "' has 'get' command only, will not be written";
}
if (readOnlyArchives.empty() && readWriteArchives.empty())
{
CLOG(FATAL, "History")
<< "No readable archives configured, catchup will fail.";
badArchives = true;
}
if (readWriteArchives.empty())
{
CLOG(WARNING, "History")
<< "No writable archives configured, history will not be written.";
}
if (badArchives)
{
CLOG(ERROR, "History") << "History archives misconfigured.";
return false;
}
return true;
}
std::shared_ptr<HistoryArchive>
HistoryArchiveManager::selectRandomReadableHistoryArchive() const
{
std::vector<std::shared_ptr<HistoryArchive>> archives;
// First try for archives that _only_ have a get command; they're
// archives we're explicitly not publishing to, so likely ones we want.
std::copy_if(std::begin(mArchives), std::end(mArchives),
std::back_inserter(archives),
[](std::shared_ptr<HistoryArchive> const& x) {
return x->hasGetCmd() && !x->hasPutCmd();
});
// If we have none of those, accept those with get+put
if (archives.size() == 0)
{
std::copy_if(std::begin(mArchives), std::end(mArchives),
std::back_inserter(archives),
[](std::shared_ptr<HistoryArchive> const& x) {
return x->hasGetCmd();
});
}
if (archives.size() == 0)
{
throw std::runtime_error("No GET-enabled history archive in config");
}
else if (archives.size() == 1)
{
CLOG(DEBUG, "History")
<< "Fetching from sole readable history archive '"
<< archives[0]->getName() << "'";
return archives[0];
}
else
{
std::uniform_int_distribution<size_t> dist(0, archives.size() - 1);
size_t i = dist(gRandomEngine);
CLOG(DEBUG, "History") << "Fetching from readable history archive #"
<< i << ", '" << archives[i]->getName() << "'";
return archives[i];
}
}
bool
HistoryArchiveManager::initializeHistoryArchive(std::string const& arch) const
{
auto archive = getHistoryArchive(arch);
if (!archive)
{
CLOG(FATAL, "History")
<< "Can't initialize unknown history archive '" << arch << "'";
return false;
}
auto& ws = mApp.getWorkScheduler();
// First check that there's no existing HAS in the archive
HistoryArchiveState existing;
CLOG(INFO, "History") << "Probing history archive '" << arch
<< "' for existing state";
auto getHas =
ws.executeWork<GetHistoryArchiveStateWork>(existing, 0, archive, 0);
if (getHas->getState() == BasicWork::State::WORK_SUCCESS)
{
CLOG(ERROR, "History")
<< "History archive '" << arch << "' already initialized!";
return false;
}
CLOG(INFO, "History") << "History archive '" << arch
<< "' appears uninitialized";
HistoryArchiveState has;
CLOG(INFO, "History") << "Initializing history archive '" << arch << "'";
has.resolveAllFutures();
auto putHas = ws.executeWork<PutHistoryArchiveStateWork>(has, archive);
if (putHas->getState() == BasicWork::State::WORK_SUCCESS)
{
CLOG(INFO, "History") << "Initialized history archive '" << arch << "'";
return true;
}
else
{
CLOG(FATAL, "History")
<< "Failed to initialize history archive '" << arch << "'";
return false;
}
}
bool
HistoryArchiveManager::hasAnyWritableHistoryArchive() const
{
return std::any_of(std::begin(mArchives), std::end(mArchives),
[](std::shared_ptr<HistoryArchive> const& x) {
return x->hasGetCmd() && x->hasPutCmd();
});
}
std::shared_ptr<HistoryArchive>
HistoryArchiveManager::getHistoryArchive(std::string const& name) const
{
auto it = std::find_if(std::begin(mArchives), std::end(mArchives),
[&name](std::shared_ptr<HistoryArchive> const& x) {
return x->getName() == name;
});
return it == std::end(mArchives) ? nullptr : *it;
}
std::vector<std::shared_ptr<HistoryArchive>>
HistoryArchiveManager::getWritableHistoryArchives() const
{
auto result = std::vector<std::shared_ptr<HistoryArchive>>{};
std::copy_if(std::begin(mArchives), std::end(mArchives),
std::back_inserter(result),
[](std::shared_ptr<HistoryArchive> const& x) {
return x->hasGetCmd() && x->hasPutCmd();
});
return result;
}
double
HistoryArchiveManager::getFailureRate() const
{
uint64_t successCount{0};
uint64_t failureCount{0};
for (auto archive : mArchives)
{
successCount += archive->getSuccessCount();
failureCount += archive->getFailureCount();
}
auto total = successCount + failureCount;
if (total == 0)
{
return 0.0;
}
else
{
return static_cast<double>(failureCount) / total;
}
}
}