From 33b5ccd72f8e0717d47b8820fff6d6fa310f9eca 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 ++++++-- .../carapaceproxy/MaintenanceModeTest.java | 20 +++++++------ .../backends/ConnectionPoolTest.java | 2 +- .../backends/StuckRequestsTest.java | 28 +++++++++++++++---- .../backends/UnreachableBackendTest.java | 9 +++--- .../carapaceproxy/core/MaxHeaderSizeTest.java | 19 +++++++------ .../server/certificates/CertificatesTest.java | 1 + .../BasicStandardEndpointMapperTest.java | 2 ++ .../utils/EndpointMapperHolder.java | 26 ----------------- 11 files changed, 67 insertions(+), 63 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/MaintenanceModeTest.java b/carapace-server/src/test/java/org/carapaceproxy/MaintenanceModeTest.java index 5dea0e248..daa47a2e2 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/MaintenanceModeTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/MaintenanceModeTest.java @@ -1,21 +1,22 @@ package org.carapaceproxy; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import com.github.tomakehurst.wiremock.junit.WireMockRule; -import org.carapaceproxy.api.UseAdminServer; -import org.carapaceproxy.utils.TestUtils; -import org.junit.Rule; -import org.junit.Test; - import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.Base64; import java.util.Properties; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import org.carapaceproxy.api.UseAdminServer; +import org.carapaceproxy.utils.TestUtils; +import org.junit.Rule; +import org.junit.Test; public class MaintenanceModeTest extends UseAdminServer { @@ -35,6 +36,7 @@ public void test() throws Exception { .withBody("it works !!"))); config = new Properties(HTTP_ADMIN_SERVER_CONFIG); + config.put("healthmanager.tolerant", "true"); startServer(config); // Default certificate diff --git a/carapace-server/src/test/java/org/carapaceproxy/backends/ConnectionPoolTest.java b/carapace-server/src/test/java/org/carapaceproxy/backends/ConnectionPoolTest.java index 8e9fac3da..708af19ee 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/ConnectionPoolTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/ConnectionPoolTest.java @@ -69,7 +69,6 @@ public class ConnectionPoolTest extends UseAdminServer { public WireMockRule wireMockRule = new WireMockRule(0); private void configureAndStartServer() throws Exception { - HttpTestUtils.overrideJvmWideHttpsVerifier(); stubFor(get(urlEqualTo("/index.html")) @@ -85,6 +84,7 @@ private void configureAndStartServer() throws Exception { config.put("db.server.base.dir", tmpDir.newFolder().getAbsolutePath()); config.put("aws.accesskey", "accesskey"); config.put("aws.secretkey", "secretkey"); + config.put("healthmanager.tolerant", "true"); startServer(config); // Default certificate 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..9c16d1b23 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,39 @@ 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("healthmanager.tolerant", "true"); + 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..ebbaceb66 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,12 @@ 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(); + properties.put("healthmanager.tolerant", "true"); // 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 +183,11 @@ 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(); + properties.put("healthmanager.tolerant", "true"); 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/core/MaxHeaderSizeTest.java b/carapace-server/src/test/java/org/carapaceproxy/core/MaxHeaderSizeTest.java index 77a5a5a69..a91e74c1e 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/core/MaxHeaderSizeTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/core/MaxHeaderSizeTest.java @@ -1,19 +1,20 @@ package org.carapaceproxy.core; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static org.junit.Assert.assertEquals; import com.github.tomakehurst.wiremock.junit.WireMockRule; -import org.carapaceproxy.api.UseAdminServer; -import org.carapaceproxy.utils.TestUtils; -import org.junit.Rule; -import org.junit.Test; - import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.Properties; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static org.junit.Assert.assertEquals; +import org.carapaceproxy.api.UseAdminServer; +import org.carapaceproxy.utils.TestUtils; +import org.junit.Rule; +import org.junit.Test; public class MaxHeaderSizeTest extends UseAdminServer { @@ -33,6 +34,7 @@ public void test() throws Exception { .withBody("it works !!"))); config = new Properties(HTTP_ADMIN_SERVER_CONFIG); + config.put("healthmanager.tolerant", "true"); startServer(config); // Default certificate @@ -71,7 +73,6 @@ public void test() throws Exception { changeDynamicConfiguration(config); - HttpClient httpClient = HttpClient.newBuilder() .version(HttpClient.Version.HTTP_1_1) .build(); diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/certificates/CertificatesTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/certificates/CertificatesTest.java index da3f01f59..2b2ae8b27 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/certificates/CertificatesTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/certificates/CertificatesTest.java @@ -123,6 +123,7 @@ private void configureAndStartServer() throws Exception { config.put("db.server.base.dir", tmpDir.newFolder().getAbsolutePath()); config.put("aws.accesskey", "accesskey"); config.put("aws.secretkey", "secretkey"); + config.put("healthmanager.tolerant", "true"); startServer(config); // Default certificate diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/BasicStandardEndpointMapperTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/BasicStandardEndpointMapperTest.java index c990ebf8b..d12988a87 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/BasicStandardEndpointMapperTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/BasicStandardEndpointMapperTest.java @@ -380,6 +380,7 @@ public void testAlwaysServeStaticContent() throws Exception { { Properties configuration = new Properties(); + configuration.put("healthmanager.tolerant", "true"); configuration.put("backend.1.id", "foo"); configuration.put("backend.1.host", "localhost"); configuration.put("backend.1.port", String.valueOf(backend.port())); @@ -531,6 +532,7 @@ public void testCustomAndDebuggingHeaders() throws Exception { try (HttpProxyServer server = new HttpProxyServer(StandardEndpointMapper::new, tmpDir.newFolder())) { Properties configuration = new Properties(); + configuration.put("healthmanager.tolerant", "true"); configuration.put("backend.1.id", "b1"); configuration.put("backend.1.host", "localhost"); configuration.put("backend.1.port", String.valueOf(backend.port())); 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; - } -}