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/01/14 12:01:53 UTC

[shardingsphere] branch master updated: support custom props for ClassBasedShardingAlgorithm (#14784)

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

zhangliang 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 5bbdd6b  support custom props for ClassBasedShardingAlgorithm (#14784)
5bbdd6b is described below

commit 5bbdd6b5149857c3847f78a55b2723982237c00d
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Fri Jan 14 20:01:08 2022 +0800

    support custom props for ClassBasedShardingAlgorithm (#14784)
    
    * support custom props for ClassBasedShardingAlgorithm
    
    * update java doc
---
 .../ClassBasedStandardShardingAlgorithmFixture.java  | 20 ++++++++++++++++++--
 .../main/resources/META-INF/sharding-databases.yaml  |  1 +
 .../classbased/ClassBasedShardingAlgorithm.java      |  6 +++---
 .../ClassBasedShardingAlgorithmFactory.java          | 20 +++++++++++++++++---
 .../registry/workerid/node/WorkerIdNode.java         | 17 +++++++++++++++++
 5 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/fixture/ClassBasedStandardShardingAlgorithmFixture.java b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardin [...]
index 7125216..099a291 100644
--- a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/fixture/ClassBasedStandardShardingAlgorithmFixture.java
+++ b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/fixture/ClassBasedStandardShardingAlgorithmFixture.java
@@ -17,22 +17,38 @@
 
 package org.apache.shardingsphere.example.extension.sharding.algortihm.classbased.fixture;
 
+import com.google.common.base.Preconditions;
+import com.google.common.primitives.Ints;
+import lombok.Getter;
+import lombok.Setter;
 import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
 import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
 import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
 
 import java.util.Collection;
+import java.util.Properties;
 
+@Getter
+@Setter
 public final class ClassBasedStandardShardingAlgorithmFixture implements StandardShardingAlgorithm<Integer> {
-
+    
+    private static final String SHARDING_COUNT = "sharding-count";
+    
+    private Integer shardingCount;
+    
+    private Properties props = new Properties();
+    
     @Override
     public void init() {
+        Preconditions.checkArgument(props.containsKey(SHARDING_COUNT), "%s can not be null.", SHARDING_COUNT);
+        shardingCount = Ints.tryParse(props.getProperty(SHARDING_COUNT));
+        Preconditions.checkArgument(null != shardingCount, "%s is not valid.", SHARDING_COUNT);
     }
 
     @Override
     public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {
         for (String each : availableTargetNames) {
-            if (each.endsWith(String.valueOf(shardingValue.getValue() % 2))) {
+            if (each.endsWith(String.valueOf(shardingValue.getValue() % shardingCount))) {
                 return each;
             }
         }
diff --git a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/META-INF/sharding-databases.yaml b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/META-INF/sharding-databases.yaml
index 92c7796..3fcb816 100644
--- a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/META-INF/sharding-databases.yaml
+++ b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/META-INF/sharding-databases.yaml
@@ -59,6 +59,7 @@ rules:
       props:
         strategy: standard
         algorithmClassName: org.apache.shardingsphere.example.extension.sharding.algortihm.classbased.fixture.ClassBasedStandardShardingAlgorithmFixture
+        sharding-count: 2
     
   keyGenerators:
     snowflake:
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java
index 7e743d6..e377481 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java
@@ -69,13 +69,13 @@ public final class ClassBasedShardingAlgorithm implements StandardShardingAlgori
     private void createAlgorithmInstance() {
         switch (strategy) {
             case STANDARD:
-                standardShardingAlgorithm = ClassBasedShardingAlgorithmFactory.newInstance(algorithmClassName, StandardShardingAlgorithm.class);
+                standardShardingAlgorithm = ClassBasedShardingAlgorithmFactory.newInstance(algorithmClassName, StandardShardingAlgorithm.class, props);
                 break;
             case COMPLEX:
-                complexKeysShardingAlgorithm = ClassBasedShardingAlgorithmFactory.newInstance(algorithmClassName, ComplexKeysShardingAlgorithm.class);
+                complexKeysShardingAlgorithm = ClassBasedShardingAlgorithmFactory.newInstance(algorithmClassName, ComplexKeysShardingAlgorithm.class, props);
                 break;
             case HINT:
-                hintShardingAlgorithm = ClassBasedShardingAlgorithmFactory.newInstance(algorithmClassName, HintShardingAlgorithm.class);
+                hintShardingAlgorithm = ClassBasedShardingAlgorithmFactory.newInstance(algorithmClassName, HintShardingAlgorithm.class, props);
                 break;
             default:
                 break;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
index 2bdfb55..30c1223 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
@@ -22,30 +22,44 @@ import lombok.NoArgsConstructor;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+import org.apache.shardingsphere.spi.typed.TypedSPI;
+
+import java.util.Properties;
 
 /**
  * ShardingSphere class based algorithm factory.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class ClassBasedShardingAlgorithmFactory {
-
+    
     /**
      * Create sharding algorithm.
-     *
+     * 
      * @param shardingAlgorithmClassName sharding algorithm class name
      * @param superShardingAlgorithmClass sharding algorithm super class
+     * @param props properties
      * @param <T> class generic type
      * @return sharding algorithm instance
      */
     @SneakyThrows
     @SuppressWarnings("unchecked")
-    public static <T extends ShardingAlgorithm> T newInstance(final String shardingAlgorithmClassName, final Class<T> superShardingAlgorithmClass) {
+    public static <T extends ShardingAlgorithm> T newInstance(final String shardingAlgorithmClassName, final Class<T> superShardingAlgorithmClass, final Properties props) {
         Class<?> result = Class.forName(shardingAlgorithmClassName);
         if (!superShardingAlgorithmClass.isAssignableFrom(result)) {
             throw new ShardingSphereException("Class %s should be implement %s", shardingAlgorithmClassName, superShardingAlgorithmClass.getName());
         }
         T instance = (T) result.newInstance();
+        setProperties(instance, props);
         instance.init();
         return instance;
     }
+    
+    private static <T extends TypedSPI> void setProperties(final T instance, final Properties props) {
+        if (null == props) {
+            return;
+        }
+        Properties newProps = new Properties();
+        props.forEach((key, value) -> newProps.setProperty(key.toString(), null == value ? null : value.toString()));
+        instance.setProps(newProps);
+    }
 }
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/workerid/node/WorkerIdNode.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/workerid/node/WorkerIdNode.java
index e7c16df..7a4eba3 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/workerid/node/WorkerIdNode.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/workerid/node/WorkerIdNode.java
@@ -1,3 +1,20 @@
+/*
+ * 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.mode.manager.cluster.coordinator.registry.workerid.node;
 
 /**