-
Notifications
You must be signed in to change notification settings - Fork 988
/
Copy pathApplyBufferedLedgersWork.cpp
112 lines (97 loc) · 3.06 KB
/
ApplyBufferedLedgersWork.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
// Copyright 2019 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 "catchup/ApplyBufferedLedgersWork.h"
#include "bucket/BucketList.h"
#include "bucket/BucketManager.h"
#include "catchup/ApplyLedgerWork.h"
#include "ledger/LedgerManager.h"
#include "main/Application.h"
#include <Tracy.hpp>
#include <fmt/format.h>
namespace stellar
{
ApplyBufferedLedgersWork::ApplyBufferedLedgersWork(Application& app)
: BasicWork(app, "apply-buffered-ledgers", BasicWork::RETRY_NEVER)
{
}
void
ApplyBufferedLedgersWork::onReset()
{
mConditionalWork.reset();
}
BasicWork::State
ApplyBufferedLedgersWork::onRun()
{
ZoneScoped;
if (mConditionalWork)
{
mConditionalWork->crankWork();
if (mConditionalWork->getState() != State::WORK_SUCCESS)
{
return mConditionalWork->getState();
}
}
std::optional<LedgerCloseData> maybeLcd =
mApp.getCatchupManager().maybeGetNextBufferedLedgerToApply();
if (!maybeLcd)
{
CLOG_INFO(History, "No more buffered ledgers to apply");
return State::WORK_SUCCESS;
}
auto const& lcd = maybeLcd.value();
CLOG_INFO(
History,
"Scheduling buffered ledger-close: [seq={}, prev={}, txs={}, "
"ops={}, sv: {}]",
lcd.getLedgerSeq(), hexAbbrev(lcd.getTxSet()->previousLedgerHash()),
lcd.getTxSet()->sizeTxTotal(), lcd.getTxSet()->sizeOpTotalForLogging(),
stellarValueToString(mApp.getConfig(), lcd.getValue()));
// Pass `nullptr` for `hEntries` because SCP messages of buffered ledgers
// have already been logged.
auto applyLedger = std::make_shared<ApplyLedgerWork>(mApp, lcd, nullptr);
auto predicate = [](Application& app) {
auto& bl = app.getBucketManager().getBucketList();
auto& lm = app.getLedgerManager();
bl.resolveAnyReadyFutures();
return bl.futuresAllResolved(
bl.getMaxMergeLevel(lm.getLastClosedLedgerNum() + 1));
};
mConditionalWork = std::make_shared<ConditionalWork>(
mApp,
fmt::format(
FMT_STRING("apply-buffered-ledger-conditional ledger({:d})"),
lcd.getLedgerSeq()),
predicate, applyLedger, std::chrono::milliseconds(500));
mConditionalWork->startWork(wakeSelfUpCallback());
return State::WORK_RUNNING;
}
std::string
ApplyBufferedLedgersWork::getStatus() const
{
return fmt::format(FMT_STRING("Applying buffered ledgers: {}"),
mConditionalWork ? mConditionalWork->getStatus()
: BasicWork::getStatus());
}
void
ApplyBufferedLedgersWork::shutdown()
{
ZoneScoped;
if (mConditionalWork)
{
mConditionalWork->shutdown();
}
BasicWork::shutdown();
}
bool
ApplyBufferedLedgersWork::onAbort()
{
ZoneScoped;
if (mConditionalWork && !mConditionalWork->isDone())
{
mConditionalWork->crankWork();
return false;
}
return true;
}
}