balance: Add debug logging for p2c decisions (#77)

When debugging load balancer behavior, it's convenient to observe the
individual node selection decisions. To that end, this change requires
that `Load::Metric` implement `fmt::Debug` when used by
`PowerOfTwoChoices`.
This commit is contained in:
Oliver Gould 2018-06-05 07:32:10 -07:00 committed by GitHub
parent 01fd57c053
commit 5030a4f852
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -59,14 +59,24 @@ impl PowerOfTwoChoices {
impl<K, L> Choose<K, L> for PowerOfTwoChoices
where
L: Load,
L::Metric: PartialOrd,
L::Metric: PartialOrd + ::std::fmt::Debug,
{
/// Chooses two distinct nodes at random and compares their load.
///
/// Returns the index of the lesser-loaded node.
fn choose(&mut self, replicas: Replicas<K, L>) -> usize {
let (a, b) = self.random_pair(replicas.len());
if replicas[a].load() <= replicas[b].load() {
let a_load = replicas[a].load();
let b_load = replicas[b].load();
trace!(
"choose node[{a}]={a_load:?} node[{b}]={b_load:?}",
a = a,
b = b,
a_load = a_load,
b_load = b_load
);
if a_load <= b_load {
a
} else {
b

View File

@ -42,7 +42,7 @@ pub fn power_of_two_choices<D>(loaded: D) -> Balance<D, choose::PowerOfTwoChoice
where
D: Discover,
D::Service: Load,
<D::Service as Load>::Metric: PartialOrd,
<D::Service as Load>::Metric: PartialOrd + fmt::Debug,
{
Balance::new(loaded, choose::PowerOfTwoChoices::default())
}