Skip to content

Commit

Permalink
Merge pull request #442 from weibocom/dev_fnv132_20240312
Browse files Browse the repository at this point in the history
1、support 新hash;
2、增加hash/dist校验工具及示例文件
  • Loading branch information
FicoHu authored Mar 28, 2024
2 parents da4dc6c + 1b4d5e4 commit 112930c
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 4 deletions.
1 change: 1 addition & 0 deletions sharding/src/distribution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl Distribute {
"secmod" => Self::SecMod(SecMod::from(names.len())),
_ => {
log::warn!("'{}' is not valid , use modula instead", distribution);
println!("'{}' is not valid , use modula instead!", distribution);
Self::Modula(Modula::from(names.len(), false))
}
}
Expand Down
24 changes: 22 additions & 2 deletions sharding/src/hash/fnv1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
/// 按需支持fnv1系列所有相关的hash算法,目前支持fnv1a_64
/// 按需支持fnv1系列所有相关的hash算法,目前支持fnv1_32、fnv1a_64;
/// 对应算法源自twemproxy

/// fnv1_32相关
#[derive(Debug, Default, Clone)]
pub struct Fnv1F32;

const FNV_32_INIT: u32 = 2166136261;
const FNV_32_PRIME: u32 = 16777619;

impl super::Hash for Fnv1F32 {
fn hash<S: crate::HashKey>(&self, key: &S) -> i64 {
let mut hash = FNV_32_INIT;
for i in 0..key.len() {
hash = hash.wrapping_mul(FNV_32_PRIME);
hash = hash ^ (key.at(i) as u32);
}
hash as i64
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
/// fnv1a_64相关
#[derive(Debug, Default, Clone)]
pub struct Fnv1aF64;

Expand All @@ -13,7 +34,6 @@ impl super::Hash for Fnv1aF64 {
hash ^= key.at(i) as u32;
hash = hash.wrapping_mul(FNV_64_PRIME as u32);
}

hash as i64
}
}
6 changes: 4 additions & 2 deletions sharding/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ pub use rawsuffix::RawSuffix;
pub mod crc;

use enum_dispatch::enum_dispatch;

use self::{bkdrsub::Bkdrsub, crc64::Crc64, fnv1::Fnv1aF64};
use self::{bkdrsub::Bkdrsub, crc64::Crc64, fnv1::Fnv1F32, fnv1::Fnv1aF64};

// 占位hash,主要用于兼容服务框架,供mq等业务使用
pub const HASH_PADDING: &str = "padding";
Expand Down Expand Up @@ -88,6 +87,7 @@ pub enum Hasher {
Fnv1aF64(Fnv1aF64),
Random(RandomHash), // random hash
RawSuffix(RawSuffix),
Fnv1_32(Fnv1F32),
}

