This is an automated email from the ASF dual-hosted git repository.
zhaoqingran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new cc90de5541 [fix] fixed cache parsing issues (#3977)
cc90de5541 is described below
commit cc90de5541376d2ade3ac53e99548056894ab16f
Author: Duansg <[email protected]>
AuthorDate: Fri Jan 16 15:47:09 2026 +0800
[fix] fixed cache parsing issues (#3977)
---
.../alert/service/impl/DataSourceServiceImpl.java | 13 ++++---------
.../hertzbeat/alert/service/DataSourceServiceTest.java | 15 ++++-----------
2 files changed, 8 insertions(+), 20 deletions(-)
diff --git
a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/DataSourceServiceImpl.java
b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/DataSourceServiceImpl.java
index 93fd115928..470ddeee86 100644
---
a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/DataSourceServiceImpl.java
+++
b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/DataSourceServiceImpl.java
@@ -72,13 +72,6 @@ public class DataSourceServiceImpl implements
DataSourceService {
.recordStats()
.build();
- @Getter
- private final Cache<String, CommonTokenStream> tokenStreamCache =
Caffeine.newBuilder()
- .maximumSize(512)
- .expireAfterWrite(30, TimeUnit.MINUTES)
- .recordStats()
- .build();
-
public DataSourceServiceImpl(@Autowired(required = false)
List<QueryExecutor> executors) {
this.executors = executors != null ? executors :
Collections.emptyList();
this.sqlSecurityValidator = new
SqlSecurityValidator(DEFAULT_ALLOWED_TABLES);
@@ -159,10 +152,12 @@ public class DataSourceServiceImpl implements
DataSourceService {
}
private List<Map<String, Object>> evaluate(String expr, QueryExecutor
executor) {
- CommonTokenStream tokens = tokenStreamCache.get(expr,
this::createTokenStream);
+ CommonTokenStream tokens = createTokenStream(expr);
AlertExpressionParser parser = new AlertExpressionParser(tokens);
ParseTree tree = expressionCache.get(expr, e -> parser.expr());
- if (null != tokens && tokens.LA(1) != Token.EOF) {
+ // Validate EOF only during the first parsing (when tokens.index > 0)
+ // Skip EOF check when cache hit, as it has already been validated on
the first pass.
+ if (tokens.index() > 0 && tokens.LA(1) != Token.EOF) {
throw new
AlertExpressionException(bundle.getString("alerter.calculate.parse.error"));
}
AlertExpressionEvalVisitor visitor = new
AlertExpressionEvalVisitor(executor, tokens);
diff --git
a/hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/service/DataSourceServiceTest.java
b/hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/service/DataSourceServiceTest.java
index 8fd9e4acc2..e9b098b1b0 100644
---
a/hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/service/DataSourceServiceTest.java
+++
b/hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/service/DataSourceServiceTest.java
@@ -18,7 +18,6 @@
package org.apache.hertzbeat.alert.service;
import com.github.benmanes.caffeine.cache.Cache;
-import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.hertzbeat.alert.service.impl.DataSourceServiceImpl;
import org.apache.hertzbeat.common.support.exception.AlertExpressionException;
@@ -557,13 +556,11 @@ class DataSourceServiceTest {
dataSourceService.setExecutors(List.of(mockExecutor));
String expr = "node_cpu_seconds_total > 50";
Cache<String, ParseTree> expressionCache =
dataSourceService.getExpressionCache();
- Cache<String, CommonTokenStream> tokenStreamCache =
dataSourceService.getTokenStreamCache();
expressionCache.invalidateAll();
- tokenStreamCache.invalidateAll();
- long beforeHits = tokenStreamCache.stats().hitCount();
+ long beforeHits = expressionCache.stats().hitCount();
dataSourceService.calculate("promql", expr);
dataSourceService.calculate("promql", expr);
- long actualHits = tokenStreamCache.stats().hitCount() - beforeHits;
+ long actualHits = expressionCache.stats().hitCount() - beforeHits;
assertEquals(1, actualHits, "expression cache should hit but miss");
}
@@ -578,9 +575,7 @@ class DataSourceServiceTest {
dataSourceService.setExecutors(List.of(mockExecutor));
dataSourceService.calculate("promql", "node_cpu_seconds_total > 50");
Cache<String, ParseTree> expressionCache =
dataSourceService.getExpressionCache();
- Cache<String, CommonTokenStream> tokenStreamCache =
dataSourceService.getTokenStreamCache();
expressionCache.invalidateAll();
- tokenStreamCache.invalidateAll();
String expr1 = "node_cpu_seconds_total > 30";
String expr2 = "node_cpu_seconds_total > 50";
long beforeHits = expressionCache.stats().hitCount();
@@ -601,15 +596,13 @@ class DataSourceServiceTest {
dataSourceService.setExecutors(List.of(mockExecutor));
dataSourceService.calculate("promql", "node_cpu_seconds_total > 50");
Cache<String, ParseTree> expressionCache =
dataSourceService.getExpressionCache();
- Cache<String, CommonTokenStream> tokenStreamCache =
dataSourceService.getTokenStreamCache();
expressionCache.invalidateAll();
- tokenStreamCache.invalidateAll();
String expr1 = "node_cpu_seconds_total > 30";
String expr2 = "node_cpu_seconds_total > 50";
- long beforeHits = tokenStreamCache.stats().hitCount();
+ long beforeHits = expressionCache.stats().hitCount();
dataSourceService.calculate("promql", expr1);
dataSourceService.calculate("promql", expr2);
- long actualHits = tokenStreamCache.stats().hitCount() - beforeHits;
+ long actualHits = expressionCache.stats().hitCount() - beforeHits;
assertEquals(0, actualHits, "expression cache should miss but hit");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]