You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/07/02 08:40:10 UTC
[shardingsphere] branch master updated: Add InstanceMetaDataBuilderFactory.create (#18785)
This is an automated email from the ASF dual-hosted git repository.
zhonghongsheng 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 4ea86438a0c Add InstanceMetaDataBuilderFactory.create (#18785)
4ea86438a0c is described below
commit 4ea86438a0c3a642ef45ac99de2cec1be22b0f40
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Jul 2 16:40:03 2022 +0800
Add InstanceMetaDataBuilderFactory.create (#18785)
---
.../metadata/InstanceMetaDataBuilderFactory.java | 20 +++++++++++++++++---
.../jdbc/JDBCInstanceMetaDataBuilderTest.java | 2 +-
.../proxy/ProxyInstanceMetaDataBuilderTest.java | 2 +-
.../core/datasource/ShardingSphereDataSource.java | 2 +-
.../compute/service/ComputeNodeStatusService.java | 12 +++---------
.../watcher/ComputeNodeStateChangedWatcher.java | 9 ++-------
.../proxy/initializer/BootstrapInitializer.java | 2 +-
7 files changed, 26 insertions(+), 23 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataBuilderFactory.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataBuilderFactory.java
index 0df724069f5..b86ce84da9b 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataBuilderFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataBuilderFactory.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.infra.instance.metadata;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData;
+import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
@@ -33,13 +35,25 @@ public final class InstanceMetaDataBuilderFactory {
}
/**
- * Create instance of instance meta data.
+ * Create instance meta data.
*
* @param type type
* @param port port
- * @return created instance of instance meta data
+ * @return created instance meta data
*/
- public static InstanceMetaData newInstance(final String type, final int port) {
+ public static InstanceMetaData create(final String type, final int port) {
return TypedSPIRegistry.getRegisteredService(InstanceMetaDataBuilder.class, type).build(port);
}
+
+ /**
+ * Create instance meta data.
+ *
+ * @param instanceId instance ID
+ * @param instanceType instance type
+ * @param attributes attributes
+ * @return created instance meta data
+ */
+ public static InstanceMetaData create(final String instanceId, final InstanceType instanceType, final String attributes) {
+ return InstanceType.JDBC == instanceType ? new JDBCInstanceMetaData(instanceId) : new ProxyInstanceMetaData(instanceId, attributes);
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/jdbc/JDBCInstanceMetaDataBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/jdbc/JDBCInstanceMetaDataBuilderTest.java
index b378d8e9d9a..49972ac4851 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/jdbc/JDBCInstanceMetaDataBuilderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/jdbc/JDBCInstanceMetaDataBuilderTest.java
@@ -30,7 +30,7 @@ public final class JDBCInstanceMetaDataBuilderTest {
@Test
public void assertNewInstance() {
- InstanceMetaData actual = InstanceMetaDataBuilderFactory.newInstance("JDBC", -1);
+ InstanceMetaData actual = InstanceMetaDataBuilderFactory.create("JDBC", -1);
assertNotNull(actual.getInstanceId());
assertNotNull(actual.getIp());
assertThat(actual.getAttributes(), is(""));
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/proxy/ProxyInstanceMetaDataBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/proxy/ProxyInstanceMetaDataBuilderTest.java
index d6070a09d42..efbdabf6641 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/proxy/ProxyInstanceMetaDataBuilderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/proxy/ProxyInstanceMetaDataBuilderTest.java
@@ -30,7 +30,7 @@ public final class ProxyInstanceMetaDataBuilderTest {
@Test
public void assertNewInstance() {
- ProxyInstanceMetaData actual = (ProxyInstanceMetaData) InstanceMetaDataBuilderFactory.newInstance("Proxy", 3307);
+ ProxyInstanceMetaData actual = (ProxyInstanceMetaData) InstanceMetaDataBuilderFactory.create("Proxy", 3307);
assertNotNull(actual.getInstanceId());
assertNotNull(actual.getIp());
assertThat(actual.getPort(), is(3307));
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index 097c13b82c5..39f71dc5522 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -74,7 +74,7 @@ public final class ShardingSphereDataSource extends AbstractDataSourceAdapter im
private ContextManager createContextManager(final String databaseName, final ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
final Collection<RuleConfiguration> ruleConfigs, final Properties props) throws SQLException {
- InstanceMetaData instanceMetaData = InstanceMetaDataBuilderFactory.newInstance("JDBC", -1);
+ InstanceMetaData instanceMetaData = InstanceMetaDataBuilderFactory.create("JDBC", -1);
Collection<RuleConfiguration> globalRuleConfigs = ruleConfigs.stream().filter(each -> each instanceof GlobalRuleConfiguration).collect(Collectors.toList());
ContextManagerBuilderParameter parameter = new ContextManagerBuilderParameter(modeConfig, Collections.singletonMap(databaseName,
new DataSourceProvidedDatabaseConfiguration(dataSourceMap, ruleConfigs)), globalRuleConfigs, props, Collections.emptyList(), instanceMetaData);
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/registry/status/compute/service/ComputeNodeStatusService.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusService.java
index 853f4deb627..e09473f9a20 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusService.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusService.java
@@ -22,9 +22,8 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
+import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaDataBuilderFactory;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
-import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData;
-import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.mode.metadata.persist.node.ComputeNode;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
@@ -130,13 +129,8 @@ public final class ComputeNodeStatusService {
private Collection<ComputeNodeInstance> loadComputeNodeInstances(final InstanceType instanceType) {
Collection<String> onlineComputeNodes = repository.getChildrenKeys(ComputeNode.getOnlineNodePath(instanceType));
- return onlineComputeNodes.stream().map(each -> loadComputeNodeInstance(createInstanceMetaData(each, instanceType))).collect(Collectors.toList());
- }
-
- private InstanceMetaData createInstanceMetaData(final String instanceId, final InstanceType type) {
- return InstanceType.JDBC == type
- ? new JDBCInstanceMetaData(instanceId)
- : new ProxyInstanceMetaData(instanceId, repository.get(ComputeNode.getOnlineInstanceNodePath(instanceId, type)));
+ return onlineComputeNodes.stream().map(each -> loadComputeNodeInstance(
+ InstanceMetaDataBuilderFactory.create(each, instanceType, repository.get(ComputeNode.getOnlineInstanceNodePath(each, instanceType))))).collect(Collectors.toList());
}
/**
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/registry/status/compute/watcher/ComputeNodeStateChangedWatcher.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChan [...]
index 6e43dea7b44..a83ad72c400 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChangedWatcher.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChangedWatcher.java
@@ -19,9 +19,8 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.stat
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
+import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaDataBuilderFactory;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
-import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData;
-import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher;
@@ -103,7 +102,7 @@ public final class ComputeNodeStateChangedWatcher implements GovernanceWatcher<G
private Optional<GovernanceEvent> createInstanceEvent(final DataChangedEvent event) {
Matcher matcher = matchInstanceOnlinePath(event.getKey());
if (matcher.find()) {
- InstanceMetaData instanceMetaData = createInstanceMetaData(matcher.group(2), InstanceType.valueOf(matcher.group(1).toUpperCase()), event.getValue());
+ InstanceMetaData instanceMetaData = InstanceMetaDataBuilderFactory.create(matcher.group(2), InstanceType.valueOf(matcher.group(1).toUpperCase()), event.getValue());
if (Type.ADDED == event.getType()) {
return Optional.of(new InstanceOnlineEvent(instanceMetaData));
}
@@ -117,8 +116,4 @@ public final class ComputeNodeStateChangedWatcher implements GovernanceWatcher<G
private Matcher matchInstanceOnlinePath(final String onlineInstancePath) {
return Pattern.compile(ComputeNode.getOnlineInstanceNodePath() + "/[\\S]+/([\\S]+)$", Pattern.CASE_INSENSITIVE).matcher(onlineInstancePath);
}
-
- private InstanceMetaData createInstanceMetaData(final String instanceId, final InstanceType instanceType, final String attributes) {
- return InstanceType.JDBC == instanceType ? new JDBCInstanceMetaData(instanceId) : new ProxyInstanceMetaData(instanceId, attributes);
- }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
index 0e182106c79..5c991b29a8f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
@@ -60,7 +60,7 @@ public final class BootstrapInitializer {
private ContextManager createContextManager(final YamlProxyConfiguration yamlConfig, final ModeConfiguration modeConfig, final int port) throws SQLException {
ProxyConfiguration proxyConfig = new YamlProxyConfigurationSwapper().swap(yamlConfig);
- InstanceMetaData instanceMetaData = InstanceMetaDataBuilderFactory.newInstance("Proxy", port);
+ InstanceMetaData instanceMetaData = InstanceMetaDataBuilderFactory.create("Proxy", port);
ContextManagerBuilderParameter parameter = new ContextManagerBuilderParameter(modeConfig, proxyConfig.getDatabaseConfigurations(),
proxyConfig.getGlobalConfiguration().getRules(), proxyConfig.getGlobalConfiguration().getProperties(), proxyConfig.getGlobalConfiguration().getLabels(), instanceMetaData);
return ContextManagerBuilderFactory.getInstance(modeConfig).build(parameter);