Skip to content

Commit a4a7e8b

Browse files
committed
Set mem_limit on linkeddatahub service
Set a custom `HttpRequestRetryHandler` lambda on the `ApacheConnector`
1 parent 78c5c55 commit a4a7e8b

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ services:
2828
linkeddatahub:
2929
user: root # otherwise the ldh user does not have permissions to the mounted folder which is owner by root
3030
build: .
31+
mem_limit: 2048m
3132
depends_on:
3233
- fuseki-admin
3334
- fuseki-end-user
@@ -117,7 +118,7 @@ services:
117118
- CLIENT_HOST=linkeddatahub
118119
- VARNISH_SIZE=1G
119120
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\" \"$$@\""
120-
command: [ "-t", "86400" ] # time to live
121+
command: [ "-t", "86400", "-p", "timeout_idle=60s" ] # time to live
121122
volumes:
122123
- ./platform/varnish-backend.vcl.template:/etc/varnish/default.vcl.template:ro
123124
varnish-end-user:
@@ -132,7 +133,7 @@ services:
132133
- CLIENT_HOST=linkeddatahub
133134
- VARNISH_SIZE=1G
134135
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\" \"$$@\""
135-
command: [ "-t", "86400" ] # time to live
136+
command: [ "-t", "86400", "-p", "timeout_idle=60s" ] # time to live
136137
volumes:
137138
- ./platform/varnish-backend.vcl.template:/etc/varnish/default.vcl.template:ro
138139
email-server:

src/main/java/com/atomgraph/linkeddatahub/Application.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@
204204
import net.sf.saxon.s9api.XsltExecutable;
205205
import nu.xom.XPathException;
206206
import org.apache.http.HttpClientConnection;
207+
import org.apache.http.HttpHost;
207208
import org.apache.http.HttpResponse;
209+
import org.apache.http.client.HttpRequestRetryHandler;
208210
import org.apache.http.config.Registry;
209211
import org.apache.http.config.RegistryBuilder;
210212
import org.apache.http.conn.ConnectionKeepAliveStrategy;
@@ -214,6 +216,7 @@
214216
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
215217
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
216218
import org.apache.http.protocol.HttpContext;
219+
import org.apache.http.protocol.HttpCoreContext;
217220
import org.apache.jena.query.DatasetFactory;
218221
import org.apache.jena.rdf.model.ModelFactory;
219222
import org.apache.jena.rdf.model.ResIterator;
@@ -230,7 +233,6 @@
230233
import org.glassfish.hk2.utilities.binding.AbstractBinder;
231234
import org.glassfish.jersey.client.ClientConfig;
232235
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
233-
import org.glassfish.jersey.apache.connector.ApacheConnectionClosingStrategy;
234236
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
235237
import org.glassfish.jersey.client.ClientProperties;
236238
import org.glassfish.jersey.client.RequestEntityProcessing;
@@ -1384,7 +1386,8 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
13841386
};
13851387
if (maxConnPerRoute != null) conman.setDefaultMaxPerRoute(maxConnPerRoute);
13861388
if (maxTotalConn != null) conman.setMaxTotal(maxTotalConn);
1387-
1389+
int maxRetryCount = 3;
1390+
13881391
ClientConfig config = new ClientConfig();
13891392
config.connectorProvider(new ApacheConnectorProvider());
13901393
config.register(MultiPartFeature.class);
@@ -1396,6 +1399,23 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
13961399
config.property(ClientProperties.FOLLOW_REDIRECTS, true);
13971400
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
13981401
config.property(ApacheClientProperties.CONNECTION_MANAGER, conman);
1402+
config.property(ApacheClientProperties.RETRY_HANDLER, (HttpRequestRetryHandler) (IOException ex, int executionCount, HttpContext context) ->
1403+
{
1404+
// Extract the HTTP host from the context
1405+
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
1406+
String serverName = targetHost != null ? targetHost.getHostName() : "Unknown";
1407+
1408+
if (executionCount > maxRetryCount) {
1409+
if (log.isWarnEnabled()) log.warn("Maximum tries reached for client HTTP pool to server '{}'", serverName);
1410+
return false;
1411+
}
1412+
if (ex instanceof org.apache.http.NoHttpResponseException) {
1413+
if (log.isWarnEnabled()) log.warn("No response from server '{}' on {} call", serverName, executionCount);
1414+
return true;
1415+
}
1416+
return false;
1417+
});
1418+
13991419
//config.property(ApacheClientProperties.CONNECTION_CLOSING_STRATEGY, new ApacheConnectionClosingStrategy.GracefulClosingStrategy());
14001420
if (keepAliveStrategy != null) config.property(ApacheClientProperties.KEEPALIVE_STRATEGY, keepAliveStrategy);
14011421

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

14651486
ClientConfig config = new ClientConfig();
14661487
config.connectorProvider(new ApacheConnectorProvider());
@@ -1473,7 +1494,23 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
14731494
config.property(ClientProperties.FOLLOW_REDIRECTS, true);
14741495
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
14751496
config.property(ApacheClientProperties.CONNECTION_MANAGER, conman);
1497+
config.property(ApacheClientProperties.RETRY_HANDLER, (HttpRequestRetryHandler) (IOException ex, int executionCount, HttpContext context) ->
1498+
{
1499+
// Extract the HTTP host from the context
1500+
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
1501+
String serverName = targetHost != null ? targetHost.getHostName() : "Unknown";
14761502

1503+
if (executionCount > maxRetryCount) {
1504+
if (log.isWarnEnabled()) log.warn("Maximum tries reached for client HTTP pool to server '{}'", serverName);
1505+
return false;
1506+
}
1507+
if (ex instanceof org.apache.http.NoHttpResponseException) {
1508+
if (log.isWarnEnabled()) log.warn("No response from server '{}' on {} call", serverName, executionCount);
1509+
return true;
1510+
}
1511+
return false;
1512+
});
1513+
14771514
return ClientBuilder.newBuilder().
14781515
withConfig(config).
14791516
sslContext(ctx).

0 commit comments

Comments
 (0)