From bc63d2a97f3540e7e978e3616358f2c1e5c53043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Tue, 29 Oct 2024 16:23:39 +0100 Subject: [PATCH 1/9] refactor: delombok EndpointKey --- .../org/carapaceproxy/client/EndpointKey.java | 17 +++++------------ .../java/org/carapaceproxy/RawClientTest.java | 8 ++++---- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java b/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java index f780dab1e..8ac4cbce6 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java +++ b/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java @@ -19,18 +19,12 @@ */ package org.carapaceproxy.client; -import lombok.Data; - /** * Identifier of an endpoint * * @author enrico.olivelli */ -@Data -public final class EndpointKey { - - private final String host; - private final int port; +public record EndpointKey(String host, int port) { public static EndpointKey make(String host, int port) { return new EndpointKey(host, port); @@ -46,13 +40,12 @@ public static EndpointKey make(String hostAndPort) { return new EndpointKey(host, port); } - public EndpointKey(String host, int port) { - this.host = host; - this.port = port; - } - public String getHostPort() { return host + ":" + port; } + @Override + public String toString() { + return getHostPort(); + } } diff --git a/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java b/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java index 405bd2c62..2a416b3b0 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java @@ -215,13 +215,13 @@ public void downloadSmallPayloadsTest() throws Exception { public void endpointKeyTest() { { EndpointKey entryPoint = EndpointKey.make("localhost:8080"); - assertThat(entryPoint.getHost(), is("localhost")); - assertThat(entryPoint.getPort(), is(8080)); + assertThat(entryPoint.host(), is("localhost")); + assertThat(entryPoint.port(), is(8080)); } { EndpointKey entryPoint = EndpointKey.make("localhost"); - assertThat(entryPoint.getHost(), is("localhost")); - assertThat(entryPoint.getPort(), is(0)); + assertThat(entryPoint.host(), is("localhost")); + assertThat(entryPoint.port(), is(0)); } } From 91810a5c2e8985d558f1a1f0f8aedebf71b3a5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Tue, 29 Oct 2024 16:24:47 +0100 Subject: [PATCH 2/9] refactor: prefer EndpointKey#toString --- .../java/org/carapaceproxy/api/ListenersResource.java | 3 ++- .../main/java/org/carapaceproxy/client/EndpointKey.java | 6 +----- .../main/java/org/carapaceproxy/core/ConnectionKey.java | 2 +- .../java/org/carapaceproxy/core/ProxyRequestsManager.java | 8 ++++---- .../org/carapaceproxy/backends/StuckRequestsTest.java | 2 +- .../carapaceproxy/backends/UnreachableBackendTest.java | 8 ++++---- 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/carapace-server/src/main/java/org/carapaceproxy/api/ListenersResource.java b/carapace-server/src/main/java/org/carapaceproxy/api/ListenersResource.java index 7dbab0f8d..bffb18e88 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/api/ListenersResource.java +++ b/carapace-server/src/main/java/org/carapaceproxy/api/ListenersResource.java @@ -72,7 +72,8 @@ public Map getAllListeners() { config.getDefaultCertificate(), (int) listener.getValue().getTotalRequests().get() ); - return Map.entry(EndpointKey.make(config.getHost(), port).getHostPort(), bean); + EndpointKey endpointKey = EndpointKey.make(config.getHost(), port); + return Map.entry(endpointKey.toString(), bean); }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } diff --git a/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java b/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java index 8ac4cbce6..0c91b2b3e 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java +++ b/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java @@ -40,12 +40,8 @@ public static EndpointKey make(String hostAndPort) { return new EndpointKey(host, port); } - public String getHostPort() { - return host + ":" + port; - } - @Override public String toString() { - return getHostPort(); + return host + ":" + port; } } diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/ConnectionKey.java b/carapace-server/src/main/java/org/carapaceproxy/core/ConnectionKey.java index bb50a598a..ae83d3a49 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/ConnectionKey.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/ConnectionKey.java @@ -11,6 +11,6 @@ */ public record ConnectionKey(String host, HttpProtocol protocolVersion) { public ConnectionKey(final EndpointKey key, final String id, final HttpProtocol protocolVersion) { - this(key.getHostPort() + "_" + id, protocolVersion); + this(key.toString() + "_" + id, protocolVersion); } } diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java index f1d228d97..3af50d336 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java @@ -401,7 +401,7 @@ public Publisher forward(ProxyRequest request, boolean cache) { .doOnRequest((req, conn) -> { if (CarapaceLogger.isLoggingDebugEnabled()) { CarapaceLogger.debug("Start sending request for " - + " Using client id " + key.getHostPort() + "_" + connectionConfig.getId() + + " Using client id " + key.toString() + "_" + connectionConfig.getId() + " Uri " + req.resourceUrl() + " Timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS")) + " Backend " + endpointHost + ":" + endpointPort); @@ -411,7 +411,7 @@ public Publisher forward(ProxyRequest request, boolean cache) { }).doAfterRequest((req, conn) -> { if (CarapaceLogger.isLoggingDebugEnabled()) { CarapaceLogger.debug("Finished sending request for " - + " Using client id " + key.getHostPort() + "_" + connectionConfig.getId() + + " Using client id " + key.toString() + "_" + connectionConfig.getId() + " Uri " + request.getUri() + " Timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS")) + " Backend " + endpointHost + ":" + endpointPort); @@ -443,7 +443,7 @@ public Publisher forward(ProxyRequest request, boolean cache) { .response((resp, flux) -> { // endpoint response if (CarapaceLogger.isLoggingDebugEnabled()) { CarapaceLogger.debug("Receive response from backend for " + request.getRemoteAddress() - + " Using client id " + key.getHostPort() + "_" + connectionConfig.getId() + + " Using client id " + key.toString() + "_" + connectionConfig.getId() + " uri" + request.getUri() + " timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS")) + " Backend: " + request.getAction().host); @@ -482,7 +482,7 @@ public Publisher forward(ProxyRequest request, boolean cache) { }).doOnComplete(() -> { if (CarapaceLogger.isLoggingDebugEnabled()) { CarapaceLogger.debug("Send all response to client " + request.getRemoteAddress() - + " Using client id " + key.getHostPort() + "_" + connectionConfig.getId() + + " Using client id " + key.toString() + "_" + connectionConfig.getId() + " for uri " + request.getUri() + " timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS")) + " Backend: " + request.getAction().host); 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 8ca039e5e..5474ae141 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java @@ -116,7 +116,7 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); assertThat((int) ProxyRequestsManager.STUCK_REQUESTS_COUNTER.get() > 0, is(true)); - assertEquals(backendsUnreachableOnStuckRequests, !server.getBackendHealthManager().isAvailable(key.getHostPort())); + assertEquals(backendsUnreachableOnStuckRequests, !server.getBackendHealthManager().isAvailable(key.toString())); try (RawHttpClient client = new RawHttpClient("localhost", port)) { RawHttpClient.HttpResponse resp = client.executeRequest("GET /good-index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"); 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 8a4e27e62..698d1450e 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java @@ -102,7 +102,7 @@ public void testWithUnreachableBackend() throws Exception { + " \n" + "\n", resp.getBodyString()); } - assertFalse(server.getBackendHealthManager().isAvailable(key.getHostPort())); + assertFalse(server.getBackendHealthManager().isAvailable(key.toString())); assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); } } @@ -138,7 +138,7 @@ public void testEmptyResponse() throws Exception { + " \n" + "\n", resp.getBodyString()); } - assertTrue(server.getBackendHealthManager().isAvailable(key.getHostPort())); // no troubles for new connections + assertTrue(server.getBackendHealthManager().isAvailable(key.toString())); // no troubles for new connections assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); } } @@ -168,7 +168,7 @@ public void testConnectionResetByPeer() throws Exception { + " \n" + "\n", resp.getBodyString()); } - assertTrue(server.getBackendHealthManager().isAvailable(key.getHostPort())); // no troubles for new connections + assertTrue(server.getBackendHealthManager().isAvailable(key.toString())); // no troubles for new connections assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); } } @@ -202,7 +202,7 @@ public void testNonHttpResponseThenClose() throws Exception { + " \n" + "\n", resp.getBodyString()); } - assertTrue(server.getBackendHealthManager().isAvailable(key.getHostPort())); + assertTrue(server.getBackendHealthManager().isAvailable(key.toString())); assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); } } From 3566c75f7c402e691f59554bef1629d6e82bc88e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Tue, 29 Oct 2024 16:27:48 +0100 Subject: [PATCH 3/9] refactor: drop HostPort in favor of EndpointKey --- .../org/carapaceproxy/core/Listeners.java | 34 +++++++++---------- .../org/carapaceproxy/core/ProxyRequest.java | 6 ++-- .../core/RuntimeServerConfiguration.java | 4 +-- .../carapaceproxy/server/config/HostPort.java | 4 --- .../config/NetworkListenerConfiguration.java | 5 +-- .../listeners/ListenerConfigurationTest.java | 10 +++--- .../requestmatcher/RequestMatcherTest.java | 4 +-- 7 files changed, 32 insertions(+), 35 deletions(-) delete mode 100644 carapace-server/src/main/java/org/carapaceproxy/server/config/HostPort.java diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/Listeners.java b/carapace-server/src/main/java/org/carapaceproxy/core/Listeners.java index 7c9ef4f97..21a96ebe2 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/Listeners.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/Listeners.java @@ -65,7 +65,7 @@ import jdk.net.ExtendedSocketOptions; import lombok.Data; import org.carapaceproxy.server.config.ConfigurationNotValidException; -import org.carapaceproxy.server.config.HostPort; +import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.SSLCertificateConfiguration; import org.carapaceproxy.utils.CarapaceLogger; @@ -100,7 +100,7 @@ public class Listeners { private final HttpProxyServer parent; private final Map sslContexts = new ConcurrentHashMap<>(); - private final Map listeningChannels = new ConcurrentHashMap<>(); + private final Map listeningChannels = new ConcurrentHashMap<>(); private final File basePath; private boolean started; @@ -120,7 +120,7 @@ public int getLocalPort() { return -1; } - public Map getListeningChannels() { + public Map getListeningChannels() { return listeningChannels; } @@ -130,7 +130,7 @@ public void start() throws InterruptedException, ConfigurationNotValidException } public void stop() { - for (HostPort key : listeningChannels.keySet()) { + for (EndpointKey key : listeningChannels.keySet()) { try { stopListener(key); } catch (InterruptedException ex) { @@ -140,7 +140,7 @@ public void stop() { } } - private void stopListener(HostPort hostport) throws InterruptedException { + private void stopListener(EndpointKey hostport) throws InterruptedException { ListeningChannel channel = listeningChannels.remove(hostport); if (channel != null) { channel.channel.disposeNow(Duration.ofSeconds(10)); @@ -173,10 +173,10 @@ void reloadConfiguration(RuntimeServerConfiguration newConfiguration) throws Int sslContexts.clear(); // stop dropped listeners, start new one - List listenersToStop = new ArrayList<>(); - List listenersToRestart = new ArrayList<>(); - for (Map.Entry channel : listeningChannels.entrySet()) { - HostPort key = channel.getKey(); + List listenersToStop = new ArrayList<>(); + List listenersToRestart = new ArrayList<>(); + for (Map.Entry channel : listeningChannels.entrySet()) { + EndpointKey key = channel.getKey(); NetworkListenerConfiguration actualListenerConfig = currentConfiguration.getListener(key); NetworkListenerConfiguration newConfigurationForListener = newConfiguration.getListener(key); if (newConfigurationForListener == null) { @@ -190,9 +190,9 @@ void reloadConfiguration(RuntimeServerConfiguration newConfiguration) throws Int } channel.getValue().clear(); } - List listenersToStart = new ArrayList<>(); + List listenersToStart = new ArrayList<>(); for (NetworkListenerConfiguration config : newConfiguration.getListeners()) { - HostPort key = config.getKey(); + EndpointKey key = config.getKey(); if (!listeningChannels.containsKey(key)) { LOG.log(Level.INFO, "listener: {0} is to be started", key); listenersToStart.add(key); @@ -203,19 +203,19 @@ void reloadConfiguration(RuntimeServerConfiguration newConfiguration) throws Int currentConfiguration = newConfiguration; try { - for (HostPort hostport : listenersToStop) { + for (EndpointKey hostport : listenersToStop) { LOG.log(Level.INFO, "Stopping {0}", hostport); stopListener(hostport); } - for (HostPort hostport : listenersToRestart) { + for (EndpointKey hostport : listenersToRestart) { LOG.log(Level.INFO, "Restart {0}", hostport); stopListener(hostport); NetworkListenerConfiguration newConfigurationForListener = currentConfiguration.getListener(hostport); bootListener(newConfigurationForListener); } - for (HostPort hostport : listenersToStart) { + for (EndpointKey hostport : listenersToStart) { LOG.log(Level.INFO, "Starting {0}", hostport); NetworkListenerConfiguration newConfigurationForListener = currentConfiguration.getListener(hostport); bootListener(newConfigurationForListener); @@ -227,7 +227,7 @@ void reloadConfiguration(RuntimeServerConfiguration newConfiguration) throws Int } private void bootListener(NetworkListenerConfiguration config) throws InterruptedException { - HostPort hostPort = new HostPort(config.getHost(), config.getPort() + parent.getListenersOffsetPort()); + EndpointKey hostPort = new EndpointKey(config.getHost(), config.getPort() + parent.getListenersOffsetPort()); ListeningChannel listeningChannel = new ListeningChannel(hostPort, config); LOG.log(Level.INFO, "Starting listener at {0}:{1} ssl:{2}", new Object[]{hostPort.host(), String.valueOf(hostPort.port()), config.isSsl()}); @@ -331,13 +331,13 @@ protected SslHandler newSslHandler(SslContext context, ByteBufAllocator allocato @Data public final class ListeningChannel implements io.netty.util.AsyncMapping { - private final HostPort hostPort; + private final EndpointKey hostPort; private final NetworkListenerConfiguration config; private final Counter.Child totalRequests; private final Map listenerSslContexts = new HashMap<>(); DisposableServer channel; - public ListeningChannel(HostPort hostPort, NetworkListenerConfiguration config) { + public ListeningChannel(EndpointKey hostPort, NetworkListenerConfiguration config) { this.hostPort = hostPort; this.config = config; totalRequests = TOTAL_REQUESTS_PER_LISTENER_COUNTER.labels(hostPort.host() + "_" + hostPort.port()); diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequest.java b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequest.java index e1c04a881..7b02c2256 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequest.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequest.java @@ -35,7 +35,7 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.regex.Pattern; import lombok.Data; -import org.carapaceproxy.server.config.HostPort; +import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.server.filters.UrlEncodedQueryString; import org.carapaceproxy.server.mapper.MapResult; import org.carapaceproxy.server.mapper.requestmatcher.MatchingContext; @@ -69,7 +69,7 @@ public class ProxyRequest implements MatchingContext { private final long id = REQUESTS_ID_GENERATOR.incrementAndGet(); private final HttpServerRequest request; private final HttpServerResponse response; - private final HostPort listener; + private final EndpointKey listener; private MapResult action; private String userId; private String sessionId; @@ -83,7 +83,7 @@ public class ProxyRequest implements MatchingContext { private boolean servedFromCache; private HttpVersion httpProtocol; - public ProxyRequest(HttpServerRequest request, HttpServerResponse response, HostPort listener) { + public ProxyRequest(HttpServerRequest request, HttpServerResponse response, EndpointKey listener) { this.request = request; this.response = response; this.listener = listener; diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/RuntimeServerConfiguration.java b/carapace-server/src/main/java/org/carapaceproxy/core/RuntimeServerConfiguration.java index 41422ae3b..641eabcf3 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/RuntimeServerConfiguration.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/RuntimeServerConfiguration.java @@ -52,7 +52,7 @@ import org.carapaceproxy.configstore.ConfigurationStore; import org.carapaceproxy.server.config.ConfigurationNotValidException; import org.carapaceproxy.server.config.ConnectionPoolConfiguration; -import org.carapaceproxy.server.config.HostPort; +import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.RequestFilterConfiguration; import org.carapaceproxy.server.config.SSLCertificateConfiguration; @@ -492,7 +492,7 @@ public List getRequestFilters() { return requestFilters; } - NetworkListenerConfiguration getListener(HostPort hostPort) { + NetworkListenerConfiguration getListener(EndpointKey hostPort) { return listeners .stream() .filter(s -> s.getHost().equalsIgnoreCase(hostPort.host()) && s.getPort() == hostPort.port()) diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/config/HostPort.java b/carapace-server/src/main/java/org/carapaceproxy/server/config/HostPort.java deleted file mode 100644 index 1c8a73004..000000000 --- a/carapace-server/src/main/java/org/carapaceproxy/server/config/HostPort.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.carapaceproxy.server.config; - -public record HostPort(String host, int port) { -} diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/config/NetworkListenerConfiguration.java b/carapace-server/src/main/java/org/carapaceproxy/server/config/NetworkListenerConfiguration.java index 0fa651d39..e06af18b4 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/config/NetworkListenerConfiguration.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/config/NetworkListenerConfiguration.java @@ -28,6 +28,7 @@ import java.util.Set; import java.util.stream.Collectors; import lombok.Getter; +import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.core.ForwardedStrategies; import reactor.netty.http.HttpProtocol; @@ -154,8 +155,8 @@ public NetworkListenerConfiguration( this.protocols = httpProtocols; } - public HostPort getKey() { - return new HostPort(host, port); + public EndpointKey getKey() { + return new EndpointKey(host, port); } public static Set getDefaultHttpProtocols(final boolean ssl) { diff --git a/carapace-server/src/test/java/org/carapaceproxy/listeners/ListenerConfigurationTest.java b/carapace-server/src/test/java/org/carapaceproxy/listeners/ListenerConfigurationTest.java index 8dcff3553..e028b2131 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/listeners/ListenerConfigurationTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/listeners/ListenerConfigurationTest.java @@ -9,7 +9,7 @@ import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.core.Listeners; import org.carapaceproxy.server.config.ConfigurationChangeInProgressException; -import org.carapaceproxy.server.config.HostPort; +import org.carapaceproxy.client.EndpointKey; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -33,10 +33,10 @@ public void testListenerKeepAliveConfiguration() throws Exception { } server.start(); - HostPort listenerKey = new HostPort("localhost", 8080); + EndpointKey listenerKey = new EndpointKey("localhost", 8080); { - Map listeners = server.getListeners().getListeningChannels(); + Map listeners = server.getListeners().getListeningChannels(); //check default configuration assertTrue(listeners.get(listenerKey).getConfig().isKeepAlive()); @@ -56,7 +56,7 @@ public void testListenerKeepAliveConfiguration() throws Exception { reloadConfiguration(configuration, server); - Map listeners = server.getListeners().getListeningChannels(); + Map listeners = server.getListeners().getListeningChannels(); assertEquals(1, listeners.size()); assertFalse(listeners.get(listenerKey).getConfig().isKeepAlive()); @@ -77,7 +77,7 @@ public void testListenerKeepAliveConfiguration() throws Exception { configuration.put("listener.1.enabled", "true"); reloadConfiguration(configuration, server); - Map listeners = server.getListeners().getListeningChannels(); + Map listeners = server.getListeners().getListeningChannels(); assertTrue(listeners.get(listenerKey).getConfig().isKeepAlive()); assertEquals(10, listeners.get(listenerKey).getConfig().getSoBacklog()); diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/requestmatcher/RequestMatcherTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/requestmatcher/RequestMatcherTest.java index ab0d4d574..089cb4afd 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/requestmatcher/RequestMatcherTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/requestmatcher/RequestMatcherTest.java @@ -31,7 +31,7 @@ import javax.ws.rs.core.HttpHeaders; import org.carapaceproxy.core.ProxyRequest; import org.carapaceproxy.server.config.ConfigurationNotValidException; -import org.carapaceproxy.server.config.HostPort; +import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.server.mapper.requestmatcher.parser.ParseException; import org.carapaceproxy.server.mapper.requestmatcher.parser.RequestMatchParser; import org.carapaceproxy.server.mapper.requestmatcher.parser.TokenMgrError; @@ -232,7 +232,7 @@ public void test2() throws ParseException, ConfigurationNotValidException { when(serverRequest.requestHeaders()).thenReturn(headers); when(serverRequest.protocol()).thenReturn("HTTP/2"); - ProxyRequest request = new ProxyRequest(serverRequest, null, new HostPort("localhost", 8080)); + ProxyRequest request = new ProxyRequest(serverRequest, null, new EndpointKey("localhost", 8080)); // Test headers { From a08955f3f9a645f66806dca716f78ef43b39d6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Tue, 29 Oct 2024 16:29:22 +0100 Subject: [PATCH 4/9] refactor: move EndpointKey to org.carapaceproxy.core package --- .../src/main/java/org/carapaceproxy/EndpointStats.java | 2 +- .../src/main/java/org/carapaceproxy/api/BackendsResource.java | 2 +- .../src/main/java/org/carapaceproxy/api/ListenersResource.java | 2 +- .../main/java/org/carapaceproxy/client/ConnectionsManager.java | 1 + .../java/org/carapaceproxy/client/ConnectionsManagerStats.java | 1 + .../main/java/org/carapaceproxy/client/EndpointConnection.java | 1 + .../src/main/java/org/carapaceproxy/core/ConnectionKey.java | 1 - .../java/org/carapaceproxy/{client => core}/EndpointKey.java | 2 +- .../src/main/java/org/carapaceproxy/core/HttpProxyServer.java | 1 - .../src/main/java/org/carapaceproxy/core/Listeners.java | 1 - .../src/main/java/org/carapaceproxy/core/ProxyRequest.java | 1 - .../main/java/org/carapaceproxy/core/ProxyRequestsManager.java | 1 - .../java/org/carapaceproxy/core/RuntimeServerConfiguration.java | 1 - .../server/config/NetworkListenerConfiguration.java | 2 +- .../src/test/java/org/carapaceproxy/BigUploadTest.java | 2 +- .../src/test/java/org/carapaceproxy/ConcurrentClientsTest.java | 2 +- .../src/test/java/org/carapaceproxy/RawClientTest.java | 2 +- .../src/test/java/org/carapaceproxy/SimpleHTTPProxyTest.java | 2 +- .../carapaceproxy/backends/ChunckedEncodingRequestsTest.java | 2 +- .../java/org/carapaceproxy/backends/ConnectionPoolTest.java | 2 +- .../test/java/org/carapaceproxy/backends/StuckRequestsTest.java | 2 +- .../java/org/carapaceproxy/backends/UnreachableBackendTest.java | 2 +- .../test/java/org/carapaceproxy/core/RequestsLoggerTest.java | 1 - .../org/carapaceproxy/listeners/ListenerConfigurationTest.java | 2 +- .../src/test/java/org/carapaceproxy/listeners/SSLSNITest.java | 2 +- .../carapaceproxy/server/cache/CacheContentLengthLimitTest.java | 2 +- .../src/test/java/org/carapaceproxy/server/cache/CacheTest.java | 2 +- .../java/org/carapaceproxy/server/cache/NotModifiedTest.java | 2 +- .../carapaceproxy/server/filters/XForwardedForFilterTest.java | 2 +- .../org/carapaceproxy/server/filters/XTlsCipherFilterTest.java | 2 +- .../carapaceproxy/server/filters/XTlsProtocolFilterTest.java | 2 +- .../server/mapper/requestmatcher/RequestMatcherTest.java | 2 +- 32 files changed, 25 insertions(+), 29 deletions(-) rename carapace-server/src/main/java/org/carapaceproxy/{client => core}/EndpointKey.java (97%) diff --git a/carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java b/carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java index 109bace38..f518d2fc9 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java +++ b/carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java @@ -23,7 +23,7 @@ import java.util.concurrent.atomic.AtomicLong; import lombok.Getter; import lombok.ToString; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; /** * Stats about an endpoint diff --git a/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java b/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java index 970807186..d268d5f99 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java +++ b/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java @@ -27,7 +27,7 @@ import javax.ws.rs.Produces; import lombok.Data; import org.carapaceproxy.EndpointStats; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.core.HttpProxyServer.ConnectionPoolStats; import org.carapaceproxy.server.backends.BackendHealthCheck; diff --git a/carapace-server/src/main/java/org/carapaceproxy/api/ListenersResource.java b/carapace-server/src/main/java/org/carapaceproxy/api/ListenersResource.java index bffb18e88..3fe946871 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/api/ListenersResource.java +++ b/carapace-server/src/main/java/org/carapaceproxy/api/ListenersResource.java @@ -27,7 +27,7 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import lombok.Data; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.server.config.NetworkListenerConfiguration; diff --git a/carapace-server/src/main/java/org/carapaceproxy/client/ConnectionsManager.java b/carapace-server/src/main/java/org/carapaceproxy/client/ConnectionsManager.java index 6aae779de..0f26d6a84 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/client/ConnectionsManager.java +++ b/carapace-server/src/main/java/org/carapaceproxy/client/ConnectionsManager.java @@ -19,6 +19,7 @@ */ package org.carapaceproxy.client; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.RuntimeServerConfiguration; /** diff --git a/carapace-server/src/main/java/org/carapaceproxy/client/ConnectionsManagerStats.java b/carapace-server/src/main/java/org/carapaceproxy/client/ConnectionsManagerStats.java index fb4f2a396..745c25b4d 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/client/ConnectionsManagerStats.java +++ b/carapace-server/src/main/java/org/carapaceproxy/client/ConnectionsManagerStats.java @@ -21,6 +21,7 @@ import java.util.Map; import org.carapaceproxy.EndpointStats; +import org.carapaceproxy.core.EndpointKey; /** * Stats diff --git a/carapace-server/src/main/java/org/carapaceproxy/client/EndpointConnection.java b/carapace-server/src/main/java/org/carapaceproxy/client/EndpointConnection.java index 95aeb3604..7b9babe5b 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/client/EndpointConnection.java +++ b/carapace-server/src/main/java/org/carapaceproxy/client/EndpointConnection.java @@ -22,6 +22,7 @@ import io.netty.handler.codec.http.HttpContent; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.LastHttpContent; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.ProxyRequestsManager; /** diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/ConnectionKey.java b/carapace-server/src/main/java/org/carapaceproxy/core/ConnectionKey.java index ae83d3a49..7ababc7c9 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/ConnectionKey.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/ConnectionKey.java @@ -1,6 +1,5 @@ package org.carapaceproxy.core; -import org.carapaceproxy.client.EndpointKey; import reactor.netty.http.HttpProtocol; /** diff --git a/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java b/carapace-server/src/main/java/org/carapaceproxy/core/EndpointKey.java similarity index 97% rename from carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java rename to carapace-server/src/main/java/org/carapaceproxy/core/EndpointKey.java index 0c91b2b3e..5871b4e66 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/client/EndpointKey.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/EndpointKey.java @@ -17,7 +17,7 @@ under the License. */ -package org.carapaceproxy.client; +package org.carapaceproxy.core; /** * Identifier of an endpoint 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 b7a245ae0..ace281e18 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java @@ -76,7 +76,6 @@ import org.carapaceproxy.api.AuthAPIRequestsFilter; import org.carapaceproxy.api.ConfigResource; import org.carapaceproxy.api.ForceHeadersAPIRequestsFilter; -import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.cluster.GroupMembershipHandler; import org.carapaceproxy.cluster.impl.NullGroupMembershipHandler; import org.carapaceproxy.cluster.impl.ZooKeeperGroupMembershipHandler; diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/Listeners.java b/carapace-server/src/main/java/org/carapaceproxy/core/Listeners.java index 21a96ebe2..d701ec7b8 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/Listeners.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/Listeners.java @@ -65,7 +65,6 @@ import jdk.net.ExtendedSocketOptions; import lombok.Data; import org.carapaceproxy.server.config.ConfigurationNotValidException; -import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.SSLCertificateConfiguration; import org.carapaceproxy.utils.CarapaceLogger; diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequest.java b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequest.java index 7b02c2256..0489be936 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequest.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequest.java @@ -35,7 +35,6 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.regex.Pattern; import lombok.Data; -import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.server.filters.UrlEncodedQueryString; import org.carapaceproxy.server.mapper.MapResult; import org.carapaceproxy.server.mapper.requestmatcher.MatchingContext; diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java index 3af50d336..56ea9d0cf 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java @@ -59,7 +59,6 @@ import org.apache.http.HttpStatus; import org.carapaceproxy.EndpointStats; import org.carapaceproxy.SimpleHTTPResponse; -import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.server.cache.ContentsCache; import org.carapaceproxy.server.config.BackendConfiguration; import org.carapaceproxy.server.config.ConnectionPoolConfiguration; diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/RuntimeServerConfiguration.java b/carapace-server/src/main/java/org/carapaceproxy/core/RuntimeServerConfiguration.java index 641eabcf3..930d679e6 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/RuntimeServerConfiguration.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/RuntimeServerConfiguration.java @@ -52,7 +52,6 @@ import org.carapaceproxy.configstore.ConfigurationStore; import org.carapaceproxy.server.config.ConfigurationNotValidException; import org.carapaceproxy.server.config.ConnectionPoolConfiguration; -import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.RequestFilterConfiguration; import org.carapaceproxy.server.config.SSLCertificateConfiguration; diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/config/NetworkListenerConfiguration.java b/carapace-server/src/main/java/org/carapaceproxy/server/config/NetworkListenerConfiguration.java index e06af18b4..3dead41df 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/config/NetworkListenerConfiguration.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/config/NetworkListenerConfiguration.java @@ -28,7 +28,7 @@ import java.util.Set; import java.util.stream.Collectors; import lombok.Getter; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.ForwardedStrategies; import reactor.netty.http.HttpProtocol; diff --git a/carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java b/carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java index 7fb9b45ec..c9add7f64 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java @@ -37,7 +37,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.IOUtils; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.utils.TestEndpointMapper; import org.junit.Rule; diff --git a/carapace-server/src/test/java/org/carapaceproxy/ConcurrentClientsTest.java b/carapace-server/src/test/java/org/carapaceproxy/ConcurrentClientsTest.java index 75b64a537..25ae19096 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/ConcurrentClientsTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/ConcurrentClientsTest.java @@ -35,7 +35,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.io.IOUtils; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.utils.TestEndpointMapper; import org.junit.Rule; diff --git a/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java b/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java index 2a416b3b0..fa98b5ea6 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java @@ -90,7 +90,7 @@ import javax.servlet.http.HttpServletResponse; import junitparams.JUnitParamsRunner; import junitparams.Parameters; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.server.config.ConnectionPoolConfiguration; import org.carapaceproxy.server.config.NetworkListenerConfiguration; diff --git a/carapace-server/src/test/java/org/carapaceproxy/SimpleHTTPProxyTest.java b/carapace-server/src/test/java/org/carapaceproxy/SimpleHTTPProxyTest.java index a837ad15c..cdc25ed04 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/SimpleHTTPProxyTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/SimpleHTTPProxyTest.java @@ -38,7 +38,7 @@ import java.util.Collections; import java.util.Set; import org.apache.commons.io.IOUtils; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.SSLCertificateConfiguration; diff --git a/carapace-server/src/test/java/org/carapaceproxy/backends/ChunckedEncodingRequestsTest.java b/carapace-server/src/test/java/org/carapaceproxy/backends/ChunckedEncodingRequestsTest.java index ff5e81a53..1e7ef8abc 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/ChunckedEncodingRequestsTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/ChunckedEncodingRequestsTest.java @@ -25,7 +25,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import com.github.tomakehurst.wiremock.junit.WireMockRule; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.utils.RawHttpClient; import static org.junit.Assert.assertTrue; 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 7743303a1..8e9fac3da 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/ConnectionPoolTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/ConnectionPoolTest.java @@ -48,7 +48,7 @@ import java.util.Properties; import org.carapaceproxy.api.ConnectionPoolsResource; import org.carapaceproxy.api.UseAdminServer; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.core.ProxyRequest; import org.carapaceproxy.core.ProxyRequestsManager; 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 5474ae141..a295a6198 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java @@ -32,7 +32,7 @@ import java.util.Properties; import junitparams.JUnitParamsRunner; import junitparams.Parameters; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.configstore.PropertiesConfigurationStore; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.core.ProxyRequestsManager; 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 698d1450e..807ecd7f2 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java @@ -32,7 +32,7 @@ import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.util.Arrays; import java.util.Collection; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.utils.RawHttpClient; diff --git a/carapace-server/src/test/java/org/carapaceproxy/core/RequestsLoggerTest.java b/carapace-server/src/test/java/org/carapaceproxy/core/RequestsLoggerTest.java index 3b494b3ac..2b45aecd7 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/core/RequestsLoggerTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/core/RequestsLoggerTest.java @@ -40,7 +40,6 @@ import io.netty.handler.codec.http.HttpVersion; import org.carapaceproxy.server.mapper.MapResult; -import org.carapaceproxy.client.EndpointKey; import org.carapaceproxy.utils.RawHttpClient; import org.carapaceproxy.utils.TestEndpointMapper; import static org.hamcrest.CoreMatchers.is; diff --git a/carapace-server/src/test/java/org/carapaceproxy/listeners/ListenerConfigurationTest.java b/carapace-server/src/test/java/org/carapaceproxy/listeners/ListenerConfigurationTest.java index e028b2131..a4c21fe34 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/listeners/ListenerConfigurationTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/listeners/ListenerConfigurationTest.java @@ -9,7 +9,7 @@ import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.core.Listeners; import org.carapaceproxy.server.config.ConfigurationChangeInProgressException; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; diff --git a/carapace-server/src/test/java/org/carapaceproxy/listeners/SSLSNITest.java b/carapace-server/src/test/java/org/carapaceproxy/listeners/SSLSNITest.java index 86705b09a..41067a718 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/listeners/SSLSNITest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/listeners/SSLSNITest.java @@ -35,7 +35,7 @@ import java.security.cert.X509Certificate; import java.util.Set; import javax.net.ssl.SSLSession; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.server.config.ConfigurationNotValidException; import org.carapaceproxy.server.config.NetworkListenerConfiguration; diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/cache/CacheContentLengthLimitTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/cache/CacheContentLengthLimitTest.java index 566249bf5..54af84804 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/cache/CacheContentLengthLimitTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/cache/CacheContentLengthLimitTest.java @@ -26,7 +26,7 @@ import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.io.IOException; import org.carapaceproxy.EndpointStats; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.utils.RawHttpClient; import org.carapaceproxy.utils.TestEndpointMapper; diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/cache/CacheTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/cache/CacheTest.java index e2ac4aaf6..3a41a2fff 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/cache/CacheTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/cache/CacheTest.java @@ -43,7 +43,7 @@ import junitparams.JUnitParamsRunner; import junitparams.Parameters; import org.carapaceproxy.EndpointStats; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.core.RuntimeServerConfiguration; import org.carapaceproxy.server.config.NetworkListenerConfiguration; diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/cache/NotModifiedTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/cache/NotModifiedTest.java index c38cf0c2e..6e305e4bb 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/cache/NotModifiedTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/cache/NotModifiedTest.java @@ -27,7 +27,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import com.github.tomakehurst.wiremock.junit.WireMockRule; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.utils.RawHttpClient; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/filters/XForwardedForFilterTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/filters/XForwardedForFilterTest.java index 82ab6b0d4..fb3413da5 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/filters/XForwardedForFilterTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/filters/XForwardedForFilterTest.java @@ -29,7 +29,7 @@ import static org.junit.Assert.assertTrue; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.util.Collections; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.server.config.RequestFilterConfiguration; import org.carapaceproxy.utils.RawHttpClient; diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/filters/XTlsCipherFilterTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/filters/XTlsCipherFilterTest.java index 04ce6de54..f1a65a0a1 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/filters/XTlsCipherFilterTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/filters/XTlsCipherFilterTest.java @@ -14,7 +14,7 @@ import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.util.Collections; import java.util.Set; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.RequestFilterConfiguration; diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/filters/XTlsProtocolFilterTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/filters/XTlsProtocolFilterTest.java index bc3ae8051..1d62390a5 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/filters/XTlsProtocolFilterTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/filters/XTlsProtocolFilterTest.java @@ -13,7 +13,7 @@ import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.util.Collections; import java.util.Set; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.RequestFilterConfiguration; diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/requestmatcher/RequestMatcherTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/requestmatcher/RequestMatcherTest.java index 089cb4afd..b64edfcec 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/requestmatcher/RequestMatcherTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/requestmatcher/RequestMatcherTest.java @@ -31,7 +31,7 @@ import javax.ws.rs.core.HttpHeaders; import org.carapaceproxy.core.ProxyRequest; import org.carapaceproxy.server.config.ConfigurationNotValidException; -import org.carapaceproxy.client.EndpointKey; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.server.mapper.requestmatcher.parser.ParseException; import org.carapaceproxy.server.mapper.requestmatcher.parser.RequestMatchParser; import org.carapaceproxy.server.mapper.requestmatcher.parser.TokenMgrError; From e6c10515db4475b2dd52f13c5c917518cd4928d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Tue, 29 Oct 2024 16:41:11 +0100 Subject: [PATCH 5/9] refactor: use EndpointKey in BackendConfiguration --- .../carapaceproxy/api/BackendsResource.java | 5 ++-- .../server/backends/BackendHealthManager.java | 10 +++---- .../server/config/BackendConfiguration.java | 17 +++++++++--- .../server/mapper/StandardEndpointMapper.java | 2 +- .../server/mapper/HealthCheckTest.java | 26 +++++++++---------- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java b/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java index d268d5f99..c18ee299f 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java +++ b/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java @@ -80,10 +80,9 @@ public Map getAll() { server.getMapper().getBackends().values().forEach(backendConf -> { String id = backendConf.id(); - String hostPort = backendConf.getHostPort(); BackendBean bean = new BackendBean(id, backendConf.host(), backendConf.port()); bean.lastProbePath = backendConf.probePath(); - EndpointKey key = EndpointKey.make(hostPort); + EndpointKey key = backendConf.hostPort(); Map poolsStats = server.getConnectionPoolsStats().get(key); if (poolsStats != null) { bean.openConnections = poolsStats.values().stream() @@ -95,7 +94,7 @@ public Map getAll() { bean.totalRequests = epstats.getTotalRequests().longValue(); bean.lastActivityTs = epstats.getLastActivity().longValue(); } - BackendHealthStatus bhs = backendsSnapshot.get(hostPort); + BackendHealthStatus bhs = backendsSnapshot.get(key.toString()); if (bhs != null) { bean.available = bhs.isAvailable(); bean.reportedAsUnreachable = bhs.isReportedAsUnreachable(); 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 22f4331bc..0290021ab 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 @@ -33,9 +33,9 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; -import org.carapaceproxy.server.mapper.EndpointMapper; import org.carapaceproxy.core.RuntimeServerConfiguration; import org.carapaceproxy.server.config.BackendConfiguration; +import org.carapaceproxy.server.mapper.EndpointMapper; import org.carapaceproxy.utils.PrometheusUtils; /** @@ -141,8 +141,8 @@ public void run() { } Collection backendConfigurations = mapper.getBackends().values(); for (BackendConfiguration bconf : backendConfigurations) { - String hostPort = bconf.getHostPort(); - BackendHealthStatus status = backends.computeIfAbsent(hostPort, (_hostPort) -> new BackendHealthStatus(_hostPort)); + String hostPort = bconf.hostPort().toString(); + BackendHealthStatus status = backends.computeIfAbsent(hostPort, BackendHealthStatus::new); BackendHealthCheck checkResult = BackendHealthCheck.check( bconf.host(), bconf.port(), bconf.probePath(), connectTimeout); @@ -176,7 +176,7 @@ public void run() { for (String key : backends.keySet()) { boolean found = false; for (BackendConfiguration bconf : backendConfigurations) { - if (bconf.getHostPort().equals(key)) { + if (bconf.hostPort().toString().equals(key)) { found = true; break; } @@ -197,7 +197,7 @@ public void reportBackendUnreachable(String hostPort, long timestamp, String cau } private BackendHealthStatus getBackendStatus(String hostPort) { - BackendHealthStatus status = backends.computeIfAbsent(hostPort, (_hostPort) -> new BackendHealthStatus(_hostPort)); + BackendHealthStatus status = backends.computeIfAbsent(hostPort, BackendHealthStatus::new); if (status == null) { throw new RuntimeException("Unknown backend " + hostPort); } diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/config/BackendConfiguration.java b/carapace-server/src/main/java/org/carapaceproxy/server/config/BackendConfiguration.java index a4de09880..e73bc127b 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/config/BackendConfiguration.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/config/BackendConfiguration.java @@ -19,11 +19,22 @@ */ package org.carapaceproxy.server.config; +import org.carapaceproxy.core.EndpointKey; + /** * Configuration of a single backend server */ -public record BackendConfiguration(String id, String host, int port, String probePath) { - public String getHostPort() { - return host + ":" + port; +public record BackendConfiguration(String id, EndpointKey hostPort, String probePath) { + + public BackendConfiguration(String id, String host, int port, String probePath) { + this(id, new EndpointKey(host, port), probePath); + } + + public String host() { + return hostPort.host(); + } + + public int port() { + return hostPort.port(); } } 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 624b4d50f..b836e09ad 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,7 +219,7 @@ public MapResult map(ProxyRequest request) { } BackendConfiguration backend = this.backends.get(backendId); - if (backend != null && parent.getBackendHealthManager().isAvailable(backend.getHostPort())) { + if (backend != null && parent.getBackendHealthManager().isAvailable(backend.hostPort().toString())) { List customHeaders = action.getCustomHeaders(); if (this.debuggingHeaderEnabled) { customHeaders = new ArrayList<>(customHeaders); diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java index c58464b95..ca279d0da 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java @@ -57,7 +57,7 @@ public void test() throws Exception { Map backends = new HashMap<>(); BackendConfiguration b1conf = new BackendConfiguration("myid", "localhost", wireMockRule.port(), "/status.html"); - backends.put(b1conf.getHostPort(), b1conf); + backends.put(b1conf.hostPort().toString(), b1conf); EndpointMapper mapper = new TestEndpointMapper(null, 0, false, backends); RuntimeServerConfiguration conf = new RuntimeServerConfiguration(); @@ -79,15 +79,15 @@ public void test() throws Exception { System.out.println("status=" + status); assertThat(status.size(), is(1)); - BackendConfiguration bconf = mapper.getBackends().get(b1conf.getHostPort()); + BackendConfiguration bconf = mapper.getBackends().get(b1conf.hostPort().toString()); assertThat(bconf.id(), is("myid")); assertThat(bconf.host(), is("localhost")); assertThat(bconf.port(), is(wireMockRule.port())); assertThat(bconf.probePath(), is("/status.html")); - BackendHealthStatus _status = status.get(b1conf.getHostPort()); + BackendHealthStatus _status = status.get(b1conf.hostPort().toString()); assertThat(_status, is(not(nullValue()))); - assertThat(_status.getHostPort(), is(b1conf.getHostPort())); + assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); assertThat(_status.isAvailable(), is(true)); assertThat(_status.isReportedAsUnreachable(), is(false)); assertThat(_status.getReportedAsUnreachableTs(), is(0L)); @@ -117,15 +117,15 @@ public void test() throws Exception { System.out.println("status=" + status); assertThat(status.size(), is(1)); - BackendConfiguration bconf = mapper.getBackends().get(b1conf.getHostPort()); + BackendConfiguration bconf = mapper.getBackends().get(b1conf.hostPort().toString()); assertThat(bconf.id(), is("myid")); assertThat(bconf.host(), is("localhost")); assertThat(bconf.port(), is(wireMockRule.port())); assertThat(bconf.probePath(), is("/status.html")); - BackendHealthStatus _status = status.get(b1conf.getHostPort()); + BackendHealthStatus _status = status.get(b1conf.hostPort().toString()); assertThat(_status, is(not(nullValue()))); - assertThat(_status.getHostPort(), is(b1conf.getHostPort())); + assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); assertThat(_status.isAvailable(), is(false)); assertThat(_status.isReportedAsUnreachable(), is(true)); assertThat(_status.getReportedAsUnreachableTs() >= startTs, is(true)); @@ -158,15 +158,15 @@ public void test() throws Exception { System.out.println("status=" + status); assertThat(status.size(), is(1)); - BackendConfiguration bconf = mapper.getBackends().get(b1conf.getHostPort()); + BackendConfiguration bconf = mapper.getBackends().get(b1conf.hostPort().toString()); assertThat(bconf.id(), is("myid")); assertThat(bconf.host(), is("localhost")); assertThat(bconf.port(), is(wireMockRule.port())); assertThat(bconf.probePath(), is("/status.html")); - BackendHealthStatus _status = status.get(b1conf.getHostPort()); + BackendHealthStatus _status = status.get(b1conf.hostPort().toString()); assertThat(_status, is(not(nullValue()))); - assertThat(_status.getHostPort(), is(b1conf.getHostPort())); + assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); assertThat(_status.isAvailable(), is(false)); assertThat(_status.isReportedAsUnreachable(), is(true)); assertThat(_status.getReportedAsUnreachableTs(), is(reportedAsUnreachableTs)); @@ -197,15 +197,15 @@ public void test() throws Exception { System.out.println("status=" + status); assertThat(status.size(), is(1)); - BackendConfiguration bconf = mapper.getBackends().get(b1conf.getHostPort()); + BackendConfiguration bconf = mapper.getBackends().get(b1conf.hostPort().toString()); assertThat(bconf.id(), is("myid")); assertThat(bconf.host(), is("localhost")); assertThat(bconf.port(), is(wireMockRule.port())); assertThat(bconf.probePath(), is("/status.html")); - BackendHealthStatus _status = status.get(b1conf.getHostPort()); + BackendHealthStatus _status = status.get(b1conf.hostPort().toString()); assertThat(_status, is(not(nullValue()))); - assertThat(_status.getHostPort(), is(b1conf.getHostPort())); + assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); assertThat(_status.isAvailable(), is(true)); assertThat(_status.isReportedAsUnreachable(), is(false)); assertThat(_status.getReportedAsUnreachableTs(), is(0L)); From b7dc21605fe9a464105007b754a7188c8fe9a722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Tue, 29 Oct 2024 16:47:23 +0100 Subject: [PATCH 6/9] refactor: use EndpointKey in BackendHealthStatus --- .../carapaceproxy/api/BackendsResource.java | 4 +-- .../core/ProxyRequestsManager.java | 2 +- .../server/backends/BackendHealthManager.java | 25 +++++++++---------- .../server/backends/BackendHealthStatus.java | 7 +++--- .../server/mapper/StandardEndpointMapper.java | 2 +- .../server/mapper/HealthCheckTest.java | 17 +++++++------ 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java b/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java index c18ee299f..41cd05e0a 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java +++ b/carapace-server/src/main/java/org/carapaceproxy/api/BackendsResource.java @@ -75,7 +75,7 @@ public BackendBean(String id, String host, int port) { @GET public Map getAll() { HttpProxyServer server = (HttpProxyServer) context.getAttribute("server"); - Map backendsSnapshot = server.getBackendHealthManager().getBackendsSnapshot(); + Map backendsSnapshot = server.getBackendHealthManager().getBackendsSnapshot(); Map res = new HashMap<>(); server.getMapper().getBackends().values().forEach(backendConf -> { @@ -94,7 +94,7 @@ public Map getAll() { bean.totalRequests = epstats.getTotalRequests().longValue(); bean.lastActivityTs = epstats.getLastActivity().longValue(); } - BackendHealthStatus bhs = backendsSnapshot.get(key.toString()); + BackendHealthStatus bhs = backendsSnapshot.get(key); if (bhs != null) { bean.available = bhs.isAvailable(); bean.reportedAsUnreachable = bhs.isReportedAsUnreachable(); diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java index 56ea9d0cf..402c1cb69 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/ProxyRequestsManager.java @@ -493,7 +493,7 @@ public Publisher forward(ProxyRequest request, boolean cache) { }).onErrorResume(err -> { // custom endpoint request/response error handling PENDING_REQUESTS_GAUGE.dec(); - String endpoint = request.getAction().host + ":" + request.getAction().port; + EndpointKey endpoint = EndpointKey.make(request.getAction().host, request.getAction().port); if (err instanceof io.netty.handler.timeout.ReadTimeoutException) { STUCK_REQUESTS_COUNTER.inc(); LOGGER.log(Level.SEVERE, "Read timeout error occurred for endpoint {0}; request: {1}", new Object[]{endpoint, request}); 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 0290021ab..e4baad921 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 @@ -23,7 +23,6 @@ import io.prometheus.client.Gauge; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -33,6 +32,7 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.RuntimeServerConfiguration; import org.carapaceproxy.server.config.BackendConfiguration; import org.carapaceproxy.server.mapper.EndpointMapper; @@ -62,7 +62,7 @@ public class BackendHealthManager implements Runnable { private volatile int connectTimeout; private volatile boolean started; // keep track of start() calling - private final ConcurrentHashMap backends = new ConcurrentHashMap<>(); + private final ConcurrentHashMap backends = new ConcurrentHashMap<>(); public BackendHealthManager(RuntimeServerConfiguration conf, EndpointMapper mapper) { @@ -141,8 +141,7 @@ public void run() { } Collection backendConfigurations = mapper.getBackends().values(); for (BackendConfiguration bconf : backendConfigurations) { - String hostPort = bconf.hostPort().toString(); - BackendHealthStatus status = backends.computeIfAbsent(hostPort, BackendHealthStatus::new); + BackendHealthStatus status = backends.computeIfAbsent(bconf.hostPort(), BackendHealthStatus::new); BackendHealthCheck checkResult = BackendHealthCheck.check( bconf.host(), bconf.port(), bconf.probePath(), connectTimeout); @@ -172,11 +171,11 @@ public void run() { BACKEND_UPSTATUS_GAUGE.labels(bconf.host() + "_" + bconf.port()).set(1); } } - List toRemove = new ArrayList<>(); - for (String key : backends.keySet()) { + List toRemove = new ArrayList<>(); + for (EndpointKey key : backends.keySet()) { boolean found = false; for (BackendConfiguration bconf : backendConfigurations) { - if (bconf.hostPort().toString().equals(key)) { + if (bconf.hostPort().equals(key)) { found = true; break; } @@ -191,12 +190,12 @@ public void run() { } } - public void reportBackendUnreachable(String hostPort, long timestamp, String cause) { + public void reportBackendUnreachable(EndpointKey hostPort, long timestamp, String cause) { BackendHealthStatus backend = getBackendStatus(hostPort); backend.reportAsUnreachable(timestamp); } - private BackendHealthStatus getBackendStatus(String hostPort) { + private BackendHealthStatus getBackendStatus(EndpointKey hostPort) { BackendHealthStatus status = backends.computeIfAbsent(hostPort, BackendHealthStatus::new); if (status == null) { throw new RuntimeException("Unknown backend " + hostPort); @@ -204,16 +203,16 @@ private BackendHealthStatus getBackendStatus(String hostPort) { return status; } - public void reportBackendReachable(String hostPort) { + public void reportBackendReachable(EndpointKey hostPort) { BackendHealthStatus backend = getBackendStatus(hostPort); backend.reportAsReachable(); } - public Map getBackendsSnapshot() { - return new HashMap<>(backends); + public Map getBackendsSnapshot() { + return Map.copyOf(backends); } - public boolean isAvailable(String hostPort) { + public boolean isAvailable(EndpointKey hostPort) { BackendHealthStatus backend = getBackendStatus(hostPort); return backend != null && backend.isAvailable(); } diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/backends/BackendHealthStatus.java b/carapace-server/src/main/java/org/carapaceproxy/server/backends/BackendHealthStatus.java index b62b9b502..38b951373 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/backends/BackendHealthStatus.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/backends/BackendHealthStatus.java @@ -21,6 +21,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import org.carapaceproxy.core.EndpointKey; /** * Health of a backend @@ -31,18 +32,18 @@ public class BackendHealthStatus { private static final Logger LOG = Logger.getLogger(BackendHealthStatus.class.getName()); - private final String hostPort; + private final EndpointKey hostPort; private volatile boolean reportedAsUnreachable; private long reportedAsUnreachableTs; private BackendHealthCheck lastProbe; - public BackendHealthStatus(String hostPort) { + public BackendHealthStatus(EndpointKey hostPort) { this.hostPort = hostPort; } - public String getHostPort() { + public EndpointKey getHostPort() { return hostPort; } 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 b836e09ad..acd83ffc4 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,7 +219,7 @@ public MapResult map(ProxyRequest request) { } BackendConfiguration backend = this.backends.get(backendId); - if (backend != null && parent.getBackendHealthManager().isAvailable(backend.hostPort().toString())) { + if (backend != null && parent.getBackendHealthManager().isAvailable(backend.hostPort())) { List customHeaders = action.getCustomHeaders(); if (this.debuggingHeaderEnabled) { customHeaders = new ArrayList<>(customHeaders); diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java index ca279d0da..de75d5eb1 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java @@ -30,6 +30,7 @@ import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.util.HashMap; import java.util.Map; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.RuntimeServerConfiguration; import org.carapaceproxy.server.backends.BackendHealthCheck; import org.carapaceproxy.server.backends.BackendHealthManager; @@ -75,7 +76,7 @@ public void test() throws Exception { hman.run(); long endTs = System.currentTimeMillis(); - Map status = hman.getBackendsSnapshot(); + Map status = hman.getBackendsSnapshot(); System.out.println("status=" + status); assertThat(status.size(), is(1)); @@ -85,7 +86,7 @@ public void test() throws Exception { assertThat(bconf.port(), is(wireMockRule.port())); assertThat(bconf.probePath(), is("/status.html")); - BackendHealthStatus _status = status.get(b1conf.hostPort().toString()); + BackendHealthStatus _status = status.get(b1conf.hostPort()); assertThat(_status, is(not(nullValue()))); assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); assertThat(_status.isAvailable(), is(true)); @@ -113,7 +114,7 @@ public void test() throws Exception { hman.run(); long endTs = System.currentTimeMillis(); - Map status = hman.getBackendsSnapshot(); + Map status = hman.getBackendsSnapshot(); System.out.println("status=" + status); assertThat(status.size(), is(1)); @@ -123,7 +124,7 @@ public void test() throws Exception { assertThat(bconf.port(), is(wireMockRule.port())); assertThat(bconf.probePath(), is("/status.html")); - BackendHealthStatus _status = status.get(b1conf.hostPort().toString()); + BackendHealthStatus _status = status.get(b1conf.hostPort()); assertThat(_status, is(not(nullValue()))); assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); assertThat(_status.isAvailable(), is(false)); @@ -154,7 +155,7 @@ public void test() throws Exception { hman.run(); long endTs = System.currentTimeMillis(); - Map status = hman.getBackendsSnapshot(); + Map status = hman.getBackendsSnapshot(); System.out.println("status=" + status); assertThat(status.size(), is(1)); @@ -164,7 +165,7 @@ public void test() throws Exception { assertThat(bconf.port(), is(wireMockRule.port())); assertThat(bconf.probePath(), is("/status.html")); - BackendHealthStatus _status = status.get(b1conf.hostPort().toString()); + BackendHealthStatus _status = status.get(b1conf.hostPort()); assertThat(_status, is(not(nullValue()))); assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); assertThat(_status.isAvailable(), is(false)); @@ -193,7 +194,7 @@ public void test() throws Exception { hman.run(); long endTs = System.currentTimeMillis(); - Map status = hman.getBackendsSnapshot(); + Map status = hman.getBackendsSnapshot(); System.out.println("status=" + status); assertThat(status.size(), is(1)); @@ -203,7 +204,7 @@ public void test() throws Exception { assertThat(bconf.port(), is(wireMockRule.port())); assertThat(bconf.probePath(), is("/status.html")); - BackendHealthStatus _status = status.get(b1conf.hostPort().toString()); + BackendHealthStatus _status = status.get(b1conf.hostPort()); assertThat(_status, is(not(nullValue()))); assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); assertThat(_status.isAvailable(), is(true)); From 0a1306c561699f29db7443bfadc3b1cce781df3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Tue, 29 Oct 2024 16:51:47 +0100 Subject: [PATCH 7/9] refactor: de-lombok EndpointStats --- .../java/org/carapaceproxy/EndpointStats.java | 18 ++++++++++----- .../org/carapaceproxy/core/EndpointKey.java | 22 ++++++++++++++++++- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java b/carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java index f518d2fc9..35312c6ae 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java +++ b/carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java @@ -21,8 +21,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; -import lombok.Getter; -import lombok.ToString; import org.carapaceproxy.core.EndpointKey; /** @@ -30,17 +28,25 @@ * * @author enrico.olivelli */ -@ToString public class EndpointStats { private final EndpointKey key; - @Getter private final AtomicInteger totalRequests = new AtomicInteger(); - @Getter private final AtomicLong lastActivity = new AtomicLong(); - public EndpointStats(EndpointKey key) { + public EndpointStats(final EndpointKey key) { this.key = key; } + public String toString() { + return "EndpointStats(key=" + this.key + ", totalRequests=" + this.totalRequests + ", lastActivity=" + this.lastActivity + ")"; + } + + public AtomicInteger getTotalRequests() { + return this.totalRequests; + } + + public AtomicLong getLastActivity() { + return this.lastActivity; + } } diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/EndpointKey.java b/carapace-server/src/main/java/org/carapaceproxy/core/EndpointKey.java index 5871b4e66..66b487f50 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/EndpointKey.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/EndpointKey.java @@ -19,6 +19,8 @@ */ package org.carapaceproxy.core; +import org.carapaceproxy.utils.StringUtils; + /** * Identifier of an endpoint * @@ -26,6 +28,24 @@ */ public record EndpointKey(String host, int port) { + /** + * The minimum port value according to RFC 6335. + */ + public static final int MIN_PORT = 0; + /** + * The maximum port value according to RFC 6335. + */ + public static final int MAX_PORT = 0xffff; + + public EndpointKey { + if (StringUtils.isBlank(host)) { + throw new IllegalArgumentException("Hostname cannot be blank"); + } + if (port > MAX_PORT || port < MIN_PORT) { + throw new IllegalArgumentException("Invalid port: " + port); + } + } + public static EndpointKey make(String host, int port) { return new EndpointKey(host, port); } @@ -33,7 +53,7 @@ public static EndpointKey make(String host, int port) { public static EndpointKey make(String hostAndPort) { int pos = hostAndPort.indexOf(':'); if (pos <= 0) { - return new EndpointKey(hostAndPort, 0); + return new EndpointKey(hostAndPort, MIN_PORT); } String host = hostAndPort.substring(0, pos); int port = Integer.parseInt(hostAndPort.substring(pos + 1)); From ae4b887246c4be0f834b15f79f549407c79fca36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Tue, 29 Oct 2024 17:20:25 +0100 Subject: [PATCH 8/9] refactor: use EndpointKey in BackendHealthStatus --- .../server/cache/ContentsCache.java | 16 ++++- .../backends/StuckRequestsTest.java | 26 ++++---- .../backends/UnreachableBackendTest.java | 66 +++++++++++-------- .../server/cache/CaffeineCacheImplTest.java | 23 +++---- .../filters/XForwardedForFilterTest.java | 1 + .../BasicStandardEndpointMapperTest.java | 9 +-- .../org/carapaceproxy/utils/TestUtils.java | 61 +---------------- 7 files changed, 80 insertions(+), 122 deletions(-) diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/cache/ContentsCache.java b/carapace-server/src/main/java/org/carapaceproxy/server/cache/ContentsCache.java index 298251ce2..65f67a37c 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/cache/ContentsCache.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/cache/ContentsCache.java @@ -275,14 +275,24 @@ public ContentSender getCacheSender(ProxyRequest request) { public static class CachedContent { HttpClientResponse response; - final List chunks = new ArrayList<>(); - final long creationTs = System.currentTimeMillis(); + final List chunks; + final long creationTs; long lastModified; - long expiresTs = -1; + long expiresTs; long heapSize; long directSize; int hits; + public CachedContent(final long creationTs) { + this.creationTs = creationTs; + this.chunks = new ArrayList<>(); + this.expiresTs = -1; + } + + public CachedContent() { + this(System.currentTimeMillis()); + } + public boolean modifiedSince(ProxyRequest request) { final long ifModifiedSince = request.getRequestHeaders().getTimeMillis(IF_MODIFIED_SINCE, -1); return ifModifiedSince == -1 || getLastModified() <= 0 || ifModifiedSince < getLastModified(); 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 a295a6198..2f88ad7a6 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java @@ -106,17 +106,19 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt String s = resp.toString(); System.out.println("s:" + s); assertEquals("HTTP/1.1 500 Internal Server Error\r\n", resp.getStatusLine()); - assertEquals("\n" - + " \n" - + " An internal error occurred\n" - + " \n" - + "\n", resp.getBodyString()); + assertEquals(""" + + + An internal error occurred + \s + + """, resp.getBodyString()); } assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); assertThat((int) ProxyRequestsManager.STUCK_REQUESTS_COUNTER.get() > 0, is(true)); - assertEquals(backendsUnreachableOnStuckRequests, !server.getBackendHealthManager().isAvailable(key.toString())); + assertEquals(backendsUnreachableOnStuckRequests, !server.getBackendHealthManager().isAvailable(key)); try (RawHttpClient client = new RawHttpClient("localhost", port)) { RawHttpClient.HttpResponse resp = client.executeRequest("GET /good-index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"); @@ -124,11 +126,13 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt System.out.println("s:" + s); if (backendsUnreachableOnStuckRequests) { assertEquals("HTTP/1.1 503 Service Unavailable\r\n", resp.getStatusLine()); - assertEquals("\n" - + " \n" - + " Service Unavailable\n" - + " \n" - + "\n", resp.getBodyString()); + assertEquals(""" + + + Service Unavailable + + + """, resp.getBodyString()); } else { assertEquals("HTTP/1.1 200 OK\r\n", resp.getStatusLine()); assertEquals("it works !!", resp.getBodyString()); 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 807ecd7f2..8430b06bb 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java @@ -19,7 +19,6 @@ */ package org.carapaceproxy.backends; -import org.carapaceproxy.utils.TestEndpointMapper; 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; @@ -28,18 +27,19 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import com.github.tomakehurst.wiremock.http.Fault; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.util.Arrays; import java.util.Collection; +import java.util.Properties; +import org.carapaceproxy.configstore.PropertiesConfigurationStore; import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; +import org.carapaceproxy.core.ProxyRequestsManager; import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.utils.RawHttpClient; -import static org.junit.Assert.assertTrue; -import java.util.Properties; -import org.carapaceproxy.configstore.PropertiesConfigurationStore; -import org.carapaceproxy.core.ProxyRequestsManager; +import org.carapaceproxy.utils.TestEndpointMapper; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -96,13 +96,15 @@ public void testWithUnreachableBackend() throws Exception { String s = resp.toString(); System.out.println("s:" + s); assertEquals("HTTP/1.1 503 Service Unavailable\r\n", resp.getStatusLine()); - assertEquals("\n" - + " \n" - + " Service Unavailable\n" - + " \n" - + "\n", resp.getBodyString()); + assertEquals(""" + + + Service Unavailable + + + """, resp.getBodyString()); } - assertFalse(server.getBackendHealthManager().isAvailable(key.toString())); + assertFalse(server.getBackendHealthManager().isAvailable(key)); assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); } } @@ -132,13 +134,15 @@ public void testEmptyResponse() throws Exception { String s = resp.toString(); System.out.println("s:" + s); assertEquals("HTTP/1.1 503 Service Unavailable\r\n", resp.getStatusLine()); - assertEquals("\n" - + " \n" - + " Service Unavailable\n" - + " \n" - + "\n", resp.getBodyString()); + assertEquals(""" + + + Service Unavailable + + + """, resp.getBodyString()); } - assertTrue(server.getBackendHealthManager().isAvailable(key.toString())); // no troubles for new connections + assertTrue(server.getBackendHealthManager().isAvailable(key)); // no troubles for new connections assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); } } @@ -162,13 +166,15 @@ public void testConnectionResetByPeer() throws Exception { String s = resp.toString(); System.out.println("s:" + s); assertEquals("HTTP/1.1 503 Service Unavailable\r\n", resp.getStatusLine()); - assertEquals("\n" - + " \n" - + " Service Unavailable\n" - + " \n" - + "\n", resp.getBodyString()); + assertEquals(""" + + + Service Unavailable + + + """, resp.getBodyString()); } - assertTrue(server.getBackendHealthManager().isAvailable(key.toString())); // no troubles for new connections + assertTrue(server.getBackendHealthManager().isAvailable(key)); // no troubles for new connections assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); } } @@ -196,13 +202,15 @@ public void testNonHttpResponseThenClose() throws Exception { String s = resp.toString(); System.out.println("s:" + s); assertEquals("HTTP/1.1 503 Service Unavailable\r\n", resp.getStatusLine()); - assertEquals("\n" - + " \n" - + " Service Unavailable\n" - + " \n" - + "\n", resp.getBodyString()); + assertEquals(""" + + + Service Unavailable + + + """, resp.getBodyString()); } - assertTrue(server.getBackendHealthManager().isAvailable(key.toString())); + assertTrue(server.getBackendHealthManager().isAvailable(key)); assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0)); } } diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/cache/CaffeineCacheImplTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/cache/CaffeineCacheImplTest.java index 4bd90184a..4c00587a5 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/cache/CaffeineCacheImplTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/cache/CaffeineCacheImplTest.java @@ -19,8 +19,13 @@ */ package org.carapaceproxy.server.cache; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertTrue; import com.github.benmanes.caffeine.cache.RemovalCause; - +import io.netty.buffer.Unpooled; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; @@ -29,21 +34,10 @@ import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; - -import org.carapaceproxy.server.cache.ContentsCache.ContentKey; import org.carapaceproxy.server.cache.ContentsCache.CachedContent; +import org.carapaceproxy.server.cache.ContentsCache.ContentKey; import org.carapaceproxy.utils.TestUtils; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; - import org.junit.After; - -import static org.junit.Assert.assertTrue; - -import io.netty.buffer.Unpooled; import org.junit.Test; /** @@ -104,9 +98,8 @@ public CacheEntry(ContentKey key, CachedContent payload, boolean copy) { this.payload = payload; } else { this.key = key; - this.payload = new CachedContent(); + this.payload = new CachedContent(payload.creationTs); this.payload.chunks.addAll(payload.chunks); - TestUtils.setFinalField(this.payload, "creationTs", payload.creationTs); this.payload.directSize = payload.directSize; this.payload.expiresTs = payload.expiresTs; this.payload.heapSize = payload.heapSize; diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/filters/XForwardedForFilterTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/filters/XForwardedForFilterTest.java index fb3413da5..5b5bbad31 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/filters/XForwardedForFilterTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/filters/XForwardedForFilterTest.java @@ -42,6 +42,7 @@ * * @author enrico.olivelli */ +@SuppressWarnings("deprecation") public class XForwardedForFilterTest { @Rule 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 8777e54be..eb8049076 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 @@ -45,6 +45,7 @@ import java.util.Properties; import org.apache.commons.io.IOUtils; import org.carapaceproxy.configstore.PropertiesConfigurationStore; +import org.carapaceproxy.core.EndpointKey; import org.carapaceproxy.core.HttpProxyServer; import org.carapaceproxy.core.StaticContentsManager; import org.carapaceproxy.server.backends.BackendHealthManager; @@ -254,8 +255,8 @@ public void testRouteErrors() throws Exception { PropertiesConfigurationStore config = new PropertiesConfigurationStore(configuration); BackendHealthManager bhMan = mock(BackendHealthManager.class); - when(bhMan.isAvailable(eq("localhost:" + backend1.port()))).thenReturn(true); - when(bhMan.isAvailable(eq("localhost-down:" + backend1.port()))).thenReturn(false); // simulate unreachable backend -> expected 500 error + when(bhMan.isAvailable(eq(EndpointKey.make("localhost:" + backend1.port())))).thenReturn(true); + when(bhMan.isAvailable(eq(EndpointKey.make("localhost-down:" + backend1.port())))).thenReturn(false); // simulate unreachable backend -> expected 500 error server.setBackendHealthManager(bhMan); server.configureAtBoot(config); server.start(); @@ -320,8 +321,8 @@ public void testDefaultRoute() throws Exception { mapper.addRoute(new RouteConfiguration("route-default", "cache", true, new RegexpRequestMatcher(PROPERTY_URI, ".*html"))); BackendHealthManager bhMan = mock(BackendHealthManager.class); - when(bhMan.isAvailable(eq("localhost:" + backendPort))).thenReturn(true); - when(bhMan.isAvailable(eq("localhost-down:" + backendPort))).thenReturn(false); // simulate unreachable backend -> expected 500 error + when(bhMan.isAvailable(eq(EndpointKey.make("localhost:" + backendPort)))).thenReturn(true); + when(bhMan.isAvailable(eq(EndpointKey.make("localhost-down:" + backendPort)))).thenReturn(false); // simulate unreachable backend -> expected 500 error try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) { server.setBackendHealthManager(bhMan); diff --git a/carapace-server/src/test/java/org/carapaceproxy/utils/TestUtils.java b/carapace-server/src/test/java/org/carapaceproxy/utils/TestUtils.java index 95da38b91..82f8eafde 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/utils/TestUtils.java +++ b/carapace-server/src/test/java/org/carapaceproxy/utils/TestUtils.java @@ -19,6 +19,7 @@ */ package org.carapaceproxy.utils; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -29,12 +30,6 @@ import java.util.Arrays; import java.util.concurrent.Callable; import org.junit.Assert; -import static org.junit.Assert.assertTrue; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.security.AccessController; -import java.security.PrivilegedAction; -import sun.misc.Unsafe; /** * Utility for tests @@ -149,58 +144,4 @@ public static T expectThrows(Class expectedThrowable, T expectedThrowable.getSimpleName()); throw new AssertionError(message); } - - public static void setFinalStaticField(Class clazz, String name, Object newValue) { - try { - Field field = clazz.getDeclaredField(name); - field.setAccessible(true); - int fieldModifiersMask = field.getModifiers(); - boolean isFinalModifierPresent = (fieldModifiersMask & Modifier.FINAL) == Modifier.FINAL; - if (isFinalModifierPresent) { - AccessController.doPrivileged((PrivilegedAction) (() -> { - try { - Field field1 = Unsafe.class.getDeclaredField("theUnsafe"); - field1.setAccessible(true); - Unsafe unsafe = (Unsafe) field1.get(null); - long offset = unsafe.staticFieldOffset(field); - unsafe.putObject(unsafe.staticFieldBase(field), offset, newValue); - return null; - } catch (Throwable t) { - throw new RuntimeException("Cannot patch final static field " + name + " on " + clazz + " with value " + newValue + ": " + t, t); - } - })); - } else { - field.set(null, newValue); - } - } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException ex) { - throw new RuntimeException(ex); - } - } - - public static void setFinalField(Object object, String name, Object newValue) { - try { - Field field = object.getClass().getDeclaredField(name); - field.setAccessible(true); - int fieldModifiersMask = field.getModifiers(); - boolean isFinalModifierPresent = (fieldModifiersMask & Modifier.FINAL) == Modifier.FINAL; - if (isFinalModifierPresent) { - AccessController.doPrivileged((PrivilegedAction) (() -> { - try { - Field field1 = Unsafe.class.getDeclaredField("theUnsafe"); - field1.setAccessible(true); - Unsafe unsafe = (Unsafe) field1.get(null); - long offset = unsafe.objectFieldOffset(field); - unsafe.putObject(object, offset, newValue); - return null; - } catch (Throwable t) { - throw new RuntimeException("Cannot patch final field " + name + " on " + object + " with value " + newValue + ": " + t, t); - } - })); - } else { - field.set(object, newValue); - } - } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException ex) { - throw new RuntimeException(ex); - } - } } From e592d249b37fb41a03c2364883d66de8b87dd536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maltoni?= Date: Wed, 20 Nov 2024 17:08:23 +0100 Subject: [PATCH 9/9] feat: fix HealthCheckTest --- .../org/carapaceproxy/server/mapper/HealthCheckTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java index de75d5eb1..79b4596e0 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java @@ -88,7 +88,7 @@ public void test() throws Exception { BackendHealthStatus _status = status.get(b1conf.hostPort()); assertThat(_status, is(not(nullValue()))); - assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); + assertThat(_status.getHostPort(), is(b1conf.hostPort())); assertThat(_status.isAvailable(), is(true)); assertThat(_status.isReportedAsUnreachable(), is(false)); assertThat(_status.getReportedAsUnreachableTs(), is(0L)); @@ -126,7 +126,7 @@ public void test() throws Exception { BackendHealthStatus _status = status.get(b1conf.hostPort()); assertThat(_status, is(not(nullValue()))); - assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); + assertThat(_status.getHostPort(), is(b1conf.hostPort())); assertThat(_status.isAvailable(), is(false)); assertThat(_status.isReportedAsUnreachable(), is(true)); assertThat(_status.getReportedAsUnreachableTs() >= startTs, is(true)); @@ -167,7 +167,7 @@ public void test() throws Exception { BackendHealthStatus _status = status.get(b1conf.hostPort()); assertThat(_status, is(not(nullValue()))); - assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); + assertThat(_status.getHostPort(), is(b1conf.hostPort())); assertThat(_status.isAvailable(), is(false)); assertThat(_status.isReportedAsUnreachable(), is(true)); assertThat(_status.getReportedAsUnreachableTs(), is(reportedAsUnreachableTs)); @@ -206,7 +206,7 @@ public void test() throws Exception { BackendHealthStatus _status = status.get(b1conf.hostPort()); assertThat(_status, is(not(nullValue()))); - assertThat(_status.getHostPort(), is(b1conf.hostPort().toString())); + assertThat(_status.getHostPort(), is(b1conf.hostPort())); assertThat(_status.isAvailable(), is(true)); assertThat(_status.isReportedAsUnreachable(), is(false)); assertThat(_status.getReportedAsUnreachableTs(), is(0L));