diff --git a/openframe-api-lib/src/main/java/com/openframe/api/service/rmm/fleet/FleetCalls.java b/openframe-api-lib/src/main/java/com/openframe/api/service/rmm/fleet/FleetCalls.java index faf1b94292..a5d7f3e34c 100644 --- a/openframe-api-lib/src/main/java/com/openframe/api/service/rmm/fleet/FleetCalls.java +++ b/openframe-api-lib/src/main/java/com/openframe/api/service/rmm/fleet/FleetCalls.java @@ -5,10 +5,15 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.Semaphore; import java.util.function.Function; public final class FleetCalls { + // one Fleet serves every tenant and its agents (1 CPU on prod), so a page must not fire dozens of calls at once + static final int MAX_IN_FLIGHT = 8; + private static final Semaphore IN_FLIGHT = new Semaphore(MAX_IN_FLIGHT); + private FleetCalls() { } @@ -17,12 +22,21 @@ public static List inParallel(Collection keys, Function call) List> pending; try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { pending = keys.stream() - .map(key -> executor.submit(() -> call.apply(key))) + .map(key -> executor.submit(() -> limited(call, key))) .toList(); } return pending.stream().map(FleetCalls::resultOf).toList(); } + private static R limited(Function call, T key) throws InterruptedException { + IN_FLIGHT.acquire(); + try { + return call.apply(key); + } finally { + IN_FLIGHT.release(); + } + } + private static R resultOf(Future future) { if (future.state() == Future.State.FAILED && future.exceptionNow() instanceof RuntimeException e) { throw e; diff --git a/openframe-api-lib/src/test/java/com/openframe/api/service/rmm/fleet/FleetCallsTest.java b/openframe-api-lib/src/test/java/com/openframe/api/service/rmm/fleet/FleetCallsTest.java new file mode 100644 index 0000000000..1a94ddd0c8 --- /dev/null +++ b/openframe-api-lib/src/test/java/com/openframe/api/service/rmm/fleet/FleetCallsTest.java @@ -0,0 +1,56 @@ +package com.openframe.api.service.rmm.fleet; + +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.IntStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class FleetCallsTest { + + @Test + void inParallel_moreCallsThanTheCap_neverMoreInFlightAndResultsKeepKeyOrder() { + // setup + AtomicInteger inFlight = new AtomicInteger(); + AtomicInteger peak = new AtomicInteger(); + List keys = IntStream.range(0, 30).boxed().toList(); + + // execution + List results = FleetCalls.inParallel(keys, key -> { + peak.accumulateAndGet(inFlight.incrementAndGet(), Math::max); + sleep(30); + inFlight.decrementAndGet(); + return key * 10; + }); + + // verifications + assertThat(results).containsExactlyElementsOf(keys.stream().map(key -> key * 10).toList()); + assertThat(peak.get()).isBetween(2, FleetCalls.MAX_IN_FLIGHT); + } + + @Test + void inParallel_failingCall_rethrowsItsException() { + // execution + IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> FleetCalls.inParallel(List.of(1, 2), key -> { + if (key == 2) { + throw new IllegalStateException("fleet down"); + } + return key; + })); + + // verifications + assertThat(ex.getMessage()).isEqualTo("fleet down"); + } + + private static void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +}