This is an automated email from the ASF dual-hosted git repository.
lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new f000581df perf(alert): bound reconciliation metadata results (#7395)
f000581df is described below
commit f000581dfe5b87effae3414caae4ccd796de5e2f
Author: zmuxuny <[email protected]>
AuthorDate: Fri Nov 27 12:29:20 2026 +0800
perf(alert): bound reconciliation metadata results (#7395)
---
.../ops/alert/MybatisPlusAlertStateRepository.java | 16 +-
.../persistence/mapper/RmqSystemAlertMapper.java | 27 +++
.../ops/alert/AlertStateMetadataQueryTest.java | 189 +++++++++++++++++++++
.../alert/MybatisPlusAlertStateRepositoryTest.java | 13 +-
4 files changed, 224 insertions(+), 21 deletions(-)
diff --git
a/server/src/main/java/org/apache/rocketmq/studio/ops/alert/MybatisPlusAlertStateRepository.java
b/server/src/main/java/org/apache/rocketmq/studio/ops/alert/MybatisPlusAlertStateRepository.java
index 73a93ec2a..94feab807 100644
---
a/server/src/main/java/org/apache/rocketmq/studio/ops/alert/MybatisPlusAlertStateRepository.java
+++
b/server/src/main/java/org/apache/rocketmq/studio/ops/alert/MybatisPlusAlertStateRepository.java
@@ -35,7 +35,6 @@ import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Optional;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -157,21 +156,10 @@ public class MybatisPlusAlertStateRepository implements
AlertStateRepository {
if (activeStates.isEmpty()) {
return Map.of();
}
- Set<Long> ruleIds =
activeStates.stream().map(RmqAlertState::getRuleId).collect(Collectors.toSet());
- Set<String> fingerprints =
activeStates.stream().map(RmqAlertState::getFingerprint).collect(Collectors.toSet());
- Set<AlertStateKey> activeKeys = activeStates.stream()
- .map(state -> new AlertStateKey(state.getRuleId(),
state.getFingerprint()))
- .collect(Collectors.toCollection(HashSet::new));
- return alertMapper.selectList(new QueryWrapper<RmqSystemAlert>()
- .in("rule_id", ruleIds)
- .in("fingerprint", fingerprints)
- .eq("domain", scope.domain().name())
- .eq("instance_id", scope.instanceId())
- .orderByDesc("time", "id"))
+ return alertMapper.selectLatestMetadata(scope.domain().name(),
scope.instanceId(), activeStates)
.stream()
- .filter(alert -> activeKeys.contains(new
AlertStateKey(alert.getRuleId(), alert.getFingerprint())))
.collect(Collectors.toMap(alert -> new
AlertStateKey(alert.getRuleId(), alert.getFingerprint()),
- alert -> alert, (latest, ignored) -> latest));
+ alert -> alert));
}
private Optional<ActiveAlertState> toActiveState(RmqAlertState state,
RmqSystemAlert alert) {
diff --git
a/server/src/main/java/org/apache/rocketmq/studio/persistence/mapper/RmqSystemAlertMapper.java
b/server/src/main/java/org/apache/rocketmq/studio/persistence/mapper/RmqSystemAlertMapper.java
index b6d64340a..76a2af88c 100644
---
a/server/src/main/java/org/apache/rocketmq/studio/persistence/mapper/RmqSystemAlertMapper.java
+++
b/server/src/main/java/org/apache/rocketmq/studio/persistence/mapper/RmqSystemAlertMapper.java
@@ -17,7 +17,34 @@
package org.apache.rocketmq.studio.persistence.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.rocketmq.studio.persistence.entity.RmqAlertState;
import org.apache.rocketmq.studio.persistence.entity.RmqSystemAlert;
+import java.util.List;
+
public interface RmqSystemAlertMapper extends BaseMapper<RmqSystemAlert> {
+ /** Return only the latest metadata per active key, without materializing
old event payloads. */
+ @Select("""
+ <script>
+ SELECT alert.rule_id, alert.fingerprint, alert.instance_id,
alert.labels_json
+ FROM rmq_system_alert alert
+ INNER JOIN (
+ SELECT id, ROW_NUMBER() OVER (
+ PARTITION BY rule_id, fingerprint ORDER BY time DESC, id
DESC
+ ) AS latest_rank
+ FROM rmq_system_alert
+ WHERE domain = #{domain} AND instance_id = #{instanceId}
+ AND (
+ <foreach collection="states" item="state" separator=" OR ">
+ (rule_id = #{state.ruleId} AND fingerprint =
#{state.fingerprint})
+ </foreach>
+ )
+ ) ranked ON alert.id = ranked.id
+ WHERE ranked.latest_rank = 1
+ </script>
+ """)
+ List<RmqSystemAlert> selectLatestMetadata(@Param("domain") String domain,
+ @Param("instanceId") String instanceId, @Param("states")
List<RmqAlertState> states);
}
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/ops/alert/AlertStateMetadataQueryTest.java
b/server/src/test/java/org/apache/rocketmq/studio/ops/alert/AlertStateMetadataQueryTest.java
new file mode 100644
index 000000000..c642af18e
--- /dev/null
+++
b/server/src/test/java/org/apache/rocketmq/studio/ops/alert/AlertStateMetadataQueryTest.java
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.studio.ops.alert;
+
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.Environment;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.Interceptor;
+import org.apache.ibatis.plugin.Intercepts;
+import org.apache.ibatis.plugin.Invocation;
+import org.apache.ibatis.plugin.Signature;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
+import org.apache.rocketmq.studio.cluster.metrics.MetricCollectionScope;
+import org.apache.rocketmq.studio.persistence.mapper.RmqAlertStateMapper;
+import org.apache.rocketmq.studio.persistence.mapper.RmqSystemAlertMapper;
+import org.h2.jdbcx.JdbcDataSource;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
+
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class AlertStateMetadataQueryTest {
+ private static final LocalDateTime NOW = LocalDateTime.of(2026, 10, 19,
12, 0);
+ private static final MetricCollectionScope SCOPE = new
MetricCollectionScope(AlertDomain.BUSINESS,
+ "local", Set.of("consumer.lag.total"));
+ private JdbcTemplate jdbc;
+ private SqlSession session;
+ private MybatisPlusAlertStateRepository repository;
+ private MetadataRows metadataRows;
+ private long eventId;
+
+ @BeforeEach
+ void setUp() {
+ JdbcDataSource dataSource = new JdbcDataSource();
+ dataSource.setURL("jdbc:h2:mem:alert-metadata-" + UUID.randomUUID()
+ + ";MODE=MySQL;DB_CLOSE_DELAY=-1;DATABASE_TO_LOWER=TRUE");
+ new ResourceDatabasePopulator(new
ClassPathResource("db/schema.sql")).execute(dataSource);
+ jdbc = new JdbcTemplate(dataSource);
+ MybatisConfiguration configuration = new MybatisConfiguration();
+ configuration.setMapUnderscoreToCamelCase(true);
+ configuration.setEnvironment(new Environment("metadata-test", new
JdbcTransactionFactory(), dataSource));
+ metadataRows = new MetadataRows();
+ configuration.addInterceptor(metadataRows);
+ configuration.addMapper(RmqAlertStateMapper.class);
+ configuration.addMapper(RmqSystemAlertMapper.class);
+ session = new
MybatisSqlSessionFactoryBuilder().build(configuration).openSession(true);
+ repository = new
MybatisPlusAlertStateRepository(session.getMapper(RmqAlertStateMapper.class),
+ session.getMapper(RmqSystemAlertMapper.class));
+ }
+
+ @AfterEach
+ void tearDown() {
+ if (session != null) session.close();
+ if (jdbc != null) jdbc.execute("SHUTDOWN");
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {1000, 2000})
+ void boundsMappedRowsByActiveKeysRatherThanRetainedHistoryTest(int
historyPerKey) {
+ state(4, "orders", "FIRING");
+ state(5, "payments", "ACKED");
+ for (int i = 0; i < historyPerKey; i++) {
+ event(4, "orders", "BUSINESS", "local",
NOW.minusSeconds(historyPerKey - i), "old-orders");
+ event(5, "payments", "BUSINESS", "local",
NOW.minusSeconds(historyPerKey - i), "old-payments");
+ }
+ event(4, "orders", "BUSINESS", "local", NOW, "latest-orders");
+ event(5, "payments", "BUSINESS", "local", NOW, "latest-payments");
+
+ List<ActiveAlertState> active = repository.findActive(SCOPE,
List.of(rule(4), rule(5)));
+
+ assertThat(active).extracting(item -> item.labels().get("resource"))
+ .containsExactlyInAnyOrder("latest-orders", "latest-payments");
+ // Count the actual mapper result before the repository's
first-per-key reduction.
+ assertThat(metadataRows.queries).isEqualTo(1);
+ assertThat(metadataRows.rows).isEqualTo(2);
+ }
+
+ @Test
+ void keepsScopedLatestMetadataAndMissingEventSemanticsTest() {
+ state(4, "a", "FIRING");
+ state(4, "b", "ACKED");
+ state(5, "c", "FIRING");
+ state(6, "d", "ACKED");
+ state(7, "missing", "FIRING");
+ state(8, "resolved", "RESOLVED");
+ event(4, "a", "BUSINESS", "local", NOW, "same-time-first");
+ event(4, "a", "BUSINESS", "local", NOW, "same-time-last");
+ event(4, "a", "BUSINESS", "local", NOW.minusDays(1),
"larger-id-older-time");
+ event(4, "a", "BUSINESS", "local", null, "larger-id-null-time");
+ event(4, "b", "BUSINESS", "local", NOW, "other-fingerprint");
+ event(5, "c", "BUSINESS", "local", null, "null-first");
+ event(5, "c", "BUSINESS", "local", null, "null-last");
+ event(6, "d", "BUSINESS", "local", NOW, "other-rule");
+ event(5, "a", "BUSINESS", "local", NOW.plusYears(1),
"inactive-cross-pair");
+ event(4, "a", "CLUSTER", "local", NOW.plusYears(1), "other-domain");
+ event(4, "a", "BUSINESS", "elsewhere", NOW.plusYears(1),
"other-instance");
+ event(8, "resolved", "BUSINESS", "local", NOW, "not-active");
+
+ Map<AlertStateKey, ActiveAlertState> active =
repository.findActive(SCOPE,
+ List.of(rule(4), rule(5), rule(6), rule(7), rule(8))).stream()
+ .collect(Collectors.toMap(ActiveAlertState::key, item ->
item));
+
+ assertThat(active).hasSize(4);
+ assertThat(active.get(new AlertStateKey(4L,
"a")).labels()).containsEntry("resource", "same-time-last");
+ assertThat(active.get(new AlertStateKey(4L,
"b")).labels()).containsEntry("resource", "other-fingerprint");
+ assertThat(active.get(new AlertStateKey(5L,
"c")).labels()).containsEntry("resource", "null-last");
+ assertThat(active.get(new AlertStateKey(6L,
"d")).labels()).containsEntry("resource", "other-rule");
+ assertThat(active.values()).allSatisfy(item ->
assertThat(item.instanceId()).isEqualTo("local"));
+ assertThat(active.get(new AlertStateKey(4L,
"b")).state().status()).isEqualTo(AlertStateStatus.ACKED);
+ }
+
+ @Test
+ void skipsMetadataQueriesWithoutActiveScopedStatesTest() {
+ assertThat(repository.findActive(null, List.of(rule(4)))).isEmpty();
+ assertThat(repository.findActive(SCOPE, List.of())).isEmpty();
+ assertThat(repository.findActive(SCOPE, List.of(rule(4)))).isEmpty();
+ state(4, "resolved", "RESOLVED");
+ session.clearCache();
+ assertThat(repository.findActive(SCOPE, List.of(rule(4)))).isEmpty();
+ assertThat(metadataRows.queries).isZero();
+ }
+
+ private void state(long ruleId, String fingerprint, String status) {
+ jdbc.update("INSERT INTO rmq_alert_state
(rule_id,fingerprint,status,consecutive_hits,version) VALUES (?,?,?,?,?)",
+ ruleId, fingerprint, status, 1, 0);
+ }
+
+ private void event(long ruleId, String fingerprint, String domain, String
instance,
+ LocalDateTime time, String resource) {
+ jdbc.update("INSERT INTO rmq_system_alert
(id,rule_id,fingerprint,domain,instance_id,time,description,labels_json) "
+ + "VALUES (?,?,?,?,?,?,?,?)", ++eventId, ruleId,
fingerprint, domain, instance, time,
+ "historical-description-".repeat(50), "{\"resource\":\"" +
resource + "\"}");
+ }
+
+ private static AlertRuleVO rule(long id) {
+ return
AlertRuleVO.builder().id(id).domain(AlertDomain.BUSINESS).enabled(true)
+ .instanceId("local").metric("consumer.lag.total").build();
+ }
+
+ @Intercepts(@Signature(type = Executor.class, method = "query",
+ args = {MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class}))
+ private static class MetadataRows implements Interceptor {
+ private int queries;
+ private int rows;
+
+ @Override
+ public Object intercept(Invocation invocation) throws Throwable {
+ Object result = invocation.proceed();
+ MappedStatement statement = (MappedStatement)
invocation.getArgs()[0];
+ if
(statement.getId().startsWith(RmqSystemAlertMapper.class.getName() + ".")) {
+ queries++;
+ rows += ((List<?>) result).size();
+ }
+ return result;
+ }
+ }
+}
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/ops/alert/MybatisPlusAlertStateRepositoryTest.java
b/server/src/test/java/org/apache/rocketmq/studio/ops/alert/MybatisPlusAlertStateRepositoryTest.java
index 93238ebef..c316f4552 100644
---
a/server/src/test/java/org/apache/rocketmq/studio/ops/alert/MybatisPlusAlertStateRepositoryTest.java
+++
b/server/src/test/java/org/apache/rocketmq/studio/ops/alert/MybatisPlusAlertStateRepositoryTest.java
@@ -88,7 +88,7 @@ class MybatisPlusAlertStateRepositoryTest {
alert.setInstanceId("local");
alert.setLabelsJson("{\"consumerGroup\":\"orders\"}");
RmqSystemAlertMapper alertMapper = mock(RmqSystemAlertMapper.class);
- when(alertMapper.selectList(any())).thenReturn(List.of(alert));
+ when(alertMapper.selectLatestMetadata(any(), any(),
any())).thenReturn(List.of(alert));
MybatisPlusAlertStateRepository repository = new
MybatisPlusAlertStateRepository(mapper, alertMapper);
AlertRuleVO rule =
AlertRuleVO.builder().id(4L).domain(AlertDomain.BUSINESS).enabled(true)
.instanceId("local").metric("consumer.lag.total").build();
@@ -102,7 +102,7 @@ class MybatisPlusAlertStateRepositoryTest {
assertThat(item.instanceId()).isEqualTo("local");
assertThat(item.labels()).isEqualTo(Map.of("consumerGroup",
"orders"));
});
- verify(alertMapper).selectList(any());
+ verify(alertMapper).selectLatestMetadata(eq("BUSINESS"), eq("local"),
any());
verify(alertMapper, never()).selectOne(any());
}
@@ -121,7 +121,7 @@ class MybatisPlusAlertStateRepositoryTest {
state.setCurrentValue(30D);
when(mapper.selectList(any())).thenReturn(List.of(state));
RmqSystemAlertMapper alertMapper = mock(RmqSystemAlertMapper.class);
- when(alertMapper.selectList(any())).thenReturn(List.of(
+ when(alertMapper.selectLatestMetadata(any(), any(),
any())).thenReturn(List.of(
alert(4L, "fingerprint", "local",
"{\"consumerGroup\":\"orders\"}")));
MybatisPlusAlertStateRepository repository = new
MybatisPlusAlertStateRepository(mapper, alertMapper);
AlertRuleVO rule =
AlertRuleVO.builder().id(4L).domain(AlertDomain.BUSINESS).enabled(true)
@@ -151,7 +151,7 @@ class MybatisPlusAlertStateRepositoryTest {
state.setCurrentValue(10D);
when(mapper.selectList(any())).thenReturn(List.of(state));
RmqSystemAlertMapper alertMapper = mock(RmqSystemAlertMapper.class);
- when(alertMapper.selectList(any())).thenReturn(List.of(
+ when(alertMapper.selectLatestMetadata(any(), any(),
any())).thenReturn(List.of(
alert(6L, "fingerprint", "local",
"{\"consumerGroup\":\"orders\"}")));
MybatisPlusAlertStateRepository repository = new
MybatisPlusAlertStateRepository(mapper, alertMapper);
AlertRuleVO rule =
AlertRuleVO.builder().id(6L).domain(AlertDomain.BUSINESS).enabled(true)
@@ -174,8 +174,7 @@ class MybatisPlusAlertStateRepositoryTest {
RmqSystemAlertMapper alertMapper = mock(RmqSystemAlertMapper.class);
RmqSystemAlert firstAlert = alert(4L, "fingerprint-a", "local",
"{\"consumerGroup\":\"orders\"}");
RmqSystemAlert secondAlert = alert(5L, "fingerprint-b", "local",
"{\"consumerGroup\":\"payments\"}");
- RmqSystemAlert staleFirstAlert = alert(4L, "fingerprint-a", "local",
"{\"consumerGroup\":\"old\"}");
- when(alertMapper.selectList(any())).thenReturn(List.of(firstAlert,
secondAlert, staleFirstAlert));
+ when(alertMapper.selectLatestMetadata(any(), any(),
any())).thenReturn(List.of(firstAlert, secondAlert));
MybatisPlusAlertStateRepository repository = new
MybatisPlusAlertStateRepository(mapper, alertMapper);
AlertRuleVO firstRule =
AlertRuleVO.builder().id(4L).domain(AlertDomain.BUSINESS).enabled(true)
.instanceId("local").metric("consumer.lag.total").build();
@@ -188,7 +187,7 @@ class MybatisPlusAlertStateRepositoryTest {
assertThat(active).hasSize(2);
assertThat(active).extracting(item ->
item.labels().get("consumerGroup"))
.containsExactly("orders", "payments");
- verify(alertMapper, times(1)).selectList(any());
+ verify(alertMapper, times(1)).selectLatestMetadata(eq("BUSINESS"),
eq("local"), any());
verify(alertMapper, never()).selectOne(any());
}