This is an automated email from the ASF dual-hosted git repository.
menghaoranss pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 88595f465ec Add metadata persistence call order tests (#39696)
88595f465ec is described below
commit 88595f465ecc6d8a46b7df1a3c4d02bc7920a1f4
Author: Haoran Meng <[email protected]>
AuthorDate: Wed Sep 2 18:24:36 2026 +0800
Add metadata persistence call order tests (#39696)
---
.../type/AlterDatabaseRuleOperatorTest.java | 131 +++++++++++++++++++++
.../type/DropDatabaseRuleOperatorTest.java | 12 +-
.../ClusterMetaDataManagerPersistServiceTest.java | 48 ++++++--
3 files changed, 179 insertions(+), 12 deletions(-)
diff --git
a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/AlterDatabaseRuleOperatorTest.java
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/AlterDatabaseRuleOperatorTest.java
new file mode 100644
index 00000000000..c62ec0e879b
--- /dev/null
+++
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/AlterDatabaseRuleOperatorTest.java
@@ -0,0 +1,131 @@
+/*
+ * 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.shardingsphere.distsql.handler.engine.update.rdl.rule.engine.database.type;
+
+import
org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.type.DatabaseRuleAlterExecutor;
+import
org.apache.shardingsphere.distsql.statement.type.rdl.rule.database.DatabaseRuleDefinitionStatement;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.config.rule.checker.DatabaseRuleConfigurationEmptyChecker;
+import
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.mode.persist.PersistServiceFacade;
+import org.apache.shardingsphere.mode.persist.mode.ModePersistServiceFacade;
+import
org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@SuppressWarnings({"rawtypes", "unchecked"})
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(TypedSPILoader.class)
+class AlterDatabaseRuleOperatorTest {
+
+ @Mock
+ private ContextManager contextManager;
+
+ @Mock
+ private PersistServiceFacade persistServiceFacade;
+
+ @Mock
+ private ModePersistServiceFacade modePersistServiceFacade;
+
+ @Mock
+ private MetaDataManagerPersistService metaDataManagerPersistService;
+
+ @Mock
+ private ShardingSphereDatabase database;
+
+ @Mock
+ private DatabaseRuleAlterExecutor executor;
+
+ private AlterDatabaseRuleOperator ruleOperator;
+
+ @BeforeEach
+ void setUp() {
+
when(contextManager.getPersistServiceFacade()).thenReturn(persistServiceFacade);
+
when(persistServiceFacade.getModeFacade()).thenReturn(modePersistServiceFacade);
+
when(modePersistServiceFacade.getMetaDataManagerService()).thenReturn(metaDataManagerPersistService);
+ ruleOperator = new AlterDatabaseRuleOperator(contextManager, executor);
+ }
+
+ @Test
+ void assertOperate() {
+ DatabaseRuleDefinitionStatement sqlStatement =
mock(DatabaseRuleDefinitionStatement.class);
+ TestDatabaseRuleConfiguration toBeAlteredRuleConfig = new
TestDatabaseRuleConfiguration();
+ RuleConfiguration decoratedRuleConfig = mock(RuleConfiguration.class);
+ TestDatabaseRuleConfiguration toBeDroppedRuleConfig = new
TestDatabaseRuleConfiguration();
+ DatabaseRuleConfigurationEmptyChecker emptyChecker =
mock(DatabaseRuleConfigurationEmptyChecker.class);
+
when(executor.buildToBeAlteredRuleConfiguration(sqlStatement)).thenReturn(toBeAlteredRuleConfig);
+
when(database.decorateRuleConfiguration(toBeAlteredRuleConfig)).thenReturn(decoratedRuleConfig);
+
when(executor.buildToBeDroppedRuleConfiguration(toBeAlteredRuleConfig)).thenReturn(toBeDroppedRuleConfig);
+ when((TypedSPI)
TypedSPILoader.getService(DatabaseRuleConfigurationEmptyChecker.class,
TestDatabaseRuleConfiguration.class)).thenReturn(emptyChecker);
+ when(emptyChecker.isEmpty(toBeDroppedRuleConfig)).thenReturn(false);
+ RuleConfiguration currentRuleConfig = mock(RuleConfiguration.class);
+ ruleOperator.operate(sqlStatement, database, currentRuleConfig);
+ InOrder actual = inOrder(metaDataManagerPersistService);
+
actual.verify(metaDataManagerPersistService).alterRuleConfiguration(database,
decoratedRuleConfig);
+
actual.verify(metaDataManagerPersistService).removeRuleConfigurationItem(database,
toBeDroppedRuleConfig);
+ }
+
+ @Test
+ void assertOperateWhenNoRuleConfigurationToRemove() {
+ DatabaseRuleDefinitionStatement sqlStatement =
mock(DatabaseRuleDefinitionStatement.class);
+ TestDatabaseRuleConfiguration toBeAlteredRuleConfig = new
TestDatabaseRuleConfiguration();
+ RuleConfiguration decoratedRuleConfig = mock(RuleConfiguration.class);
+
when(executor.buildToBeAlteredRuleConfiguration(sqlStatement)).thenReturn(toBeAlteredRuleConfig);
+
when(database.decorateRuleConfiguration(toBeAlteredRuleConfig)).thenReturn(decoratedRuleConfig);
+ RuleConfiguration currentRuleConfig = mock(RuleConfiguration.class);
+ ruleOperator.operate(sqlStatement, database, currentRuleConfig);
+ verify(metaDataManagerPersistService).alterRuleConfiguration(database,
decoratedRuleConfig);
+ verify(metaDataManagerPersistService,
never()).removeRuleConfigurationItem(database, null);
+ }
+
+ @Test
+ void assertOperateWhenRuleConfigurationToRemoveEmpty() {
+ DatabaseRuleDefinitionStatement sqlStatement =
mock(DatabaseRuleDefinitionStatement.class);
+ TestDatabaseRuleConfiguration toBeAlteredRuleConfig = new
TestDatabaseRuleConfiguration();
+ RuleConfiguration decoratedRuleConfig = mock(RuleConfiguration.class);
+ TestDatabaseRuleConfiguration toBeDroppedRuleConfig = new
TestDatabaseRuleConfiguration();
+ DatabaseRuleConfigurationEmptyChecker emptyChecker =
mock(DatabaseRuleConfigurationEmptyChecker.class);
+
when(executor.buildToBeAlteredRuleConfiguration(sqlStatement)).thenReturn(toBeAlteredRuleConfig);
+
when(database.decorateRuleConfiguration(toBeAlteredRuleConfig)).thenReturn(decoratedRuleConfig);
+
when(executor.buildToBeDroppedRuleConfiguration(toBeAlteredRuleConfig)).thenReturn(toBeDroppedRuleConfig);
+ when((TypedSPI)
TypedSPILoader.getService(DatabaseRuleConfigurationEmptyChecker.class,
TestDatabaseRuleConfiguration.class)).thenReturn(emptyChecker);
+ when(emptyChecker.isEmpty(toBeDroppedRuleConfig)).thenReturn(true);
+ RuleConfiguration currentRuleConfig = mock(RuleConfiguration.class);
+ ruleOperator.operate(sqlStatement, database, currentRuleConfig);
+ verify(metaDataManagerPersistService).alterRuleConfiguration(database,
decoratedRuleConfig);
+ verify(metaDataManagerPersistService,
never()).removeRuleConfigurationItem(database, toBeDroppedRuleConfig);
+ }
+
+ private static final class TestDatabaseRuleConfiguration implements
DatabaseRuleConfiguration {
+ }
+}
diff --git
a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/DropDatabaseRuleOperatorTest.java
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/DropDatabaseRuleOperatorTest.java
index 0fb0984719e..d31df514a3c 100644
---
a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/DropDatabaseRuleOperatorTest.java
+++
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/DropDatabaseRuleOperatorTest.java
@@ -35,11 +35,13 @@ import
org.apache.shardingsphere.mode.persist.PersistServiceFacade;
import org.apache.shardingsphere.mode.persist.mode.ModePersistServiceFacade;
import
org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import java.util.Arrays;
+import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
@@ -123,8 +125,9 @@ class DropDatabaseRuleOperatorTest {
mockedStatic.when(() ->
TypedSPILoader.getService(DatabaseRuleConfigurationEmptyChecker.class,
TestDatabaseRuleConfiguration.class)).thenReturn(emptyChecker);
ruleOperator.operate(sqlStatement, database, currentRuleConfig);
}
-
verify(metaDataManagerPersistService).removeRuleConfigurationItem(database,
toBeDroppedRuleItemConfig);
- verify(metaDataManagerPersistService).alterRuleConfiguration(database,
toBeAlteredRuleConfig);
+ InOrder actual = inOrder(metaDataManagerPersistService);
+
actual.verify(metaDataManagerPersistService).removeRuleConfigurationItem(database,
toBeDroppedRuleItemConfig);
+
actual.verify(metaDataManagerPersistService).alterRuleConfiguration(database,
toBeAlteredRuleConfig);
verify(metaDataManagerPersistService,
never()).removeRuleConfiguration(database, currentRuleConfig, "test_rule");
}
@@ -149,8 +152,9 @@ class DropDatabaseRuleOperatorTest {
mockedStatic.when(() ->
TypedSPILoader.getService(DatabaseRuleConfigurationEmptyChecker.class,
TestDatabaseRuleConfiguration.class)).thenReturn(emptyChecker);
ruleOperator.operate(sqlStatement, database, currentRuleConfig);
}
-
verify(metaDataManagerPersistService).removeRuleConfigurationItem(database,
toBeDroppedRuleItemConfig);
-
verify(metaDataManagerPersistService).removeRuleConfiguration(database,
currentRuleConfig, "test_rule");
+ InOrder actual = inOrder(metaDataManagerPersistService);
+
actual.verify(metaDataManagerPersistService).removeRuleConfigurationItem(database,
toBeDroppedRuleItemConfig);
+
actual.verify(metaDataManagerPersistService).removeRuleConfiguration(database,
currentRuleConfig, "test_rule");
verify(metaDataManagerPersistService,
never()).alterRuleConfiguration(database, toBeAlteredRuleConfig);
}
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/ClusterMetaDataManagerPersistServiceTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/ClusterMetaDataManagerPersistServiceTest.java
index 0799cbce996..6166a77910f 100644
---
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/ClusterMetaDataManagerPersistServiceTest.java
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/ClusterMetaDataManagerPersistServiceTest.java
@@ -34,11 +34,16 @@ import
org.apache.shardingsphere.mode.manager.cluster.persist.coordinator.databa
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.manager.MetaDataContextManager;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistFacade;
+import
org.apache.shardingsphere.mode.metadata.persist.config.database.DatabaseRulePersistService;
import
org.apache.shardingsphere.mode.metadata.persist.metadata.DatabaseMetaDataPersistFacade;
import
org.apache.shardingsphere.mode.metadata.persist.metadata.service.DatabaseMetaDataPersistService;
+import
org.apache.shardingsphere.mode.metadata.persist.metadata.service.TableMetaDataPersistService;
+import
org.apache.shardingsphere.mode.metadata.refresher.util.TableRefreshUtils;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
import org.apache.shardingsphere.single.rule.SingleRule;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -46,7 +51,6 @@ import org.mockito.Answers;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.internal.configuration.plugins.Plugins;
-import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Collections;
import java.util.LinkedHashMap;
@@ -64,7 +68,8 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-@ExtendWith(MockitoExtension.class)
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(TableRefreshUtils.class)
class ClusterMetaDataManagerPersistServiceTest {
private ClusterMetaDataManagerPersistService metaDataManagerPersistService;
@@ -262,16 +267,43 @@ class ClusterMetaDataManagerPersistServiceTest {
@Test
void assertCreateTable() {
ShardingSphereTable table = mock(ShardingSphereTable.class);
- metaDataManagerPersistService.createTable(new
ShardingSphereDatabase("foo_db", mock(), mock(), mock(),
Collections.emptyList(), new ConfigurationProperties(new Properties())),
"foo_schema",
- table);
-
verify(metaDataPersistFacade.getDatabaseMetaDataFacade().getTable()).persist("foo_db",
"foo_schema", Collections.singleton(table));
+ when(table.getName()).thenReturn("foo_tbl");
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(database.getName()).thenReturn("foo_db");
+ RuleMetaData ruleMetaData = mock(RuleMetaData.class);
+ when(database.getRuleMetaData()).thenReturn(ruleMetaData);
+ SingleRule singleRule = mock(SingleRule.class);
+ SingleRuleConfiguration singleRuleConfig = new
SingleRuleConfiguration();
+
when(ruleMetaData.getSingleRule(SingleRule.class)).thenReturn(singleRule);
+ when(singleRule.getConfiguration()).thenReturn(singleRuleConfig);
+ when(TableRefreshUtils.isSingleTable("foo_tbl",
database)).thenReturn(true);
+ when(TableRefreshUtils.isNeedRefresh(ruleMetaData, "foo_schema",
"foo_tbl")).thenReturn(true);
+ metaDataManagerPersistService.createTable(database, "foo_schema",
table);
+ TableMetaDataPersistService tableMetaDataPersistService =
metaDataPersistFacade.getDatabaseMetaDataFacade().getTable();
+ DatabaseRulePersistService databaseRulePersistService =
metaDataPersistFacade.getDatabaseRuleService();
+ InOrder actual = inOrder(tableMetaDataPersistService,
databaseRulePersistService);
+ actual.verify(tableMetaDataPersistService).persist("foo_db",
"foo_schema", Collections.singleton(table));
+ actual.verify(databaseRulePersistService).persist("foo_db",
Collections.singleton(singleRuleConfig));
}
@Test
void assertDropTables() {
- metaDataManagerPersistService.dropTables(new
ShardingSphereDatabase("foo_db", mock(), mock(), mock(),
- Collections.emptyList(), new ConfigurationProperties(new
Properties())), "foo_schema", Collections.singleton("foo_tbl"));
-
verify(metaDataPersistFacade.getDatabaseMetaDataFacade().getTable()).drop("foo_db",
"foo_schema", "foo_tbl");
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(database.getName()).thenReturn("foo_db");
+ RuleMetaData ruleMetaData = mock(RuleMetaData.class);
+ when(database.getRuleMetaData()).thenReturn(ruleMetaData);
+ SingleRule singleRule = mock(SingleRule.class);
+ SingleRuleConfiguration singleRuleConfig = new
SingleRuleConfiguration();
+
when(ruleMetaData.getSingleRule(SingleRule.class)).thenReturn(singleRule);
+ when(singleRule.getConfiguration()).thenReturn(singleRuleConfig);
+ when(TableRefreshUtils.isNeedRefresh(ruleMetaData, "foo_schema",
Collections.singleton("foo_tbl"))).thenReturn(true);
+ when(TableRefreshUtils.isSingleTable("foo_tbl",
database)).thenReturn(true);
+ metaDataManagerPersistService.dropTables(database, "foo_schema",
Collections.singleton("foo_tbl"));
+ TableMetaDataPersistService tableMetaDataPersistService =
metaDataPersistFacade.getDatabaseMetaDataFacade().getTable();
+ DatabaseRulePersistService databaseRulePersistService =
metaDataPersistFacade.getDatabaseRuleService();
+ InOrder actual = inOrder(tableMetaDataPersistService,
databaseRulePersistService);
+ actual.verify(tableMetaDataPersistService).drop("foo_db",
"foo_schema", "foo_tbl");
+ actual.verify(databaseRulePersistService).persist("foo_db",
Collections.singleton(singleRuleConfig));
}
private void mockReloadedMetaDataContext() {