proxygen
fizz::server::SlidingBloomReplayCache Class Reference

#include <SlidingBloomReplayCache.h>

Inheritance diagram for fizz::server::SlidingBloomReplayCache:
fizz::server::ReplayCache folly::AsyncTimeout

Public Types

using CellType = uint64_t
 
using HashFunction = std::function< CellType(const unsigned char *, size_t)>
 

Public Member Functions

 SlidingBloomReplayCache (int64_t ttlInSeconds, size_t requestsPerSecond, double acceptableFPR, folly::EventBase *evb)
 
 ~SlidingBloomReplayCache () override=default
 
void set (folly::ByteRange query)
 
bool test (folly::ByteRange query) const
 
bool testAndSet (folly::ByteRange query)
 
folly::Future< ReplayCacheResultcheck (folly::ByteRange) override
 
- Public Member Functions inherited from fizz::server::ReplayCache
virtual ~ReplayCache ()=default
 

Private Member Functions

void clearBucket (size_t bucket)
 
void clear ()
 
void timeoutExpired () noexceptoverride
 
- Private Member Functions inherited from folly::AsyncTimeout
 AsyncTimeout (TimeoutManager *timeoutManager)
 
 AsyncTimeout (EventBase *eventBase)
 
 AsyncTimeout (TimeoutManager *timeoutManager, InternalEnum internal)
 
 AsyncTimeout (EventBase *eventBase, InternalEnum internal)
 
 AsyncTimeout ()
 
virtual ~AsyncTimeout ()
 
bool scheduleTimeout (uint32_t milliseconds)
 
bool scheduleTimeout (TimeoutManager::timeout_type timeout)
 
void cancelTimeout ()
 
bool isScheduled () const
 
void attachTimeoutManager (TimeoutManager *timeoutManager, InternalEnum internal=InternalEnum::NORMAL)
 
void attachEventBase (EventBase *eventBase, InternalEnum internal=InternalEnum::NORMAL)
 
void detachTimeoutManager ()
 
void detachEventBase ()
 
const TimeoutManagergetTimeoutManager ()
 
struct event * getEvent ()
 

Private Attributes

std::chrono::milliseconds bucketWidthInMs_
 
size_t bitSize_
 
size_t currentBucket_
 
std::unique_ptr< CellType[]> bitBuf_
 
std::vector< HashFunctionhashers_
 

Additional Inherited Members

- Private Types inherited from folly::AsyncTimeout
typedef TimeoutManager::InternalEnum InternalEnum
 
- Static Private Member Functions inherited from folly::AsyncTimeout
template<typename TCallback >
static std::unique_ptr< AsyncTimeoutmake (TimeoutManager &manager, TCallback &&callback)
 
template<typename TCallback >
static std::unique_ptr< AsyncTimeoutschedule (TimeoutManager::timeout_type timeout, TimeoutManager &manager, TCallback &&callback)
 

Detailed Description

Definition at line 23 of file SlidingBloomReplayCache.h.

Member Typedef Documentation

using fizz::server::SlidingBloomReplayCache::HashFunction = std::function<CellType(const unsigned char*, size_t)>

Definition at line 30 of file SlidingBloomReplayCache.h.

Constructor & Destructor Documentation

fizz::server::SlidingBloomReplayCache::SlidingBloomReplayCache ( int64_t  ttlInSeconds,
size_t  requestsPerSecond,
double  acceptableFPR,
folly::EventBase evb 
)

Definition at line 56 of file SlidingBloomReplayCache.cpp.

References bitBuf_, bitSize_, bucketWidthInMs_, currentBucket_, hashers_, i, fizz::server::kBucketCount, fizz::server::kHashCount, folly::AsyncTimeout::scheduleTimeout(), seed, and uint64_t.

61  : folly::AsyncTimeout(evb) {
62  if (acceptableFPR <= 0.0 || acceptableFPR >= 1.0) {
63  throw std::runtime_error("false positive rate must lie between 0 and 1");
64  }
65 
66  // Do all calculations with doubles.
67  double ttlDouble = ttlInSecs;
68  double rpsDouble = requestsPerSecond;
69  double hashCountDouble = kHashCount;
70  double bucketCountDouble = kBucketCount;
71  double dividend = -hashCountDouble * rpsDouble * ttlDouble;
72  double root = pow(acceptableFPR, 1.0 / hashCountDouble);
73  double divisor = bucketCountDouble * log(1.0 - root);
74  bitSize_ = dividend / divisor;
75  VLOG(8) << "Initializing with bitSize = " << bitSize_;
76 
78  std::chrono::milliseconds(((ttlInSecs * 1000) / kBucketCount) + 1);
79 
80  // Reset bit buffer
81  bitBuf_.reset(new CellType[bitSize_]());
82 
83  // Initialize current bucket
84  currentBucket_ = 0;
85 
86  // Set up hashers
87  for (unsigned int i = 0; i < kHashCount; i++) {
88  hashers_.push_back(
89  [seed = RandomNumGenerator<uint64_t>().generateRandom()](
90  const unsigned char* buf, size_t len) -> uint64_t {
91  return SpookyHashV2::Hash64((const void*)buf, len, seed);
92  });
93  }
94 
95  // Schedule reaping function (if evb given)
96  if (evb) {
98  } else {
99  VLOG(8) << "Started replay cache without reaping";
100  }
101 }
static const int seed
static const unsigned int kBucketCount
bool scheduleTimeout(uint32_t milliseconds)
static const unsigned int kHashCount
fizz::server::SlidingBloomReplayCache::~SlidingBloomReplayCache ( )
overridedefault

