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/10/03 15:17:23 UTC

[shardingsphere] branch master updated: Simplify DataSourceState (#21333)

This is an automated email from the ASF dual-hosted git repository.

zhaojinchao 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 7bf6ec606e4 Simplify DataSourceState (#21333)
7bf6ec606e4 is described below

commit 7bf6ec606e409db7917cb730463b1666bfed683a
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Oct 3 23:17:14 2022 +0800

    Simplify DataSourceState (#21333)
---
 .../infra/datasource/state/DataSourceState.java    | 26 +---------
 .../datasource/state/DataSourceStateTest.java      | 57 ----------------------
 .../cluster/ClusterContextManagerBuilder.java      |  2 +-
 .../ClusterContextManagerCoordinator.java          |  9 ++--
 4 files changed, 6 insertions(+), 88 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceState.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceState.java
index 9eaf5020dc2..8671cb625a1 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceState.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceState.java
@@ -17,34 +17,10 @@
 
 package org.apache.shardingsphere.infra.datasource.state;
 
-import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
-
-import java.util.HashMap;
-import java.util.Map;
-
 /**
  * Data source state.
  */
 public enum DataSourceState {
     
-    DISABLED, ENABLED;
-    
-    private static final Map<String, DataSourceState> DATA_SOURCE_STATES = new HashMap<>(2, 1);
-    
-    static {
-        DATA_SOURCE_STATES.put(DISABLED.name().toLowerCase(), DISABLED);
-        DATA_SOURCE_STATES.put(ENABLED.name().toLowerCase(), ENABLED);
-    }
-    
-    /**
-     * Get data source state by state name.
-     *
-     * @param state data source state name
-     * @return data source state
-     */
-    public static DataSourceState getDataSourceState(final String state) {
-        Preconditions.checkArgument(!Strings.isNullOrEmpty(state) && DATA_SOURCE_STATES.containsKey(state.toLowerCase()), "Illegal data source state `%s`", state);
-        return DATA_SOURCE_STATES.get(state.toLowerCase());
-    }
+    DISABLED, ENABLED
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateTest.java
deleted file mode 100644
index 4c8c91bb595..00000000000
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateTest.java
+++ /dev/null
@@ -1,57 +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.datasource.state;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
-
-public class DataSourceStateTest {
-
-    @Test
-    public void testDisabled() {
-        assertEquals(DataSourceState.DISABLED, DataSourceState.getDataSourceState("disabled"));
-    }
-
-    @Test
-    public void testEnabled() {
-        assertEquals(DataSourceState.ENABLED, DataSourceState.getDataSourceState("enabled"));
-    }
-
-    @Test
-    public void shouldThrowIllegalArgumentExceptionWhenStateIsEmpty() {
-        assertThrows("Illegal data source state ``", IllegalArgumentException.class, () -> {
-            DataSourceState.getDataSourceState("");
-        });
-    }
-
-    @Test
-    public void shouldThrowIllegalArgumentExceptionWhenStateIsNull() {
-        assertThrows("Illegal data source state ``", IllegalArgumentException.class, () -> {
-            DataSourceState.getDataSourceState(null);
-        });
-    }
-
-    @Test
-    public void shouldThrowIllegalArgumentExceptionWhenStateIsInvalid() {
-        assertThrows("Illegal data source state `invalid`", IllegalArgumentException.class, () -> {
-            DataSourceState.getDataSourceState("invalid");
-        });
-    }
-}
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 d3faa5230d5..2063789473f 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
@@ -88,7 +88,7 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
             Preconditions.checkArgument(3 == values.size(), "Illegal data source of storage node.");
             String databaseName = values.get(0);
             String dataSourceName = values.get(2);
-            result.put(databaseName + "." + dataSourceName, DataSourceState.getDataSourceState(value.getStatus()));
+            result.put(databaseName + "." + dataSourceName, DataSourceState.valueOf(value.getStatus().toUpperCase()));
         });
         return result;
     }
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 a4e012487c5..87c9f32f1cd 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
@@ -93,10 +93,9 @@ public final class ClusterContextManagerCoordinator {
      * Renew to persist meta data.
      *
      * @param event database added event
-     * @throws SQLException SQL exception
      */
     @Subscribe
-    public synchronized void renew(final DatabaseAddedEvent event) throws SQLException {
+    public synchronized void renew(final DatabaseAddedEvent event) {
         contextManager.addDatabase(event.getDatabaseName());
     }
     
@@ -197,10 +196,10 @@ public final class ClusterContextManagerCoordinator {
         }
         Optional<ShardingSphereRule> staticDataSourceRule = contextManager.getMetaDataContexts().getMetaData().getDatabase(qualifiedDatabase.getDatabaseName()).getRuleMetaData()
                 .getRules().stream().filter(each -> each instanceof StaticDataSourceContainedRule).findFirst();
-        staticDataSourceRule.ifPresent(shardingSphereRule -> ((StaticDataSourceContainedRule) shardingSphereRule)
+        staticDataSourceRule.ifPresent(optional -> ((StaticDataSourceContainedRule) optional)
                 .updateStatus(new StorageNodeDataSourceChangedEvent(qualifiedDatabase, event.getDataSource())));
-        DataSourceStateManager.getInstance().updateState(qualifiedDatabase.getDatabaseName(), qualifiedDatabase.getDataSourceName(),
-                DataSourceState.getDataSourceState(event.getDataSource().getStatus()));
+        DataSourceStateManager.getInstance().updateState(
+                qualifiedDatabase.getDatabaseName(), qualifiedDatabase.getDataSourceName(), DataSourceState.valueOf(event.getDataSource().getStatus().toUpperCase()));
     }
     
     /**