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

Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new bd71994b5d fix(admin): update scale rule cache after commit (#7107)
bd71994b5d is described below

commit bd71994b5d32db3eddf4146c6dfa19b6105987eb
Author: Liming Deng <[email protected]>
AuthorDate: Wed Sep 23 11:19:43 2026 +0800

    fix(admin): update scale rule cache after commit (#7107)
    
    Co-authored-by: aias00 <[email protected]>
---
 .../admin/service/impl/ScaleRuleServiceImpl.java   | 29 ++++++--
 .../shenyu/admin/service/ScaleRuleServiceTest.java | 85 ++++++++++++++++++++++
 2 files changed, 108 insertions(+), 6 deletions(-)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ScaleRuleServiceImpl.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ScaleRuleServiceImpl.java
index af678e3c6b..03655c82c1 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ScaleRuleServiceImpl.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ScaleRuleServiceImpl.java
@@ -30,6 +30,8 @@ import org.apache.shenyu.admin.service.ScaleRuleService;
 import org.apache.shenyu.common.utils.ListUtil;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.TransactionSynchronization;
+import 
org.springframework.transaction.support.TransactionSynchronizationManager;
 
 import java.util.List;
 import java.util.Objects;
@@ -110,7 +112,7 @@ public class ScaleRuleServiceImpl implements 
ScaleRuleService {
         final ScaleRuleDO scaleRuleDO = 
ScaleRuleDO.buildScaleRuleDO(scaleRuleDTO);
         int rows = scaleRuleMapper.insertSelective(scaleRuleDO);
         if (rows > 0) {
-            scaleRuleCache.addOrUpdateRuleToCache(scaleRuleDO);
+            runAfterCommit(() -> 
scaleRuleCache.addOrUpdateRuleToCache(scaleRuleDO));
         }
         return rows;
     }
@@ -127,11 +129,13 @@ public class ScaleRuleServiceImpl implements 
ScaleRuleService {
         final ScaleRuleDO after = ScaleRuleDO.buildScaleRuleDO(scaleRuleDTO);
         int rows = scaleRuleMapper.updateByPrimaryKeySelective(after);
         if (rows > 0) {
-            if (Objects.nonNull(before) && 
!Objects.equals(before.getMetricName(), after.getMetricName())) {
-                
scaleRuleCache.removeRulesFromCache(List.of(before.getMetricName()));
-            }
             final ScaleRuleDO persisted = 
scaleRuleMapper.selectByPrimaryKey(scaleRuleDTO.getId());
-            scaleRuleCache.addOrUpdateRuleToCache(persisted);
+            runAfterCommit(() -> {
+                if (Objects.nonNull(before) && 
!Objects.equals(before.getMetricName(), after.getMetricName())) {
+                    
scaleRuleCache.removeRulesFromCache(List.of(before.getMetricName()));
+                }
+                scaleRuleCache.addOrUpdateRuleToCache(persisted);
+            });
         }
         return rows;
     }
@@ -146,8 +150,21 @@ public class ScaleRuleServiceImpl implements 
ScaleRuleService {
     public int delete(final List<String> ids) {
         int rows = scaleRuleMapper.delete(ids);
         if (rows > 0) {
-            scaleRuleCache.removeRulesFromCache(ids);
+            runAfterCommit(() -> scaleRuleCache.removeRulesFromCache(ids));
         }
         return rows;
     }
+
+    private void runAfterCommit(final Runnable action) {
+        if (!TransactionSynchronizationManager.isSynchronizationActive()) {
+            action.run();
+            return;
+        }
+        TransactionSynchronizationManager.registerSynchronization(new 
TransactionSynchronization() {
+            @Override
+            public void afterCommit() {
+                action.run();
+            }
+        });
+    }
 }
diff --git 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ScaleRuleServiceTest.java
 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ScaleRuleServiceTest.java
new file mode 100644
index 0000000000..56c499d63e
--- /dev/null
+++ 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ScaleRuleServiceTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.shenyu.admin.service;
+
+import org.apache.shenyu.admin.mapper.ScaleRuleMapper;
+import org.apache.shenyu.admin.model.dto.ScaleRuleDTO;
+import org.apache.shenyu.admin.model.entity.ScaleRuleDO;
+import org.apache.shenyu.admin.scale.monitor.subject.cache.ScaleRuleCache;
+import org.apache.shenyu.admin.service.impl.ScaleRuleServiceImpl;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import 
org.springframework.transaction.support.TransactionSynchronizationManager;
+import org.springframework.transaction.support.TransactionSynchronizationUtils;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test cases for ScaleRuleService.
+ */
+@ExtendWith(MockitoExtension.class)
+public final class ScaleRuleServiceTest {
+
+    @Mock
+    private ScaleRuleMapper scaleRuleMapper;
+
+    @Mock
+    private ScaleRuleCache scaleRuleCache;
+
+    private ScaleRuleService scaleRuleService;
+
+    @BeforeEach
+    public void setUp() {
+        scaleRuleService = new ScaleRuleServiceImpl(scaleRuleMapper, 
scaleRuleCache);
+    }
+
+    @AfterEach
+    public void tearDown() {
+        if (TransactionSynchronizationManager.isSynchronizationActive()) {
+            TransactionSynchronizationManager.clearSynchronization();
+        }
+    }
+
+    @Test
+    public void testCreateUpdatesCacheAfterCommit() {
+        
when(scaleRuleMapper.insertSelective(any(ScaleRuleDO.class))).thenReturn(1);
+        TransactionSynchronizationManager.initSynchronization();
+
+        scaleRuleService.create(new ScaleRuleDTO());
+
+        verify(scaleRuleCache, 
never()).addOrUpdateRuleToCache(any(ScaleRuleDO.class));
+        
TransactionSynchronizationUtils.invokeAfterCommit(TransactionSynchronizationManager.getSynchronizations());
+        verify(scaleRuleCache).addOrUpdateRuleToCache(any(ScaleRuleDO.class));
+    }
+
+    @Test
+    public void testCreateUpdatesCacheImmediatelyWithoutTransaction() {
+        
when(scaleRuleMapper.insertSelective(any(ScaleRuleDO.class))).thenReturn(1);
+
+        scaleRuleService.create(new ScaleRuleDTO());
+
+        verify(scaleRuleCache).addOrUpdateRuleToCache(any(ScaleRuleDO.class));
+    }
+}

Reply via email to