rippled
Loading...
Searching...
No Matches
TimeoutCounter.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/ledger/detail/TimeoutCounter.h>
21#include <xrpld/app/main/Application.h>
22#include <xrpld/core/JobQueue.h>
23#include <xrpld/overlay/Overlay.h>
24
25namespace ripple {
26
27using namespace std::chrono_literals;
28
30 Application& app,
31 uint256 const& hash,
33 QueueJobParameter&& jobParameter,
34 beast::Journal journal)
35 : app_(app)
36 , sink_(journal, to_short_string(hash) + " ")
37 , journal_(sink_)
38 , hash_(hash)
39 , timeouts_(0)
40 , complete_(false)
41 , failed_(false)
42 , progress_(false)
43 , timerInterval_(interval)
44 , queueJobParameter_(std::move(jobParameter))
45 , timer_(app_.getIOService())
46{
47 XRPL_ASSERT(
48 (timerInterval_ > 10ms) && (timerInterval_ < 30s),
49 "ripple::TimeoutCounter::TimeoutCounter : interval input inside range");
50}
51
52void
54{
55 if (isDone())
56 return;
57 JLOG(journal_.debug()) << "Setting timer for " << timerInterval_.count()
58 << "ms";
59 timer_.expires_after(timerInterval_);
60 timer_.async_wait(
61 [wptr = pmDowncast()](boost::system::error_code const& ec) {
62 if (ec == boost::asio::error::operation_aborted)
63 return;
64
65 if (auto ptr = wptr.lock())
66 {
67 JLOG(ptr->journal_.debug())
68 << "timer: ec: " << ec << " (operation_aborted: "
69 << boost::asio::error::operation_aborted << " - "
70 << (ec == boost::asio::error::operation_aborted ? "aborted"
71 : "other")
72 << ")";
73 ScopedLockType sl(ptr->mtx_);
74 ptr->queueJob(sl);
75 }
76 });
77}
78
79void
81{
82 if (isDone())
83 return;
87 {
88 JLOG(journal_.debug()) << "Deferring " << queueJobParameter_.jobName
89 << " timer due to load";
90 setTimer(sl);
91 return;
92 }
93
97 [wptr = pmDowncast()]() {
98 if (auto sptr = wptr.lock(); sptr)
99 sptr->invokeOnTimer();
100 });
101}
102
103void
105{
107
108 if (isDone())
109 return;
110
111 if (!progress_)
112 {
113 ++timeouts_;
114 JLOG(journal_.debug())
115 << "Timeout(" << timeouts_ << ") " << " acquiring " << hash_;
116 onTimer(false, sl);
117 }
118 else
119 {
120 progress_ = false;
121 onTimer(true, sl);
122 }
123
124 if (!isDone())
125 setTimer(sl);
126}
127
128void
130{
132 if (!isDone())
133 {
134 failed_ = true;
135 JLOG(journal_.info()) << "Cancel " << hash_;
136 }
137}
138
139} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream debug() const
Definition: Journal.h:317
Stream info() const
Definition: Journal.h:323
virtual JobQueue & getJobQueue()=0
int getJobCountTotal(JobType t) const
Jobs waiting plus running at this priority.
Definition: JobQueue.cpp:150
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition: JobQueue.h:166
void queueJob(ScopedLockType &)
Queue a job to call invokeOnTimer().
virtual void onTimer(bool progress, ScopedLockType &)=0
Hook called from invokeOnTimer().
bool progress_
Whether forward progress has been made.
void setTimer(ScopedLockType &)
Schedule a call to queueJob() after mTimerInterval.
void invokeOnTimer()
Calls onTimer() if in the right state.
std::chrono::milliseconds timerInterval_
The minimum time to wait between calls to execute().
QueueJobParameter queueJobParameter_
beast::Journal journal_
uint256 const hash_
The hash of the object (in practice, always a ledger) we are trying to fetch.
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
virtual std::weak_ptr< TimeoutCounter > pmDowncast()=0
Return a weak pointer to this.
TimeoutCounter(Application &app, uint256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
virtual void cancel()
Cancel the task by marking it as failed if the task is not done.
std::recursive_mutex mtx_
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::string to_short_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:636
STL namespace.
std::optional< std::uint32_t > jobLimit