You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/01/06 05:33:22 UTC
[shardingsphere] branch master updated: Refactor registry center instance node (#14560)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 8ba7daf Refactor registry center instance node (#14560)
8ba7daf is described below
commit 8ba7daf169aa4a7b9c74782c85c5c2c85e00ca5e
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Thu Jan 6 13:32:29 2022 +0800
Refactor registry center instance node (#14560)
* Refactor registry center instance node
* Refactor registry center instance node
---
.../infra/instance/InstanceDefinition.java | 43 +++++++++++++++++
.../instance/{Instance.java => InstanceId.java} | 48 +++++++------------
.../infra/instance/InstanceTest.java | 55 ----------------------
.../core/datasource/ShardingSphereDataSource.java | 3 +-
.../core/api/ModeScheduleContextFactory.java | 12 ++---
.../manager/ContextManagerBuilderParameter.java | 6 +--
.../cluster/ClusterContextManagerBuilder.java | 14 +++---
.../cluster/coordinator/RegistryCenter.java | 12 ++---
.../lock/service/LockRegistryService.java | 4 +-
.../compute/service/ComputeNodeStatusService.java | 9 ++--
.../subscriber/ComputeNodeStatusSubscriber.java | 4 +-
.../ClusterContextManagerCoordinatorTest.java | 3 +-
.../lock/service/LockRegistryServiceTest.java | 2 -
.../service/ComputeNodeStatusServiceTest.java | 13 ++---
.../ComputeNodeStateChangedWatcherTest.java | 21 ---------
.../StandaloneContextManagerBuilderTextTest.java | 4 +-
.../proxy/initializer/BootstrapInitializer.java | 5 +-
17 files changed, 103 insertions(+), 155 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceDefinition.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceDefinition.java
new file mode 100644
index 0000000..4569633
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceDefinition.java
@@ -0,0 +1,43 @@
+/*
+ * 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.infra.instance;
+
+import lombok.Getter;
+
+/**
+ * Instance definition.
+ */
+@Getter
+public final class InstanceDefinition {
+
+ private static final String DELIMITER = "@";
+
+ private final InstanceType instanceType;
+
+ private final InstanceId instanceId;
+
+ public InstanceDefinition(final InstanceType instanceType) {
+ this.instanceType = instanceType;
+ instanceId = new InstanceId();
+ }
+
+ public InstanceDefinition(final InstanceType instanceType, final Integer port) {
+ this.instanceType = instanceType;
+ instanceId = new InstanceId(port);
+ }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/Instance.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceId.java
similarity index 54%
rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/Instance.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceId.java
index 7e3d141..21bc69d 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/Instance.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceId.java
@@ -17,52 +17,40 @@
package org.apache.shardingsphere.infra.instance;
-import lombok.AccessLevel;
import lombok.Getter;
-import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.instance.utils.IpUtils;
import java.lang.management.ManagementFactory;
/**
- * Instance.
+ * Instance id.
*/
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
-public final class Instance {
+public final class InstanceId {
private static final String DELIMITER = "@";
- private static final Instance INSTANCE = new Instance();
+ private final String id;
- private volatile String id;
+ private final String ip;
- /**
- * Init instance.
- *
- * @param port port
- */
- public synchronized void init(final Integer port) {
- id = String.join(DELIMITER, IpUtils.getIp(), null == port ? ManagementFactory.getRuntimeMXBean().getName().split(DELIMITER)[0] : String.valueOf(port));
+ private final Integer port;
+
+ public InstanceId(final String ip, final Integer port) {
+ this.ip = ip;
+ this.port = port;
+ id = String.join(DELIMITER, ip, String.valueOf(port));
}
- /**
- * Get instance id.
- *
- * @param ip ip
- * @param port port
- * @return instance id
- */
- public String getInstanceId(final String ip, final String port) {
- return String.join(DELIMITER, ip, port);
+ public InstanceId(final Integer port) {
+ this.port = port;
+ ip = IpUtils.getIp();
+ id = String.join(DELIMITER, ip, String.valueOf(port));
}
- /**
- * Get instance.
- *
- * @return singleton instance
- */
- public static Instance getInstance() {
- return INSTANCE;
+ public InstanceId() {
+ port = 0;
+ ip = IpUtils.getIp();
+ id = String.join(DELIMITER, ip, ManagementFactory.getRuntimeMXBean().getName().split(DELIMITER)[0]);
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/InstanceTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/InstanceTest.java
deleted file mode 100644
index e0a6d9b..0000000
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/InstanceTest.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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.infra.instance;
-
-import com.google.common.base.Joiner;
-import org.apache.shardingsphere.infra.instance.utils.IpUtils;
-import org.junit.Test;
-
-import java.lang.management.ManagementFactory;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class InstanceTest {
-
- private String ip = IpUtils.getIp();
-
- @Test
- public void assertGetIdWithPort() {
- Instance.getInstance().init(3307);
- String id = Instance.getInstance().getId();
- assertThat(id.split("@").length, is(2));
- assertThat(id, is(Joiner.on("@").join(ip, 3307)));
- }
-
- @Test
- public void assertGetDefaultId() {
- Instance.getInstance().init(null);
- String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
- String id = Instance.getInstance().getId();
- assertThat(id.split("@").length, is(2));
- assertThat(id, is(Joiner.on("@").join(ip, pid)));
- }
-
- @Test
- public void assertGetInstanceId() {
- Instance.getInstance().init(null);
- assertThat(Instance.getInstance().getInstanceId("127.0.0.1", "3307"), is("127.0.0.1@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 7e9b455..38ffb6b 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
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.config.checker.RuleConfigurationCheckerFa
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.config.scope.GlobalRuleConfiguration;
import org.apache.shardingsphere.infra.config.scope.SchemaRuleConfiguration;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
import org.apache.shardingsphere.infra.instance.InstanceType;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderFactory;
@@ -75,7 +76,7 @@ public final class ShardingSphereDataSource extends AbstractDataSourceAdapter im
schemaName, ruleConfigs.stream().filter(each -> each instanceof SchemaRuleConfiguration).collect(Collectors.toList()));
Collection<RuleConfiguration> globalRuleConfigs = ruleConfigs.stream().filter(each -> each instanceof GlobalRuleConfiguration).collect(Collectors.toList());
ContextManagerBuilderParameter parameter = ContextManagerBuilderParameter.builder().modeConfig(modeConfig).dataSourcesMap(dataSourcesMap).schemaRuleConfigs(schemaRuleConfigs)
- .globalRuleConfigs(globalRuleConfigs).props(props).isOverwrite(isOverwrite).schemaName(schemaName).instanceType(InstanceType.JDBC).build();
+ .globalRuleConfigs(globalRuleConfigs).props(props).isOverwrite(isOverwrite).schemaName(schemaName).instanceDefinition(new InstanceDefinition(InstanceType.JDBC)).build();
return ContextManagerBuilderFactory.newInstance(modeConfig).build(parameter);
}
diff --git a/shardingsphere-kernel/shardingsphere-schedule/shardingsphere-schedule-core/src/main/java/org/apache/shardingsphere/schedule/core/api/ModeScheduleContextFactory.java b/shardingsphere-kernel/shardingsphere-schedule/shardingsphere-schedule-core/src/main/java/org/apache/shardingsphere/schedule/core/api/ModeScheduleContextFactory.java
index de52172..0079385 100644
--- a/shardingsphere-kernel/shardingsphere-schedule/shardingsphere-schedule-core/src/main/java/org/apache/shardingsphere/schedule/core/api/ModeScheduleContextFactory.java
+++ b/shardingsphere-kernel/shardingsphere-schedule/shardingsphere-schedule-core/src/main/java/org/apache/shardingsphere/schedule/core/api/ModeScheduleContextFactory.java
@@ -17,12 +17,10 @@
package org.apache.shardingsphere.schedule.core.api;
-import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
-import org.apache.shardingsphere.infra.instance.Instance;
import java.util.Map;
import java.util.Optional;
@@ -42,10 +40,11 @@ public final class ModeScheduleContextFactory {
/**
* Init mode schedule context.
*
+ * @param instanceId instance id
* @param modeConfig mode configuration
*/
- public void init(final ModeConfiguration modeConfig) {
- modeScheduleContexts.put(Instance.getInstance().getId(), new ModeScheduleContext(modeConfig));
+ public void init(final String instanceId, final ModeConfiguration modeConfig) {
+ modeScheduleContexts.put(instanceId, new ModeScheduleContext(modeConfig));
}
/**
@@ -54,10 +53,11 @@ public final class ModeScheduleContextFactory {
* @return mode schedule context
*/
public Optional<ModeScheduleContext> get() {
- if (Strings.isNullOrEmpty(Instance.getInstance().getId())) {
+ // TODO get by instance id
+ if (modeScheduleContexts.values().isEmpty()) {
return Optional.empty();
}
- return Optional.ofNullable(modeScheduleContexts.get(Instance.getInstance().getId()));
+ return Optional.ofNullable(modeScheduleContexts.values().iterator().next());
}
/**
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java
index fe7bdcc..3388d27 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java
@@ -21,7 +21,7 @@ import lombok.Builder;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
-import org.apache.shardingsphere.infra.instance.InstanceType;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
import javax.sql.DataSource;
import java.util.Collection;
@@ -47,11 +47,9 @@ public final class ContextManagerBuilderParameter {
private boolean isOverwrite;
- private Integer port;
-
private String schemaName;
private Collection<String> labels;
- private InstanceType instanceType;
+ private InstanceDefinition instanceDefinition;
}
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 6488762..60b17d0 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -22,7 +22,7 @@ import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.datasource.pool.creator.DataSourcePoolCreatorUtil;
-import org.apache.shardingsphere.infra.instance.Instance;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
import org.apache.shardingsphere.infra.metadata.schema.QualifiedSchema;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader;
@@ -88,11 +88,11 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
private void beforeBuildContextManager(final ContextManagerBuilderParameter parameter) throws SQLException {
ClusterPersistRepository repository = createClusterPersistRepository((ClusterPersistRepositoryConfiguration) parameter.getModeConfig().getRepository());
- registryCenter = new RegistryCenter(repository, parameter.getPort());
- ModeScheduleContextFactory.getInstance().init(parameter.getModeConfig());
+ registryCenter = new RegistryCenter(repository);
+ ModeScheduleContextFactory.getInstance().init(parameter.getInstanceDefinition().getInstanceId().getId(), parameter.getModeConfig());
metaDataPersistService = new MetaDataPersistService(repository);
persistConfigurations(metaDataPersistService, parameter.getDataSourcesMap(), parameter.getSchemaRuleConfigs(), parameter.getGlobalRuleConfigs(), parameter.getProps(), parameter.isOverwrite());
- persistInstanceConfigurations(parameter.getLabels());
+ persistInstanceConfigurations(parameter.getLabels(), parameter.getInstanceDefinition());
Collection<String> schemaNames = Strings.isNullOrEmpty(parameter.getSchemaName()) ? metaDataPersistService.getSchemaMetaDataService()
.loadAllNames() : Collections.singletonList(parameter.getSchemaName());
Map<String, Map<String, DataSource>> clusterDataSources = loadDataSourcesMap(metaDataPersistService, parameter.getDataSourcesMap(), schemaNames);
@@ -109,7 +109,7 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
private void afterBuildContextManager(final ContextManagerBuilderParameter parameter) {
new ClusterContextManagerCoordinator(metaDataPersistService, contextManager);
disableDataSources();
- registryCenter.onlineInstance(parameter.getInstanceType());
+ registryCenter.onlineInstance(parameter.getInstanceDefinition());
}
private ClusterPersistRepository createClusterPersistRepository(final ClusterPersistRepositoryConfiguration config) {
@@ -127,9 +127,9 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
}
}
- private void persistInstanceConfigurations(final Collection<String> labels) {
+ private void persistInstanceConfigurations(final Collection<String> labels, final InstanceDefinition instanceDefinition) {
if (null != labels && !labels.isEmpty()) {
- metaDataPersistService.persistInstanceConfigurations(Instance.getInstance().getId(), labels);
+ metaDataPersistService.persistInstanceConfigurations(instanceDefinition.getInstanceId().getId(), labels);
}
}
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/RegistryCenter.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java
index 04662e5..4d04bd1 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java
@@ -18,8 +18,7 @@
package org.apache.shardingsphere.mode.manager.cluster.coordinator;
import lombok.Getter;
-import org.apache.shardingsphere.infra.instance.Instance;
-import org.apache.shardingsphere.infra.instance.InstanceType;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.service.LockRegistryService;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcherFactory;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.cache.subscriber.ScalingRegistrySubscriber;
@@ -51,9 +50,8 @@ public final class RegistryCenter {
private final GovernanceWatcherFactory listenerFactory;
- public RegistryCenter(final ClusterPersistRepository repository, final Integer port) {
+ public RegistryCenter(final ClusterPersistRepository repository) {
this.repository = repository;
- Instance.getInstance().init(port);
storageNodeStatusService = new StorageNodeStatusService(repository);
computeNodeStatusService = new ComputeNodeStatusService(repository);
lockService = new LockRegistryService(repository);
@@ -73,10 +71,10 @@ public final class RegistryCenter {
/**
* Online instance.
*
- * @param instanceType instance type
+ * @param instanceDefinition instance definition
*/
- public void onlineInstance(final InstanceType instanceType) {
- computeNodeStatusService.registerOnline(instanceType);
+ public void onlineInstance(final InstanceDefinition instanceDefinition) {
+ computeNodeStatusService.registerOnline(instanceDefinition);
listenerFactory.watchListeners();
}
}
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/lock/service/LockRegistryService.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/service/LockRegistryService.java
index 05bc201..f366d98 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/service/LockRegistryService.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/service/LockRegistryService.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.service;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
-import org.apache.shardingsphere.infra.instance.Instance;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.ComputeNodeStatus;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.node.ComputeStatusNode;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
@@ -39,7 +38,8 @@ public final class LockRegistryService {
private final ClusterPersistRepository repository;
public LockRegistryService(final ClusterPersistRepository repository) {
- instanceId = Instance.getInstance().getId();
+ // TODO will be removed
+ instanceId = "";
this.repository = repository;
initLockNode();
}
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 db8ec9d..846ee41 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
@@ -18,8 +18,7 @@
package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.instance.Instance;
-import org.apache.shardingsphere.infra.instance.InstanceType;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
import org.apache.shardingsphere.mode.metadata.persist.node.ComputeNode;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
@@ -34,9 +33,9 @@ public final class ComputeNodeStatusService {
/**
* Register online.
*
- * @param instanceType instance type
+ * @param instanceDefinition instance definition
*/
- public void registerOnline(final InstanceType instanceType) {
- repository.persistEphemeral(ComputeNode.getOnlineInstanceNodePath(Instance.getInstance().getId(), instanceType), "");
+ public void registerOnline(final InstanceDefinition instanceDefinition) {
+ repository.persistEphemeral(ComputeNode.getOnlineInstanceNodePath(instanceDefinition.getInstanceId().getId(), instanceDefinition.getInstanceType()), "");
}
}
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/subscriber/ComputeNodeStatusSubscriber.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/subscriber/ComputeNodeStatus [...]
index e999f33..09f6ff4 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/subscriber/ComputeNodeStatusSubscriber.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/subscriber/ComputeNodeStatusSubscriber.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.stat
import com.google.common.base.Strings;
import com.google.common.eventbus.Subscribe;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
-import org.apache.shardingsphere.infra.instance.Instance;
+import org.apache.shardingsphere.infra.instance.InstanceId;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.ComputeNodeStatus;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.ComputeNodeStatusChangedEvent;
@@ -49,7 +49,7 @@ public final class ComputeNodeStatusSubscriber {
*/
@Subscribe
public void update(final ComputeNodeStatusChangedEvent event) {
- String computeStatusNodePath = ComputeNode.getInstanceStatusNodePath(Instance.getInstance().getInstanceId(event.getIp(), event.getPort()));
+ String computeStatusNodePath = ComputeNode.getInstanceStatusNodePath(new InstanceId(event.getIp(), Integer.valueOf(event.getPort())).getId());
String yamlContext = repository.get(computeStatusNodePath);
Collection<String> status = Strings.isNullOrEmpty(yamlContext) ? new ArrayList<>() : YamlEngine.unmarshal(yamlContext, Collection.class);
if (event.getStatus() == ComputeNodeStatus.CIRCUIT_BREAKER) {
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 280bf90..b0e486c 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
@@ -31,6 +31,7 @@ import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKe
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
import org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContext;
import org.apache.shardingsphere.infra.federation.optimizer.metadata.FederationSchemaMetaData;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
import org.apache.shardingsphere.infra.instance.InstanceType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
@@ -111,7 +112,7 @@ public final class ClusterContextManagerCoordinatorTest {
ModeConfiguration configuration = new ModeConfiguration("Cluster", persistRepositoryConfiguration, false);
ClusterContextManagerBuilder builder = new ClusterContextManagerBuilder();
contextManager = builder.build(ContextManagerBuilderParameter.builder().modeConfig(configuration).dataSourcesMap(new HashMap<>()).schemaRuleConfigs(new HashMap<>())
- .globalRuleConfigs(new LinkedList<>()).props(new Properties()).isOverwrite(false).instanceType(InstanceType.PROXY).build());
+ .globalRuleConfigs(new LinkedList<>()).props(new Properties()).isOverwrite(false).instanceDefinition(new InstanceDefinition(InstanceType.PROXY, 3307)).build());
contextManager.renewMetaDataContexts(new MetaDataContexts(contextManager.getMetaDataContexts().getMetaDataPersistService().get(), createMetaDataMap(), globalRuleMetaData,
mock(ExecutorEngine.class),
new ConfigurationProperties(new Properties()), createOptimizerContext()));
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/lock/service/LockRegistryServiceTest.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/service/LockRegistryServiceTest.java
index c0f231f..e9d07cc 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/service/LockRegistryServiceTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/service/LockRegistryServiceTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.service;
-import org.apache.shardingsphere.infra.instance.Instance;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.junit.Before;
import org.junit.Test;
@@ -41,7 +40,6 @@ public final class LockRegistryServiceTest {
@Before
public void setUp() throws ReflectiveOperationException {
- Instance.getInstance().init(3307);
lockRegistryService = new LockRegistryService(clusterPersistRepository);
Field field = lockRegistryService.getClass().getDeclaredField("repository");
field.setAccessible(true);
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/registry/status/compute/service/ComputeNodeStatusServiceTest.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusServi [...]
index c169b76..9098636 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusServiceTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusServiceTest.java
@@ -17,10 +17,9 @@
package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service;
-import org.apache.shardingsphere.infra.instance.Instance;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
import org.apache.shardingsphere.infra.instance.InstanceType;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -34,14 +33,10 @@ public final class ComputeNodeStatusServiceTest {
@Mock
private ClusterPersistRepository repository;
- @Before
- public void setUp() {
- Instance.getInstance().init(3307);
- }
-
@Test
public void assertRegisterOnline() {
- new ComputeNodeStatusService(repository).registerOnline(InstanceType.PROXY);
- verify(repository).persistEphemeral("/nodes/compute_nodes/online/proxy/" + Instance.getInstance().getId(), "");
+ InstanceDefinition instanceDefinition = new InstanceDefinition(InstanceType.PROXY, 3307);
+ new ComputeNodeStatusService(repository).registerOnline(instanceDefinition);
+ verify(repository).persistEphemeral("/nodes/compute_nodes/online/proxy/" + instanceDefinition.getInstanceId().getId(), "");
}
}
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/registry/status/compute/watcher/ComputeNodeStateChangedWatcherTest.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeState [...]
index b6c00c7..1e9a251 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChangedWatcherTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChangedWatcherTest.java
@@ -18,16 +18,12 @@
package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.watcher;
import org.apache.shardingsphere.infra.state.StateEvent;
-import org.apache.shardingsphere.infra.instance.Instance;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.ComputeNodeStatus;
import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent.Type;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
-import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Optional;
@@ -36,23 +32,6 @@ import static org.junit.Assert.assertTrue;
public final class ComputeNodeStateChangedWatcherTest {
- private String originalClusterInstanceId;
-
- @Before
- public void setUp() throws NoSuchFieldException, IllegalAccessException {
- originalClusterInstanceId = Instance.getInstance().getId();
- Field field = Instance.class.getDeclaredField("id");
- field.setAccessible(true);
- field.set(Instance.getInstance(), "127.0.0.1@3307");
- }
-
- @After
- public void tearDown() throws NoSuchFieldException, IllegalAccessException {
- Field field = Instance.class.getDeclaredField("id");
- field.setAccessible(true);
- field.set(Instance.getInstance(), originalClusterInstanceId);
- }
-
@Test
public void assertCreateEventWhenEnabled() {
Optional<StateEvent> actual = new ComputeNodeStateChangedWatcher().createGovernanceEvent(new DataChangedEvent("/nodes/compute_nodes/attributes/127.0.0.1@3307/status",
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTextTest.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTextTest.java
index 1470d8b..8a8ba44 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTextTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTextTest.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.mode.manager.standalone;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
+import org.apache.shardingsphere.infra.instance.InstanceType;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
@@ -59,7 +61,7 @@ public final class StandaloneContextManagerBuilderTextTest {
StandaloneContextManagerBuilder standaloneContextManagerBuilder = new StandaloneContextManagerBuilder();
ContextManager actual = standaloneContextManagerBuilder.build(ContextManagerBuilderParameter.builder().modeConfig(modeConfiguration)
.dataSourcesMap(dataSourceMap).schemaRuleConfigs(schemaRuleConfigs).globalRuleConfigs(globalRuleConfigurationCollection).props(props)
- .isOverwrite(false).port(1000).build());
+ .isOverwrite(false).instanceDefinition(new InstanceDefinition(InstanceType.PROXY, 3307)).build());
MetaDataContexts metaDataContexts = actual.getMetaDataContexts();
assertNotNull(metaDataContexts.getMetaDataMap().get(TEST_DATA_SOURCE_INNER_MAP));
assertNotNull(metaDataContexts.getExecutorEngine());
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 00dc9cf..7eb6bc9 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
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
import org.apache.shardingsphere.infra.config.datasource.pool.creator.DataSourcePoolCreatorUtil;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
import org.apache.shardingsphere.infra.instance.InstanceType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.yaml.config.swapper.mode.ModeConfigurationYamlSwapper;
@@ -77,8 +78,8 @@ public final class BootstrapInitializer {
boolean isOverwrite = null == modeConfig || modeConfig.isOverwrite();
Map<String, Map<String, DataSource>> dataSourcesMap = getDataSourcesMap(proxyConfig.getSchemaDataSources());
ContextManagerBuilderParameter parameter = ContextManagerBuilderParameter.builder().modeConfig(modeConfig).dataSourcesMap(dataSourcesMap).schemaRuleConfigs(proxyConfig.getSchemaRules())
- .globalRuleConfigs(proxyConfig.getGlobalRules()).props(proxyConfig.getProps()).isOverwrite(isOverwrite).port(port).labels(proxyConfig.getLabels())
- .instanceType(InstanceType.PROXY).build();
+ .globalRuleConfigs(proxyConfig.getGlobalRules()).props(proxyConfig.getProps()).isOverwrite(isOverwrite).labels(proxyConfig.getLabels())
+ .instanceDefinition(new InstanceDefinition(InstanceType.PROXY, port)).build();
ContextManager contextManager = ContextManagerBuilderFactory.newInstance(modeConfig).build(parameter);
ProxyContext.getInstance().init(contextManager);
}