mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(lambda): warn before runtime api port exhaustion (#3496)
This commit is contained in:
1 parent
eb622254bc
commit
bea8f85f62
2 files changed
+76
-10
No files matched your search
@@ -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<Integer> inUse = ConcurrentHashMap.newKeySet();
|
||||
private final AtomicBoolean pressureWarningEmitted = new AtomicBoolean();
|
||||
private final Consumer<String> 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<String> 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));
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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<String> warnings = new ArrayList<>();
|
||||
PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add);
|
||||
List<Integer> 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);
|
||||
|
||||
Reference in new issue
Block a user