Skip to content

Commit

Permalink
Set mem_limit on linkeddatahub service
Browse files Browse the repository at this point in the history
Set a custom `HttpRequestRetryHandler` lambda on the `ApacheConnector`
  • Loading branch information
namedgraph committed Dec 13, 2024
1 parent 78c5c55 commit a4a7e8b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
linkeddatahub:
user: root # otherwise the ldh user does not have permissions to the mounted folder which is owner by root
build: .
mem_limit: 2048m
depends_on:
- fuseki-admin
- fuseki-end-user
Expand Down Expand Up @@ -117,7 +118,7 @@ services:
- CLIENT_HOST=linkeddatahub
- VARNISH_SIZE=1G
entrypoint: /bin/sh -c "cp /etc/varnish/default.vcl.template /etc/varnish/default.vcl && sed -i 's|$${BACKEND_HOST}|'"$$BACKEND_HOST"'|g' /etc/varnish/default.vcl && sed -i 's|$${BACKEND_PORT}|'"$$BACKEND_PORT"'|g' /etc/varnish/default.vcl && sed -i 's|$${CLIENT_HOST}|'"$$CLIENT_HOST"'|g' /etc/varnish/default.vcl && /usr/local/bin/docker-varnish-entrypoint \"$$0\" \"$$@\""
command: [ "-t", "86400" ] # time to live
command: [ "-t", "86400", "-p", "timeout_idle=60s" ] # time to live
volumes:
- ./platform/varnish-backend.vcl.template:/etc/varnish/default.vcl.template:ro
varnish-end-user:
Expand All @@ -132,7 +133,7 @@ services:
- CLIENT_HOST=linkeddatahub
- VARNISH_SIZE=1G
entrypoint: /bin/sh -c "cp /etc/varnish/default.vcl.template /etc/varnish/default.vcl && sed -i 's|$${BACKEND_HOST}|'"$$BACKEND_HOST"'|g' /etc/varnish/default.vcl && sed -i 's|$${BACKEND_PORT}|'"$$BACKEND_PORT"'|g' /etc/varnish/default.vcl && sed -i 's|$${CLIENT_HOST}|'"$$CLIENT_HOST"'|g' /etc/varnish/default.vcl && /usr/local/bin/docker-varnish-entrypoint \"$$0\" \"$$@\""
command: [ "-t", "86400" ] # time to live
command: [ "-t", "86400", "-p", "timeout_idle=60s" ] # time to live
volumes:
- ./platform/varnish-backend.vcl.template:/etc/varnish/default.vcl.template:ro
email-server:
Expand Down
41 changes: 39 additions & 2 deletions src/main/java/com/atomgraph/linkeddatahub/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@
import net.sf.saxon.s9api.XsltExecutable;
import nu.xom.XPathException;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
Expand All @@ -214,6 +216,7 @@
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.ResIterator;
Expand All @@ -230,7 +233,6 @@
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.apache.connector.ApacheConnectionClosingStrategy;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
Expand Down Expand Up @@ -1384,7 +1386,8 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
};
if (maxConnPerRoute != null) conman.setDefaultMaxPerRoute(maxConnPerRoute);
if (maxTotalConn != null) conman.setMaxTotal(maxTotalConn);

int maxRetryCount = 3;

ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.register(MultiPartFeature.class);
Expand All @@ -1396,6 +1399,23 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
config.property(ClientProperties.FOLLOW_REDIRECTS, true);
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED); // https://stackoverflow.com/questions/42139436/jersey-client-throws-cannot-retry-request-with-a-non-repeatable-request-entity
config.property(ApacheClientProperties.CONNECTION_MANAGER, conman);
config.property(ApacheClientProperties.RETRY_HANDLER, (HttpRequestRetryHandler) (IOException ex, int executionCount, HttpContext context) ->
{
// Extract the HTTP host from the context
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
String serverName = targetHost != null ? targetHost.getHostName() : "Unknown";

if (executionCount > maxRetryCount) {
if (log.isWarnEnabled()) log.warn("Maximum tries reached for client HTTP pool to server '{}'", serverName);
return false;
}
if (ex instanceof org.apache.http.NoHttpResponseException) {
if (log.isWarnEnabled()) log.warn("No response from server '{}' on {} call", serverName, executionCount);
return true;
}
return false;
});

//config.property(ApacheClientProperties.CONNECTION_CLOSING_STRATEGY, new ApacheConnectionClosingStrategy.GracefulClosingStrategy());
if (keepAliveStrategy != null) config.property(ApacheClientProperties.KEEPALIVE_STRATEGY, keepAliveStrategy);

Expand Down Expand Up @@ -1461,6 +1481,7 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
};
if (maxConnPerRoute != null) conman.setDefaultMaxPerRoute(maxConnPerRoute);
if (maxTotalConn != null) conman.setMaxTotal(maxTotalConn);
int maxRetryCount = 3;

ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
Expand All @@ -1473,7 +1494,23 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
config.property(ClientProperties.FOLLOW_REDIRECTS, true);
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED); // https://stackoverflow.com/questions/42139436/jersey-client-throws-cannot-retry-request-with-a-non-repeatable-request-entity
config.property(ApacheClientProperties.CONNECTION_MANAGER, conman);
config.property(ApacheClientProperties.RETRY_HANDLER, (HttpRequestRetryHandler) (IOException ex, int executionCount, HttpContext context) ->
{
// Extract the HTTP host from the context
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
String serverName = targetHost != null ? targetHost.getHostName() : "Unknown";

if (executionCount > maxRetryCount) {
if (log.isWarnEnabled()) log.warn("Maximum tries reached for client HTTP pool to server '{}'", serverName);
return false;
}
if (ex instanceof org.apache.http.NoHttpResponseException) {
if (log.isWarnEnabled()) log.warn("No response from server '{}' on {} call", serverName, executionCount);
return true;
}
return false;
});

return ClientBuilder.newBuilder().
withConfig(config).
sslContext(ctx).
Expand Down

0 comments on commit a4a7e8b

Please sign in to comment.