Skip to content

Commit 392f32d

Browse files
Configure thread pool executor to run in LIFO order to minimize blocked task count (#4701)
Fixes #4611 We basically reverse the execution order of tasks submitted to the `mill.exec.ExecutionContexts.ThreadPool` executor to be LIFO order instead of FIFO order. Since child tasks are always spawned after parent tasks, this prioritizes scheduling child tasks (which can actually run and do work) over parent tasks (which are sometimes blocked waiting for child tasks to be scheduled, run, and complete). There's no `LinkedBlockingStack` equivalent of `LinkedBlockingQueue`, but `LinkedBlockingDeque` can serve the same purpose by forwarding `poll` and `take` to `pollLast` and `takeLast`. `ThreadPoolExecutor` only seems to call `size`, `isEmpty`, `remove`, `poll`, `take`, `offer` on it's `workQueue` reference, so we don't need to worry about forwarding other methods. This LIFO order is similar to the approach that `ForkJoinPool` and other schedulers that prioritize child tasks take. Ideally we would use a `PriorityBlockingQueue` to properly prioritize all child tasks to run before all parent tasks, but because of our usage of Scala `Future`s tasks end up being wrapped and it gets hard to distinguish exactly where the `Runnable`s submitted to the queue actually come from. Using a LIFO is a crude approximation of the prioritization we actually want, but has the benefit of working without any knowledge of the individual tasks that are submitted, with the downside that later-scheduled parent tasks can take priority over earlier-scheduled child tasks spawned from an even-earlier parent. Tested locally via `./mill-assembly.jar -i integration.__.local.server`. Without this patch, we see large numbers of tasks buffering up in the prompt. with this PR, it seems to be limited to 5. Also tweaked some of the `test-grouping-parallel` tests --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent a51c98d commit 392f32d

File tree

93 files changed

+65
-480
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+65
-480
lines changed

core/exec/src/mill/exec/ExecutionContexts.scala

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import os.Path
55

66
import scala.concurrent.{Await, Future}
77
import scala.concurrent.duration.Duration
8-
import java.util.concurrent.{ExecutorService, LinkedBlockingQueue, ThreadPoolExecutor, TimeUnit}
8+
import java.util.concurrent.{ExecutorService, LinkedBlockingDeque, ThreadPoolExecutor, TimeUnit}
99
import mill.api.Logger
1010

1111
private object ExecutionContexts {
@@ -38,7 +38,15 @@ private object ExecutionContexts {
3838
threadCount0,
3939
0,
4040
TimeUnit.SECONDS,
41-
new LinkedBlockingQueue[Runnable]()
41+
// Use a `Deque` rather than a normal `Queue`, with the various `poll`/`take`
42+
// operations reversed, providing elements in a LIFO order. This ensures that
43+
// child `fork.async` tasks always take priority over parent tasks, avoiding
44+
// large numbers of blocked parent tasks from piling up
45+
new LinkedBlockingDeque[Runnable]() {
46+
override def poll(): Runnable = super.pollLast()
47+
override def poll(timeout: Long, unit: TimeUnit): Runnable = super.pollLast(timeout, unit)
48+
override def take(): Runnable = super.takeLast()
49+
}
4250
)
4351

4452
val threadPool: ExecutorService = executor

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsA.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class RandomTestsA extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Storm", 38);
8+
testGreeting("Storm", 380);
99
}
1010
// Removed other tests to simplify
1111
}

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsB.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class RandomTestsB extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Dakota", 18);
8+
testGreeting("Dakota", 180);
99
}
1010
}

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsC.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class RandomTestsC extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Jordan", 95);
8+
testGreeting("Jordan", 950);
99
}
1010
}

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsD.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class RandomTestsD extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Kai", 14);
8+
testGreeting("Kai", 140);
99
}
1010
}

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsE.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class RandomTestsE extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Sage", 28);
8+
testGreeting("Sage", 280);
99
}
1010
}

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsF.java

-10
This file was deleted.

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsG.java

-10
This file was deleted.

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsH.java

-10
This file was deleted.

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsI.java

-10
This file was deleted.

example/javalib/testing/4-test-parallel/foo/test/src/foo/RandomTestsJ.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX1.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupX1 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Aether", 55);
8+
testGreeting("Aether", 550);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX10.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX2.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupX2 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Chronos", 35);
8+
testGreeting("Chronos", 350);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX3.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupX3 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Fortuna", 25);
8+
testGreeting("Fortuna", 250);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX4.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupX4 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Janus", 21);
8+
testGreeting("Janus", 210);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX5.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupX5 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Orion", 95);
8+
testGreeting("Orion", 950);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX6.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX7.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX8.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupX9.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY1.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupY1 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Hades", 15);
8+
testGreeting("Hades", 150);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY10.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY2.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupY2 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Odin", 34);
8+
testGreeting("Odin", 340);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY3.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupY3 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Ra", 21);
8+
testGreeting("Ra", 210);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY4.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupY4 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Wotan", 95);
8+
testGreeting("Wotan", 950);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY5.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class GroupY5 extends RandomTestsUtils {
66
@Test
77
public void test1() throws Exception {
8-
testGreeting("Xiuhtecuhtli", 26);
8+
testGreeting("Xiuhtecuhtli", 260);
99
}
1010
}

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY6.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY7.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY8.java

-10
This file was deleted.

example/javalib/testing/6-test-group-parallel/foo/test/src/foo/GroupY9.java

-10
This file was deleted.

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsA.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class RandomTestsA : RandomTestsUtils() {
66
@Test
77
@Throws(Exception::class)
88
fun test1() {
9-
testGreeting("Storm", 38)
9+
testGreeting("Storm", 380)
1010
}
1111
}

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsB.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class RandomTestsB : RandomTestsUtils() {
66
@Test
77
@Throws(Exception::class)
88
fun test1() {
9-
testGreeting("Dakota", 18)
9+
testGreeting("Dakota", 180)
1010
}
1111
}

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsC.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class RandomTestsC : RandomTestsUtils() {
66
@Test
77
@Throws(Exception::class)
88
fun test1() {
9-
testGreeting("Jordan", 95)
9+
testGreeting("Jordan", 950)
1010
}
1111
}

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsD.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class RandomTestsD : RandomTestsUtils() {
66
@Test
77
@Throws(Exception::class)
88
fun test1() {
9-
testGreeting("Kai", 14)
9+
testGreeting("Kai", 140)
1010
}
1111
}

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsE.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class RandomTestsE : RandomTestsUtils() {
66
@Test
77
@Throws(Exception::class)
88
fun test1() {
9-
testGreeting("Sage", 28)
9+
testGreeting("Sage", 280)
1010
}
1111
}

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsF.kt

-11
This file was deleted.

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsG.kt

-11
This file was deleted.

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsH.kt

-11
This file was deleted.

example/kotlinlib/testing/4-test-parallel/foo/test/src/foo/RandomTestsI.kt

-11
This file was deleted.

0 commit comments

Comments
 (0)