fix: ensure `ConsistenHasher` is consistent

The std `DefaultHasher` is NOT guaranteed to stay the same, so let's
directly use the `SipHasher13` which at the moment (2021-11-15) is used
by the standard lib.

Fixes #3063.
pull/24376/head
Marco Neumann 2021-11-15 17:39:17 +01:00
parent 3cd7d2eda2
commit 4e71de508e
3 changed files with 11 additions and 5 deletions

7
Cargo.lock generated
View File

@ -792,6 +792,7 @@ dependencies = [
"ordered-float 2.8.0", "ordered-float 2.8.0",
"percent-encoding", "percent-encoding",
"regex", "regex",
"siphasher",
"snafu", "snafu",
"test_helpers", "test_helpers",
"time 0.1.0", "time 0.1.0",
@ -3974,6 +3975,12 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "siphasher"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b"
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.5" version = "0.4.5"

View File

@ -13,6 +13,7 @@ observability_deps = { path = "../observability_deps" }
ordered-float = "2" ordered-float = "2"
percent-encoding = "2.1.0" percent-encoding = "2.1.0"
regex = "1.4" regex = "1.4"
siphasher = "0.3"
snafu = "0.6" snafu = "0.6"
time = { path = "../time" } time = { path = "../time" }
uuid = { version = "0.8", features = ["v4"] } uuid = { version = "0.8", features = ["v4"] }

View File

@ -1,6 +1,7 @@
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use siphasher::sip::SipHasher13;
/// A ConsistentHasher implements a simple consistent hashing mechanism /// A ConsistentHasher implements a simple consistent hashing mechanism
/// that maps a point to the nearest "node" N. /// that maps a point to the nearest "node" N.
/// ///
@ -47,7 +48,7 @@ where
} }
fn hash<H: Hash>(h: H) -> u64 { fn hash<H: Hash>(h: H) -> u64 {
let mut hasher = DefaultHasher::new(); let mut hasher = SipHasher13::new();
h.hash(&mut hasher); h.hash(&mut hasher);
hasher.finish() hasher.finish()
} }
@ -86,9 +87,6 @@ where
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn test_roundtrip() {}
#[test] #[test]
fn test_consistent_hasher() { fn test_consistent_hasher() {
let ch = ConsistentHasher::new(&[10, 20, 30, 40]); let ch = ConsistentHasher::new(&[10, 20, 30, 40]);