fn compute_worker_quotas<W, S>(
workers: &BTreeMap<W, NonZeroUsize>,
actor_counts: &HashMap<&W, usize>,
total_vnodes: usize,
salt: S,
) -> BTreeMap<W, NonZeroUsize>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 implementOrd,Clone,Hash,Eq, andDebug.S: Salt type. Used for deterministic hashing. Must implementHashandCopy.
§Parameters
workers: ABTreeMapmapping each worker ID to its non-zero weight (NonZeroUsize).actor_counts: AHashMapmapping 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
- Compute
base_totalas the sum of all actor counts. - Compute
extra_vnodes = total_vnodes - base_total. - For each active worker:
a. Set
base_quotaequal to its actor count. b. Computeideal_extra = extra_vnodes * weight / total_weight. c. Recordextra_floor = floor(ideal_extra)andextra_remainder = ideal_extra % total_weight. - Sort workers by descending
extra_remainder; tie-break bystable_hash((salt, worker_id))ascending. - Distribute the remaining slots (
extra_vnodes - sum(extra_floor)) by incrementingextra_floorfor the top workers. - Final quota for each worker is
base_quota + extra_floor.