Skip to content

Commit 3f49c52

Browse files
ilvokhinfacebook-github-bot
authored andcommitted
Do not depend on folly::Hash values stability in OriginalClientHashRoute
Summary: We are planning to randomize values of `folly::Hash` between binary invocations. Seems like `OriginalClientHashRoute` relies on the values to be stable. Use `folly::hash::SpookyHashV2::Hash64` with fixed seed instead of `folly::Hash` to preserve old semantic. Reviewed By: yfeldblum Differential Revision: D79348475 fbshipit-source-id: e01e90b1e18713a2e44e187d4e4d4570daffc667
1 parent 2dbee3d commit 3f49c52

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

mcrouter/routes/OriginalClientHashRoute.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
#pragma once
99

10-
#include <folly/hash/Hash.h>
1110
#include <memory>
1211
#include <string>
1312

13+
#include <folly/hash/SpookyHashV2.h>
14+
1415
#include "mcrouter/CarbonRouterInstanceBase.h"
1516
#include "mcrouter/McrouterFiberContext.h"
1617
#include "mcrouter/ProxyRequestContext.h"
@@ -27,6 +28,14 @@ namespace facebook {
2728
namespace memcache {
2829
namespace mcrouter {
2930

31+
struct StableHasher {
32+
constexpr size_t operator()(folly::StringPiece sp) const {
33+
constexpr uint64_t kSeed = 0;
34+
return static_cast<size_t>(
35+
folly::hash::SpookyHashV2::Hash64(sp.begin(), sp.size(), kSeed));
36+
}
37+
};
38+
3039
/**
3140
* Use userIpAddress to hash request among children
3241
* Optional offset param to pick different child
@@ -57,7 +66,7 @@ class OriginalClientHashRoute {
5766

5867
if (auto ip = req.getSourceIpAddr()) {
5968
return t(
60-
*children_[(folly::Hash()(ip->str()) + offset_) % children_.size()],
69+
*children_[(StableHasher()(ip->str()) + offset_) % children_.size()],
6170
req);
6271
}
6372
return false;
@@ -69,7 +78,7 @@ class OriginalClientHashRoute {
6978
assert(children_.size() > 1);
7079

7180
if (auto ip = req.getSourceIpAddr()) {
72-
return children_[(folly::Hash()(ip->str()) + offset_) % children_.size()]
81+
return children_[(StableHasher()(ip->str()) + offset_) % children_.size()]
7382
->route(req);
7483
} else {
7584
return createReply<Request>(

0 commit comments

Comments
 (0)