Function compute_worker_quotas

Source
fn compute_worker_quotas<W, S>(
    workers: &BTreeMap<W, NonZeroUsize>,
    actor_counts: &HashMap<&W, usize>,
    total_vnodes: usize,
    salt: S,
) -> BTreeMap<W, NonZeroUsize>
where W: Ord + Clone + Hash + Eq + Debug, S: Hash + Copy,
Expand description

Computes per-worker VNode quotas based on actor counts and worker weights.

This function allocates virtual nodes to workers such that:

  • Each active worker receives at least as many virtual nodes as it has actors (base_quota).
  • The remaining virtual nodes (extra_vnodes) are distributed proportionally to the original worker weights.
  • Deterministic tie-breaking on equal remainders uses a hash of (salt, worker_id).

§Type Parameters

  • W: Worker identifier type. Must implement Ord, Clone, Hash, Eq, and Debug.
  • S: Salt type. Used for deterministic hashing. Must implement Hash and Copy.

§Parameters

  • workers: A BTreeMap mapping each worker ID to its non-zero weight (NonZeroUsize).
  • actor_counts: A HashMap mapping each worker ID to the number of actors assigned.
  • total_vnodes: The total number of virtual nodes to distribute across all active workers.
  • salt: A salt value for deterministic tie-breaking in remainder sorting.

§Returns

A BTreeMap from worker ID to its allocated quota (NonZeroUsize), such that the sum of all quotas equals total_vnodes.

§Panics

Panics if any computed quota is zero, which should not occur when total_vnodes >= sum(actor_counts).

§Algorithm

  1. Compute base_total as the sum of all actor counts.
  2. Compute extra_vnodes = total_vnodes - base_total.
  3. For each active worker: a. Set base_quota equal to its actor count. b. Compute ideal_extra = extra_vnodes * weight / total_weight. c. Record extra_floor = floor(ideal_extra) and extra_remainder = ideal_extra % total_weight.
  4. Sort workers by descending extra_remainder; tie-break by stable_hash((salt, worker_id)) ascending.
  5. Distribute the remaining slots (extra_vnodes - sum(extra_floor)) by incrementing extra_floor for the top workers.
  6. Final quota for each worker is base_quota + extra_floor.