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/10/30 07:30:54 UTC

[shardingsphere] branch master updated: Refactor ConsulRepository (#21848)

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 8504d6335ef Refactor ConsulRepository (#21848)
8504d6335ef is described below

commit 8504d6335efb3f044fe3649ff0be455e43ee0a2e
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Oct 30 15:30:48 2022 +0800

    Refactor ConsulRepository (#21848)
---
 .../mode/repository/cluster/consul/ConsulRepository.java     | 12 +++---------
 .../mode/repository/cluster/consul/ConsulRepositoryTest.java |  8 +++-----
 2 files changed, 6 insertions(+), 14 deletions(-)

diff --git a/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java b/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
index 50ced8431be..f7a501da8a8 100644
--- a/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
+++ b/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
@@ -25,6 +25,7 @@ import com.ecwid.consul.v1.kv.model.GetValue;
 import com.ecwid.consul.v1.kv.model.PutParams;
 import com.ecwid.consul.v1.session.model.NewSession;
 import com.ecwid.consul.v1.session.model.Session;
+import com.google.common.base.Strings;
 import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
 import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
 import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
@@ -61,13 +62,7 @@ public class ConsulRepository implements ClusterPersistRepository {
     
     @Override
     public void init(final ClusterPersistRepositoryConfiguration config, final InstanceMetaData instanceMetaData) {
-        ConsulRawClient rawClient;
-        String serverList = config.getServerLists();
-        if (serverList == null || serverList.length() <= 0) {
-            rawClient = new ConsulRawClient();
-        } else {
-            rawClient = new ConsulRawClient(serverList);
-        }
+        ConsulRawClient rawClient = Strings.isNullOrEmpty(config.getServerLists()) ? new ConsulRawClient() : new ConsulRawClient(config.getServerLists());
         consulClient = new ShardingSphereConsulClient(rawClient);
         consulProps = new ConsulProperties(config.getProps());
         consulDistributedLockHolder = new ConsulDistributedLockHolder(consulClient, consulProps);
@@ -88,8 +83,7 @@ public class ConsulRepository implements ClusterPersistRepository {
     
     @Override
     public boolean isExisted(final String key) {
-        Response<GetValue> response = consulClient.getKVValue(key);
-        return response.getValue() == null ? false : true;
+        return null != consulClient.getKVValue(key).getValue();
     }
     
     @Override
diff --git a/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java b/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
index b9a9c97f3f8..d713df3e801 100644
--- a/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
+++ b/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
@@ -134,7 +134,7 @@ public final class ConsulRepositoryTest {
     }
     
     @Test
-    public void assertPersistEphemeral() throws InterruptedException {
+    public void assertPersistEphemeral() {
         repository.persistEphemeral("key1", "value1");
         verify(client).sessionCreate(any(NewSession.class), any(QueryParams.class));
         verify(client).setKVValue(any(String.class), any(String.class), any(PutParams.class));
@@ -158,8 +158,7 @@ public final class ConsulRepositoryTest {
             try {
                 verify(client, atLeastOnce()).getKVValues(any(String.class), any(QueryParams.class));
                 break;
-            } catch (MockitoException e) {
-                continue;
+            } catch (final MockitoException ignored) {
             }
         }
     }
@@ -185,8 +184,7 @@ public final class ConsulRepositoryTest {
             try {
                 verify(client, atLeastOnce()).getKVValues(any(String.class), any(QueryParams.class));
                 break;
-            } catch (MockitoException e) {
-                continue;
+            } catch (final MockitoException ignored) {
             }
         }
     }