You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/11/21 08:59:26 UTC

[shardingsphere] branch master updated: Add properties check when registering storage nodes (#22270)

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

jianglongtao 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 66af573dca8 Add properties check when registering storage nodes (#22270)
66af573dca8 is described below

commit 66af573dca8303ad96a62d00ca34a70b1cda8dcc
Author: jiangML <10...@qq.com>
AuthorDate: Mon Nov 21 16:59:20 2022 +0800

    Add properties check when registering storage nodes (#22270)
    
    * Add validation for datasource properties
    
    * add final
    
    * optimize code
    
    * add final for DefaultDataSourcePoolPropertiesValidator
    
    * optimize code
---
 .../pool/metadata/DataSourcePoolMetaData.java      |   8 ++
 .../DataSourcePoolPropertiesValidator.java         |  33 +++++++
 .../DefaultDataSourcePoolPropertiesValidator.java  |  30 ++++++
 .../type/c3p0/C3P0DataSourcePoolMetaData.java      |   7 ++
 .../type/dbcp/DBCPDataSourcePoolMetaData.java      |   7 ++
 .../type/hikari/HikariDataSourcePoolMetaData.java  |   6 ++
 .../HikariDataSourcePoolPropertiesValidator.java   | 104 +++++++++++++++++++++
 .../props/DataSourcePropertiesValidator.java       |  22 ++++-
 .../fixture/MockedDataSourcePoolMetaData.java      |   7 ++
 .../metadata/MockedDataSourcePoolMetaData.java     |   7 ++
 10 files changed, 229 insertions(+), 2 deletions(-)

diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaData.java
index e5519cfb59d..5176e0266ea 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaData.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaData.java
@@ -63,4 +63,12 @@ public interface DataSourcePoolMetaData extends TypedSPI {
      * @return data source pool field meta data
      */
     DataSourcePoolFieldMetaData getFieldMetaData();
+    
+    /**
+     * Get data source pool properties validator.
+     * 
+     * @return data source pool properties validator
+     */
+    DataSourcePoolPropertiesValidator getDataSourcePoolPropertiesValidator();
+    
 }
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolPropertiesValidator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolPropertiesValidator.java
new file mode 100644
index 00000000000..f7507dbb956
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolPropertiesValidator.java
@@ -0,0 +1,33 @@
+/*
+ * 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.pool.metadata;
+
+import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
+
+/**
+ * Data source pool properties validator.
+ */
+public interface DataSourcePoolPropertiesValidator {
+    
+    /**
+     * Check properties.
+     * 
+     * @param dataSourceProps Data source properties
+     */
+    void validateProperties(DataSourceProperties dataSourceProps);
+}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DefaultDataSourcePoolPropertiesValidator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DefaultDataSourcePoolPropertiesValidator.java
new file mode 100644
index 00000000000..2c97893ddc1
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DefaultDataSourcePoolPropertiesValidator.java
@@ -0,0 +1,30 @@
+/*
+ * 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.pool.metadata;
+
+import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
+
+/**
+ * Default data source pool properties validator.
+ */
+public final class DefaultDataSourcePoolPropertiesValidator implements DataSourcePoolPropertiesValidator {
+    
+    @Override
+    public void validateProperties(final DataSourceProperties dataSourceProps) {
+    }
+}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/c3p0/C3P0DataSourcePoolMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/c3p0/C3P0DataSourcePoolMetaData.java
index ea8a01988e5..e3155cbeb6f 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/c3p0/C3P0DataSourcePoolMetaData.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/c3p0/C3P0DataSourcePoolMetaData.java
@@ -18,6 +18,8 @@
 package org.apache.shardingsphere.infra.datasource.pool.metadata.type.c3p0;
 
 import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolPropertiesValidator;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DefaultDataSourcePoolPropertiesValidator;
 
 import java.util.Collection;
 import java.util.HashMap;
@@ -100,4 +102,9 @@ public final class C3P0DataSourcePoolMetaData implements DataSourcePoolMetaData
     public String getType() {
         return "com.mchange.v2.c3p0.ComboPooledDataSource";
     }
+    
+    @Override
+    public DataSourcePoolPropertiesValidator getDataSourcePoolPropertiesValidator() {
+        return new DefaultDataSourcePoolPropertiesValidator();
+    }
 }
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolMetaData.java
index 30fd3d77a0c..1adda2673e4 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolMetaData.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolMetaData.java
@@ -18,6 +18,8 @@
 package org.apache.shardingsphere.infra.datasource.pool.metadata.type.dbcp;
 
 import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolPropertiesValidator;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DefaultDataSourcePoolPropertiesValidator;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -74,4 +76,9 @@ public final class DBCPDataSourcePoolMetaData implements DataSourcePoolMetaData
     public Collection<String> getTypeAliases() {
         return Arrays.asList("org.apache.commons.dbcp.BasicDataSource", "org.apache.tomcat.dbcp.dbcp2.BasicDataSource");
     }
+    
+    @Override
+    public DataSourcePoolPropertiesValidator getDataSourcePoolPropertiesValidator() {
+        return new DefaultDataSourcePoolPropertiesValidator();
+    }
 }
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolMetaData.java
index f04a244a059..afcd74d1561 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolMetaData.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolMetaData.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.infra.datasource.pool.metadata.type.hikari;
 
 import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolPropertiesValidator;
 
 import java.util.Collection;
 import java.util.HashMap;
@@ -99,6 +100,11 @@ public final class HikariDataSourcePoolMetaData implements DataSourcePoolMetaDat
         return new HikariDataSourcePoolFieldMetaData();
     }
     
