Content-Length: 352589 | pFad | http://github.com/doyoubi/undermoon/commit/10b9b646c0f110ea6b2d94aebb79f3058943798e

75 Amend node id format in the output of CLUSTER NODES · doyoubi/undermoon@10b9b64 · GitHub
Skip to content

Commit

Permalink
Amend node id format in the output of CLUSTER NODES
Browse files Browse the repository at this point in the history
  • Loading branch information
doyoubi committed Mar 20, 2022
1 parent 1c13057 commit 10b9b64
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/proxy/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,8 @@ fn gen_cluster_nodes_helper(
cluster_nodes_version: ClusterNodesVersion,
) -> String {
let mut cluster_nodes = String::from("");
let name_seg = gen_cluster_name_seg(name);
for (addr, ranges) in slot_ranges {
let id = gen_node_id(name_seg.as_str(), addr.as_str());
let id = gen_node_id(name, addr.as_str());
let address = match cluster_nodes_version {
ClusterNodesVersion::V1 => addr.clone(),
ClusterNodesVersion::V2 => format!("{}@{}", addr, CLUSTER_NODES_CPORT),
Expand Down Expand Up @@ -681,17 +680,15 @@ fn gen_cluster_nodes_helper(
}

#[inline]
fn gen_cluster_name_seg(cluster_name: &ClusterName) -> String {
let mut name_seg = format!("{:_<20}", cluster_name.to_string());
name_seg.truncate(20);
name_seg
}

#[inline]
fn gen_node_id(cluster_name_seg: &str, addr: &str) -> String {
let mut addr_hash_seg = format!("{:_<20x}", crc64(0, addr.as_bytes()));
addr_hash_seg.truncate(20);
format!("{}{}", cluster_name_seg, addr_hash_seg)
fn gen_node_id(cluster_name: &ClusterName, addr: &str) -> String {
// 24 bytes cluster name + 16 bytes address hash
// format!("{:x}", u64::MAX).len() == 16
let mut name_seg = format!("{:_<24}", cluster_name.to_string());
name_seg.truncate(24);

let mut addr_hash_seg = format!("{:_<16x}", crc64(0, addr.as_bytes()));
addr_hash_seg.truncate(16);
format!("{}{}", name_seg, addr_hash_seg)
}

fn should_ignore_slots(
Expand Down Expand Up @@ -720,8 +717,6 @@ fn gen_cluster_slots_helper(
slot_ranges: &HashMap<String, Vec<SlotRange>>,
migration_states: &HashMap<RangeList, MigrationState>,
) -> Result<Vec<RespVec>, String> {
let name_seg = gen_cluster_name_seg(name);

let mut slot_range_element = Vec::new();
for (addr, ranges) in slot_ranges {
let mut segs = addr.split(':');
Expand All @@ -737,7 +732,7 @@ fn gen_cluster_slots_helper(
continue;
}

let node_id = gen_node_id(name_seg.as_str(), addr.as_str());
let node_id = gen_node_id(name, addr.as_str());
let ip_port_array = Resp::Arr(Array::Arr(vec![
Resp::Bulk(BulkStr::Str(host.as_bytes().to_vec())),
Resp::Integer(port.as_bytes().to_vec()),
Expand Down Expand Up @@ -765,6 +760,21 @@ mod tests {
use std::convert::TryFrom;
use std::iter::repeat;

#[test]
fn test_gen_node_id() {
assert_eq!(format!("{:x}", u64::MAX).len(), 16);

let short_cluster_name: String = (0..10).map(|_| 'x').collect();
let short = ClusterName::try_from(short_cluster_name.as_str()).unwrap();
let node_id = gen_node_id(&short, "127.0.0.1:6379");
assert_eq!(node_id.len(), 40);

let long_cluster_name: String = (0..30).map(|_| 'x').collect();
let long = ClusterName::try_from(long_cluster_name.as_str()).unwrap();
let node_id = gen_node_id(&long, "127.0.0.1:6379");
assert_eq!(node_id.len(), 40);
}

fn gen_testing_slot_ranges(address: &str) -> HashMap<String, Vec<SlotRange>> {
let mut slot_ranges = HashMap::new();
slot_ranges.insert(
Expand Down Expand Up @@ -819,7 +829,7 @@ mod tests {
true,
ClusterNodesVersion::V2,
);
assert_eq!(output, "testcluster_________9f8fca2805923328____ 127.0.0.1:5299@5299 myself,master - 0 0 233 connected 0-100 300\n");
assert_eq!(output, "testcluster_____________9f8fca2805923328 127.0.0.1:5299@5299 myself,master - 0 0 233 connected 0-100 300\n");
}

#[test]
Expand All @@ -837,7 +847,7 @@ mod tests {
false,
ClusterNodesVersion::V2,
);
assert_eq!(output, format!("testcluster_________a744988af9aa86ed____ {}@5299 master - 0 0 233 connected 0-100 300\n", long_address));
assert_eq!(output, format!("testcluster_____________a744988af9aa86ed {}@5299 master - 0 0 233 connected 0-100 300\n", long_address));
}

#[test]
Expand All @@ -855,7 +865,7 @@ mod tests {
);
assert_eq!(
output,
"testcluster_________9f8fca2805923328____ 127.0.0.1:5299@5299 master - 0 0 233 connected 0-1000\n"
"testcluster_____________9f8fca2805923328 127.0.0.1:5299@5299 master - 0 0 233 connected 0-1000\n"
);
}

Expand All @@ -878,7 +888,7 @@ mod tests {
);
assert_eq!(
output,
"testcluster_________9f8fca2805923328____ 127.0.0.1:5299@5299 myself,master - 0 0 233 connected\n"
"testcluster_____________9f8fca2805923328 127.0.0.1:5299@5299 myself,master - 0 0 233 connected\n"
);
}

Expand All @@ -901,7 +911,7 @@ mod tests {
);
assert_eq!(
output,
"testcluster_________9f8fca2805923328____ 127.0.0.1:5299@5299 myself,master - 0 0 233 connected 0-1000\n"
"testcluster_____________9f8fca2805923328 127.0.0.1:5299@5299 myself,master - 0 0 233 connected 0-1000\n"
);
}

Expand All @@ -920,7 +930,7 @@ mod tests {
);
assert_eq!(
output,
"testcluster_________9f8fca2805923328____ 127.0.0.1:5299@5299 master - 0 0 233 connected\n"
"testcluster_____________9f8fca2805923328 127.0.0.1:5299@5299 master - 0 0 233 connected\n"
);
}

Expand All @@ -943,7 +953,7 @@ mod tests {
);
assert_eq!(
output,
"testcluster_________9f8fca2805923328____ 127.0.0.1:5299@5299 myself,master - 0 0 233 connected 0-1000\n"
"testcluster_____________9f8fca2805923328 127.0.0.1:5299@5299 myself,master - 0 0 233 connected 0-1000\n"
);
}

Expand All @@ -966,7 +976,7 @@ mod tests {
);
assert_eq!(
output,
"testcluster_________9f8fca2805923328____ 127.0.0.1:5299@5299 myself,master - 0 0 233 connected\n"
"testcluster_____________9f8fca2805923328 127.0.0.1:5299@5299 myself,master - 0 0 233 connected\n"
);
}

Expand All @@ -983,7 +993,7 @@ mod tests {
Resp::Bulk(BulkStr::Str("127.0.0.1".to_string().into_bytes())),
Resp::Integer(5299.to_string().into_bytes()),
Resp::Bulk(BulkStr::Str(
b"test_cluster_name___9f8fca2805923328____".to_vec(),
b"test_cluster_name_______9f8fca2805923328".to_vec(),
)),
])),
]));
Expand All @@ -994,7 +1004,7 @@ mod tests {
Resp::Bulk(BulkStr::Str("127.0.0.1".to_string().into_bytes())),
Resp::Integer(5299.to_string().into_bytes()),
Resp::Bulk(BulkStr::Str(
b"test_cluster_name___9f8fca2805923328____".to_vec(),
b"test_cluster_name_______9f8fca2805923328".to_vec(),
)),
])),
]));
Expand Down

0 comments on commit 10b9b64

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/doyoubi/undermoon/commit/10b9b646c0f110ea6b2d94aebb79f3058943798e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy