From 4c597c4f8e3c121b383903b4d0038518bf1eaabc Mon Sep 17 00:00:00 2001 From: Sky Ao Date: Fri, 18 Mar 2022 11:08:05 +0800 Subject: [PATCH 1/4] set maxRequestsPerHost of okjava to support slow response requests and high TPS Signed-off-by: Sky Ao --- .../java/io/dapr/client/DaprHttpBuilder.java | 23 ++++++++++++ .../main/java/io/dapr/config/Properties.java | 37 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java b/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java index 5f71b0f7d4..91f9796f0e 100644 --- a/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java +++ b/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java @@ -14,9 +14,12 @@ package io.dapr.client; import io.dapr.config.Properties; +import okhttp3.ConnectionPool; +import okhttp3.Dispatcher; import okhttp3.OkHttpClient; import java.time.Duration; +import java.util.concurrent.TimeUnit; /** * A builder for the DaprHttp. @@ -33,6 +36,13 @@ public class DaprHttpBuilder { */ private static final Object LOCK = new Object(); + /** + * HTTP keep alive duration in seconds. + * + *

Just hard code to a reasonable value. + */ + private static final int KEEP_ALIVE_DURATION = 30; + /** * Build an instance of the Http client based on the provided setup. * @@ -55,6 +65,19 @@ private DaprHttp buildDaprHttp() { OkHttpClient.Builder builder = new OkHttpClient.Builder(); Duration readTimeout = Duration.ofSeconds(Properties.HTTP_CLIENT_READ_TIMEOUT_SECONDS.get()); builder.readTimeout(readTimeout); + + Dispatcher dispatcher = new Dispatcher(); + dispatcher.setMaxRequests(Properties.HTTP_CLIENT_MAX_REQUESTS.get()); + // The maximum number of requests for each host to execute concurrently. + // Default value is 5 in okjava which is totally UNACCEPTABLE! + // For sidecar case, set it the same as maxRequests. + dispatcher.setMaxRequestsPerHost(Properties.HTTP_CLIENT_MAX_REQUESTS.get()); + builder.dispatcher(dispatcher); + + ConnectionPool pool = new ConnectionPool(Properties.HTTP_CLIENT_MAX_IDLE_CONNECTIONS.get(), + KEEP_ALIVE_DURATION, TimeUnit.SECONDS); + builder.connectionPool(pool); + OK_HTTP_CLIENT = builder.build(); } } diff --git a/sdk/src/main/java/io/dapr/config/Properties.java b/sdk/src/main/java/io/dapr/config/Properties.java index 53b8720ec7..ad5e9ba956 100644 --- a/sdk/src/main/java/io/dapr/config/Properties.java +++ b/sdk/src/main/java/io/dapr/config/Properties.java @@ -56,7 +56,24 @@ public class Properties { /** * Dapr's default timeout in seconds for HTTP client reads. */ - private static final Integer DEFAULT_HTTP_CLIENT_READTIMEOUTSECONDS = 60; + private static final Integer DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECONDS = 60; + + /** + * The default maximum number of requests to execute concurrently. + * + *

Above this requests queue in memory, waiting for the running calls to complete. + * Default is 64 in okjava which is OK for most case, but for some special case + * which is slow response and high concurrency, the value should set to a little big. + */ + private static final Integer DEFAULT_HTTP_CLIENT_MAX_REQUESTS = 1024; + + /** + * The default maximum number of idle connections of HTTP connection pool. + * + *

Attention! This is max IDLE connection, NOT max connection! + * It is also very important for high concurrency cases. + */ + private static final Integer DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS = 128; /** * IP for Dapr's sidecar. @@ -123,5 +140,21 @@ public class Properties { public static final Property HTTP_CLIENT_READ_TIMEOUT_SECONDS = new IntegerProperty( "dapr.http.client.readTimeoutSeconds", "DAPR_HTTP_CLIENT_READ_TIMEOUT_SECONDS", - DEFAULT_HTTP_CLIENT_READTIMEOUTSECONDS); + DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECONDS); + + /** + * Dapr's timeout in seconds for HTTP client reads. + */ + public static final Property HTTP_CLIENT_MAX_REQUESTS = new IntegerProperty( + "dapr.http.client.maxRequests", + "DAPR_HTTP_CLIENT_MAX_REQUESTS", + DEFAULT_HTTP_CLIENT_MAX_REQUESTS); + + /** + * The default maximum number of idle connections of HTTP connection pool. + */ + public static final Property HTTP_CLIENT_MAX_IDLE_CONNECTIONS = new IntegerProperty( + "dapr.http.client.maxIdleConnections", + "DAPR_HTTP_CLIENT_MAX_IDLE_CONNECTIONS", + DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS); } From 26a1c705a7164a6bf4c685cfc265b20a42dd2523 Mon Sep 17 00:00:00 2001 From: Sky Ao Date: Fri, 18 Mar 2022 14:56:48 +0800 Subject: [PATCH 2/4] fix typo in comments Signed-off-by: Sky Ao --- sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java b/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java index 91f9796f0e..94b8f0cda3 100644 --- a/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java +++ b/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java @@ -69,7 +69,7 @@ private DaprHttp buildDaprHttp() { Dispatcher dispatcher = new Dispatcher(); dispatcher.setMaxRequests(Properties.HTTP_CLIENT_MAX_REQUESTS.get()); // The maximum number of requests for each host to execute concurrently. - // Default value is 5 in okjava which is totally UNACCEPTABLE! + // Default value is 5 in okhttp which is totally UNACCEPTABLE! // For sidecar case, set it the same as maxRequests. dispatcher.setMaxRequestsPerHost(Properties.HTTP_CLIENT_MAX_REQUESTS.get()); builder.dispatcher(dispatcher); From abc0bff9b4d77b830fab4edf9b57ebf67efb32a5 Mon Sep 17 00:00:00 2001 From: Sky Ao Date: Sat, 19 Mar 2022 09:42:41 +0800 Subject: [PATCH 3/4] update comments Signed-off-by: Sky Ao --- sdk/src/main/java/io/dapr/config/Properties.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/main/java/io/dapr/config/Properties.java b/sdk/src/main/java/io/dapr/config/Properties.java index ad5e9ba956..134938b080 100644 --- a/sdk/src/main/java/io/dapr/config/Properties.java +++ b/sdk/src/main/java/io/dapr/config/Properties.java @@ -59,7 +59,7 @@ public class Properties { private static final Integer DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECONDS = 60; /** - * The default maximum number of requests to execute concurrently. + * Dapr's default maximum number of requests for HTTP client to execute concurrently. * *

Above this requests queue in memory, waiting for the running calls to complete. * Default is 64 in okjava which is OK for most case, but for some special case From b13a2fc82844bdfc8dfe504e8cd7a919413e9865 Mon Sep 17 00:00:00 2001 From: Sky Ao Date: Sun, 20 Mar 2022 19:45:24 +0800 Subject: [PATCH 4/4] update javadoc Signed-off-by: Sky Ao --- sdk/src/main/java/io/dapr/config/Properties.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/src/main/java/io/dapr/config/Properties.java b/sdk/src/main/java/io/dapr/config/Properties.java index 134938b080..fcd09d9b65 100644 --- a/sdk/src/main/java/io/dapr/config/Properties.java +++ b/sdk/src/main/java/io/dapr/config/Properties.java @@ -62,13 +62,13 @@ public class Properties { * Dapr's default maximum number of requests for HTTP client to execute concurrently. * *

Above this requests queue in memory, waiting for the running calls to complete. - * Default is 64 in okjava which is OK for most case, but for some special case + * Default is 64 in okhttp which is OK for most case, but for some special case * which is slow response and high concurrency, the value should set to a little big. */ private static final Integer DEFAULT_HTTP_CLIENT_MAX_REQUESTS = 1024; /** - * The default maximum number of idle connections of HTTP connection pool. + * Dapr's default maximum number of idle connections of HTTP connection pool. * *

Attention! This is max IDLE connection, NOT max connection! * It is also very important for high concurrency cases. @@ -143,7 +143,7 @@ public class Properties { DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECONDS); /** - * Dapr's timeout in seconds for HTTP client reads. + * Dapr's default maximum number of requests for HTTP client to execute concurrently. */ public static final Property HTTP_CLIENT_MAX_REQUESTS = new IntegerProperty( "dapr.http.client.maxRequests", @@ -151,7 +151,7 @@ public class Properties { DEFAULT_HTTP_CLIENT_MAX_REQUESTS); /** - * The default maximum number of idle connections of HTTP connection pool. + * Dapr's default maximum number of idle connections for HTTP connection pool. */ public static final Property HTTP_CLIENT_MAX_IDLE_CONNECTIONS = new IntegerProperty( "dapr.http.client.maxIdleConnections",