Member Function Documentation

folly::Future< ReplayCacheResult > fizz::server::SlidingBloomReplayCache::check ( folly::ByteRange  query)
overridevirtual
void fizz::server::SlidingBloomReplayCache::clear ( )
private

Definition at line 155 of file SlidingBloomReplayCache.cpp.

References clearBucket(), and currentBucket_.

Referenced by timeoutExpired().

155  {
156  // Clear the soon to be occupied bucket
158 
159  // Increment current bucket
161 }
static const unsigned int kBucketCount
void fizz::server::SlidingBloomReplayCache::clearBucket ( size_t  bucket)
private

Definition at line 145 of file SlidingBloomReplayCache.cpp.

References bitBuf_, bitSize_, currentBucket_, and i.

Referenced by clear().

145  {
146  VLOG(8) << "Clearing bit " << bucket << ", current bucket is "
147  << currentBucket_;
148 
149  CellType mask = ~((static_cast<CellType>(1)) << bucket);
150  for (size_t i = 0; i < bitSize_; ++i) {
151  bitBuf_[i] &= mask;
152  }
153 }
void fizz::server::SlidingBloomReplayCache::set ( folly::ByteRange  query)

Definition at line 103 of file SlidingBloomReplayCache.cpp.

References bitBuf_, bitSize_, currentBucket_, folly::Range< Iter >::data(), hashers_, and folly::Range< Iter >::size().

Referenced by fizz::server::test::TEST().

103  {
104  CellType mask = (static_cast<CellType>(1)) << currentBucket_;
105 
106  for (auto& hasher : hashers_) {
107  size_t idx = hasher(query.data(), query.size()) % bitSize_;
108 
109  bitBuf_[idx] |= mask;
110  }
111 }
constexpr size_type size() const
Definition: Range.h:431
constexpr Iter data() const
Definition: Range.h:446
bool fizz::server::SlidingBloomReplayCache::test ( folly::ByteRange  query) const

Definition at line 113 of file SlidingBloomReplayCache.cpp.

References bitBuf_, bitSize_, folly::Range< Iter >::data(), hashers_, max, and folly::Range< Iter >::size().

Referenced by fizz::server::test::TEST().

113  {
115 
116  for (auto& hasher : hashers_) {
117  size_t idx = hasher(query.data(), query.size()) % bitSize_;
118 
119  ret &= bitBuf_[idx];
120  }
121 
122  return (ret != 0);
123 }
LogLevel max
Definition: LogLevel.cpp:31
constexpr size_type size() const
Definition: Range.h:431
constexpr Iter data() const
Definition: Range.h:446
bool fizz::server::SlidingBloomReplayCache::testAndSet ( folly::ByteRange  query)

Definition at line 125 of file SlidingBloomReplayCache.cpp.

References bitBuf_, bitSize_, currentBucket_, folly::Range< Iter >::data(), hashers_, max, and folly::Range< Iter >::size().

Referenced by check(), and fizz::server::test::TEST().

125  {
127  CellType mask = (static_cast<CellType>(1)) << currentBucket_;
128 
129  for (auto& hasher : hashers_) {
130  size_t idx = hasher(query.data(), query.size()) % bitSize_;
131 
132  ret &= bitBuf_[idx];
133  bitBuf_[idx] |= mask;
134  }
135 
136  return (ret != 0);
137 }
LogLevel max
Definition: LogLevel.cpp:31
constexpr size_type size() const
Definition: Range.h:431
constexpr Iter data() const
Definition: Range.h:446
void fizz::server::SlidingBloomReplayCache::timeoutExpired ( )
overrideprivatevirtualnoexcept

timeoutExpired() is invoked when the timeout period has expired.

Implements folly::AsyncTimeout.

Definition at line 163 of file SlidingBloomReplayCache.cpp.

References bucketWidthInMs_, clear(), and folly::AsyncTimeout::scheduleTimeout().

163  {
164  clear();
166 }
bool scheduleTimeout(uint32_t milliseconds)

Member Data Documentation

std::unique_ptr<CellType[]> fizz::server::SlidingBloomReplayCache::bitBuf_
private

Definition at line 65 of file SlidingBloomReplayCache.h.

Referenced by clearBucket(), set(), SlidingBloomReplayCache(), test(), and testAndSet().

size_t fizz::server::SlidingBloomReplayCache::bitSize_
private

Definition at line 60 of file SlidingBloomReplayCache.h.

Referenced by clearBucket(), set(), SlidingBloomReplayCache(), test(), and testAndSet().

std::chrono::milliseconds fizz::server::SlidingBloomReplayCache::bucketWidthInMs_
private

Definition at line 59 of file SlidingBloomReplayCache.h.

Referenced by SlidingBloomReplayCache(), and timeoutExpired().

size_t fizz::server::SlidingBloomReplayCache::currentBucket_
private
std::vector<HashFunction> fizz::server::SlidingBloomReplayCache::hashers_
private

Definition at line 67 of file SlidingBloomReplayCache.h.

Referenced by set(), SlidingBloomReplayCache(), test(), and testAndSet().


The documentation for this class was generated from the following files: