Skip to content

Shard balancing can put all of a project's tests on one already busy shard #89

Done

Description

@miluoshi

shardBalancing sometimes puts all tests of one project on a single shard, even though that shard already has as much work as the others. That shard then runs much longer than the rest. It happens when projects depend on different sets of setup projects, for example A on setup-a and B on setup-b, or A on auth and db and B only on auth. A single project, projects without dependencies, and projects that all depend on the same setup projects aren't affected.

A small example with 2 shards:

  • project A: setup 10 s, two tests of 100 s
  • project B: its own setup 1 s, two tests of 50 s

On main (7608bc3), balanceShards() returns [211, 110]. Both B tests end up on the same shard. With one on each shard, it would be [161, 161].

A has the bigger setup, so it's placed first and each shard already has 110 s when B is placed. To pick how many shards B should use, computeSpan() estimates the busiest one as setup + work / k, as if the shards were empty. With k = 1 that's 101 s, below the 160.5 s average, so one shard looks best and a second one would only add another B setup.

Proposed fix

This adds the work already on the k least loaded shards to that estimate, the same way selectShards() already counts shard.total. The existing sharding tests pass with it.

@@ -82,9 +82,9 @@ function unseenSetup(shards: Shard[], family: Family): number {
  * that the makespan lower bound is as low as possible, for the current shard allocation.
 
  * The makespan lower bound is defined like this:
- * - makespan >= family.work / K + family.setup
- *   Makespan is AT LEAST the chunk of work + a family setup. We might have a shard that
- *   has only this chunk of a family
+ * - makespan >= max(load[K], (load[1] + ... + load[K] + family.work) / K)
+ *   where load[i] is the i-th least loaded shard's total plus the family setup it
+ *   still misses. The family's chunks land on top of what the hosts already run.
  * - makespan >= (total + minMissingSetup(K) - unseenSetup) / N
  *   Makespan is AT LEAST the absolute perfect balancing of the job. `minMissingSetup(K)`
  *   is the setup missing from the K cheapest host shards, while `unseenSetup` is the
@@ -165,6 +165,8 @@ function estimateTotalWork(shards: Shard[], families: Family[]): number {
 function computeSpan(shards: Shard[], families: Family[], heaviest: Family, N: number) {
   const total = estimateTotalWork(shards, [...families, heaviest]);
   const missingSetups = shards.map(shard => missingSetup(shard, heaviest)).sort((a, b) => a - b);
+  // Each shard's load if it hosted this family (current total + missing setup), least loaded first.
+  const hostLoads = shards.map(shard => shard.total + missingSetup(shard, heaviest)).sort((a, b) => a - b);
   const unseen = unseenSetup(shards, heaviest);
 
   let minMakeSpan = Infinity;
@@ -173,9 +175,12 @@ function computeSpan(shards: Shard[], families: Family[], heaviest: Family, N: n
   // Add the setup missing from the K cheapest hosts, then subtract the
   // reserved copy once to avoid double-counting it.
   let minMissingSetup = 0;
+  let hostLoad = 0;
   for (let k = 1; k <= Math.min(N, heaviest.groups.length); ++k) {
     minMissingSetup += missingSetups[k - 1];
-    const localMakespan = heaviest.setup + heaviest.work / k;
+    hostLoad += hostLoads[k - 1];
+    // The K hosts already have work, so the family's share lands on top of it.
+    const localMakespan = Math.max(hostLoads[k - 1], (hostLoad + heaviest.work) / k);
     const avgMakespan = (total + minMissingSetup - unseen) / N;
     const atLeast = Math.max(avgMakespan, localMakespan);
     if (atLeast <= minMakeSpan) {

On our suite (about 900 tests, 16 projects, 20 shards), this brought the slowest shard down from 344 s to 245–289 s. The downside is that small projects run their setup on more shards: about 36–38 setup runs per CI run instead of 30.

I can open a PR with a test for this case in tests/sharding.spec.ts.

Activity

  1. aslushnikov commented on Nov 24, 2026

    @aslushnikov
    Contributor

    @miluoshi Interesting! A PR would be awesome; I'll play with it tonight.

    Than you for filing and doing the investigation!

  2. added 2 commits that reference this issue on Nov 24, 2026
    f68043d
    6a08922
  3. miluoshi commented on Nov 24, 2026

    @miluoshi
    ContributorAuthor

    Thanks! I opened #90 with the fix and a test for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions