Skip to content

Commit

Permalink
[eclipse-hono#3659] Add configurable cache refresh divisor to adjust …
Browse files Browse the repository at this point in the history
…cached item refresh time duration
  • Loading branch information
harism committed Oct 25, 2024
1 parent c110669 commit 5e7ff49
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, Object> builder = Caffeine.newBuilder()
// make sure we run one Prometheus query at a time
.executor(Executors.newSingleThreadExecutor(r -> {
Expand All @@ -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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
* <p>
* 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.
* <p>
* The default value of this property is {@value #DEFAULT_CACHE_REFRESH_DIVISOR}.
*
* @param divisor The refresh time divisor value.
* @throws IllegalArgumentException if divisor is &lt;= 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.
* <p>
Expand Down
1 change: 1 addition & 0 deletions site/documentation/content/admin-guide/common-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ configuring the checker.
| `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. |
| `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. |
| `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. |
| `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. |
| `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.|
| `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.|

Expand Down

0 comments on commit 5e7ff49

Please sign in to comment.