You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/02/23 11:13:56 UTC

[shardingsphere] branch master updated: Add RunnerParameters to simplify ParallelRunnerScheduler (#9474)

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

menghaoran 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 0949622  Add RunnerParameters to simplify ParallelRunnerScheduler (#9474)
0949622 is described below

commit 0949622974e44a176ad71d97131ce10cd1f88466
Author: Liang Zhang <te...@163.com>
AuthorDate: Tue Feb 23 19:13:20 2021 +0800

    Add RunnerParameters to simplify ParallelRunnerScheduler (#9474)
    
    * Cleanup code
    
    * Refactor ParallelRunnerScheduler
    
    * Add RunnerParameters
---
 .../integration/engine/junit/ITParameterized.java  |  2 +-
 .../junit/parallel/ParallelRunnerScheduler.java    | 52 +++++-----------------
 .../integration/engine/param/RunnerParameters.java | 47 +++++++++++++++++++
 3 files changed, 60 insertions(+), 41 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITParameterized.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITParameterized.java
index 3d33f03..1e9a507 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITParameterized.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITParameterized.java
@@ -32,7 +32,7 @@ public final class ITParameterized extends Parameterized {
         super(klass);
         ParallelRuntimeStrategy parallelRuntimeStrategy = klass.getAnnotation(ParallelRuntimeStrategy.class);
         if (null != parallelRuntimeStrategy) {
-            setScheduler(new ParallelRunnerScheduler(parallelRuntimeStrategy));
+            setScheduler(new ParallelRunnerScheduler(parallelRuntimeStrategy.value()));
         }
     }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerScheduler.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerScheduler.java
index 1c35f9d..efb7c33 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerScheduler.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerScheduler.java
@@ -18,16 +18,14 @@
 package org.apache.shardingsphere.test.integration.engine.junit.parallel;
 
 import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.test.integration.engine.junit.parallel.annotaion.ParallelRuntimeStrategy;
+import org.apache.shardingsphere.test.integration.engine.junit.parallel.annotaion.ParallelLevel;
 import org.apache.shardingsphere.test.integration.engine.junit.parallel.impl.CaseParallelRunnerExecutor;
 import org.apache.shardingsphere.test.integration.engine.junit.parallel.impl.ScenarioParallelRunnerExecutor;
+import org.apache.shardingsphere.test.integration.engine.param.RunnerParameters;
 import org.apache.shardingsphere.test.integration.engine.param.model.ParameterizedArray;
 import org.junit.runners.model.RunnerScheduler;
-import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
 
-import java.lang.reflect.Field;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.locks.Lock;
@@ -36,59 +34,35 @@ import java.util.concurrent.locks.ReentrantLock;
 /**
  * Parallel runner scheduler.
  */
+@RequiredArgsConstructor
 public final class ParallelRunnerScheduler implements RunnerScheduler {
     
-    private final Field parametersField;
+    private final ParallelLevel parallelLevel;
     
     private final ConcurrentMap<RunnerExecutorKey, ParallelRunnerExecutor> runnerExecutors = new ConcurrentHashMap<>();
     
-    private final ParallelRuntimeStrategy runtimeStrategy;
-    
-    private volatile Field runnerField;
-    
     private final Lock lock = new ReentrantLock();
     
-    public ParallelRunnerScheduler(final ParallelRuntimeStrategy runtimeStrategy) {
-        this.runtimeStrategy = runtimeStrategy;
-        parametersField = getParametersField();
-    }
-    
-    @SneakyThrows(NoSuchFieldException.class)
-    private Field getParametersField() {
-        Field result = BlockJUnit4ClassRunnerWithParameters.class.getDeclaredField("parameters");
-        result.setAccessible(true);
-        return result;
-    }
-    
     @Override
     public void schedule(final Runnable childStatement) {
-        Object[] parameters = getParameters(childStatement);
+        Object[] parameters = new RunnerParameters(childStatement).getRunnerParameters();
         ParameterizedArray parameterizedArray = (ParameterizedArray) parameters[0];
         getRunnerExecutor(new RunnerExecutorKey(parameterizedArray.getDatabaseType())).execute(parameterizedArray, childStatement);
     }
     
-    @SneakyThrows(ReflectiveOperationException.class)
-    private Object[] getParameters(final Runnable childStatement) {
-        if (null == runnerField) {
-            runnerField = childStatement.getClass().getDeclaredField("val$each");
-            runnerField.setAccessible(true);
-        }
-        return (Object[]) parametersField.get(runnerField.get(childStatement));
-    }
-    
     private ParallelRunnerExecutor getRunnerExecutor(final RunnerExecutorKey runnerExecutorKey) {
-        ParallelRunnerExecutor runnerExecutor = this.runnerExecutors.get(runnerExecutorKey);
+        ParallelRunnerExecutor runnerExecutor = runnerExecutors.get(runnerExecutorKey);
         if (null != runnerExecutor) {
             return runnerExecutor;
         }
         try {
             lock.lock();
-            runnerExecutor = this.runnerExecutors.get(runnerExecutorKey);
+            runnerExecutor = runnerExecutors.get(runnerExecutorKey);
             if (null != runnerExecutor) {
                 return runnerExecutor;
             }
             runnerExecutor = getRunnerExecutor();
-            this.runnerExecutors.put(runnerExecutorKey, runnerExecutor);
+            runnerExecutors.put(runnerExecutorKey, runnerExecutor);
             return runnerExecutor;
         } finally {
             lock.unlock();
@@ -96,7 +70,7 @@ public final class ParallelRunnerScheduler implements RunnerScheduler {
     }
     
     private ParallelRunnerExecutor getRunnerExecutor() {
-        switch (runtimeStrategy.value()) {
+        switch (parallelLevel) {
             case CASE:
                 return new CaseParallelRunnerExecutor();
             case SCENARIO:
@@ -108,9 +82,7 @@ public final class ParallelRunnerScheduler implements RunnerScheduler {
     
     @Override
     public void finished() {
-        if (null != runnerExecutors) {
-            runnerExecutors.values().forEach(ParallelRunnerExecutor::finished);
-        }
+        runnerExecutors.values().forEach(ParallelRunnerExecutor::finished);
     }
     
     /**
@@ -120,7 +92,7 @@ public final class ParallelRunnerScheduler implements RunnerScheduler {
     private static final class RunnerExecutorKey {
         
         private final DatabaseType databaseType;
-    
+        
         @Override
         public boolean equals(final Object o) {
             if (this == o) {
@@ -132,7 +104,7 @@ public final class ParallelRunnerScheduler implements RunnerScheduler {
             RunnerExecutorKey that = (RunnerExecutorKey) o;
             return databaseType.getName().equals(that.databaseType.getName());
         }
-    
+        
         @Override
         public int hashCode() {
             return databaseType.hashCode();
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/param/RunnerParameters.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/param/RunnerParameters.java
new file mode 100644
index 0000000..d46b80a
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/param/RunnerParameters.java
@@ -0,0 +1,47 @@
+/*
+ * 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.test.integration.engine.param;
+
+import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
+import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
+
+import java.lang.reflect.Field;
+
+/**
+ * Runner parameters.
+ */
+@RequiredArgsConstructor
+public final class RunnerParameters {
+    
+    private final Runnable childStatement;
+    
+    @SneakyThrows(ReflectiveOperationException.class)
+    public Object[] getRunnerParameters() {
+        Field parametersField = BlockJUnit4ClassRunnerWithParameters.class.getDeclaredField("parameters");
+        parametersField.setAccessible(true);
+        return (Object[]) parametersField.get(getRunner());
+    }
+    
+    @SneakyThrows(ReflectiveOperationException.class)
+    private Object getRunner() {
+        Field field = childStatement.getClass().getDeclaredField("val$each");
+        field.setAccessible(true);
+        return field.get(childStatement);
+    }
+}