+    @Override
+    public DataSourcePoolPropertiesValidator getDataSourcePoolPropertiesValidator() {
+        return new HikariDataSourcePoolPropertiesValidator();
+    }
+    
     @Override
     public String getType() {
         return "com.zaxxer.hikari.HikariDataSource";
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolPropertiesValidator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolPropertiesValidator.java
new file mode 100644
index 00000000000..fb5576e1d51
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolPropertiesValidator.java
@@ -0,0 +1,104 @@
+/*
+ * 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.pool.metadata.type.hikari;
+
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolPropertiesValidator;
+import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Hikari data source pool properties validator.
+ */
+public final class HikariDataSourcePoolPropertiesValidator implements DataSourcePoolPropertiesValidator {
+    
+    private static final long CONNECTION_TIMEOUT_FLOOR = 250L;
+    
+    private static final long MAX_LIFETIME_FLOOR = TimeUnit.SECONDS.toMillis(30);
+    
+    private static final long KEEP_ALIVE_TIME_FLOOR = TimeUnit.SECONDS.toMillis(30);
+    
+    @Override
+    public void validateProperties(final DataSourceProperties dataSourceProps) throws IllegalArgumentException {
+        validateConnectionTimeout(dataSourceProps);
+        validateIdleTimeout(dataSourceProps);
+        validateMaxLifetime(dataSourceProps);
+        validateMaximumPoolSize(dataSourceProps);
+        validateMinimumIdle(dataSourceProps);
+        validateKeepaliveTime(dataSourceProps);
+    }
+    
+    private void validateConnectionTimeout(final DataSourceProperties dataSourceProps) {
+        if (!checkValueExist(dataSourceProps, "connectionTimeout")) {
+            return;
+        }
+        long connectionTimeout = Long.parseLong(dataSourceProps.getAllLocalProperties().get("connectionTimeout").toString());
+        ShardingSpherePreconditions.checkState(connectionTimeout >= CONNECTION_TIMEOUT_FLOOR,
+                () -> new IllegalArgumentException(String.format("connectionTimeout cannot be less than %sms", CONNECTION_TIMEOUT_FLOOR)));
+    }
+    
+    private void validateIdleTimeout(final DataSourceProperties dataSourceProps) {
+        if (!checkValueExist(dataSourceProps, "idleTimeout")) {
+            return;
+        }
+        long idleTimeout = Long.parseLong(dataSourceProps.getAllLocalProperties().get("idleTimeout").toString());
+        ShardingSpherePreconditions.checkState(idleTimeout >= 0, () -> new IllegalArgumentException("idleTimeout cannot be negative"));
+    }
+    
+    private void validateMaxLifetime(final DataSourceProperties dataSourceProps) {
+        if (!checkValueExist(dataSourceProps, "maxLifetime")) {
+            return;
+        }
+        long maxLifetime = Long.parseLong(dataSourceProps.getAllLocalProperties().get("maxLifetime").toString());
+        ShardingSpherePreconditions.checkState(maxLifetime >= MAX_LIFETIME_FLOOR, () -> new IllegalArgumentException(String.format("maxLifetime cannot be less than %sms", MAX_LIFETIME_FLOOR)));
+    }
+    
+    private void validateMaximumPoolSize(final DataSourceProperties dataSourceProps) {
+        if (!checkValueExist(dataSourceProps, "maximumPoolSize")) {
+            return;
+        }
+        int maximumPoolSize = Integer.parseInt(dataSourceProps.getAllLocalProperties().get("maximumPoolSize").toString());
+        ShardingSpherePreconditions.checkState(maximumPoolSize >= 1, () -> new IllegalArgumentException("maxPoolSize cannot be less than 1"));
+    }
+    
+    private void validateMinimumIdle(final DataSourceProperties dataSourceProps) {
+        if (!checkValueExist(dataSourceProps, "minimumIdle")) {
+            return;
+        }
+        int minimumIdle = Integer.parseInt(dataSourceProps.getAllLocalProperties().get("minimumIdle").toString());
+        ShardingSpherePreconditions.checkState(minimumIdle >= 0, () -> new IllegalArgumentException("minimumIdle cannot be negative"));
+    }
+    
+    private void validateKeepaliveTime(final DataSourceProperties dataSourceProps) {
+        if (!checkValueExist(dataSourceProps, "keepaliveTime")) {
+            return;
+        }
+        int keepaliveTime = Integer.parseInt(dataSourceProps.getAllLocalProperties().get("keepaliveTime").toString());
+        if (keepaliveTime == 0) {
+            return;
+        }
+        ShardingSpherePreconditions.checkState(keepaliveTime >= KEEP_ALIVE_TIME_FLOOR,
+                () -> new IllegalArgumentException(String.format("keepaliveTime cannot be less than %sms", KEEP_ALIVE_TIME_FLOOR)));
+    }
+    
+    private boolean checkValueExist(final DataSourceProperties dataSourceProps, final String key) {
+        return dataSourceProps.getAllLocalProperties().containsKey(key) && Objects.nonNull(dataSourceProps.getAllLocalProperties().get(key));
+    }
+}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
index 3fd07a9a905..12291c23d44 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
@@ -19,6 +19,9 @@ package org.apache.shardingsphere.infra.datasource.props;
 
 import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
 import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaDataFactory;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolPropertiesValidator;
 import org.apache.shardingsphere.infra.distsql.exception.resource.InvalidResourcesException;
 
 import javax.sql.DataSource;
@@ -27,6 +30,7 @@ import java.util.Collection;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 
 /**
  * Data source properties validator.
@@ -43,7 +47,8 @@ public final class DataSourcePropertiesValidator {
         Collection<String> errorMessages = new LinkedList<>();
         for (Entry<String, DataSourceProperties> entry : dataSourcePropertiesMap.entrySet()) {
             try {
-                validate(entry.getKey(), entry.getValue());
+                validateProperties(entry.getKey(), entry.getValue());
+                validateConnection(entry.getKey(), entry.getValue());
             } catch (final InvalidDataSourcePropertiesException ex) {
                 errorMessages.add(ex.getMessage());
             }
@@ -53,7 +58,20 @@ public final class DataSourcePropertiesValidator {
         }
     }
     
-    private void validate(final String dataSourceName, final DataSourceProperties dataSourceProps) throws InvalidDataSourcePropertiesException {
+    private void validateProperties(final String dataSourceName, final DataSourceProperties dataSourceProps) throws InvalidDataSourcePropertiesException {
+        Optional<DataSourcePoolMetaData> poolMetaData = DataSourcePoolMetaDataFactory.findInstance(dataSourceProps.getDataSourceClassName());
+        if (!poolMetaData.isPresent()) {
+            return;
+        }
+        try {
+            DataSourcePoolPropertiesValidator propertiesValidator = poolMetaData.get().getDataSourcePoolPropertiesValidator();
+            propertiesValidator.validateProperties(dataSourceProps);
+        } catch (final IllegalArgumentException ex) {
+            throw new InvalidDataSourcePropertiesException(dataSourceName, ex.getMessage());
+        }
+    }
+    
+    private void validateConnection(final String dataSourceName, final DataSourceProperties dataSourceProps) throws InvalidDataSourcePropertiesException {
         DataSource dataSource = null;
         try {
             dataSource = DataSourcePoolCreator.create(dataSourceProps);
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolMetaData.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolMetaData.java
index cd53a7bf19a..ddd991059ef 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolMetaData.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolMetaData.java
@@ -18,6 +18,8 @@
 package org.apache.shardingsphere.infra.datasource.pool.metadata.fixture;
 
 import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolPropertiesValidator;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DefaultDataSourcePoolPropertiesValidator;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -57,6 +59,11 @@ public final class MockedDataSourcePoolMetaData implements DataSourcePoolMetaDat
         return new MockedDataSourcePoolFieldMetaData();
     }
     
+    @Override
+    public DataSourcePoolPropertiesValidator getDataSourcePoolPropertiesValidator() {
+        return new DefaultDataSourcePoolPropertiesValidator();
+    }
+    
     @Override
     public String getType() {
         return "org.apache.shardingsphere.test.mock.MockedDataSource";
diff --git a/test/fixture/src/main/java/org/apache/shardingsphere/test/fixture/datasource/pool/metadata/MockedDataSourcePoolMetaData.java b/test/fixture/src/main/java/org/apache/shardingsphere/test/fixture/datasource/pool/metadata/MockedDataSourcePoolMetaData.java
index 44a7bd64033..5e4ea7d66ef 100644
--- a/test/fixture/src/main/java/org/apache/shardingsphere/test/fixture/datasource/pool/metadata/MockedDataSourcePoolMetaData.java
+++ b/test/fixture/src/main/java/org/apache/shardingsphere/test/fixture/datasource/pool/metadata/MockedDataSourcePoolMetaData.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.test.fixture.datasource.pool.metadata;
 
 import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolFieldMetaData;
 import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolPropertiesValidator;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DefaultDataSourcePoolPropertiesValidator;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -61,6 +63,11 @@ public final class MockedDataSourcePoolMetaData implements DataSourcePoolMetaDat
         return new MockedDataSourcePoolFieldMetaData();
     }
     
+    @Override
+    public DataSourcePoolPropertiesValidator getDataSourcePoolPropertiesValidator() {
+        return new DefaultDataSourcePoolPropertiesValidator();
+    }
+    
     @Override
     public String getType() {
         return "org.apache.shardingsphere.test.mock.MockedDataSource";