This is an automated email from the ASF dual-hosted git repository.

Duansg 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 4a4e582036 maintenance: bound alert persistence locks (#4285)
4a4e582036 is described below

commit 4a4e58203601dbd421e681e182186b586633eb04
Author: Logic <[email protected]>
AuthorDate: Mon Aug 3 12:12:36 2026 +0800

    maintenance: bound alert persistence locks (#4285)
    
    Co-authored-by: Duansg <[email protected]>
---
 .../alert/notice/impl/DbAlertStoreHandlerImpl.java   | 20 ++++++++++++++++++--
 .../notice/impl/DbAlertStoreHandlerImplTest.java     | 17 +++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git 
a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImpl.java
 
b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImpl.java
index 24fd7d1141..7430fde223 100644
--- 
a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImpl.java
+++ 
b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImpl.java
@@ -42,6 +42,10 @@ import org.springframework.stereotype.Component;
 @Slf4j
 final class DbAlertStoreHandlerImpl implements AlertStoreHandler {
 
+    static final int LOCK_STRIPE_COUNT = 256;
+
+    private static final Object[] KEY_LOCKS = createKeyLocks();
+
     private final GroupAlertDao groupAlertDao;
     
     private final SingleAlertDao singleAlertDao;
@@ -58,7 +62,7 @@ final class DbAlertStoreHandlerImpl implements 
AlertStoreHandler {
         List<SingleAlert> newAlerts = new ArrayList<>();
 
         for (SingleAlert singleAlert : originalAlerts) {
-            synchronized (singleAlert.getFingerprint().intern()) {
+            synchronized (lockFor(singleAlert.getFingerprint())) {
                 SingleAlert existAlert = 
singleAlertDao.findByFingerprint(singleAlert.getFingerprint());
                 if (existAlert != null) {
                     // Update the existing alert with the ID and creation time 
from the database
@@ -90,7 +94,7 @@ final class DbAlertStoreHandlerImpl implements 
AlertStoreHandler {
         }
         groupAlert.setAlerts(newAlerts);
         // Find existing alert group
-        synchronized (groupAlert.getGroupKey().intern()) {
+        synchronized (lockFor(groupAlert.getGroupKey())) {
             GroupAlert existGroupAlert = 
groupAlertDao.findByGroupKey(groupAlert.getGroupKey());
             // Process resolved alerts
             if (existGroupAlert != null) {
@@ -132,4 +136,16 @@ final class DbAlertStoreHandlerImpl implements 
AlertStoreHandler {
             return savedGroupAlert;
         }
     }
+
+    static Object lockFor(String key) {
+        return KEY_LOCKS[Math.floorMod(key.hashCode(), LOCK_STRIPE_COUNT)];
+    }
+
+    private static Object[] createKeyLocks() {
+        Object[] locks = new Object[LOCK_STRIPE_COUNT];
+        for (int index = 0; index < locks.length; index++) {
+            locks[index] = new Object();
+        }
+        return locks;
+    }
 }
diff --git 
a/hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImplTest.java
 
b/hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImplTest.java
index 560d6501fd..2afa824e25 100644
--- 
a/hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImplTest.java
+++ 
b/hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImplTest.java
@@ -18,6 +18,8 @@
 package org.apache.hertzbeat.alert.notice.impl;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -33,7 +35,9 @@ import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 /**
  * Test case for {@link DbAlertStoreHandlerImpl}
@@ -122,4 +126,17 @@ class DbAlertStoreHandlerImplTest {
         assertEquals(1L, groupAlert.getId());
     }
 
+    @Test
+    void usesBoundedLocksForExternalKeys() {
+        int stripeCount = DbAlertStoreHandlerImpl.LOCK_STRIPE_COUNT;
+        Object firstLock = DbAlertStoreHandlerImpl.lockFor("same-key");
+        assertSame(firstLock, DbAlertStoreHandlerImpl.lockFor("same-key"));
+
+        Set<Object> locks = new HashSet<>();
+        for (int index = 0; index < stripeCount * 4; index++) {
+            locks.add(DbAlertStoreHandlerImpl.lockFor("external-key-" + 
index));
+        }
+        assertTrue(locks.size() <= stripeCount);
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to