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 12:03:44 UTC

[shardingsphere] branch master updated: Simplify ParallelRunnerScheduler.getRunnerExecutor() (#9476)

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 7dcd547  Simplify ParallelRunnerScheduler.getRunnerExecutor() (#9476)
7dcd547 is described below

commit 7dcd547ea9886df4c8a3b51edbdfa1733d00acaf
Author: Liang Zhang <te...@163.com>
AuthorDate: Tue Feb 23 20:03:07 2021 +0800

    Simplify ParallelRunnerScheduler.getRunnerExecutor() (#9476)
---
 .../junit/parallel/ParallelRunnerScheduler.java    | 58 +++-------------------
 .../integration/engine/param/RunnerParameters.java | 12 +++--
 2 files changed, 15 insertions(+), 55 deletions(-)

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 efb7c33..6375324 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
@@ -28,8 +28,6 @@ import org.junit.runners.model.RunnerScheduler;
 
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * Parallel runner scheduler.
@@ -39,34 +37,20 @@ public final class ParallelRunnerScheduler implements RunnerScheduler {
     
     private final ParallelLevel parallelLevel;
     
-    private final ConcurrentMap<RunnerExecutorKey, ParallelRunnerExecutor> runnerExecutors = new ConcurrentHashMap<>();
-    
-    private final Lock lock = new ReentrantLock();
+    private final ConcurrentMap<DatabaseType, ParallelRunnerExecutor> runnerExecutors = new ConcurrentHashMap<>();
     
     @Override
     public void schedule(final Runnable childStatement) {
-        Object[] parameters = new RunnerParameters(childStatement).getRunnerParameters();
-        ParameterizedArray parameterizedArray = (ParameterizedArray) parameters[0];
-        getRunnerExecutor(new RunnerExecutorKey(parameterizedArray.getDatabaseType())).execute(parameterizedArray, childStatement);
+        ParameterizedArray parameterizedArray = new RunnerParameters(childStatement).getParameterizedArray();
+        getRunnerExecutor(parameterizedArray.getDatabaseType()).execute(parameterizedArray, childStatement);
     }
     
-    private ParallelRunnerExecutor getRunnerExecutor(final RunnerExecutorKey runnerExecutorKey) {
-        ParallelRunnerExecutor runnerExecutor = runnerExecutors.get(runnerExecutorKey);
-        if (null != runnerExecutor) {
-            return runnerExecutor;
-        }
-        try {
-            lock.lock();
-            runnerExecutor = runnerExecutors.get(runnerExecutorKey);
-            if (null != runnerExecutor) {
-                return runnerExecutor;
-            }
-            runnerExecutor = getRunnerExecutor();
-            runnerExecutors.put(runnerExecutorKey, runnerExecutor);
-            return runnerExecutor;
-        } finally {
-            lock.unlock();
+    private ParallelRunnerExecutor getRunnerExecutor(final DatabaseType databaseType) {
+        if (runnerExecutors.containsKey(databaseType)) {
+            return runnerExecutors.get(databaseType);
         }
+        runnerExecutors.putIfAbsent(databaseType, getRunnerExecutor());
+        return runnerExecutors.get(databaseType);
     }
     
     private ParallelRunnerExecutor getRunnerExecutor() {
@@ -84,30 +68,4 @@ public final class ParallelRunnerScheduler implements RunnerScheduler {
     public void finished() {
         runnerExecutors.values().forEach(ParallelRunnerExecutor::finished);
     }
-    
-    /**
-     * Runner executor key.
-     */
-    @RequiredArgsConstructor
-    private static final class RunnerExecutorKey {
-        
-        private final DatabaseType databaseType;
-        
-        @Override
-        public boolean equals(final Object o) {
-            if (this == o) {
-                return true;
-            }
-            if (o == null || getClass() != o.getClass()) {
-                return false;
-            }
-            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
index c66c6ae..d234df1 100644
--- 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
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.test.integration.engine.param;
 
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
+import org.apache.shardingsphere.test.integration.engine.param.model.ParameterizedArray;
 import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
 
 import java.lang.reflect.Field;
@@ -32,15 +33,16 @@ public final class RunnerParameters {
     private final Runnable childStatement;
     
     /**
-     * Get runner parameters.
-     * 
-     * @return runner parameters
+     * Get parameterized array.
+     *
+     * @return parameterized array
      */
     @SneakyThrows(ReflectiveOperationException.class)
-    public Object[] getRunnerParameters() {
+    public ParameterizedArray getParameterizedArray() {
         Field parametersField = BlockJUnit4ClassRunnerWithParameters.class.getDeclaredField("parameters");
         parametersField.setAccessible(true);
-        return (Object[]) parametersField.get(getRunner());
+        Object[] parameters = (Object[]) parametersField.get(getRunner());
+        return (ParameterizedArray) parameters[0];
     }
     
     @SneakyThrows(ReflectiveOperationException.class)