Skip to content

Commit

Permalink
test: drop mapper injection again
Browse files Browse the repository at this point in the history
  • Loading branch information
NiccoMlt committed Nov 14, 2024
1 parent c8925be commit e731bb0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,6 @@ public int getLocalPort() {
return listeners.getLocalPort();
}

@VisibleForTesting
public void setMapper(final EndpointMapper mapper) {
this.mapper = mapper;
}

public void startAdminInterface() throws Exception {
if (!adminServerEnabled) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public BackendHealthManager(final RuntimeServerConfiguration conf, final Endpoin
this.period = DEFAULT_PERIOD;
}

public boolean isTolerant() {
return tolerant;
}

public int getPeriod() {
return period;
}
Expand Down Expand Up @@ -217,7 +221,7 @@ public boolean exceedsCapacity(final String backendId) {
return false;
}
final BackendHealthStatus backendStatus = getBackendStatus(backendConfiguration.hostPort());
return backendConfiguration.safeCapacity() > backendStatus.getConnections() && !this.tolerant;
return backendConfiguration.safeCapacity() > backendStatus.getConnections();
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,23 @@ public MapResult map(final ProxyRequest request) {
final BackendHealthStatus backendStatus = backendHealthManager.getBackendStatus(backend.hostPort());
switch (backendStatus.getStatus()) {
case DOWN:
LOG.info("Backend {} is down, skipping...", backendId);
continue;
case COLD:
final int capacity = backend.safeCapacity();
if (backendHealthManager.exceedsCapacity(backendId)) {
final int capacity = backend.safeCapacity();
if (!backendHealthManager.isTolerant()) {
// default behavior, exceeding safe capacity is not tolerated...
LOG.info("Backend {} is cold and exceeds safe capacity of {} connections, skipping...", backendId, capacity);
continue;
}
/*
* backends are returned by the mapper sorted
* from the most desirable to the less desirable;
* if the execution reaches this point,
* we should use a cold backend even if over the recommended capacity anyway...
* we may use the cold backend even if over the recommended capacity anyway...
*/
LOG.warn("Backend {} exceeds cold capacity of {}, but will use it anyway", backendId, capacity);
LOG.warn("Cold backend {} exceeds safe capacity of {} connections, but will use it anyway", backendId, capacity);
}
// falls through
case STABLE: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
import org.carapaceproxy.server.config.NetworkListenerConfiguration;
import org.carapaceproxy.server.config.RouteConfiguration;
import org.carapaceproxy.server.config.SafeBackendSelector;
import org.carapaceproxy.server.mapper.EndpointMapper;
import org.carapaceproxy.server.mapper.StandardEndpointMapper;
import org.carapaceproxy.server.mapper.requestmatcher.RegexpRequestMatcher;
import org.carapaceproxy.utils.EndpointMapperHolder;
import org.carapaceproxy.utils.RawHttpClient;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -64,6 +64,7 @@ public class StuckRequestsTest {

@Test
@Parameters({"true", "false"})
@junitparams.naming.TestCaseName("test(backend unreachable on stuck request: {0})")
public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnStuckRequests) throws Exception {

stubFor(get(urlEqualTo("/index.html"))
Expand All @@ -85,22 +86,38 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt
final int theport = wireMockRule.port();
EndpointKey key = new EndpointKey("localhost", theport);

final EndpointMapperHolder mapperFactory = new EndpointMapperHolder(parent -> {
final EndpointMapper.Factory mapperFactory = parent -> {
StandardEndpointMapper mapper = new StandardEndpointMapper(parent, SafeBackendSelector::new);
mapper.addBackend(new BackendConfiguration("backend-a", "localhost", theport, "/", -1));
mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a"));
mapper.addAction(new ActionConfiguration("proxy-1", ActionConfiguration.TYPE_PROXY, "director-1", null, -1));
mapper.addRoute(new RouteConfiguration("route-1", "proxy-1", true, new RegexpRequestMatcher(PROPERTY_URI, ".*index.html.*")));
return mapper;
});
try (HttpProxyServer server = new HttpProxyServer(mapperFactory, tmpDir.newFolder());) {
};
try (HttpProxyServer server = new HttpProxyServer(mapperFactory, tmpDir.newFolder())) {
Properties properties = new Properties();
properties.put("backend.1.id", "backend-a");
properties.put("backend.1.enabled", "true");
properties.put("backend.1.host", "localhost");
properties.put("backend.1.port", theport);
properties.put("backend.1.probePath", "/");
properties.put("director.1.id", "director-1");
properties.put("director.1.backends", properties.getProperty("backend.1.id"));
properties.put("director.1.enabled", "true");
properties.put("action.1.id", "proxy-1");
properties.put("action.1.enabled", "true");
properties.put("action.1.type", ActionConfiguration.TYPE_PROXY);
properties.put("action.1.director", properties.getProperty("director.1.id"));
properties.put("route.100.id", "route-1");
properties.put("route.100.enabled", "true");
properties.put("route.100.match", "request.uri ~ \".*index.html.*\"");
properties.put("route.100.action", properties.getProperty("action.1.id"));

properties.put("connectionsmanager.stuckrequesttimeout", "100"); // ms
properties.put("connectionsmanager.backendsunreachableonstuckrequests", backendsUnreachableOnStuckRequests + "");
// configure resets all listeners configurations
server.configureAtBoot(new PropertiesConfigurationStore(properties));
server.addListener(new NetworkListenerConfiguration("localhost", 0));
server.setMapper(mapperFactory.getEndpointMapper());
assertEquals(100, server.getCurrentConfiguration().getStuckRequestTimeout());
assertEquals(backendsUnreachableOnStuckRequests, server.getCurrentConfiguration().isBackendsUnreachableOnStuckRequests());
server.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public UnreachableBackendTest(boolean useCache) {

@Test
public void testWithUnreachableBackend() throws Exception {

stubFor(get(urlEqualTo("/index.html"))
.willReturn(aResponse()
.withStatus(200)
Expand Down Expand Up @@ -113,12 +114,11 @@ public void testEmptyResponse() throws Exception {
TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache);
EndpointKey key = new EndpointKey("localhost", dummyport);

try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder());) {
try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) {
Properties properties = new Properties();
// configure resets all listeners configurations
server.configureAtBoot(new PropertiesConfigurationStore(properties));
server.addListener(new NetworkListenerConfiguration("localhost", 0));
server.setMapper(mapper);
server.start();
int port = server.getLocalPort();
assertTrue(port > 0);
Expand Down Expand Up @@ -182,11 +182,10 @@ public void testNonHttpResponseThenClose() throws Exception {
TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache);
EndpointKey key = new EndpointKey("localhost", dummyport);

try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder());) {
try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) {
Properties properties = new Properties();
server.configureAtBoot(new PropertiesConfigurationStore(properties));
server.addListener(new NetworkListenerConfiguration("localhost", 0));
server.setMapper(mapper);
server.start();
int port = server.getLocalPort();

Expand Down

This file was deleted.

0 comments on commit e731bb0

Please sign in to comment.