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 8cfc5ae5b fix(web): keep mock alert rule ids unique within the same
millisecond (#7123)
8cfc5ae5b is described below
commit 8cfc5ae5b604b8259ab4ddad43d0296534c29318
Author: bob <[email protected]>
AuthorDate: Fri Nov 27 12:00:25 2026 +0800
fix(web): keep mock alert rule ids unique within the same millisecond
(#7123)
---
web/src/services/opsService.test.ts | 48 +++++++++++++++++++++++++++++++++++++
web/src/services/opsService.ts | 14 +++++++++--
2 files changed, 60 insertions(+), 2 deletions(-)
diff --git a/web/src/services/opsService.test.ts
b/web/src/services/opsService.test.ts
index 4eb78aeb4..116cf8ae0 100644
--- a/web/src/services/opsService.test.ts
+++ b/web/src/services/opsService.test.ts
@@ -258,6 +258,54 @@ describe('ops service mock data', () => {
);
});
+ it('assigns unique ids to rules created or imported in the same
millisecond', async () => {
+ const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1_750_000_000_000);
+ try {
+ const first = await createAlertRule({ metric: 'broker.tps', threshold:
100, name: 'same-tick-first', channels: ['email'] });
+ const second = await createAlertRule({ metric: 'broker.tps', threshold:
200, name: 'same-tick-second', channels: ['email'] });
+ const imported = await importAlertRulesTransfer(
+ {
+ version: 1,
+ domain: 'CLUSTER',
+ rules: [
+ {
+ name: 'same-tick-import-1',
+ metric: 'consumer.lag.total',
+ operator: '>',
+ threshold: 100,
+ duration: '5m',
+ channels: ['email'],
+ enabled: true,
+ description: 'same-tick import',
+ },
+ {
+ name: 'same-tick-import-2',
+ metric: 'consumer.lag.total',
+ operator: '>',
+ threshold: 200,
+ duration: '5m',
+ channels: ['email'],
+ enabled: true,
+ description: 'same-tick import',
+ },
+ ],
+ },
+ 'CLUSTER',
+ );
+ const afterImport = await createAlertRule({ metric: 'broker.tps',
threshold: 300, name: 'same-tick-third', channels: ['email'] });
+
+ const ids = [first.id, second.id, afterImport.id, ...imported.map((rule)
=> rule.id)];
+ expect(new Set(ids).size).toBe(ids.length);
+ expect(afterImport.id).toBeGreaterThan(Math.max(first.id, second.id));
+
+ for (const id of ids) await deleteAlertRule(id, 'CLUSTER');
+ const remaining = await listAlertRules('CLUSTER');
+ expect(remaining.map((rule) => rule.id)).not.toContain(first.id);
+ } finally {
+ nowSpy.mockRestore();
+ }
+ });
+
it('imports and exports alert rules within the selected domain only', async
() => {
const imported = await importAlertRulesTransfer(
{
diff --git a/web/src/services/opsService.ts b/web/src/services/opsService.ts
index f6bfa4666..e5d185dc3 100644
--- a/web/src/services/opsService.ts
+++ b/web/src/services/opsService.ts
@@ -63,6 +63,16 @@ const alertRulesState: Record<AlertRuleDomain, AlertRule[]>
= {
};
let alertSilencesState: AlertSilence[] = [];
+// Mock alert-rule ids must stay unique even when several rules are created or
+// imported within the same millisecond, so each id is one past the largest
+// id currently in use (never below the clock).
+function nextAlertRuleId(): number {
+ const existingIds = [...alertRulesState.CLUSTER,
...alertRulesState.BUSINESS].map(
+ (rule) => rule.id,
+ );
+ return Math.max(Date.now(), ...existingIds) + 1;
+}
+
function copyAlertRule(rule: AlertRule): AlertRule {
return {
...rule,
@@ -196,7 +206,7 @@ export async function importAlertRulesTransfer(
domain: AlertRuleDomain = 'CLUSTER',
): Promise<AlertRule[]> {
if (!isMockMode()) return opsApi.importAlertRulesTransfer(data, domain);
- const startId = Date.now();
+ const startId = nextAlertRuleId();
const imported = data.rules.map((rule, index) => ({
...copyAlertRule(rule as AlertRule),
id: startId + index,
@@ -308,7 +318,7 @@ export async function createAlertRule(
): Promise<AlertRule> {
if (isMockMode()) {
const rule: AlertRule = {
- id: Date.now(),
+ id: nextAlertRuleId(),
name: '',
metric: '',
operator: '>',