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 ac0dfff69 fix(nameserver): apply registry replacements atomically
(#6884)
ac0dfff69 is described below
commit ac0dfff69fe1ba3fb5fc02d31444ef806df93b4a
Author: Xiao Yang <[email protected]>
AuthorDate: Fri Nov 27 11:51:08 2026 +0800
fix(nameserver): apply registry replacements atomically (#6884)
---
.../nameserver/NameserverRegistryService.java | 44 ++-----
.../NameserverRegistryPersistenceTest.java | 132 +++++++++++++++++++++
.../nameserver/NameserverRegistryServiceTest.java | 65 +++++-----
3 files changed, 178 insertions(+), 63 deletions(-)
diff --git
a/server/src/main/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryService.java
b/server/src/main/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryService.java
index 01546ba86..60d1c057e 100644
---
a/server/src/main/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryService.java
+++
b/server/src/main/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryService.java
@@ -89,13 +89,17 @@ public class NameserverRegistryService {
if (duplicates != null && duplicates > 0) {
throw duplicateName(name);
}
- entity.setName(name);
-
entity.setNamesrvAddr(NamesrvAddrParser.normalize(command.getNamesrvAddr()));
- entity.setK8sNamespace(command.getK8sNamespace());
- entity.setK8sId(command.getK8sId());
- entity.setDescription(command.getDescription());
+ // Replace all editable fields in one statement, including null
optional values.
+ // A second clearing UPDATE could overwrite a replacement committed in
between.
+ UpdateWrapper<RmqNameserver> replacement = new
UpdateWrapper<RmqNameserver>()
+ .eq("id", command.getId())
+ .set("name", name)
+ .set("namesrv_addr",
NamesrvAddrParser.normalize(command.getNamesrvAddr()))
+ .set("k8s_namespace", command.getK8sNamespace())
+ .set("k8s_id", command.getK8sId())
+ .set("description", command.getDescription());
try {
- int updated = nameserverMapper.updateById(entity);
+ int updated = nameserverMapper.update(null, replacement);
if (updated == 0) {
throw concurrentlyDeleted(command.getId());
}
@@ -103,7 +107,6 @@ public class NameserverRegistryService {
// The unique index is the final guard against concurrent rename
collisions.
throw duplicateName(name);
}
- clearOmittedOptionalColumns(entity);
RmqNameserver stored = nameserverMapper.selectById(entity.getId());
if (stored == null) {
// The row vanished between the update and the reload; do not
convert null to a VO.
@@ -175,31 +178,4 @@ public class NameserverRegistryService {
.gmtModified(entity.getGmtModified())
.build();
}
-
- /**
- * The registry update replaces every editable field of the entry, but
MyBatis-Plus
- * {@code updateById} omits null entity fields. An omitted k8s namespace,
k8s id or
- * description would therefore silently keep its previous value even
though the request
- * submitted no value for it; assign those columns explicitly so the
stored entry matches
- * what the request asked for.
- */
- private void clearOmittedOptionalColumns(RmqNameserver entity) {
- UpdateWrapper<RmqNameserver> cleared = new UpdateWrapper<>();
- boolean anyCleared = false;
- if (entity.getK8sNamespace() == null) {
- cleared.set("k8s_namespace", null);
- anyCleared = true;
- }
- if (entity.getK8sId() == null) {
- cleared.set("k8s_id", null);
- anyCleared = true;
- }
- if (entity.getDescription() == null) {
- cleared.set("description", null);
- anyCleared = true;
- }
- if (anyCleared) {
- nameserverMapper.update(null, cleared.eq("id", entity.getId()));
- }
- }
}
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryPersistenceTest.java
b/server/src/test/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryPersistenceTest.java
new file mode 100644
index 000000000..10ebcc98a
--- /dev/null
+++
b/server/src/test/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryPersistenceTest.java
@@ -0,0 +1,132 @@
+/*
+ * 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.cluster.nameserver;
+
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
+import org.apache.ibatis.mapping.Environment;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
+import org.apache.rocketmq.studio.persistence.entity.RmqNameserver;
+import org.apache.rocketmq.studio.persistence.mapper.RmqNameserverMapper;
+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.springframework.jdbc.core.JdbcTemplate;
+
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.AdditionalAnswers.delegatesTo;
+import static org.mockito.Mockito.mock;
+
+class NameserverRegistryPersistenceTest {
+
+ private SqlSession session;
+ private RmqNameserverMapper mapper;
+
+ @BeforeEach
+ void setUp() {
+ JdbcDataSource dataSource = new JdbcDataSource();
+ dataSource.setURL("jdbc:h2:mem:registry-" + UUID.randomUUID() +
";MODE=MySQL;DATABASE_TO_LOWER=TRUE");
+ MybatisConfiguration configuration = new MybatisConfiguration();
+ configuration.setEnvironment(new Environment("test", new
JdbcTransactionFactory(), dataSource));
+ configuration.setMapUnderscoreToCamelCase(true);
+ configuration.addMapper(RmqNameserverMapper.class);
+ session = new
MybatisSqlSessionFactoryBuilder().build(configuration).openSession(true);
+ mapper = session.getMapper(RmqNameserverMapper.class);
+ // Keep the database alive while JdbcTemplate prepares it on a
separate connection.
+ session.getConnection();
+ new JdbcTemplate(dataSource).execute("""
+ CREATE TABLE rmq_nameserver (
+ id BIGINT PRIMARY KEY, name VARCHAR(128) UNIQUE NOT NULL,
+ namesrv_addr VARCHAR(512) NOT NULL, k8s_namespace
VARCHAR(128),
+ k8s_id VARCHAR(128), description TEXT, status VARCHAR(32),
+ gmt_create TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ gmt_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP)
+ """);
+ RmqNameserver initial = new RmqNameserver();
+ initial.setId(1L);
+ initial.setName("original");
+ initial.setNamesrvAddr("original:9876");
+ initial.setK8sNamespace("original-namespace");
+ initial.setK8sId("original-k8s");
+ initial.setDescription("original description");
+ initial.setStatus("healthy");
+ mapper.insert(initial);
+ }
+
+ @AfterEach
+ void tearDown() {
+ if (session != null) {
+ session.close();
+ }
+ }
+
+ @Test
+ void updateShouldNotClearFieldsFromACompetingReplacementTest() {
+ NameserverRegistryService competing = new
NameserverRegistryService(mapper);
+ AtomicInteger writes = new AtomicInteger();
+ RmqNameserverMapper interleaved = mock(RmqNameserverMapper.class,
invocation -> {
+ Object result = delegatesTo(mapper).answer(invocation);
+ String method = invocation.getMethod().getName();
+ if ((method.equals("updateById") || method.equals("update"))
+ && writes.incrementAndGet() == 1) {
+ // Finish another request after the first SQL write commits,
before the
+ // original request can issue its optional-column clear.
+ competing.update(UpdateNameserverRegistryDTO.builder()
+ .id(1L).name("competing").namesrvAddr("competing:9876")
+
.k8sNamespace("competing-namespace").k8sId("competing-k8s")
+ .description("competing description").build());
+ }
+ return result;
+ });
+
+ new
NameserverRegistryService(interleaved).update(UpdateNameserverRegistryDTO.builder()
+ .id(1L).name("first").namesrvAddr("first:9876").build());
+
+ session.clearCache();
+ RmqNameserver stored = mapper.selectById(1L);
+ assertThat(stored.getName()).isEqualTo("competing");
+ assertThat(stored.getNamesrvAddr()).isEqualTo("competing:9876");
+ assertThat(stored.getK8sNamespace()).isEqualTo("competing-namespace");
+ assertThat(stored.getK8sId()).isEqualTo("competing-k8s");
+ assertThat(stored.getDescription()).isEqualTo("competing description");
+ assertThat(writes).hasValue(1);
+ }
+
+ @Test
+ void updateShouldPersistMixedSetAndClearedFieldsTest() {
+ NameserverRegistryVO updated = new
NameserverRegistryService(mapper).update(
+ UpdateNameserverRegistryDTO.builder().id(1L).name(" renamed
")
+ .namesrvAddr(" NS1:9876 ; NS2:9876 ")
+ .k8sId("replacement-k8s").build());
+
+ session.clearCache();
+ RmqNameserver stored = mapper.selectById(1L);
+ assertThat(stored.getName()).isEqualTo("renamed");
+ assertThat(stored.getNamesrvAddr()).isEqualTo("ns1:9876,ns2:9876");
+ assertThat(stored.getK8sNamespace()).isNull();
+ assertThat(stored.getK8sId()).isEqualTo("replacement-k8s");
+ assertThat(stored.getDescription()).isNull();
+ assertThat(stored.getStatus()).isEqualTo("healthy");
+ assertThat(updated.getK8sId()).isEqualTo(stored.getK8sId());
+ assertThat(updated.getDescription()).isNull();
+ }
+}
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryServiceTest.java
b/server/src/test/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryServiceTest.java
index c0020903e..0ca7d9559 100644
---
a/server/src/test/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryServiceTest.java
+++
b/server/src/test/java/org/apache/rocketmq/studio/cluster/nameserver/NameserverRegistryServiceTest.java
@@ -264,9 +264,10 @@ class NameserverRegistryServiceTest {
RmqNameserver existing = new RmqNameserver();
existing.setId(1L);
existing.setName("rocketmq1");
+ existing.setNamesrvAddr(expected);
when(nameserverMapper.selectById(1L)).thenReturn(existing).thenReturn(existing);
when(nameserverMapper.selectCount(any())).thenReturn(0L);
-
when(nameserverMapper.updateById(any(RmqNameserver.class))).thenReturn(1);
+ when(nameserverMapper.update(isNull(),
any(UpdateWrapper.class))).thenReturn(1);
NameserverRegistryVO updated =
service.update(UpdateNameserverRegistryDTO.builder()
.id(1L)
@@ -276,8 +277,9 @@ class NameserverRegistryServiceTest {
.build());
assertThat(updated.getNamesrvAddr()).isEqualTo(expected);
- verify(nameserverMapper).updateById(existing);
- assertThat(existing.getNamesrvAddr()).isEqualTo(expected);
+ UpdateWrapper<?> replacement = captureReplacement();
+ assertThat(replacement.getSqlSet()).contains("namesrv_addr");
+
assertThat(replacement.getParamNameValuePairs()).containsValue(expected);
}
@Test
@@ -291,7 +293,7 @@ class NameserverRegistryServiceTest {
.build()))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("not found");
- verify(nameserverMapper, never()).updateById(any(RmqNameserver.class));
+ verify(nameserverMapper, never()).update(isNull(),
any(UpdateWrapper.class));
}
@Test
@@ -308,7 +310,7 @@ class NameserverRegistryServiceTest {
.build()))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("already exists");
- verify(nameserverMapper, never()).updateById(any(RmqNameserver.class));
+ verify(nameserverMapper, never()).update(isNull(),
any(UpdateWrapper.class));
}
@Test
@@ -324,7 +326,7 @@ class NameserverRegistryServiceTest {
.build()))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("out of range");
- verify(nameserverMapper, never()).updateById(any(RmqNameserver.class));
+ verify(nameserverMapper, never()).update(isNull(),
any(UpdateWrapper.class));
}
@Test
@@ -334,7 +336,7 @@ class NameserverRegistryServiceTest {
existing.setName("rocketmq1");
when(nameserverMapper.selectById(1L)).thenReturn(existing).thenReturn(existing);
when(nameserverMapper.selectCount(any())).thenReturn(0L);
-
when(nameserverMapper.updateById(any(RmqNameserver.class))).thenReturn(1);
+ when(nameserverMapper.update(isNull(),
any(UpdateWrapper.class))).thenReturn(1);
service.update(UpdateNameserverRegistryDTO.builder()
.id(1L)
@@ -342,7 +344,9 @@ class NameserverRegistryServiceTest {
.namesrvAddr("rocketmq1-nameserver.svc:9876")
.build());
- assertThat(existing.getName()).isEqualTo("rocketmq1");
+ UpdateWrapper<?> replacement = captureReplacement();
+ assertThat(replacement.getSqlSet()).contains("name");
+
assertThat(replacement.getParamNameValuePairs()).containsValue("rocketmq1");
}
@Test
@@ -351,7 +355,7 @@ class NameserverRegistryServiceTest {
existing.setId(1L);
when(nameserverMapper.selectById(1L)).thenReturn(existing);
when(nameserverMapper.selectCount(any())).thenReturn(0L);
- when(nameserverMapper.updateById(any(RmqNameserver.class)))
+ when(nameserverMapper.update(isNull(), any(UpdateWrapper.class)))
.thenThrow(new DuplicateKeyException("uk_nameserver_name"));
assertThatThrownBy(() ->
service.update(UpdateNameserverRegistryDTO.builder()
@@ -369,7 +373,7 @@ class NameserverRegistryServiceTest {
existing.setId(1L);
when(nameserverMapper.selectById(1L)).thenReturn(existing);
when(nameserverMapper.selectCount(any())).thenReturn(0L);
-
when(nameserverMapper.updateById(any(RmqNameserver.class))).thenReturn(0);
+ when(nameserverMapper.update(isNull(),
any(UpdateWrapper.class))).thenReturn(0);
assertThatThrownBy(() ->
service.update(UpdateNameserverRegistryDTO.builder()
.id(1L)
@@ -393,26 +397,19 @@ class NameserverRegistryServiceTest {
existing.setDescription("community chart cluster");
when(nameserverMapper.selectById(1L)).thenReturn(existing).thenReturn(existing);
when(nameserverMapper.selectCount(any())).thenReturn(0L);
-
when(nameserverMapper.updateById(any(RmqNameserver.class))).thenReturn(1);
when(nameserverMapper.update(isNull(),
any(UpdateWrapper.class))).thenReturn(1);
- NameserverRegistryVO updated =
service.update(UpdateNameserverRegistryDTO.builder()
+ service.update(UpdateNameserverRegistryDTO.builder()
.id(1L)
.name("rocketmq1")
.namesrvAddr("rocketmq1-nameserver.svc:9876")
.build());
- @SuppressWarnings("rawtypes")
- ArgumentCaptor<UpdateWrapper> captor =
ArgumentCaptor.forClass(UpdateWrapper.class);
- verify(nameserverMapper).update(isNull(), captor.capture());
- assertThat(captor.getValue().getSqlSet())
- .contains("k8s_namespace")
- .contains("k8s_id")
- .contains("description");
-
assertThat(captor.getValue().getParamNameValuePairs()).containsValue(null);
- assertThat(existing.getK8sNamespace()).isNull();
- assertThat(existing.getK8sId()).isNull();
- assertThat(updated.getDescription()).isNull();
+ UpdateWrapper<?> replacement = captureReplacement();
+ assertThat(replacement.getSqlSet())
+ .contains("name", "namesrv_addr", "k8s_namespace", "k8s_id",
"description");
+
assertThat(replacement.getParamNameValuePairs().values()).containsExactlyInAnyOrder(
+ 1L, "rocketmq1", "rocketmq1-nameserver.svc:9876", null, null,
null);
}
@Test
@@ -424,7 +421,7 @@ class NameserverRegistryServiceTest {
existing.setDescription("old description");
when(nameserverMapper.selectById(1L)).thenReturn(existing).thenReturn(existing);
when(nameserverMapper.selectCount(any())).thenReturn(0L);
-
when(nameserverMapper.updateById(any(RmqNameserver.class))).thenReturn(1);
+ when(nameserverMapper.update(isNull(),
any(UpdateWrapper.class))).thenReturn(1);
service.update(UpdateNameserverRegistryDTO.builder()
.id(1L)
@@ -435,10 +432,10 @@ class NameserverRegistryServiceTest {
.description("new description")
.build());
- verify(nameserverMapper, never()).update(any(), any());
- assertThat(existing.getK8sNamespace()).isEqualTo("new-namespace");
- assertThat(existing.getK8sId()).isEqualTo("new-k8s");
- assertThat(existing.getDescription()).isEqualTo("new description");
+ UpdateWrapper<?> replacement = captureReplacement();
+ assertThat(replacement.getSqlSet()).contains("k8s_namespace",
"k8s_id", "description");
+ assertThat(replacement.getParamNameValuePairs())
+
.containsValue("new-namespace").containsValue("new-k8s").containsValue("new
description");
}
@Test
@@ -447,7 +444,7 @@ class NameserverRegistryServiceTest {
existing.setId(1L);
when(nameserverMapper.selectById(1L)).thenReturn(existing).thenReturn(null);
when(nameserverMapper.selectCount(any())).thenReturn(0L);
-
when(nameserverMapper.updateById(any(RmqNameserver.class))).thenReturn(1);
+ when(nameserverMapper.update(isNull(),
any(UpdateWrapper.class))).thenReturn(1);
assertThatThrownBy(() ->
service.update(UpdateNameserverRegistryDTO.builder()
.id(1L)
@@ -491,4 +488,14 @@ class NameserverRegistryServiceTest {
.hasMessageContaining("not found");
verify(nameserverMapper, never()).deleteById(anyLong());
}
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private UpdateWrapper<?> captureReplacement() {
+ ArgumentCaptor<UpdateWrapper> captor =
ArgumentCaptor.forClass(UpdateWrapper.class);
+ verify(nameserverMapper).update(isNull(), captor.capture());
+ verify(nameserverMapper, never()).updateById(any(RmqNameserver.class));
+ assertThat(captor.getValue().getSqlSegment()).contains("id");
+
assertThat(captor.getValue().getParamNameValuePairs()).containsValue(1L);
+ return captor.getValue();
+ }
}