Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ The exported CSV report includes all summary metrics along with granular site-le
* **Site Ordering**: Each site's ETA is calculated and sites are sorted in Descending Order (Heaviest -> Lightest) by ETA.
* **P90 Batches**: Sites whose ETA is above the 90th-percentile ETA are grouped `n_tail` at a time (largest first), where `n_tail` = (number of P90 sites ÷ **Parallel Batches**), rounded down and clamped between 1 and `k`. `k` depends on the license tier: ≤1,000 licenses → 1, 1,001–5,000 → 2, >5,000 → 3 (the lowest tier applies if licenses could not be determined). These groups are greedily assigned to the least-loaded lane and shown in **orange** in the Gantt chart and Batch Details. P90 batches in the same lane are merged while they hold ≤ 5,000 sites.
* **Remaining Sites**: Assigned one by one to the least-loaded lane, then split within each lane (heaviest first) into batches of up to 5,000 sites.
* **Lane Load**: "Least-loaded" is measured with the same batch ETAs shown in the Gantt chart (each batch's ETA is computed on its combined file count and size, as merged/split), so lane assignment always matches the displayed lane durations.
* **Total ETA** = The duration of the single longest bucket (lane).

---
Expand Down
137 changes: 119 additions & 18 deletions tests/files/batch_planner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
from util import files_batch_planner as planner


def _sum_eta(etas):
return lambda positions: sum(etas[p] for p in positions)
def _additive(etas):
"""Per-site totals and an additive ETA: a batch ETA is the sum of its site ETAs."""
return [(e,) for e in etas], lambda totals: totals[0]


def _all_positions(plan):
Expand Down Expand Up @@ -71,7 +72,7 @@ def test_cap_limits_p90_group_size(self):
etas = [float(v) for v in range(100, 0, -1)]
for k, expected_sizes in [(1, [1] * 10), (2, [2] * 5), (3, [3, 3, 4])]:
with self.subTest(k=k):
plan = planner.plan_p90_lanes(etas, 2, 5000, _sum_eta(etas), k)
plan = planner.plan_p90_lanes(etas, 2, 5000, *_additive(etas), k)
self.assertEqual(plan.tail_group_size, k)
self.assertEqual(
planner.split_tail_into_groups(plan.tail_count, plan.tail_group_size),
Expand All @@ -81,7 +82,7 @@ def test_cap_limits_p90_group_size(self):

def test_invalid_cap_is_clamped_to_one(self):
etas = [float(v) for v in range(100, 0, -1)]
plan = planner.plan_p90_lanes(etas, 2, 5000, _sum_eta(etas), 0)
plan = planner.plan_p90_lanes(etas, 2, 5000, *_additive(etas), 0)
self.assertEqual(plan.tail_group_size, 1)


Expand All @@ -108,31 +109,31 @@ class PlanP90LanesTest(unittest.TestCase):
"""Covers end-to-end lane and batch planning."""

def test_empty_input(self):
plan = planner.plan_p90_lanes([], 3, 5000, lambda _p: 0.0, 5)
plan = planner.plan_p90_lanes([], 3, 5000, [], lambda _t: 0.0, 5)
self.assertEqual(plan.lanes, [[], [], []])
self.assertEqual(plan.tail_count, 0)

def test_single_site_has_no_tail(self):
plan = planner.plan_p90_lanes([5.0], 2, 5000, _sum_eta([5.0]), 5)
plan = planner.plan_p90_lanes([5.0], 2, 5000, *_additive([5.0]), 5)
self.assertEqual(plan.tail_count, 0)
self.assertEqual(plan.lanes, [[([0], False)], []])

def test_identical_etas_have_no_tail(self):
etas = [2.0] * 20
plan = planner.plan_p90_lanes(etas, 2, 5000, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 2, 5000, *_additive(etas), 5)
self.assertEqual(plan.tail_count, 0)
self.assertEqual([len(lane[0][0]) for lane in plan.lanes], [10, 10])

def test_zero_eta_sites_stay_out_of_tail(self):
etas = [9.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
plan = planner.plan_p90_lanes(etas, 2, 5000, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 2, 5000, *_additive(etas), 5)
self.assertEqual(plan.tail_count, 1)
self.assertEqual(_all_positions(plan), list(range(10)))

def test_user_example_two_lanes(self):
"""100 sites, 2 lanes: 10 P90 sites -> two P90 batches of 5 (top 5, next 5)."""
etas = [float(v) for v in range(100, 0, -1)]
plan = planner.plan_p90_lanes(etas, 2, 5000, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 2, 5000, *_additive(etas), 5)

self.assertEqual(plan.tail_count, 10)
self.assertEqual(plan.tail_group_size, 5)
Expand All @@ -143,36 +144,36 @@ def test_user_example_two_lanes(self):
def test_user_example_three_lanes(self):
"""10 P90 sites with 3 lanes -> P90 groups of 3, 3, 4."""
etas = [float(v) for v in range(100, 0, -1)]
plan = planner.plan_p90_lanes(etas, 3, 5000, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 3, 5000, *_additive(etas), 5)

p90_groups = [positions for lane in plan.lanes for positions, is_p90 in lane if is_p90]
self.assertEqual(sorted(len(g) for g in p90_groups), [3, 3, 4])
self.assertEqual(sorted(p for g in p90_groups for p in g), list(range(10)))

def test_p90_batches_come_first_in_each_lane(self):
etas = [float(v) for v in range(100, 0, -1)]
plan = planner.plan_p90_lanes(etas, 3, 5000, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 3, 5000, *_additive(etas), 5)
for lane in plan.lanes:
flags = [is_p90 for _, is_p90 in lane]
self.assertEqual(flags, sorted(flags, reverse=True))

def test_p90_batches_in_same_lane_are_merged(self):
"""With 1 lane every P90 group lands in the same lane and is merged into one batch."""
etas = [float(v) for v in range(200, 0, -1)]
plan = planner.plan_p90_lanes(etas, 1, 5000, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 1, 5000, *_additive(etas), 5)
p90_batches = [positions for positions, is_p90 in plan.lanes[0] if is_p90]
self.assertEqual(len(p90_batches), 1)
self.assertEqual(p90_batches[0], list(range(plan.tail_count)))

def test_p90_merge_respects_max_sites(self):
etas = [float(v) for v in range(200, 0, -1)] # 20 P90 sites, groups of 5
plan = planner.plan_p90_lanes(etas, 1, 12, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 1, 12, *_additive(etas), 5)
p90_sizes = [len(positions) for positions, is_p90 in plan.lanes[0] if is_p90]
self.assertEqual(p90_sizes, [10, 10])

def test_remaining_sites_chunked_by_max_sites_in_descending_eta(self):
etas = [float(v) for v in range(100, 0, -1)]
plan = planner.plan_p90_lanes(etas, 2, 7, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 2, 7, *_additive(etas), 5)
for lane in plan.lanes:
rest = [positions for positions, is_p90 in lane if not is_p90]
self.assertTrue(all(len(chunk) <= 7 for chunk in rest))
Expand All @@ -183,24 +184,91 @@ def test_remaining_sites_chunked_by_max_sites_in_descending_eta(self):
def test_remaining_sites_go_to_least_loaded_lane(self):
"""A lane loaded by a heavy P90 batch receives fewer remaining sites."""
etas = [1000.0] + [1.0] * 19
plan = planner.plan_p90_lanes(etas, 2, 5000, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 2, 5000, *_additive(etas), 5)
self.assertEqual(plan.tail_count, 1)
self.assertEqual(plan.lanes[0], [([0], True)])
self.assertEqual(len(plan.lanes[1][0][0]), 19)

def test_fewer_sites_than_lanes(self):
etas = [3.0, 2.0]
plan = planner.plan_p90_lanes(etas, 5, 5000, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 5, 5000, *_additive(etas), 5)
self.assertEqual(_all_positions(plan), [0, 1])
self.assertEqual(sum(1 for lane in plan.lanes if lane), 2)

def test_invalid_settings_are_clamped(self):
etas = [3.0, 2.0, 1.0]
plan = planner.plan_p90_lanes(etas, 0, 0, _sum_eta(etas), 5)
plan = planner.plan_p90_lanes(etas, 0, 0, *_additive(etas), 5)
self.assertEqual(len(plan.lanes), 1)
self.assertTrue(all(len(positions) == 1 for positions, _ in plan.lanes[0]))


def _max_eta(totals):
"""Non-additive batch ETA like the Files model: max(count cost, size cost)."""
return max(totals[0], totals[1])


def _sorted_sites(sites):
"""Sorts (count_cost, size_cost) sites by their standalone ETA, descending."""
sites = sorted(sites, key=_max_eta, reverse=True)
return [_max_eta(s) for s in sites], sites


class ExactLaneAccountingTest(unittest.TestCase):
"""Lane loads must equal the displayed batch ETAs for a non-additive ETA."""

def test_lane_loads_equal_recomputed_batch_etas(self):
import random

rng = random.Random(7)
sites = [
(rng.choice([rng.uniform(0, 50), 0.0]), rng.choice([rng.uniform(0, 50), 0.0]))
for _ in range(3000)
]
etas, totals = _sorted_sites(sites)
plan = planner.plan_p90_lanes(etas, 5, 400, totals, _max_eta, 3)

for lane, load in zip(plan.lanes, plan.lane_loads):
recomputed = sum(
_max_eta(tuple(map(sum, zip(*(totals[p] for p in positions)))))
for positions, _ in lane
)
self.assertAlmostEqual(load, recomputed, places=6)
self.assertEqual(_all_positions(plan), list(range(3000)))

def test_merged_p90_overlap_does_not_misplace_regular_sites(self):
"""Regression: a lane whose merged P90 batch overlaps must still look light.

A (count-bound, 100h) takes lane 1. B, C, D (30h each, mixed count/size)
merge into one 60h P90 batch in lane 2 (the old additive sum said 90h).
All 36 small sites must go to lane 2, keeping the total at 100h.
"""
sites = [(100.0, 0.0), (0.0, 30.0), (30.0, 0.0), (0.0, 30.0)] + [(0.0, 1.0)] * 36
etas, totals = _sorted_sites(sites)
plan = planner.plan_p90_lanes(etas, 2, 5000, totals, _max_eta, 1)

self.assertEqual(plan.tail_count, 4)
self.assertEqual(plan.lanes[0], [([0], True)])
self.assertEqual([is_p90 for _, is_p90 in plan.lanes[1]], [True, False])
self.assertEqual(len(plan.lanes[1][1][0]), 36)
self.assertAlmostEqual(max(plan.lane_loads), 100.0)
self.assertAlmostEqual(plan.lane_loads[1], 96.0)

def test_eta_evaluations_grow_linearly(self):
"""One ETA evaluation per P90 group and per regular site (no O(n^2) rescans)."""
n = 20000
calls = [0]

def counting_eta(totals):
calls[0] += 1
return _max_eta(totals)

etas, totals = _sorted_sites([(float(i % 97), float(i % 89)) for i in range(n)])
plan = planner.plan_p90_lanes(etas, 7, 5000, totals, counting_eta, 3)

groups = len(planner.split_tail_into_groups(plan.tail_count, plan.tail_group_size))
self.assertEqual(calls[0], groups + (n - plan.tail_count))


class CalculateMigrationBatchesTest(unittest.TestCase):
"""Integration test for FileMigrationEstimatorTool.calculate_migration_batches."""

Expand Down Expand Up @@ -270,7 +338,40 @@ def test_license_tier_sets_tail_group_cap(self):
planner, "plan_p90_lanes", wraps=planner.plan_p90_lanes
) as spy:
tool.calculate_migration_batches(self._df(list(range(1000, 0, -10))), license_metrics)
self.assertEqual(spy.call_args[0][4], expected_k)
self.assertEqual(spy.call_args[0][5], expected_k)

def test_real_eta_mixed_p90_sites_keep_lanes_balanced(self):
"""With the real ETA formula, overlapping P90 sites must not push work onto the longest lane."""
import pandas as pd
from estimators.file_estimator import FileEstimator
from util.constants import FILES_GLOBAL_CORPUS_SIZE_LIMIT

class _RealEta:
def calculate_migration_eta(self, data):
return FileEstimator.calculate_migration_eta(None, data)

tool = self._make_tool(num_lanes=2)
tool.factory.get_files_estimator.return_value = _RealEta()
qps = 4.8 * 0.8 # lowest license tier, OneDrive
count_hours = lambda h: int(h * 3600 * qps)
size_hours = lambda h: h * 3600 * FILES_GLOBAL_CORPUS_SIZE_LIMIT
rows = [
("A", count_hours(100), 1), # count-bound, 100h
("B", 1, size_hours(30)), # size-bound, 30h
("C", count_hours(30), 1), # count-bound, 30h
("D", 1, size_hours(30)), # size-bound, 30h
] + [(f"s{i}", 1, size_hours(1)) for i in range(36)]
df = pd.DataFrame([
{"Site Id": sid, "File Count": f, "Corpus Size": s, "Resource Count": f}
for sid, f, s in rows
])

_, batches, total_eta, buckets = tool.calculate_migration_batches(df, {})

lane_a = next(b for b in buckets if "A" in set(b["batches"][0]["df_subset"]["Site Id"]))
self.assertEqual(len(lane_a["batches"]), 1)
self.assertAlmostEqual(total_eta, 100.0, places=2)
self.assertEqual(sum(b["sites"] for b in batches), 40)

def test_empty_dataframe(self):
import pandas as pd
Expand Down
7 changes: 6 additions & 1 deletion ui/files_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,12 @@ def _positions_eta(positions):
get_total_license_count(licenseMetrics)
)
plan = files_batch_planner.plan_p90_lanes(
sorted_etas, num_lanes, max_sites_per_batch, _positions_eta, max_tail_group_size
sorted_etas,
num_lanes,
max_sites_per_batch,
list(zip(sizes, files, enc_files, enc_sizes)),
lambda totals: _eta_from_totals(*totals),
max_tail_group_size,
)

def _build_batch(positions, is_p90):
Expand Down
Loading
Loading