diff --git a/adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterApplication.java b/adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterApplication.java index 11292e1a26..aeafa8ffd0 100644 --- a/adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterApplication.java +++ b/adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterApplication.java @@ -646,6 +646,7 @@ protected ResourceLimitChecks prometheusResourceLimitChecks( final var webClient = WebClient.create(vertx, webClientOptions); final var cacheTimeout = Duration.ofSeconds(config.getCacheTimeout()); + final var cacheRefreshDivisor = config.getCacheRefreshDivisor(); final Caffeine builder = Caffeine.newBuilder() // make sure we run one Prometheus query at a time .executor(Executors.newSingleThreadExecutor(r -> { @@ -656,7 +657,7 @@ protected ResourceLimitChecks prometheusResourceLimitChecks( .initialCapacity(config.getCacheMinSize()) .maximumSize(config.getCacheMaxSize()) .expireAfterWrite(cacheTimeout) - .refreshAfterWrite(cacheTimeout.dividedBy(2)); + .refreshAfterWrite(cacheTimeout.dividedBy(cacheRefreshDivisor)); return new PrometheusBasedResourceLimitChecks( builder.buildAsync(new ConnectedDevicesAsyncCacheLoader(webClient, config, tracer)), diff --git a/adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitCheckOptions.java b/adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitCheckOptions.java index 572b0de7ba..a8045f4ec7 100644 --- a/adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitCheckOptions.java +++ b/adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitCheckOptions.java @@ -64,6 +64,14 @@ public interface PrometheusBasedResourceLimitCheckOptions { @WithDefault("60") long cacheTimeout(); + /** + * Gets the divisor value for calculating cached data refresh time duration from cache timeout duration. + * + * @return The divisor value for calculating refresh time duration. + */ + @WithDefault("2") + int cacheRefreshDivisor(); + /** * Gets the period of time after which a request to a Prometheus server are closed. * diff --git a/adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitChecksConfig.java b/adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitChecksConfig.java index 307ba72369..59d0e75e9f 100644 --- a/adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitChecksConfig.java +++ b/adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitChecksConfig.java @@ -32,6 +32,10 @@ public class PrometheusBasedResourceLimitChecksConfig extends AuthenticatingClie * The default timeout for cached data in seconds until they are considered invalid. */ public static final long DEFAULT_CACHE_TIMEOUT = 60L; + /** + * The default divisor value for cached data refresh time duration calculation. + */ + public static final int DEFAULT_CACHE_REFRESH_DIVISOR = 2; /** * The default number of milliseconds that the client waits for a connection to * the Prometheus server. @@ -46,6 +50,7 @@ public class PrometheusBasedResourceLimitChecksConfig extends AuthenticatingClie private int cacheMinSize = DEFAULT_CACHE_MIN_SIZE; private long cacheMaxSize = DEFAULT_CACHE_MAX_SIZE; private long cacheTimeout = DEFAULT_CACHE_TIMEOUT; + private int cacheRefreshDivisor = DEFAULT_CACHE_REFRESH_DIVISOR; private long queryTimeout = DEFAULT_QUERY_TIMEOUT; private int connectTimeout = DEFAULT_CONNECT_TIMEOUT; @@ -73,6 +78,7 @@ public PrometheusBasedResourceLimitChecksConfig(final PrometheusBasedResourceLim this.cacheMaxSize = options.cacheMaxSize(); this.cacheMinSize = options.cacheMinSize(); this.cacheTimeout = options.cacheTimeout(); + this.cacheRefreshDivisor = options.cacheRefreshDivisor(); this.connectTimeout = options.connectTimeout(); this.queryTimeout = options.queryTimeout(); } @@ -167,6 +173,32 @@ public void setCacheTimeout(final long timeout) { this.cacheTimeout = timeout; } + /** + * Gets the divisor for cache refresh time duration calculation which is cache timeout duration divided by this divisor value. + *

+ * The default value of this property is {@value #DEFAULT_CACHE_REFRESH_DIVISOR}. + * + * @return The divisor value for cache refresh time duration calculation. + */ + public int getCacheRefreshDivisor() { + return cacheRefreshDivisor; + } + + /** + * Sets the divisor value for cache refresh time duration calculation after which cached responses are updated in the background. + *

+ * The default value of this property is {@value #DEFAULT_CACHE_REFRESH_DIVISOR}. + * + * @param divisor The refresh time divisor value. + * @throws IllegalArgumentException if divisor is <= 0. + */ + public void setCacheRefreshDivisor(final int divisor) { + if (divisor <= 0) { + throw new IllegalArgumentException("cache refresh divisor must be greater than zero"); + } + this.cacheRefreshDivisor = divisor; + } + /** * Gets the period of time after which a request to a Prometheus server are closed. *

diff --git a/site/documentation/content/admin-guide/common-config.md b/site/documentation/content/admin-guide/common-config.md index d16004a360..6fb6c06b0d 100644 --- a/site/documentation/content/admin-guide/common-config.md +++ b/site/documentation/content/admin-guide/common-config.md @@ -165,6 +165,7 @@ configuring the checker. | `HONO_RESOURCELIMITS_PROMETHEUSBASED_CACHEMINSIZE`
`hono.resourceLimits.prometheusBased.cacheMinSize` | no | `20` | The minimum size of the cache to store the metrics data retrieved from the Prometheus server. The cache is used for storing the current amount of data exchanged with devices of tenants. | | `HONO_RESOURCELIMITS_PROMETHEUSBASED_CACHEMAXSIZE`
`hono.resourceLimits.prometheusBased.cacheMaxSize` | no | `1000` | The maximum size of the cache to store the metrics data retrieved from the Prometheus server. | | `HONO_RESOURCELIMITS_PROMETHEUSBASED_CACHETIMEOUT`
`hono.resourceLimits.prometheusBased.cacheTimeout` | no | `60` | The number of seconds after which the cached metrics data should be considered invalid. | +| `HONO_RESOURCELIMITS_PROMETHEUSBASED_CACHEREFRESHDIVISOR`
`hono.resourceLimits.prometheusBased.cacheRefreshDivisor` | no | `2` | The divisor used to calculate the refresh time duration after which cached metrics data refreshing in the background is allowed. The duration is calculated as the cache timeout duration divided by this value. | | `HONO_RESOURCELIMITS_PROMETHEUSBASED_CONNECTTIMEOUT`
`hono.resourceLimits.prometheusBased.connectTimeout` | no | `1000` | The maximum number of milliseconds that the adapter waits for a TCP connection to a Prometheus server to be established.| | `HONO_RESOURCELIMITS_PROMETHEUSBASED_QUERYTIMEOUT`
`hono.resourceLimits.prometheusBased.queryTimeout` | no | `500` | The number of milliseconds after which a request to a Prometheus server is closed. Setting zero or a negative value disables the timeout.|