rippled
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 <ripple/app/ledger/impl/TimeoutCounter.h>
21 #include <ripple/app/main/Application.h>
22 #include <ripple/core/JobQueue.h>
23 #include <ripple/overlay/Overlay.h>
24 
25 namespace ripple {
26 
27 using namespace std::chrono_literals;
28 
30  Application& app,
31  uint256 const& hash,
33  QueueJobParameter&& jobParameter,
34  beast::Journal journal)
35  : app_(app)
36  , journal_(journal)
37  , hash_(hash)
38  , timeouts_(0)
39  , complete_(false)
40  , failed_(false)
41  , progress_(false)
42  , timerInterval_(interval)
43  , queueJobParameter_(std::move(jobParameter))
44  , timer_(app_.getIOService())
45 {
46  assert((timerInterval_ > 10ms) && (timerInterval_ < 30s));
47 }
48 
49 void
51 {
52  if (isDone())
53  return;
54  timer_.expires_after(timerInterval_);
55  timer_.async_wait(
56  [wptr = pmDowncast()](boost::system::error_code const& ec) {
57  if (ec == boost::asio::error::operation_aborted)
58  return;
59 
60  if (auto ptr = wptr.lock())
61  {
62  ScopedLockType sl(ptr->mtx_);
63  ptr->queueJob(sl);
64  }
65  });
66 }
67 
68 void
70 {
71  if (isDone())
72  return;
76  {
77  JLOG(journal_.debug()) << "Deferring " << queueJobParameter_.jobName
78  << " timer due to load";
79  setTimer(sl);
80  return;
81  }
82 
86  [wptr = pmDowncast()]() {
87  if (auto sptr = wptr.lock(); sptr)
88  sptr->invokeOnTimer();
89  });
90 }
91 
92 void
94 {
95  ScopedLockType sl(mtx_);
96 
97  if (isDone())
98  return;
99 
100  if (!progress_)
101  {
102  ++timeouts_;
103  JLOG(journal_.debug()) << "Timeout(" << timeouts_ << ") "
104  << " acquiring " << hash_;
105  onTimer(false, sl);
106  }
107  else
108  {
109  progress_ = false;
110  onTimer(true, sl);
111  }
112 
113  if (!isDone())
114  setTimer(sl);
115 }
116 
117 void
119 {
120  ScopedLockType sl(mtx_);
121  if (!isDone())
122  {
123  failed_ = true;
124  JLOG(journal_.info()) << "Cancel " << hash_;
125  }
126 }
127 
128 } // namespace ripple
ripple::TimeoutCounter::timer_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
Definition: TimeoutCounter.h:147
ripple::Application
Definition: Application.h:115
ripple::TimeoutCounter::QueueJobParameter::jobLimit
std::optional< std::uint32_t > jobLimit
Definition: TimeoutCounter.h:87
ripple::TimeoutCounter::QueueJobParameter::jobType
JobType jobType
Definition: TimeoutCounter.h:85
ripple::TimeoutCounter::cancel
virtual void cancel()
Cancel the task by marking it as failed if the task is not done.
Definition: TimeoutCounter.cpp:118
ripple::TimeoutCounter::setTimer
void setTimer(ScopedLockType &)
Schedule a call to queueJob() after mTimerInterval.
Definition: TimeoutCounter.cpp:50
ripple::TimeoutCounter::pmDowncast
virtual std::weak_ptr< TimeoutCounter > pmDowncast()=0
Return a weak pointer to this.
std::chrono::milliseconds
ripple::TimeoutCounter::queueJob
void queueJob(ScopedLockType &)
Queue a job to call invokeOnTimer().
Definition: TimeoutCounter.cpp:69
ripple::TimeoutCounter::progress_
bool progress_
Whether forward progress has been made.
Definition: TimeoutCounter.h:134
ripple::JobQueue::addJob
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition: JobQueue.h:166
ripple::TimeoutCounter::QueueJobParameter::jobName
std::string jobName
Definition: TimeoutCounter.h:86
ripple::TimeoutCounter::mtx_
std::recursive_mutex mtx_
Definition: TimeoutCounter.h:125
ripple::base_uint< 256 >
ripple::JobQueue::getJobCountTotal
int getJobCountTotal(JobType t) const
Jobs waiting plus running at this priority.
Definition: JobQueue.cpp:138
ripple::TimeoutCounter::app_
Application & app_
Definition: TimeoutCounter.h:123
ripple::TimeoutCounter::invokeOnTimer
void invokeOnTimer()
Calls onTimer() if in the right state.
Definition: TimeoutCounter.cpp:93
ripple::TimeoutCounter::failed_
bool failed_
Definition: TimeoutCounter.h:132
ripple::TimeoutCounter::QueueJobParameter
Definition: TimeoutCounter.h:83
std::unique_lock< std::recursive_mutex >
ripple::Application::getJobQueue
virtual JobQueue & getJobQueue()=0
beast::Journal::info
Stream info() const
Definition: Journal.h:321
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::TimeoutCounter::isDone
bool isDone() const
Definition: TimeoutCounter.h:116
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TimeoutCounter::hash_
const uint256 hash_
The hash of the object (in practice, always a ledger) we are trying to fetch.
Definition: TimeoutCounter.h:129
std
STL namespace.
ripple::TimeoutCounter::queueJobParameter_
QueueJobParameter queueJobParameter_
Definition: TimeoutCounter.h:138
beast::Journal::debug
Stream debug() const
Definition: Journal.h:315
ripple::TimeoutCounter::timerInterval_
std::chrono::milliseconds timerInterval_
The minimum time to wait between calls to execute().
Definition: TimeoutCounter.h:136
ripple::TimeoutCounter::TimeoutCounter
TimeoutCounter(Application &app, uint256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
Definition: TimeoutCounter.cpp:29
ripple::TimeoutCounter::onTimer
virtual void onTimer(bool progress, ScopedLockType &)=0
Hook called from invokeOnTimer().
ripple::TimeoutCounter::timeouts_
int timeouts_
Definition: TimeoutCounter.h:130
ripple::TimeoutCounter::journal_
beast::Journal journal_
Definition: TimeoutCounter.h:124