Skip to content

Commit

Permalink
feat: add backend.<id>.coldcapacity configuration (default -1)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiccoMlt committed Nov 8, 2024
1 parent 0deb03e commit 9d81ada
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
public record BackendConfiguration(
String id,
EndpointKey hostPort,
String probePath
String probePath,
int coldCapacity
) {

public BackendConfiguration(final String id, final String host, final int port, final String probePath) {
this(id, new EndpointKey(host, port), probePath);
public BackendConfiguration(final String id, final String host, final int port, final String probePath, final int coldCapacity) {
this(id, new EndpointKey(host, port), probePath, coldCapacity);
}

public String host() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ public MapResult map(final ProxyRequest request) {
case DOWN:
continue;
case COLD:
if (backendStatus.getConnections() > THRESHOLD) {
final int capacity = backend.coldCapacity();
if (capacity > 0 && backendStatus.getConnections() > capacity) {
// can't use this
continue;
}
Expand Down Expand Up @@ -467,14 +468,14 @@ public void configure(ConfigurationStore properties) throws ConfigurationNotVali
String prefix = "backend." + i + ".";
String id = properties.getString(prefix + "id", "");
if (!id.isEmpty()) {
boolean enabled = properties.getBoolean(prefix + "enabled", false);
String host = properties.getString(prefix + "host", "localhost");
int port = properties.getInt(prefix + "port", 8086);
String probePath = properties.getString(prefix + "probePath", "");
LOG.info("configured backend {} {}:{} enabled:{}", id, host, port, enabled);
final boolean enabled = properties.getBoolean(prefix + "enabled", false);
final String host = properties.getString(prefix + "host", "localhost");
final int port = properties.getInt(prefix + "port", 8086);
final String probePath = properties.getString(prefix + "probePath", "");
final int coldCapacity = properties.getInt(prefix + "coldCapacity", -1);
LOG.info("configured backend {} {}:{} enabled={} capacity={}", id, host, port, enabled, coldCapacity);
if (enabled) {
BackendConfiguration config = new BackendConfiguration(id, host, port, probePath);
addBackend(config);
addBackend(new BackendConfiguration(id, host, port, probePath, coldCapacity));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt
EndpointKey key = new EndpointKey("localhost", theport);

StandardEndpointMapper mapper = new StandardEndpointMapper();
mapper.addBackend(new BackendConfiguration("backend-a", "localhost", theport, "/"));
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.*")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public void test() throws Exception {
int backendPort = backend.port();
StandardEndpointMapper mapper = new StandardEndpointMapper();

mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/"));
mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/"));
mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/", -1));
mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/", -1));
mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a"));
mapper.addDirector(new DirectorConfiguration("director-2").addBackend("backend-b"));
mapper.addDirector(new DirectorConfiguration("director-all").addBackend("*")); // all the known backends
Expand Down Expand Up @@ -317,8 +317,8 @@ public void testDefaultRoute() throws Exception {
int backendPort = backend.port();

StandardEndpointMapper mapper = new StandardEndpointMapper();
mapper.addBackend(new BackendConfiguration("backend", "localhost", backendPort, "/"));
mapper.addBackend(new BackendConfiguration("backend-down", "localhost-down", backendPort, "/"));
mapper.addBackend(new BackendConfiguration("backend", "localhost", backendPort, "/", -1));
mapper.addBackend(new BackendConfiguration("backend-down", "localhost-down", backendPort, "/", -1));
mapper.addDirector(new DirectorConfiguration("director").addBackend("backend"));
mapper.addDirector(new DirectorConfiguration("director-down").addBackend("backend-down"));
mapper.addAction(new ActionConfiguration("cache", ActionConfiguration.TYPE_CACHE, "director", null, -1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void test() throws Exception {
assertEquals("thedirector", mapper.getForceDirectorParameter());
assertEquals("thebackend", mapper.getForceBackendParameter());

mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/"));
mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/"));
mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/", -1));
mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/", -1));
mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a"));
mapper.addDirector(new DirectorConfiguration("director-2").addBackend("backend-b"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class HealthCheckTest {
@Test
public void test() throws Exception {
final Map<String, BackendConfiguration> backends = new HashMap<>();
final BackendConfiguration b1conf = new BackendConfiguration("myid", "localhost", wireMockRule.port(), "/status.html");
final BackendConfiguration b1conf = new BackendConfiguration("myid", "localhost", wireMockRule.port(), "/status.html", -1);
backends.put(b1conf.hostPort().toString(), b1conf);
final EndpointMapper mapper = new TestEndpointMapper(null, 0, false, backends);
final RuntimeServerConfiguration conf = new RuntimeServerConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public TestEndpointMapper(String host, int port) {
}

public TestEndpointMapper(String host, int port, boolean cacheAll) {
this(host, port, cacheAll, Map.of(host, new BackendConfiguration(host, host, port, "/index.html")));
this(host, port, cacheAll, Map.of(host, new BackendConfiguration(host, host, port, "/index.html", -1)));
}

public TestEndpointMapper(@Nullable String host, int port, boolean cacheAll, Map<String, BackendConfiguration> backends) {
Expand Down

0 comments on commit 9d81ada

Please sign in to comment.