From e731bb0c78d6cc6506f314957c95f70731064ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Thu, 14 Nov 2024 18:06:26 +0100 Subject: [PATCH] test: drop mapper injection again --- .../carapaceproxy/core/HttpProxyServer.java | 5 ---- .../server/backends/BackendHealthManager.java | 6 ++++- .../server/mapper/StandardEndpointMapper.java | 12 ++++++--- .../backends/StuckRequestsTest.java | 27 +++++++++++++++---- .../backends/UnreachableBackendTest.java | 7 +++-- .../utils/EndpointMapperHolder.java | 26 ------------------ 6 files changed, 39 insertions(+), 44 deletions(-) delete mode 100644 carapace-server/src/test/java/org/carapaceproxy/utils/EndpointMapperHolder.java diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java b/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java index e76130534..5e6fadde9 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java @@ -287,11 +287,6 @@ public int getLocalPort() { return listeners.getLocalPort(); } - @VisibleForTesting - public void setMapper(final EndpointMapper mapper) { - this.mapper = mapper; - } - public void startAdminInterface() throws Exception { if (!adminServerEnabled) { return; diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/backends/BackendHealthManager.java b/carapace-server/src/main/java/org/carapaceproxy/server/backends/BackendHealthManager.java index 1cfbb5539..296232a28 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/backends/BackendHealthManager.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/backends/BackendHealthManager.java @@ -74,6 +74,10 @@ public BackendHealthManager(final RuntimeServerConfiguration conf, final Endpoin this.period = DEFAULT_PERIOD; } + public boolean isTolerant() { + return tolerant; + } + public int getPeriod() { return period; } @@ -217,7 +221,7 @@ public boolean exceedsCapacity(final String backendId) { return false; } final BackendHealthStatus backendStatus = getBackendStatus(backendConfiguration.hostPort()); - return backendConfiguration.safeCapacity() > backendStatus.getConnections() && !this.tolerant; + return backendConfiguration.safeCapacity() > backendStatus.getConnections(); } @VisibleForTesting diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/mapper/StandardEndpointMapper.java b/carapace-server/src/main/java/org/carapaceproxy/server/mapper/StandardEndpointMapper.java index 03755361d..8105ebedc 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/mapper/StandardEndpointMapper.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/mapper/StandardEndpointMapper.java @@ -219,17 +219,23 @@ public MapResult map(final ProxyRequest request) { final BackendHealthStatus backendStatus = backendHealthManager.getBackendStatus(backend.hostPort()); switch (backendStatus.getStatus()) { case DOWN: + LOG.info("Backend {} is down, skipping...", backendId); continue; case COLD: - final int capacity = backend.safeCapacity(); if (backendHealthManager.exceedsCapacity(backendId)) { + final int capacity = backend.safeCapacity(); + if (!backendHealthManager.isTolerant()) { + // default behavior, exceeding safe capacity is not tolerated... + LOG.info("Backend {} is cold and exceeds safe capacity of {} connections, skipping...", backendId, capacity); + continue; + } /* * backends are returned by the mapper sorted * from the most desirable to the less desirable; * if the execution reaches this point, - * we should use a cold backend even if over the recommended capacity anyway... + * we may use the cold backend even if over the recommended capacity anyway... */ - LOG.warn("Backend {} exceeds cold capacity of {}, but will use it anyway", backendId, capacity); + LOG.warn("Cold backend {} exceeds safe capacity of {} connections, but will use it anyway", backendId, capacity); } // falls through case STABLE: { diff --git a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java index b9680c18f..5bcf7cc0c 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java @@ -44,9 +44,9 @@ import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.RouteConfiguration; import org.carapaceproxy.server.config.SafeBackendSelector; +import org.carapaceproxy.server.mapper.EndpointMapper; import org.carapaceproxy.server.mapper.StandardEndpointMapper; import org.carapaceproxy.server.mapper.requestmatcher.RegexpRequestMatcher; -import org.carapaceproxy.utils.EndpointMapperHolder; import org.carapaceproxy.utils.RawHttpClient; import org.junit.Rule; import org.junit.Test; @@ -64,6 +64,7 @@ public class StuckRequestsTest { @Test @Parameters({"true", "false"}) + @junitparams.naming.TestCaseName("test(backend unreachable on stuck request: {0})") public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnStuckRequests) throws Exception { stubFor(get(urlEqualTo("/index.html")) @@ -85,22 +86,38 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt final int theport = wireMockRule.port(); EndpointKey key = new EndpointKey("localhost", theport); - final EndpointMapperHolder mapperFactory = new EndpointMapperHolder(parent -> { + final EndpointMapper.Factory mapperFactory = parent -> { StandardEndpointMapper mapper = new StandardEndpointMapper(parent, SafeBackendSelector::new); mapper.addBackend(new BackendConfiguration("backend-a", "localhost", theport, "/", -1)); mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a")); mapper.addAction(new ActionConfiguration("proxy-1", ActionConfiguration.TYPE_PROXY, "director-1", null, -1)); mapper.addRoute(new RouteConfiguration("route-1", "proxy-1", true, new RegexpRequestMatcher(PROPERTY_URI, ".*index.html.*"))); return mapper; - }); - try (HttpProxyServer server = new HttpProxyServer(mapperFactory, tmpDir.newFolder());) { + }; + try (HttpProxyServer server = new HttpProxyServer(mapperFactory, tmpDir.newFolder())) { Properties properties = new Properties(); + properties.put("backend.1.id", "backend-a"); + properties.put("backend.1.enabled", "true"); + properties.put("backend.1.host", "localhost"); + properties.put("backend.1.port", theport); + properties.put("backend.1.probePath", "/"); + properties.put("director.1.id", "director-1"); + properties.put("director.1.backends", properties.getProperty("backend.1.id")); + properties.put("director.1.enabled", "true"); + properties.put("action.1.id", "proxy-1"); + properties.put("action.1.enabled", "true"); + properties.put("action.1.type", ActionConfiguration.TYPE_PROXY); + properties.put("action.1.director", properties.getProperty("director.1.id")); + properties.put("route.100.id", "route-1"); + properties.put("route.100.enabled", "true"); + properties.put("route.100.match", "request.uri ~ \".*index.html.*\""); + properties.put("route.100.action", properties.getProperty("action.1.id")); + properties.put("connectionsmanager.stuckrequesttimeout", "100"); // ms properties.put("connectionsmanager.backendsunreachableonstuckrequests", backendsUnreachableOnStuckRequests + ""); // configure resets all listeners configurations server.configureAtBoot(new PropertiesConfigurationStore(properties)); server.addListener(new NetworkListenerConfiguration("localhost", 0)); - server.setMapper(mapperFactory.getEndpointMapper()); assertEquals(100, server.getCurrentConfiguration().getStuckRequestTimeout()); assertEquals(backendsUnreachableOnStuckRequests, server.getCurrentConfiguration().isBackendsUnreachableOnStuckRequests()); server.start(); diff --git a/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java b/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java index 574ea016f..9ed576dd3 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java @@ -69,6 +69,7 @@ public UnreachableBackendTest(boolean useCache) { @Test public void testWithUnreachableBackend() throws Exception { + stubFor(get(urlEqualTo("/index.html")) .willReturn(aResponse() .withStatus(200) @@ -113,12 +114,11 @@ public void testEmptyResponse() throws Exception { TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache); EndpointKey key = new EndpointKey("localhost", dummyport); - try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder());) { + try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) { Properties properties = new Properties(); // configure resets all listeners configurations server.configureAtBoot(new PropertiesConfigurationStore(properties)); server.addListener(new NetworkListenerConfiguration("localhost", 0)); - server.setMapper(mapper); server.start(); int port = server.getLocalPort(); assertTrue(port > 0); @@ -182,11 +182,10 @@ public void testNonHttpResponseThenClose() throws Exception { TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache); EndpointKey key = new EndpointKey("localhost", dummyport); - try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder());) { + try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) { Properties properties = new Properties(); server.configureAtBoot(new PropertiesConfigurationStore(properties)); server.addListener(new NetworkListenerConfiguration("localhost", 0)); - server.setMapper(mapper); server.start(); int port = server.getLocalPort(); diff --git a/carapace-server/src/test/java/org/carapaceproxy/utils/EndpointMapperHolder.java b/carapace-server/src/test/java/org/carapaceproxy/utils/EndpointMapperHolder.java deleted file mode 100644 index 2ceaffe16..000000000 --- a/carapace-server/src/test/java/org/carapaceproxy/utils/EndpointMapperHolder.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.carapaceproxy.utils; - -import org.carapaceproxy.core.HttpProxyServer; -import org.carapaceproxy.server.config.ConfigurationNotValidException; -import org.carapaceproxy.server.mapper.EndpointMapper; - -public class EndpointMapperHolder implements EndpointMapper.Factory { - private final EndpointMapper.Factory factory; - private EndpointMapper endpointMapper; - public EndpointMapperHolder(final EndpointMapper.Factory factory) { - this.factory = factory; - } - - public EndpointMapper getEndpointMapper() { - return endpointMapper; - } - - @Override - public EndpointMapper build(final HttpProxyServer httpProxyServer) throws ConfigurationNotValidException { - if (endpointMapper == null) { - endpointMapper = factory.build(httpProxyServer); - } - - return endpointMapper; - } -}