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 c5ae526857..2bd85ae1a0 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 @@ -5,16 +5,20 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.util.Locale; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; +import java.util.function.IntPredicate; /** - * Hands out ports from a configured range for Lambda Runtime API servers. - * Throws {@link IllegalStateException} when the range is exhausted; the - * caller releases a port back to the pool via {@link #release(int)}. + * Hands out ports from a configured range for Lambda Runtime API servers, skipping any port + * another process already holds. Throws {@link IllegalStateException} when the range is + * exhausted; the caller releases a port back to the pool via {@link #release(int)}. */ @ApplicationScoped public class PortAllocator { @@ -29,6 +33,7 @@ public class PortAllocator { private final Set inUse = ConcurrentHashMap.newKeySet(); private final AtomicBoolean pressureWarningEmitted = new AtomicBoolean(); private final Consumer warningSink; + private final IntPredicate bindable; @Inject public PortAllocator(EmulatorConfig config) { @@ -41,17 +46,30 @@ public class PortAllocator { } PortAllocator(int basePort, int maxPort, Consumer warningSink) { + this(basePort, maxPort, warningSink, PortAllocator::isBindable); + } + + PortAllocator(int basePort, int maxPort, Consumer warningSink, IntPredicate bindable) { 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; + this.bindable = bindable; } public int allocate() { for (int p = basePort; p <= maxPort; p++) { if (inUse.add(p)) { + // inUse only knows this JVM's ports. A port held by anything else in the same + // network namespace (a second floci, another JVM) would leave RuntimeApiServer + // retrying a bind it cannot win while the rest of the range sits idle. A held + // port is not kept reserved: a later allocate() takes it once its holder lets go. + if (!bindable.test(p)) { + inUse.remove(p); + continue; + } warnIfUnderPressure(); return p; } @@ -64,8 +82,9 @@ public class PortAllocator { throw new IllegalStateException( "Lambda Runtime API port pool exhausted: no free ports in range " + 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 " + + " ports, all in use by this floci or held by another process). 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 / " + "floci.services.lambda.runtime-api-max-port."); } @@ -91,4 +110,16 @@ public class PortAllocator { + "before the pool is exhausted.", (allocated * 100) / poolSize, allocated, poolSize, basePort, maxPort)); } + + // Binds the same address RuntimeApiServer listens on, with SO_REUSEADDR as Vert.x sets it, + // so a port only lingering in TIME_WAIT from a stopped server still counts as free. + private static boolean isBindable(int port) { + try (ServerSocket socket = new ServerSocket()) { + socket.setReuseAddress(true); + socket.bind(new InetSocketAddress("0.0.0.0", port)); + return true; + } catch (IOException e) { + return false; + } + } } 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 bf2c45efcd..37d0a46049 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,6 +2,8 @@ package io.github.hectorvent.floci.core.common.port; import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.net.ServerSocket; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -10,16 +12,23 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.function.IntPredicate; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; class PortAllocatorTest { + // Pool bookkeeping is asserted against a host where every port is free, so these tests do + // not depend on what is listening on the machine running them (9200 is OpenSearch's port). + private static final IntPredicate ALWAYS_BINDABLE = p -> true; + @Test void allocatesSequentiallyFromBase() { - PortAllocator allocator = new PortAllocator(9200, 9299); + PortAllocator allocator = new PortAllocator(9200, 9299, w -> { }, ALWAYS_BINDABLE); assertEquals(9200, allocator.allocate()); assertEquals(9201, allocator.allocate()); assertEquals(9202, allocator.allocate()); @@ -27,7 +36,7 @@ class PortAllocatorTest { @Test void concurrentAllocationsAreUnique() throws InterruptedException { - PortAllocator allocator = new PortAllocator(9200, 9299); + PortAllocator allocator = new PortAllocator(9200, 9299, w -> { }, ALWAYS_BINDABLE); int threads = 50; Set ports = ConcurrentHashMap.newKeySet(); CountDownLatch latch = new CountDownLatch(threads); @@ -47,7 +56,7 @@ class PortAllocatorTest { @Test void allocateNeverReturnsPortAlreadyHandedOut() { - PortAllocator allocator = new PortAllocator(9200, 9209); + PortAllocator allocator = new PortAllocator(9200, 9209, w -> { }, ALWAYS_BINDABLE); Set handed = new HashSet<>(); for (int i = 0; i < 10; i++) { assertTrue(handed.add(allocator.allocate())); @@ -57,7 +66,7 @@ class PortAllocatorTest { @Test void exhaustionMessageNamesThePoolAndTheWideningProperty() { - PortAllocator allocator = new PortAllocator(9200, 9200); + PortAllocator allocator = new PortAllocator(9200, 9200, w -> { }, ALWAYS_BINDABLE); allocator.allocate(); IllegalStateException thrown = assertThrows(IllegalStateException.class, allocator::allocate); @@ -74,7 +83,7 @@ class PortAllocatorTest { @Test void warnsOnceWhenPoolCrossesNinetyPercent() { List warnings = new ArrayList<>(); - PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add); + PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add, ALWAYS_BINDABLE); for (int i = 0; i < 10; i++) { allocator.allocate(); @@ -89,7 +98,7 @@ class PortAllocatorTest { @Test void warningRearmsAfterPressureDropsBelowThreshold() { List warnings = new ArrayList<>(); - PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add); + PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add, ALWAYS_BINDABLE); List ports = new ArrayList<>(); for (int i = 0; i < 9; i++) { @@ -104,7 +113,7 @@ class PortAllocatorTest { @Test void releasedPortBecomesAvailableAgain() { - PortAllocator allocator = new PortAllocator(9200, 9201); + PortAllocator allocator = new PortAllocator(9200, 9201, w -> { }, ALWAYS_BINDABLE); int first = allocator.allocate(); allocator.allocate(); assertThrows(IllegalStateException.class, allocator::allocate); @@ -112,4 +121,24 @@ class PortAllocatorTest { allocator.release(first); assertEquals(first, allocator.allocate()); } + + @Test + void skipsPortHeldByAnotherProcessAndReusesItOnceReleased() throws IOException { + // Holding the base port from outside the allocator stands in for a second floci (or any + // other process) in the same network namespace. Handing it out would leave the Runtime + // API server retrying a bind it can never win while the rest of the range sits idle. + int held; + PortAllocator allocator; + try (ServerSocket holder = new ServerSocket(0)) { + held = holder.getLocalPort(); + assumeTrue(held <= 65535 - 50, "OS picked a port too close to 65535 for the range"); + allocator = new PortAllocator(held, held + 50); + int first = allocator.allocate(); + assertNotEquals(held, first, "allocate() must skip a port another process holds"); + assertTrue(first > held && first <= held + 50); + } + + assertEquals(held, allocator.allocate(), + "a port skipped while held must be handed out once its holder lets go"); + } } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/port/RuntimeApiPortPoolDefaultTest.java b/src/test/java/io/github/hectorvent/floci/core/common/port/RuntimeApiPortPoolDefaultTest.java index b8539fbbfb..a604022f8c 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/port/RuntimeApiPortPoolDefaultTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/port/RuntimeApiPortPoolDefaultTest.java @@ -124,7 +124,9 @@ class RuntimeApiPortPoolDefaultTest { // configured range, so widening the default genuinely raises the concurrency ceiling. int base = defaultOf("runtimeApiBasePort"); int max = defaultOf("runtimeApiMaxPort"); - PortAllocator allocator = new PortAllocator(base, max); + // Every port counts as bindable so the assertion is about the configured width, not about + // what happens to be listening on 12000+ on the machine running the suite. + PortAllocator allocator = new PortAllocator(base, max, w -> { }, p -> true); java.util.Set handed = new java.util.HashSet<>(); for (int i = 0; i < MIN_USABLE_POOL_WIDTH; i++) {