impl Hasher {
Expand Down Expand Up @@ -132,10 +132,12 @@ impl Hasher {
"crc32abs" => Self::Crc32Abs(Default::default()),
"crc64" => Self::Crc64(Default::default()),
"random" => Self::Random(Default::default()),
"fnv1_32" => Self::Fnv1_32(Default::default()),
"fnv1a_64" => Self::Fnv1aF64(Default::default()),
_ => {
// 默认采用mc的crc32-s hash
log::error!("found unknown hash:{}, use crc32-short instead", alg);
println!("found unknown hash:{}, use crc32-short instead!", alg);
return Self::Crc32Short(Default::default());
}
};
Expand Down
30 changes: 30 additions & 0 deletions tests/sharding_datas/common/all_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 文件格式:首先设置hash、distribution、shard_count,然后设置每个分片的数据
# header
hash=crc64
distribution=modula
shard_count=6

shard_idx=0
hot_band_conf_6041884361
hot_band_conf_7457077031
hot_band_conf_5983133201

shard_idx=1
hot_band_conf_5658472751
hot_band_conf_3306842351
hot_band_conf_7550365451
hot_band_conf_3306842351

shard_idx=2
hot_band_conf_2759747141
hot_band_conf_7202166971
hot_band_conf_7723136021

shard_idx=3
hot_band_conf_7816767401

shard_idx=4
hot_band_conf_5211889061

shard_idx=5
hot_band_conf_2739628991
7 changes: 7 additions & 0 deletions tests/sharding_datas/common/compare.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
4467599223029960
3735267922960943
3543172671127490
3542600890981532
3736215076519398
4480826111688800
4673790045847657
72 changes: 72 additions & 0 deletions tests/sharding_datas/common/redis.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 文件格式:首先设置hash、dist、shard_count,然后设置每个分片的数据
# header
hash=fnv1_32
distribution=modula
shard_count=16

shard_idx=0
ocpx_app_22320534_hot_tweets_feed_20240307
ocpx_ctcvr_cache_v2_22333703_20240313

shard_idx=1
ocpx_cpl_22268383_hot_tweets_feed_20240305lastslot

shard_idx=2
ocpx_cpl_22332413_hot_tweets_feed_20240311lastslot

shard_idx=3
ocpx_cpl_22304432_discover_hotspot_feed_20240311lastslot
coldstart:ad_info:22315012
ocpx_cpl_22229734_queryfeed_20240307lastslot

shard_idx=4
ocpx_app_22292303_other_20240313

shard_idx=5
ocpx_24h_pid_22309234_20240309
ocpx_app_22216665_other_20240311lastslot

shard_idx=6
ocpx_cpl_22186766_discover_hotspot_feed_20240307
ocpx_cpl_22333450_general_queryfeed_20240311lastslot

shard_idx=7
ocpx_app_22216118_hot_tweets_feed_20240313
ocpx_cpl_22302263_vertical_video_20240308
ocpx_cpl_22280249_mainfeed_20240302lastslot

shard_idx=8
ocpx_cpl_22255305_discover_hotspot_feed_20240312
ocpx_ctcvr_v2_22309626

shard_idx=9
ocpx_creative_custopt_3275145341_86004001_20240224
ocpx_fan_19964051_other_20240310lastslot
ocpx_fan_22287547_other_20240307

shard_idx=10
ocpx_cpl_22327987_other_20240310
ocpx_cpl_22025614_vertical_video_20240309lastslot
ocpx_app_22202692_other_20240308

shard_idx=11
ocpx_app_22243530_mainfeed_20240306
ocpx_app_18774341_other_20240305lastslot

shard_idx=12
ocpx_app_22276505_other_20240302lastslot
ocpx_app_20041469_vertical_video_20240310lastslot

shard_idx=13
ocpx_cpl_22316879_queryfeed_20240308
ocpx_app_22306747_other_20240307

shard_idx=14
ocpx_app_22024254_other_20240308
ocpx_app_21877105_comments_list_20240303lastslot

shard_idx=15
ocpx_adjustcost_22134355_20240312
ocpx_app_22257806_other_20240308
ocpx_cpl_21862394_hot_tweets_feed_20240306

1 change: 1 addition & 0 deletions tests/src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ mod mysql_strategy;
mod number;
mod ring_buffer;
mod select;
mod shard_checker;
mod time;
mod tx_buffer;
2 changes: 2 additions & 0 deletions tests/src/bkdrsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ fn check_file(idx: usize, fname: &str, hasher: Hasher, dist: Distribute) {
let idx_line = dist.index(hash);
if idx_line != idx {
println!("line:{}, hash:{}, idx:{}", line, hash, idx_line);
} else {
println!("succeed line:{}, hash:{}, idx:{}", line, hash, idx_line);
}
assert_eq!(
idx, idx_line,
Expand Down
1 change: 1 addition & 0 deletions tests/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Lookup for Dns {
}
}
}
#[ignore = "暂时注释掉,#439 有修正"]
#[test]
fn dns_lookup() {
let mut query: Vec<Vec<String>> = Vec::new();
Expand Down
Loading

0 comments on commit 112930c

Please sign in to comment.