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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -211,6 +212,13 @@ public Set<String> duplicateIds() {
return duplicateIds;
}

/**
* Returns the build steps in topological order (dependencies before dependents).
* <p>
* <strong>Note:</strong> The returned list is mutable and may be reordered in place
* by {@link #computeWeights(boolean, Map)} for scheduling purposes. After that call,
* the list reflects weight-based scheduling order rather than pure topological order.
*/
public List<BuildStep> sortedNodes() {
if (sortedNodes == null) {
synchronized (this) {
Expand Down Expand Up @@ -238,27 +246,70 @@ private static void visitNode(BuildStep node, Set<BuildStep> visited, List<Build
}

/**
* Computes critical-path weights for all steps in the plan. When {@code previousTimings}
* is non-empty, uses actual durations (in millis) from a previous build as step weights;
* steps not found in the map fall back to 1. When empty, uses graph depth (each step = 1).
* Computes critical-path weights and sorts plan steps for scheduling.
*
* <p>When {@code parallel} is true, computes critical-path weights using
* {@code previousTimings} (actual durations in millis from a prior build) or
* graph depth as fallback, then sorts by descending weight so the scheduler
* picks the heaviest-path steps first. When weights are equal, steps from the
* same project are grouped together (by reactor order) so that a project's
* phases run consecutively when no higher-weight step from another project
* is competing for a thread.</p>
*
* <p>When {@code parallel} is false (single-thread), all step weights are reset
* to zero and steps are sorted by project reactor order alone, producing
* Maven-3-like sequential per-project execution.</p>
*/
void computeWeights(Map<String, Long> previousTimings) {
void computeWeights(boolean parallel, Map<String, Long> previousTimings) {
Comment thread
gnodet marked this conversation as resolved.
Resolved
List<BuildStep> sorted = sortedNodes();
for (int i = sorted.size() - 1; i >= 0; i--) {
BuildStep step = sorted.get(i);
long maxSuccessorWeight = 0;
for (BuildStep successor : step.successors) {
maxSuccessorWeight = Math.max(maxSuccessorWeight, successor.weight);
if (parallel) {
for (int i = sorted.size() - 1; i >= 0; i--) {
BuildStep step = sorted.get(i);
long maxSuccessorWeight = 0;
for (BuildStep successor : step.successors) {
maxSuccessorWeight = Math.max(maxSuccessorWeight, successor.weight);
}
long selfWeight;
if (previousTimings.isEmpty()) {
selfWeight = 1;
} else {
Long millis = previousTimings.get(step.stepId());
selfWeight = millis != null ? Math.max(1L, millis) : 1;
}
step.weight = selfWeight + maxSuccessorWeight;
}
long selfWeight;
if (previousTimings.isEmpty()) {
selfWeight = 1;
} else {
Long millis = previousTimings.get(step.stepId());
selfWeight = millis != null ? Math.max(1L, millis) : 1;
} else {
// Reset all weights to zero for single-thread mode so sorting is by project order alone
for (BuildStep step : sorted) {
step.weight = 0;
}
step.weight = selfWeight + maxSuccessorWeight;
}
Map<MavenProject, Integer> projectIndex = projectIndex();
sorted.sort(Comparator.comparingLong((BuildStep s) -> s.weight)
Comment thread
gnodet marked this conversation as resolved.
Resolved
.reversed()
Comment thread
gnodet marked this conversation as resolved.
Resolved
.thenComparingInt(s -> {
Integer index = projectIndex.get(s.project);
if (index == null) {
throw new IllegalStateException("BuildStep project not found in plan: " + s.project.getId());
}
return index;
}));
}

/**
* Builds an index mapping each project to its position in the reactor build order.
* <p>
* <strong>Critical assumption:</strong> This relies on {@code plan} being a LinkedHashMap
* with projects inserted in reactor order. If projects are added out of order, the returned
* indices will not reflect correct reactor ordering, breaking single-thread build sequencing.
*/
private Map<MavenProject, Integer> projectIndex() {
Map<MavenProject, Integer> index = new HashMap<>();
int i = 0;
for (MavenProject project : plan.keySet()) {
Comment thread
gnodet marked this conversation as resolved.
Resolved
index.put(project, i++);
}
return index;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -880,13 +880,13 @@ private void plan() {
}

Map<String, Long> previousTimings = Map.of();
if (isStepTimingsEnabled()) {
if (threads > 1 && isStepTimingsEnabled()) {
Path rootDir = rootDirectoryOrNull();
if (rootDir != null) {
previousTimings = StepTimings.read(rootDir);
}
}
plan.computeWeights(previousTimings);
plan.computeWeights(threads > 1, previousTimings);

plan.initReadyQueue(readyQueue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Unit tests for {@link BuildPlan#computeWeights(Map)}.
* Unit tests for {@link BuildPlan#computeWeights(boolean, Map)}.
*/
class BuildPlanWeightsTest {

Expand All @@ -41,7 +41,7 @@ void singleStepGetsWeightOne() {
BuildStep a = step("compile");
BuildPlan plan = planOf(a);

plan.computeWeights(Map.of());
plan.computeWeights(true, Map.of());

assertEquals(1L, a.weight);
}
Expand All @@ -56,7 +56,7 @@ void linearChainPropagatesWeightsCorrectly() {
link(b, c);
BuildPlan plan = planOf(a, b, c);

plan.computeWeights(Map.of());
plan.computeWeights(true, Map.of());

// c has no successors → weight = 1
// b depends on c → weight = 1 (self) + 1 (c) = 2
Expand All @@ -79,7 +79,7 @@ void heavierPathWinsAtBranchPoint() {
link(root, right);
BuildPlan plan = planOf(root, left, leaf, right);

plan.computeWeights(Map.of());
plan.computeWeights(true, Map.of());

// leaf: 1, left: 2, right: 1, root: max(2,1)+1 = 3
assertEquals(1L, leaf.weight);
Expand All @@ -94,7 +94,7 @@ void disconnectedStepsEachGetWeightOne() {
BuildStep b = step("test");
BuildPlan plan = planOf(a, b);

plan.computeWeights(Map.of());
plan.computeWeights(true, Map.of());

assertEquals(1L, a.weight);
assertEquals(1L, b.weight);
Expand All @@ -115,7 +115,7 @@ void timingDataIsUsedAsStepWeight() {
Map<String, Long> timings = Map.of(
a.stepId(), 50L,
b.stepId(), 200L);
plan.computeWeights(timings);
plan.computeWeights(true, timings);

// b: weight = 200 (self, no successor)
// a: weight = 50 (self) + 200 (b) = 250
Expand All @@ -130,7 +130,7 @@ void missingTimingEntryFallsBackToOne() {
BuildPlan plan = planOf(known, unknown);

Map<String, Long> timings = Map.of(known.stepId(), 100L);
plan.computeWeights(timings);
plan.computeWeights(true, timings);

assertEquals(100L, known.weight);
assertEquals(1L, unknown.weight); // fallback
Expand All @@ -142,7 +142,7 @@ void zeroMillisTimingClampedToOne() {
BuildStep a = step("com.example", "a", "compile");
BuildPlan plan = planOf(a);

plan.computeWeights(Map.of(a.stepId(), 0L));
plan.computeWeights(true, Map.of(a.stepId(), 0L));

// Math.max(1L, 0L) = 1
assertEquals(1L, a.weight);
Expand All @@ -162,7 +162,7 @@ void sortedNodesAreOrderedByDescendingWeightAfterComputeWeights() {
link(b, c);
BuildPlan plan = planOf(a, b, c);

plan.computeWeights(Map.of());
plan.computeWeights(true, Map.of());

// After computeWeights(), sortedNodes() must be ordered heaviest-first.
java.util.List<BuildStep> sorted = plan.sortedNodes();
Expand All @@ -171,6 +171,95 @@ void sortedNodesAreOrderedByDescendingWeightAfterComputeWeights() {
assertEquals(a, sorted.get(0), "Heaviest step (root) must come first");
}

// -------------------------------------------------------------------------
Comment thread
gnodet marked this conversation as resolved.
Resolved
// Single-thread (non-parallel) mode groups steps by project
// -------------------------------------------------------------------------

@Test
void singleThreadGroupsStepsByProjectOrder() {
// Two independent projects, each with compile → test
MavenProject projectA = project("com.example", "a");
MavenProject projectB = project("com.example", "b");
BuildStep a1 = new BuildStep("compile", projectA, null);
BuildStep a2 = new BuildStep("test", projectA, null);
BuildStep b1 = new BuildStep("compile", projectB, null);
BuildStep b2 = new BuildStep("test", projectB, null);
link(a1, a2);
link(b1, b2);
BuildPlan plan = planOfMulti(
Map.entry(projectA, java.util.List.of(a1, a2)), Map.entry(projectB, java.util.List.of(b1, b2)));

plan.computeWeights(false, Map.of());

java.util.List<BuildStep> sorted = plan.sortedNodes();
// All steps for project A should come before all steps for project B
int lastA = -1, firstB = sorted.size();
for (int i = 0; i < sorted.size(); i++) {
if (sorted.get(i).project == projectA) {
lastA = i;
}
if (sorted.get(i).project == projectB && i < firstB) {
firstB = i;
}
}
assertTrue(lastA < firstB, "All steps for project A should precede all steps for project B");
}

@Test
void parallelModeWithEqualWeightsStillGroupsByProject() {
// Same setup, but parallel mode — equal weights should still group by project
MavenProject projectA = project("com.example", "a2");
MavenProject projectB = project("com.example", "b2");
BuildStep a1 = new BuildStep("compile", projectA, null);
BuildStep a2 = new BuildStep("test", projectA, null);
BuildStep b1 = new BuildStep("compile", projectB, null);
BuildStep b2 = new BuildStep("test", projectB, null);
link(a1, a2);
link(b1, b2);
BuildPlan plan = planOfMulti(
Map.entry(projectA, java.util.List.of(a1, a2)), Map.entry(projectB, java.util.List.of(b1, b2)));

plan.computeWeights(true, Map.of());

// a1 and b1 both have weight 2, a2 and b2 both have weight 1
// With the project-order tiebreaker, same-weight steps from A come before B
java.util.List<BuildStep> sorted = plan.sortedNodes();
int a1Pos = sorted.indexOf(a1);
int b1Pos = sorted.indexOf(b1);
assertTrue(a1Pos < b1Pos, "Equal-weight steps from project A should come before project B");
}

@Test
void singleThreadWithCrossProjectDependenciesRespectsTopology() {
// Project A and B in reactor order [A, B], where B's compile depends on A's test
// Topology: a1 → a2 → b1 → b2
MavenProject projectA = project("com.example", "a3");
MavenProject projectB = project("com.example", "b3");
BuildStep a1 = new BuildStep("compile", projectA, null);
BuildStep a2 = new BuildStep("test", projectA, null);
BuildStep b1 = new BuildStep("compile", projectB, null);
BuildStep b2 = new BuildStep("test", projectB, null);
link(a1, a2);
link(a2, b1); // Cross-project dependency: B's compile depends on A's test
link(b1, b2);
BuildPlan plan = planOfMulti(
Map.entry(projectA, java.util.List.of(a1, a2)), Map.entry(projectB, java.util.List.of(b1, b2)));

plan.computeWeights(false, Map.of());

// Verify that topological order is maintained despite grouping by project
java.util.List<BuildStep> sorted = plan.sortedNodes();
int a1Pos = sorted.indexOf(a1);
int a2Pos = sorted.indexOf(a2);
int b1Pos = sorted.indexOf(b1);
int b2Pos = sorted.indexOf(b2);
assertTrue(a1Pos < a2Pos, "a1 must precede a2 (intra-project dependency)");
assertTrue(a2Pos < b1Pos, "a2 must precede b1 (cross-project dependency)");
assertTrue(b1Pos < b2Pos, "b1 must precede b2 (intra-project dependency)");
// All A steps should come before all B steps due to the cross-project dependency
assertTrue(a2Pos < b1Pos && a1Pos < b1Pos, "All A steps must precede B steps");
}

// -------------------------------------------------------------------------
// stepId() does not include version
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -231,4 +320,17 @@ private static BuildPlan planOf(BuildStep... steps) {
}
return plan;
}

@SafeVarargs
private static BuildPlan planOfMulti(Map.Entry<MavenProject, java.util.List<BuildStep>>... entries) {
BuildPlan plan = new BuildPlan();
for (Map.Entry<MavenProject, java.util.List<BuildStep>> entry : entries) {
Map<String, BuildStep> stepMap = new LinkedHashMap<>();
for (BuildStep step : entry.getValue()) {
stepMap.put(step.name, step);
}
plan.addProject(entry.getKey(), stepMap);
}
return plan;
}
}
Loading