Skip to content

Commit 4de4ad5

Browse files
harismsophokles73
authored andcommitted
[#3659] Add configurable cache refresh divisor to adjust cached item refresh time duration
1 parent c110669 commit 4de4ad5

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterApplication.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ protected ResourceLimitChecks prometheusResourceLimitChecks(
646646

647647
final var webClient = WebClient.create(vertx, webClientOptions);
648648
final var cacheTimeout = Duration.ofSeconds(config.getCacheTimeout());
649+
final var cacheRefreshDivisor = config.getCacheRefreshDivisor();
649650
final Caffeine<Object, Object> builder = Caffeine.newBuilder()
650651
// make sure we run one Prometheus query at a time
651652
.executor(Executors.newSingleThreadExecutor(r -> {
@@ -656,7 +657,7 @@ protected ResourceLimitChecks prometheusResourceLimitChecks(
656657
.initialCapacity(config.getCacheMinSize())
657658
.maximumSize(config.getCacheMaxSize())
658659
.expireAfterWrite(cacheTimeout)
659-
.refreshAfterWrite(cacheTimeout.dividedBy(2));
660+
.refreshAfterWrite(cacheTimeout.dividedBy(cacheRefreshDivisor));
660661

661662
return new PrometheusBasedResourceLimitChecks(
662663
builder.buildAsync(new ConnectedDevicesAsyncCacheLoader(webClient, config, tracer)),

adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitCheckOptions.java

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public interface PrometheusBasedResourceLimitCheckOptions {
6464
@WithDefault("60")
6565
long cacheTimeout();
6666

67+
/**
68+
* Gets the divisor value for calculating cached data refresh time duration from cache timeout duration.
69+
*
70+
* @return The divisor value for calculating refresh time duration.
71+
*/
72+
@WithDefault("2")
73+
int cacheRefreshDivisor();
74+
6775
/**
6876
* Gets the period of time after which a request to a Prometheus server are closed.
6977
*

adapter-base/src/main/java/org/eclipse/hono/adapter/resourcelimits/PrometheusBasedResourceLimitChecksConfig.java

+32
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public class PrometheusBasedResourceLimitChecksConfig extends AuthenticatingClie
3232
* The default timeout for cached data in seconds until they are considered invalid.
3333
*/
3434
public static final long DEFAULT_CACHE_TIMEOUT = 60L;
35+
/**
36+
* The default divisor value for cached data refresh time duration calculation.
37+
*/
38+
public static final int DEFAULT_CACHE_REFRESH_DIVISOR = 2;
3539
/**
3640
* The default number of milliseconds that the client waits for a connection to
3741
* the Prometheus server.
@@ -46,6 +50,7 @@ public class PrometheusBasedResourceLimitChecksConfig extends AuthenticatingClie
4650
private int cacheMinSize = DEFAULT_CACHE_MIN_SIZE;
4751
private long cacheMaxSize = DEFAULT_CACHE_MAX_SIZE;
4852
private long cacheTimeout = DEFAULT_CACHE_TIMEOUT;
53+
private int cacheRefreshDivisor = DEFAULT_CACHE_REFRESH_DIVISOR;
4954
private long queryTimeout = DEFAULT_QUERY_TIMEOUT;
5055
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
5156

@@ -73,6 +78,7 @@ public PrometheusBasedResourceLimitChecksConfig(final PrometheusBasedResourceLim
7378
this.cacheMaxSize = options.cacheMaxSize();
7479
this.cacheMinSize = options.cacheMinSize();
7580
this.cacheTimeout = options.cacheTimeout();
81+
this.cacheRefreshDivisor = options.cacheRefreshDivisor();
7682
this.connectTimeout = options.connectTimeout();
7783
this.queryTimeout = options.queryTimeout();
7884
}
@@ -167,6 +173,32 @@ public void setCacheTimeout(final long timeout) {
167173
this.cacheTimeout = timeout;
168174
}
169175

176+
/**
177+
* Gets the divisor for cache refresh time duration calculation which is cache timeout duration divided by this divisor value.
178+
* <p>
179+
* The default value of this property is {@value #DEFAULT_CACHE_REFRESH_DIVISOR}.
180+
*
181+
* @return The divisor value for cache refresh time duration calculation.
182+
*/
183+
public int getCacheRefreshDivisor() {
184+
return cacheRefreshDivisor;
185+
}
186+
187+
/**
188+
* Sets the divisor value for cache refresh time duration calculation after which cached responses are updated in the background.
189+
* <p>
190+
* The default value of this property is {@value #DEFAULT_CACHE_REFRESH_DIVISOR}.
191+
*
192+
* @param divisor The refresh time divisor value.
193+
* @throws IllegalArgumentException if divisor is &lt;= 0.
194+
*/
195+
public void setCacheRefreshDivisor(final int divisor) {
196+
if (divisor <= 0) {
197+
throw new IllegalArgumentException("cache refresh divisor must be greater than zero");
198+
}
199+
this.cacheRefreshDivisor = divisor;
200+
}
201+
170202
/**
171203
* Gets the period of time after which a request to a Prometheus server are closed.
172204
* <p>

site/documentation/content/admin-guide/common-config.md

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ configuring the checker.
165165
| `HONO_RESOURCELIMITS_PROMETHEUSBASED_CACHEMINSIZE`<br>`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. |
166166
| `HONO_RESOURCELIMITS_PROMETHEUSBASED_CACHEMAXSIZE`<br>`hono.resourceLimits.prometheusBased.cacheMaxSize` | no | `1000` | The maximum size of the cache to store the metrics data retrieved from the Prometheus server. |
167167
| `HONO_RESOURCELIMITS_PROMETHEUSBASED_CACHETIMEOUT`<br>`hono.resourceLimits.prometheusBased.cacheTimeout` | no | `60` | The number of seconds after which the cached metrics data should be considered invalid. |
168+
| `HONO_RESOURCELIMITS_PROMETHEUSBASED_CACHEREFRESHDIVISOR`<br>`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. |
168169
| `HONO_RESOURCELIMITS_PROMETHEUSBASED_CONNECTTIMEOUT`<br>`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.|
169170
| `HONO_RESOURCELIMITS_PROMETHEUSBASED_QUERYTIMEOUT`<br>`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.|
170171

0 commit comments

Comments
 (0)