This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 68152d0cf46 Refactor ClusterContextManagerCoordinator (#18990)
68152d0cf46 is described below
commit 68152d0cf4609e64ff03755707515eb886b0e983
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jul 10 15:22:11 2022 +0800
Refactor ClusterContextManagerCoordinator (#18990)
---
.../ClusterContextManagerCoordinator.java | 22 ++++----
.../ClusterContextManagerCoordinatorTest.java | 65 +++++++++++-----------
2 files changed, 42 insertions(+), 45 deletions(-)
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
index ad8f3a7e187..61c8793c53e 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
@@ -25,8 +25,8 @@ import
org.apache.shardingsphere.infra.executor.sql.process.model.yaml.BatchYaml
import
org.apache.shardingsphere.infra.executor.sql.process.model.yaml.YamlExecuteProcessContext;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import
org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDatabase;
-import
org.apache.shardingsphere.infra.rule.identifier.type.StatusContainedRule;
import
org.apache.shardingsphere.infra.rule.identifier.type.RestartHeartBeatJobRule;
+import
org.apache.shardingsphere.infra.rule.identifier.type.StatusContainedRule;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent;
@@ -123,16 +123,6 @@ public final class ClusterContextManagerCoordinator {
contextManager.dropSchema(event.getDatabaseName(),
event.getSchemaName());
}
- /**
- * Renew properties.
- *
- * @param event properties changed event
- */
- @Subscribe
- public synchronized void renew(final PropertiesChangedEvent event) {
- contextManager.alterProperties(event.getProps());
- }
-
/**
* Renew meta data of the schema.
*
@@ -262,6 +252,16 @@ public final class ClusterContextManagerCoordinator {
disableDataSources();
}
+ /**
+ * Renew properties.
+ *
+ * @param event properties changed event
+ */
+ @Subscribe
+ public synchronized void renew(final PropertiesChangedEvent event) {
+ contextManager.alterProperties(event.getProps());
+ }
+
/**
* Trigger show process list.
*
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
index e0bf433d4e3..9e0b2cc0701 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
@@ -24,7 +24,6 @@ import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmC
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
-import org.apache.shardingsphere.infra.database.DefaultDatabase;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
import
org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
@@ -44,9 +43,9 @@ import
org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.
import
org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import
org.apache.shardingsphere.infra.rule.identifier.type.StatusContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule;
import
org.apache.shardingsphere.infra.rule.identifier.type.RestartHeartBeatJobRule;
+import
org.apache.shardingsphere.infra.rule.identifier.type.StatusContainedRule;
import org.apache.shardingsphere.infra.state.StateType;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
@@ -115,7 +114,6 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -153,11 +151,10 @@ public final class ClusterContextManagerCoordinatorTest {
private Map<String, ShardingSphereDatabase> createDatabases() {
when(database.getName()).thenReturn("db");
- ShardingSphereResource resource = mock(ShardingSphereResource.class);
- when(resource.getDatabaseType()).thenReturn(new MySQLDatabaseType());
- when(database.getResource()).thenReturn(resource);
+
when(database.getResource().getDataSources()).thenReturn(Collections.emptyMap());
+ when(database.getResource().getDatabaseType()).thenReturn(new
MySQLDatabaseType());
when(database.getProtocolType()).thenReturn(new MySQLDatabaseType());
-
when(database.getSchemas().get(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
+
when(database.getSchemas().get("foo_schema")).thenReturn(mock(ShardingSphereSchema.class));
when(database.getRuleMetaData().getRules()).thenReturn(new
LinkedList<>());
when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList());
when(database.getRuleMetaData().findRules(ResourceHeldRule.class)).thenReturn(Collections.emptyList());
@@ -171,52 +168,44 @@ public final class ClusterContextManagerCoordinatorTest {
}
@Test
- public void assertDatabaseAdd() throws SQLException {
-
when(persistService.getDataSourceService().load("db_add")).thenReturn(getDataSourcePropertiesMap());
-
when(persistService.getDatabaseRulePersistService().load("db_add")).thenReturn(Collections.emptyList());
- coordinator.renew(new DatabaseAddedEvent("db_add"));
-
assertNotNull(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db_add").getResource().getDataSources());
+ public void assertRenewForDatabaseAdded() throws SQLException {
+
when(persistService.getDataSourceService().load("db_added")).thenReturn(createDataSourcePropertiesMap());
+
when(persistService.getDatabaseRulePersistService().load("db_added")).thenReturn(Collections.emptyList());
+ coordinator.renew(new DatabaseAddedEvent("db_added"));
+
assertNotNull(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db_added").getResource().getDataSources());
}
- private Map<String, DataSourceProperties> getDataSourcePropertiesMap() {
+ private Map<String, DataSourceProperties> createDataSourcePropertiesMap() {
MockedDataSource dataSource = new MockedDataSource();
Map<String, DataSourceProperties> result = new LinkedHashMap<>(3, 1);
result.put("primary_ds",
DataSourcePropertiesCreator.create(dataSource));
- result.put("ds_0", DataSourcePropertiesCreator.create(dataSource));
- result.put("ds_1", DataSourcePropertiesCreator.create(dataSource));
+ result.put("replica_ds_0",
DataSourcePropertiesCreator.create(dataSource));
+ result.put("replica_ds_1",
DataSourcePropertiesCreator.create(dataSource));
return result;
}
@Test
- public void assertDatabaseDelete() {
+ public void assertRenewForDatabaseDeleted() {
coordinator.renew(new DatabaseDeletedEvent("db"));
assertNull(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db"));
}
@Test
- public void assertPropertiesChanged() {
- Properties props = new Properties();
- props.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(),
Boolean.TRUE.toString());
- coordinator.renew(new PropertiesChangedEvent(props));
-
assertThat(contextManager.getMetaDataContexts().getMetaData().getProps().getProps().getProperty(ConfigurationPropertyKey.SQL_SHOW.getKey()),
is(Boolean.TRUE.toString()));
- }
-
- @Test
- public void assertSchemaAdd() {
+ public void assertRenewForSchemaAdded() {
when(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db").getSchemas().get("foo_schema")).thenReturn(null);
coordinator.renew(new SchemaAddedEvent("db", "foo_schema"));
verify(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db").getSchemas()).put(argThat(argument
-> argument.equals("foo_schema")), any(ShardingSphereSchema.class));
}
@Test
- public void assertSchemaDelete() {
+ public void assertRenewForSchemaDeleted() {
when(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db").getSchemas().containsKey("foo_schema")).thenReturn(true);
coordinator.renew(new SchemaDeletedEvent("db", "foo_schema"));
verify(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db").getSchemas()).remove("foo_schema");
}
@Test
- public void assertSchemaChanged() {
+ public void assertRenewForSchemaChanged() {
ShardingSphereTable changedTableMetaData = new
ShardingSphereTable("t_order", Collections.emptyList(),
Collections.emptyList(), Collections.emptyList());
SchemaChangedEvent event = new SchemaChangedEvent("db", "db",
changedTableMetaData, null);
coordinator.renew(event);
@@ -225,7 +214,7 @@ public final class ClusterContextManagerCoordinatorTest {
}
@Test
- public void assertRuleConfigurationsChanged() {
+ public void assertRenewForRuleConfigurationsChanged() {
when(persistService.getDatabaseVersionPersistService().isActiveVersion("db",
"0")).thenReturn(true);
assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db"),
is(database));
coordinator.renew(new RuleConfigurationsChangedEvent("db", "0",
Collections.emptyList()));
@@ -233,24 +222,24 @@ public final class ClusterContextManagerCoordinatorTest {
}
@Test
- public void assertDisableStateChanged() {
+ public void assertRenewForDisableStateChanged() {
StatusContainedRule statusContainedRule =
mock(StatusContainedRule.class);
when(database.getRuleMetaData().getRules()).thenReturn(Collections.singletonList(statusContainedRule));
StorageNodeChangedEvent event = new StorageNodeChangedEvent(new
QualifiedDatabase("db.readwrite_ds.ds_0"), new
StorageNodeDataSource(StorageNodeRole.MEMBER, StorageNodeStatus.DISABLED));
coordinator.renew(event);
- verify(statusContainedRule, times(1)).updateStatus(argThat(
+ verify(statusContainedRule).updateStatus(argThat(
(ArgumentMatcher<StorageNodeDataSourceChangedEvent>)
argumentEvent -> Objects.equals(event.getQualifiedDatabase(),
argumentEvent.getQualifiedDatabase())
&& Objects.equals(event.getDataSource(),
argumentEvent.getDataSource())));
}
@Test
- public void assertDataSourceChanged() {
+ public void assertRenewForDataSourceChanged() {
when(persistService.getDatabaseVersionPersistService().isActiveVersion("db",
"0")).thenReturn(true);
- coordinator.renew(new DataSourceChangedEvent("db", "0",
getChangedDataSourcePropertiesMap()));
+ coordinator.renew(new DataSourceChangedEvent("db", "0",
createChangedDataSourcePropertiesMap()));
assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabases().get("db").getResource().getDataSources().containsKey("ds_2"));
}
- private Map<String, DataSourceProperties>
getChangedDataSourcePropertiesMap() {
+ private Map<String, DataSourceProperties>
createChangedDataSourcePropertiesMap() {
MockedDataSource dataSource = new MockedDataSource();
Map<String, DataSourceProperties> result = new LinkedHashMap<>(3, 1);
result.put("primary_ds",
DataSourcePropertiesCreator.create(dataSource));
@@ -260,7 +249,7 @@ public final class ClusterContextManagerCoordinatorTest {
}
@Test
- public void assertGlobalRuleConfigurationsChanged() {
+ public void assertRenewForGlobalRuleConfigurationsChanged() {
GlobalRuleConfigurationsChangedEvent event = new
GlobalRuleConfigurationsChangedEvent(getChangedGlobalRuleConfigurations());
coordinator.renew(event);
assertThat(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData(),
not(globalRuleMetaData));
@@ -352,6 +341,14 @@ public final class ClusterContextManagerCoordinatorTest {
assertThat(((LinkedList<ComputeNodeInstance>)
contextManager.getInstanceContext().getComputeNodeInstances()).get(1).getInstanceMetaData(),
is(instanceMetaData1));
}
+ @Test
+ public void assertRenewProperties() {
+ Properties props = new Properties();
+ props.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(),
Boolean.TRUE.toString());
+ coordinator.renew(new PropertiesChangedEvent(props));
+
assertThat(contextManager.getMetaDataContexts().getMetaData().getProps().getProps().getProperty(ConfigurationPropertyKey.SQL_SHOW.getKey()),
is(Boolean.TRUE.toString()));
+ }
+
@Test
public void assertCompleteUnitShowProcessList() {
String showProcessListId = "foo_process_id";