proxygen
proxygen::RendezvousHash Class Reference

#include <RendezvousHash.h>

Inheritance diagram for proxygen::RendezvousHash:
proxygen::ConsistentHash

Public Member Functions

double getMaxErrorRate () const override
 
void build (std::vector< std::pair< std::string, uint64_t >> &) override
 
size_t get (const uint64_t key, const size_t rank=0) const override
 
std::vector< size_t > selectNUnweighted (const uint64_t key, const size_t rank) const
 
- Public Member Functions inherited from proxygen::ConsistentHash
virtual ~ConsistentHash ()
 
virtual void build (std::vector< std::pair< std::string, uint64_t > > &)=0
 

Private Member Functions

size_t getNthByWeightedHash (const uint64_t key, const size_t modRank, std::vector< size_t > *returnRankIds) const
 
uint64_t computeHash (const char *data, size_t len) const
 
uint64_t computeHash (uint64_t i) const
 

Private Attributes

std::vector< std::pair< uint64_t, uint64_t > > weights_
 

Detailed Description

Definition at line 24 of file RendezvousHash.h.

Member Function Documentation

void proxygen::RendezvousHash::build ( std::vector< std::pair< std::string, uint64_t >> &  )
override

Definition at line 20 of file RendezvousHash.cpp.

References computeHash(), string, uint64_t, and weights_.

Referenced by TEST().

21  {
22  for (auto it = nodes.begin(); it != nodes.end(); ++it) {
23  std::string key = it->first;
24  uint64_t weight = it->second;
25  weights_.emplace_back(computeHash(key.c_str(), key.size()), weight);
26  }
27 }
uint64_t computeHash(const char *data, size_t len) const
const char * string
Definition: Conv.cpp:212
std::vector< std::pair< uint64_t, uint64_t > > weights_
uint64_t proxygen::RendezvousHash::computeHash ( const char *  data,
size_t  len 
) const
private

Definition at line 214 of file RendezvousHash.cpp.

References folly::hash::fnv64_buf().

Referenced by build(), and getNthByWeightedHash().

214  {
216 }
uint64_t fnv64_buf(const void *buf, size_t n, uint64_t hash=FNV_64_HASH_START) noexcept
Definition: Hash.h:199
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
uint64_t proxygen::RendezvousHash::computeHash ( uint64_t  i) const
private

Definition at line 218 of file RendezvousHash.cpp.

References folly::hash::twang_mix64().

218  {
219  return folly::hash::twang_mix64(i);
220 }
uint64_t twang_mix64(uint64_t key) noexcept
Definition: Hash.h:49
size_t proxygen::RendezvousHash::get ( const uint64_t  key,
const size_t  rank = 0 
) const
overridevirtual

get(key, N) finds the node ranked N in the consistent hashing space for the given key.

The returning value is the node's index in the input vector of build().

Implements proxygen::ConsistentHash.

Definition at line 127 of file RendezvousHash.cpp.

References getNthByWeightedHash().

Referenced by TEST().

127  {
128  return getNthByWeightedHash(key, rank, nullptr);
129 }
size_t getNthByWeightedHash(const uint64_t key, const size_t modRank, std::vector< size_t > *returnRankIds) const
double proxygen::RendezvousHash::getMaxErrorRate ( ) const
overridevirtual

get max error rate the current hashing space

Implements proxygen::ConsistentHash.

Definition at line 222 of file RendezvousHash.cpp.

222  {
223  return 0;
224 }
size_t proxygen::RendezvousHash::getNthByWeightedHash ( const uint64_t  key,
const size_t  modRank,
std::vector< size_t > *  returnRankIds 
) const
private

Definition at line 134 of file RendezvousHash.cpp.

References computeHash(), FOR_EACH_ENUMERATE, i, max, and weights_.

Referenced by get(), and selectNUnweighted().

137  {
138  size_t modRank = rank % weights_.size();
139  // optimize if required to return element with max weight, rank ==
140  // weights_.size(), keep track of the maxWeightIndex instead of populating
141  // scaledWeights array.
142  double maxWeight = -1;
143  int maxWeightIndex = 0;
144 
145  std::vector<std::pair<double, size_t>> scaledWeights;
146  if (modRank != 0) {
147  scaledWeights.reserve(weights_.size());
148  }
149  FOR_EACH_ENUMERATE(i, entry, weights_) {
150  // combine the hash with the cluster together
151  double combinedHash = computeHash(entry->first + key);
152  double scaledHash =
153  (double)combinedHash / std::numeric_limits<uint64_t>::max();
154  double scaledWeight = 0;
155  if (entry->second != 0) {
156  scaledWeight = pow(scaledHash, (double)1 / entry->second);
157  }
158  if (modRank == 0) {
159  if (scaledWeight > maxWeight) {
160  maxWeight = scaledWeight;
161  maxWeightIndex = i;
162  }
163  } else {
164  scaledWeights.emplace_back(scaledWeight, i);
165  }
166  }
167 
168  size_t rankIndex;
169  if (modRank == 0) {
170  rankIndex = maxWeightIndex;
171  } else {
172  std::nth_element(scaledWeights.begin(),
173  scaledWeights.begin() + modRank,
174  scaledWeights.end(),
175  std::greater<std::pair<double, size_t>>());
176  rankIndex = scaledWeights[modRank].second;
177  }
178 
179  if (returnRankIds) {
180  if (modRank == 0) {
181  returnRankIds->push_back(rankIndex);
182  } else {
183  returnRankIds->reserve(modRank);
184  for (size_t i = 0; i < modRank; i++) {
185  returnRankIds->push_back(scaledWeights[i].second);
186  }
187  }
188  }
189 
190  return rankIndex;
191 }
LogLevel max
Definition: LogLevel.cpp:31
uint64_t computeHash(const char *data, size_t len) const
#define FOR_EACH_ENUMERATE(count, i, c)
Definition: Foreach.h:170
std::vector< std::pair< uint64_t, uint64_t > > weights_
std::vector< size_t > proxygen::RendezvousHash::selectNUnweighted ( const uint64_t  key,
const size_t  rank 
) const

Definition at line 199 of file RendezvousHash.cpp.

References getNthByWeightedHash(), and weights_.

Referenced by TEST().

200  {
201  std::vector<size_t> selection;
202  // shortcut if rank is equal or larger than array size
203  if (rank >= weights_.size()) {
204  selection = std::vector<size_t>(weights_.size());
205  std::generate(
206  selection.begin(), selection.end(), [n = 0]() mutable { return n++; });
207  return selection;
208  }
209 
210  getNthByWeightedHash(key, rank, &selection);
211  return selection;
212 }
size_t getNthByWeightedHash(const uint64_t key, const size_t modRank, std::vector< size_t > *returnRankIds) const
std::vector< std::pair< uint64_t, uint64_t > > weights_

Member Data Documentation

std::vector<std::pair<uint64_t, uint64_t> > proxygen::RendezvousHash::weights_
private

Definition at line 43 of file RendezvousHash.h.

Referenced by build(), getNthByWeightedHash(), and selectNUnweighted().


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