diff --git a/src/main/java/io/github/hectorvent/floci/core/common/port/PortAllocator.java b/src/main/java/io/github/hectorvent/floci/core/common/port/PortAllocator.java index 36b80616b7..c5ae526857 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/port/PortAllocator.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/port/PortAllocator.java @@ -3,9 +3,13 @@ package io.github.hectorvent.floci.core.common.port; import io.github.hectorvent.floci.config.EmulatorConfig; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import org.jboss.logging.Logger; +import java.util.Locale; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Consumer; /** * Hands out ports from a configured range for Lambda Runtime API servers. @@ -15,9 +19,16 @@ import java.util.concurrent.ConcurrentHashMap; @ApplicationScoped public class PortAllocator { + private static final Logger LOG = Logger.getLogger(PortAllocator.class); + private static final int PRESSURE_WARNING_PERCENT = 90; + private final int basePort; private final int maxPort; + private final int poolSize; + private final int pressureWarningThreshold; private final Set inUse = ConcurrentHashMap.newKeySet(); + private final AtomicBoolean pressureWarningEmitted = new AtomicBoolean(); + private final Consumer warningSink; @Inject public PortAllocator(EmulatorConfig config) { @@ -26,13 +37,22 @@ public class PortAllocator { } PortAllocator(int basePort, int maxPort) { + this(basePort, maxPort, LOG::warn); + } + + PortAllocator(int basePort, int maxPort, Consumer warningSink) { this.basePort = basePort; this.maxPort = maxPort; + this.poolSize = maxPort - basePort + 1; + this.pressureWarningThreshold = Math.max(1, + (int) Math.ceil(poolSize * (PRESSURE_WARNING_PERCENT / 100.0))); + this.warningSink = warningSink; } public int allocate() { for (int p = basePort; p <= maxPort; p++) { if (inUse.add(p)) { + warnIfUnderPressure(); return p; } } @@ -43,7 +63,7 @@ public class PortAllocator { // learn the knob exists is to find this class in the source (issue #2206). throw new IllegalStateException( "Lambda Runtime API port pool exhausted: no free ports in range " - + basePort + "-" + maxPort + " (" + (maxPort - basePort + 1) + + basePort + "-" + maxPort + " (" + poolSize + " ports, all in use). One port is held per running Lambda container, " + "so this is the concurrent-execution ceiling. Widen it with " + "floci.services.lambda.runtime-api-base-port / " @@ -51,6 +71,24 @@ public class PortAllocator { } public void release(int port) { - inUse.remove(port); + if (!inUse.remove(port)) { + return; + } + if (inUse.size() < pressureWarningThreshold) { + pressureWarningEmitted.set(false); + } + } + + private void warnIfUnderPressure() { + int allocated = inUse.size(); + if (allocated < pressureWarningThreshold || !pressureWarningEmitted.compareAndSet(false, true)) { + return; + } + warningSink.accept(String.format(Locale.ROOT, + "Lambda Runtime API port pool is %d%% allocated (%d/%d ports in use, range %d-%d). " + + "One port is held per running Lambda container. If this workload may grow, widen " + + "floci.services.lambda.runtime-api-base-port / floci.services.lambda.runtime-api-max-port " + + "before the pool is exhausted.", + (allocated * 100) / poolSize, allocated, poolSize, basePort, maxPort)); } } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/port/PortAllocatorTest.java b/src/test/java/io/github/hectorvent/floci/core/common/port/PortAllocatorTest.java index b0b6896924..bf2c45efcd 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/port/PortAllocatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/port/PortAllocatorTest.java @@ -2,14 +2,18 @@ package io.github.hectorvent.floci.core.common.port; import org.junit.jupiter.api.Test; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; class PortAllocatorTest { @@ -51,13 +55,6 @@ class PortAllocatorTest { assertThrows(IllegalStateException.class, allocator::allocate); } - /** - * When the pool runs dry the exception is often the only artefact an operator ever sees — - * it reaches them second-hand as a CloudFormation rollback, with floci's own logs the only - * place the cause survives (issue #2206). "No free ports in range 9200-9299" does not say - * which pool ran dry or how to widen it, leaving the reader to find PortAllocator in the - * source to discover the knob exists. The message must carry its own diagnosis. - */ @Test void exhaustionMessageNamesThePoolAndTheWideningProperty() { PortAllocator allocator = new PortAllocator(9200, 9200); @@ -74,6 +71,37 @@ class PortAllocatorTest { "message must still report the exhausted range; got: " + message); } + @Test + void warnsOnceWhenPoolCrossesNinetyPercent() { + List warnings = new ArrayList<>(); + PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add); + + for (int i = 0; i < 10; i++) { + allocator.allocate(); + } + + assertEquals(1, warnings.size()); + assertTrue(warnings.getFirst().contains("90% allocated")); + assertTrue(warnings.getFirst().contains("9/10 ports")); + assertTrue(warnings.getFirst().contains("runtime-api-max-port")); + } + + @Test + void warningRearmsAfterPressureDropsBelowThreshold() { + List warnings = new ArrayList<>(); + PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add); + List ports = new ArrayList<>(); + + for (int i = 0; i < 9; i++) { + ports.add(allocator.allocate()); + } + allocator.release(ports.getLast()); + ports.removeLast(); + ports.add(allocator.allocate()); + + assertEquals(2, warnings.size()); + } + @Test void releasedPortBecomesAvailableAgain() { PortAllocator allocator = new PortAllocator(9200, 9201);