rippled
ETLHelpers.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 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 #ifndef RIPPLE_APP_REPORTING_ETLHELPERS_H_INCLUDED
21 #define RIPPLE_APP_REPORTING_ETLHELPERS_H_INCLUDED
22 #include <ripple/app/main/Application.h>
23 #include <ripple/ledger/ReadView.h>
24 #include <condition_variable>
25 #include <mutex>
26 #include <optional>
27 #include <queue>
28 #include <sstream>
29 
30 namespace ripple {
31 
40 {
41  // max sequence validated by network
43 
44  mutable std::mutex m_;
45 
47 
48  bool stopping_ = false;
49 
50 public:
53  void
54  push(uint32_t idx)
55  {
56  std::lock_guard lck(m_);
57  if (!max_ || idx > *max_)
58  max_ = idx;
59  cv_.notify_all();
60  }
61 
68  {
69  std::unique_lock lck(m_);
70  cv_.wait(lck, [this]() { return max_ || stopping_; });
71  return max_;
72  }
73 
78  bool
79  waitUntilValidatedByNetwork(uint32_t sequence)
80  {
81  std::unique_lock lck(m_);
82  cv_.wait(lck, [sequence, this]() {
83  return (max_ && sequence <= *max_) || stopping_;
84  });
85  return !stopping_;
86  }
87 
91  void
92  stop()
93  {
94  std::lock_guard lck(m_);
95  stopping_ = true;
96  cv_.notify_all();
97  }
98 };
99 
104 template <class T>
106 {
108 
109  mutable std::mutex m_;
112 
113 public:
116  explicit ThreadSafeQueue(uint32_t maxSize) : maxSize_(maxSize)
117  {
118  }
119 
121  ThreadSafeQueue() = default;
122 
125  void
126  push(T const& elt)
127  {
128  std::unique_lock lck(m_);
129  // if queue has a max size, wait until not full
130  if (maxSize_)
131  cv_.wait(lck, [this]() { return queue_.size() <= *maxSize_; });
132  queue_.push(elt);
133  cv_.notify_all();
134  }
135 
138  void
139  push(T&& elt)
140  {
141  std::unique_lock lck(m_);
142  // if queue has a max size, wait until not full
143  if (maxSize_)
144  cv_.wait(lck, [this]() { return queue_.size() <= *maxSize_; });
145  queue_.push(std::move(elt));
146  cv_.notify_all();
147  }
148 
150  T
151  pop()
152  {
153  std::unique_lock lck(m_);
154  cv_.wait(lck, [this]() { return !queue_.empty(); });
155  T ret = std::move(queue_.front());
156  queue_.pop();
157  // if queue has a max size, unblock any possible pushers
158  if (maxSize_)
159  cv_.notify_all();
160  return ret;
161  }
162 };
163 
167 getMarkers(size_t numMarkers)
168 {
169  assert(numMarkers <= 256);
170 
171  unsigned char incr = 256 / numMarkers;
172 
173  std::vector<uint256> markers;
174  markers.reserve(numMarkers);
175  uint256 base{0};
176  for (size_t i = 0; i < numMarkers; ++i)
177  {
178  markers.push_back(base);
179  base.data()[0] += incr;
180  }
181  return markers;
182 }
183 
184 } // namespace ripple
185 #endif
sstream
ripple::ThreadSafeQueue::queue_
std::queue< T > queue_
Definition: ETLHelpers.h:107
ripple::ThreadSafeQueue
Generic thread-safe queue with an optional maximum size Note, we can't use a lockfree queue here,...
Definition: ETLHelpers.h:105
std::vector::reserve
T reserve(T... args)
ripple::ThreadSafeQueue::push
void push(T const &elt)
Definition: ETLHelpers.h:126
std::vector
STL class.
ripple::ThreadSafeQueue::cv_
std::condition_variable cv_
Definition: ETLHelpers.h:110
ripple::NetworkValidatedLedgers::push
void push(uint32_t idx)
Notify the datastructure that idx has been validated by the network.
Definition: ETLHelpers.h:54
ripple::NetworkValidatedLedgers::waitUntilValidatedByNetwork
bool waitUntilValidatedByNetwork(uint32_t sequence)
Waits for the sequence to be validated by the network.
Definition: ETLHelpers.h:79
ripple::NetworkValidatedLedgers::stop
void stop()
Puts the datastructure in the stopped state Future calls to this datastructure will not block This op...
Definition: ETLHelpers.h:92
std::lock_guard
STL class.
ripple::NetworkValidatedLedgers
This datastructure is used to keep track of the sequence of the most recent ledger validated by the n...
Definition: ETLHelpers.h:39
ripple::ThreadSafeQueue::m_
std::mutex m_
Definition: ETLHelpers.h:109
ripple::ThreadSafeQueue::push
void push(T &&elt)
Definition: ETLHelpers.h:139
ripple::ThreadSafeQueue::pop
T pop()
Definition: ETLHelpers.h:151
ripple::ThreadSafeQueue::ThreadSafeQueue
ThreadSafeQueue()=default
Create a queue with no maximum size.
queue
ripple::base_uint::data
pointer data()
Definition: base_uint.h:113
std::vector::push_back
T push_back(T... args)
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:73
ripple::NetworkValidatedLedgers::max_
std::optional< uint32_t > max_
Definition: ETLHelpers.h:42
std::unique_lock
STL class.
ripple::ThreadSafeQueue::maxSize_
std::optional< uint32_t > maxSize_
Definition: ETLHelpers.h:111
std::condition_variable::wait
T wait(T... args)
ripple::NetworkValidatedLedgers::m_
std::mutex m_
Definition: ETLHelpers.h:44
ripple::NetworkValidatedLedgers::stopping_
bool stopping_
Definition: ETLHelpers.h:48
ripple::NetworkValidatedLedgers::getMostRecent
std::optional< uint32_t > getMostRecent()
Get most recently validated sequence.
Definition: ETLHelpers.h:67
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
condition_variable
optional
mutex
ripple::ThreadSafeQueue::ThreadSafeQueue
ThreadSafeQueue(uint32_t maxSize)
Definition: ETLHelpers.h:116
std::condition_variable::notify_all
T notify_all(T... args)
ripple::getMarkers
std::vector< uint256 > getMarkers(size_t numMarkers)
Parititions the uint256 keyspace into numMarkers partitions, each of equal size.
Definition: ETLHelpers.h:167
ripple::NetworkValidatedLedgers::cv_
std::condition_variable cv_
Definition: ETLHelpers.h:46