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/06/30 12:43:25 UTC

[shardingsphere] branch master updated: Remove uniqueSign of InstanceDefinition (#18740)

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 8ed6a8ca5d4 Remove uniqueSign of InstanceDefinition (#18740)
8ed6a8ca5d4 is described below

commit 8ed6a8ca5d4d8f2bd0a46400f864a5cbc1b41765
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Thu Jun 30 20:43:18 2022 +0800

    Remove uniqueSign of InstanceDefinition (#18740)
---
 .../instance/definition/InstanceDefinition.java    | 23 +++++-----
 .../definition/InstanceDefinitionTest.java         | 52 ++++++++++++++++++++++
 .../jdbc/core/connection/ConnectionManager.java    |  2 +-
 .../ClusterContextManagerCoordinatorTest.java      |  2 +-
 .../ral/common/queryable/ShowInstanceHandler.java  |  2 +-
 5 files changed, 65 insertions(+), 16 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/definition/InstanceDefinition.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/definition/InstanceDefinition.java
index b30c8bc8a08..db7f5fa656e 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/definition/InstanceDefinition.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/definition/InstanceDefinition.java
@@ -19,12 +19,11 @@ package org.apache.shardingsphere.infra.instance.definition;
 
 import com.google.common.base.Joiner;
 import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
 import lombok.Getter;
 import org.apache.shardingsphere.infra.instance.utils.IpUtils;
 
-import java.lang.management.ManagementFactory;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * Instance definition.
@@ -34,36 +33,34 @@ public final class InstanceDefinition {
     
     private static final String DELIMITER = "@";
     
-    private static final AtomicLong COUNTER = new AtomicLong();
-    
     private final InstanceType instanceType;
     
     private final String instanceId;
     
-    private final String ip;
+    private String ip;
     
-    private final String uniqueSign;
+    private int port;
     
     public InstanceDefinition(final String instanceId) {
         instanceType = InstanceType.JDBC;
         this.instanceId = instanceId;
-        ip = IpUtils.getIp();
-        uniqueSign = String.join("", ManagementFactory.getRuntimeMXBean().getName().split(DELIMITER)[0], String.valueOf(COUNTER.incrementAndGet()));
     }
     
     public InstanceDefinition(final int port, final String instanceId) {
         instanceType = InstanceType.PROXY;
         this.instanceId = instanceId;
         ip = IpUtils.getIp();
-        uniqueSign = String.valueOf(port);
+        this.port = port;
     }
     
     public InstanceDefinition(final InstanceType instanceType, final String instanceId, final String attributes) {
         this.instanceType = instanceType;
         this.instanceId = instanceId;
-        List<String> attributesList = Splitter.on(DELIMITER).splitToList(attributes);
-        ip = attributesList.get(0);
-        uniqueSign = attributesList.get(1);
+        if (!Strings.isNullOrEmpty(attributes) && attributes.contains(DELIMITER)) {
+            List<String> attributesList = Splitter.on(DELIMITER).splitToList(attributes);
+            ip = attributesList.get(0);
+            port = Integer.valueOf(attributesList.get(1));
+        }
     }
     
     /**
@@ -72,6 +69,6 @@ public final class InstanceDefinition {
      * @return got instance attributes, format is ip@uniqueSign
      */
     public String getAttributes() {
-        return Joiner.on(DELIMITER).join(ip, uniqueSign);
+        return instanceType == InstanceType.JDBC ? "" : Joiner.on(DELIMITER).join(ip, port);
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/definition/InstanceDefinitionTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/definition/InstanceDefinitionTest.java
new file mode 100644
index 00000000000..e5557597b96
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/definition/InstanceDefinitionTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.definition;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class InstanceDefinitionTest {
+    
+    @Test
+    public void assertJdbcType() {
+        InstanceDefinition instanceDefinition = new InstanceDefinition("foo_instance_id");
+        assertThat(instanceDefinition.getInstanceId(), is("foo_instance_id"));
+        assertThat(instanceDefinition.getInstanceType(), is(InstanceType.JDBC));
+        assertThat(instanceDefinition.getAttributes(), is(""));
+    }
+    
+    @Test
+    public void assertProxyType() {
+        InstanceDefinition instanceDefinition = new InstanceDefinition(3307, "foo_instance_id");
+        assertThat(instanceDefinition.getInstanceId(), is("foo_instance_id"));
+        assertThat(instanceDefinition.getInstanceType(), is(InstanceType.PROXY));
+        assertThat(instanceDefinition.getPort(), is(3307));
+    }
+    
+    @Test
+    public void assertAttributes() {
+        InstanceDefinition instanceDefinition = new InstanceDefinition(InstanceType.PROXY, "foo_instance_id", "127.0.0.1@3307");
+        assertThat(instanceDefinition.getInstanceId(), is("foo_instance_id"));
+        assertThat(instanceDefinition.getInstanceType(), is(InstanceType.PROXY));
+        assertThat(instanceDefinition.getIp(), is("127.0.0.1"));
+        assertThat(instanceDefinition.getPort(), is(3307));
+        assertThat(instanceDefinition.getAttributes(), is("127.0.0.1@3307"));
+    }
+}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
index 34188503292..67acffb0307 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
@@ -119,7 +119,7 @@ public final class ConnectionManager implements ExecutorJDBCConnectionManager, A
         String jdbcUrl = String.valueOf(props.get("jdbcUrl"));
         String jdbcUrlPrefix = jdbcUrl.substring(0, jdbcUrl.indexOf("//"));
         String jdbcUrlSuffix = jdbcUrl.contains("?") ? jdbcUrl.substring(jdbcUrl.indexOf("?")) : "";
-        return String.format("%s//%s:%s/%s%s", jdbcUrlPrefix, instanceDefinition.getIp(), instanceDefinition.getUniqueSign(), schema, jdbcUrlSuffix);
+        return String.format("%s//%s:%s/%s%s", jdbcUrlPrefix, instanceDefinition.getIp(), instanceDefinition.getPort(), schema, jdbcUrlSuffix);
     }
     
     private ConnectionTransaction createConnectionTransaction(final String databaseName, final ContextManager contextManager) {
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 fceeda1fc89..0c6ddf8ec5a 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
@@ -315,7 +315,7 @@ public final class ClusterContextManagerCoordinatorTest {
     @Test
     public void assertRenewInstanceOfflineEvent() {
         coordinator.renew(new InstanceOfflineEvent(contextManager.getInstanceContext().getInstance().getInstanceDefinition()));
-        assertThat(contextManager.getInstanceContext().getInstance().getInstanceDefinition().getUniqueSign(), is("3307"));
+        assertThat(contextManager.getInstanceContext().getInstance().getInstanceDefinition().getPort(), is(3307));
     }
     
     @Test
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/ShowInstanceHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/ShowInstanceHandler.java
index 35a2d271602..3053f9476ba 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/ShowInstanceHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/ShowInstanceHandler.java
@@ -66,6 +66,6 @@ public final class ShowInstanceHandler extends QueryableRALBackendHandler<ShowIn
     private LocalDataQueryResultRow buildRow(final ComputeNodeInstance instance, final String modeType) {
         String labels = null == instance.getLabels() ? "" : String.join(",", instance.getLabels());
         return new LocalDataQueryResultRow(instance.getInstanceDefinition().getInstanceId(),
-                instance.getInstanceDefinition().getIp(), instance.getInstanceDefinition().getUniqueSign(), instance.getState().getCurrentState().name(), modeType, labels);
+                instance.getInstanceDefinition().getIp(), instance.getInstanceDefinition().getPort(), instance.getState().getCurrentState().name(), modeType, labels);
     }
 }