You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2022/11/30 15:02:56 UTC
[shardingsphere] branch master updated: Refactor ParallelRunnerExecutor as final class (#22546)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 74cb6a67bd9 Refactor ParallelRunnerExecutor as final class (#22546)
74cb6a67bd9 is described below
commit 74cb6a67bd975c18b3331e28e3bdcb1de6aaa7bf
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Nov 30 23:02:48 2022 +0800
Refactor ParallelRunnerExecutor as final class (#22546)
* Refactor AbstractParallelRunnerExecutor
* Refactor ParallelRunnerExecutor as final class
---
.../ParameterizedParallelRunnerScheduler.java | 5 +-
.../parallel/impl/CaseParallelRunnerExecutor.java | 32 ---------
.../impl/ScenarioParallelRunnerExecutor.java | 75 ---------------------
...ere.test.runner.executor.ParallelRunnerExecutor | 19 ------
.../runner/executor/ParallelRunnerExecutor.java | 53 +++++++++++----
.../executor/ParallelRunnerExecutorFactory.java | 53 ---------------
.../runner/executor/ParallelRunnerExecutors.java | 7 +-
.../impl/NormalParallelRunnerExecutor.java | 76 ----------------------
.../runner/scheduler/ParallelRunnerScheduler.java | 2 +-
...ere.test.runner.executor.ParallelRunnerExecutor | 18 -----
10 files changed, 46 insertions(+), 294 deletions(-)
diff --git a/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/ParameterizedParallelRunnerScheduler.java b/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/ParameterizedParallelRunnerScheduler.java
index b9dc3f1be00..4593d890e73 100644
--- a/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/ParameterizedParallelRunnerScheduler.java
+++ b/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/ParameterizedParallelRunnerScheduler.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.test.integration.framework.runner.parallel;
import org.apache.shardingsphere.test.integration.framework.param.model.ITParameterizedArray;
-import org.apache.shardingsphere.test.integration.framework.runner.parallel.impl.ScenarioParallelRunnerExecutor.ScenarioKey;
import org.apache.shardingsphere.test.runner.ParallelRunningStrategy.ParallelLevel;
import org.apache.shardingsphere.test.runner.executor.ParallelRunnerExecutors;
import org.apache.shardingsphere.test.runner.param.RunnerParameters;
@@ -36,7 +35,7 @@ public final class ParameterizedParallelRunnerScheduler extends ParallelRunnerSc
@Override
public void schedule(final Runnable childStatement) {
ITParameterizedArray parameterizedArray = (ITParameterizedArray) new RunnerParameters(childStatement).getParameterizedArray();
- getExecutorEngine().getExecutor(parameterizedArray.getDatabaseType().getType(), getParallelLevel())
- .execute(ParallelLevel.SCENARIO == getParallelLevel() ? new ScenarioKey(parameterizedArray).toString() : "", childStatement);
+ String key = String.join("-", parameterizedArray.getAdapter(), parameterizedArray.getScenario(), parameterizedArray.getDatabaseType().getType());
+ getExecutorEngine().getExecutor(parameterizedArray.getDatabaseType().getType()).execute(ParallelLevel.SCENARIO == getParallelLevel() ? key : "", childStatement);
}
}
diff --git a/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/impl/CaseParallelRunnerExecutor.java b/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/impl/CaseParallelRunnerExecutor.java
deleted file mode 100644
index f0539423df1..00000000000
--- a/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/impl/CaseParallelRunnerExecutor.java
+++ /dev/null
@@ -1,32 +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.test.integration.framework.runner.parallel.impl;
-
-import org.apache.shardingsphere.test.runner.ParallelRunningStrategy.ParallelLevel;
-import org.apache.shardingsphere.test.runner.executor.impl.NormalParallelRunnerExecutor;
-
-/**
- * Parallel runner executor with case.
- */
-public final class CaseParallelRunnerExecutor extends NormalParallelRunnerExecutor {
-
- @Override
- public ParallelLevel getParallelLevel() {
- return ParallelLevel.CASE;
- }
-}
diff --git a/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/impl/ScenarioParallelRunnerExecutor.java b/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/impl/ScenarioParallelRunnerExecutor.java
deleted file mode 100644
index 635ddaf536b..00000000000
--- a/test/integration-test/test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/runner/parallel/impl/ScenarioParallelRunnerExecutor.java
+++ /dev/null
@@ -1,75 +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.test.integration.framework.runner.parallel.impl;
-
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import lombok.EqualsAndHashCode;
-import org.apache.shardingsphere.test.integration.framework.param.model.ITParameterizedArray;
-import org.apache.shardingsphere.test.runner.ParallelRunningStrategy.ParallelLevel;
-import org.apache.shardingsphere.test.runner.executor.impl.NormalParallelRunnerExecutor;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
-/**
- * Parallel runner executor with scenario.
- */
-public final class ScenarioParallelRunnerExecutor extends NormalParallelRunnerExecutor {
-
- @Override
- protected ExecutorService getExecutorService(final String key) {
- if (getExecutorServices().containsKey(key)) {
- return getExecutorServices().get(key);
- }
- String threadPoolNameFormat = String.join("-", "ScenarioExecutorPool", key, "%d");
- ExecutorService executorService = Executors.newFixedThreadPool(
- Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat(threadPoolNameFormat).build());
- if (null != getExecutorServices().putIfAbsent(key, executorService)) {
- executorService.shutdownNow();
- }
- return getExecutorServices().get(key);
- }
-
- @Override
- public ParallelLevel getParallelLevel() {
- return ParallelLevel.SCENARIO;
- }
-
- /**
- * Scenario key.
- */
- @EqualsAndHashCode
- public static final class ScenarioKey {
-
- private final String adapter;
-
- private final String scenario;
-
- private final String databaseType;
-
- public ScenarioKey(final ITParameterizedArray parameterizedArray) {
- adapter = parameterizedArray.getAdapter();
- scenario = parameterizedArray.getScenario();
- databaseType = parameterizedArray.getDatabaseType().getType();
- }
-
- @Override
- public String toString() {
- return String.join("-", adapter, scenario, databaseType);
- }
- }
-}
diff --git a/test/integration-test/test-suite/src/test/resources/META-INF/services/org.apache.shardingsphere.test.runner.executor.ParallelRunnerExecutor b/test/integration-test/test-suite/src/test/resources/META-INF/services/org.apache.shardingsphere.test.runner.executor.ParallelRunnerExecutor
deleted file mode 100644
index 09d270b9818..00000000000
--- a/test/integration-test/test-suite/src/test/resources/META-INF/services/org.apache.shardingsphere.test.runner.executor.ParallelRunnerExecutor
+++ /dev/null
@@ -1,19 +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.
-#
-
-org.apache.shardingsphere.test.integration.framework.runner.parallel.impl.CaseParallelRunnerExecutor
-org.apache.shardingsphere.test.integration.framework.runner.parallel.impl.ScenarioParallelRunnerExecutor
diff --git a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutor.java b/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutor.java
index e35c75fd422..4b32be78a99 100644
--- a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutor.java
+++ b/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutor.java
@@ -17,12 +17,25 @@
package org.apache.shardingsphere.test.runner.executor;
-import org.apache.shardingsphere.test.runner.ParallelRunningStrategy.ParallelLevel;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
/**
- * Parallel runner executor.
+ * Abstract parallel runner executor.
*/
-public interface ParallelRunnerExecutor {
+public final class ParallelRunnerExecutor {
+
+ private final Collection<Future<?>> futures = new LinkedList<>();
+
+ private final Map<Object, ExecutorService> executorServices = new ConcurrentHashMap<>();
/**
* Execute child statement.
@@ -30,17 +43,33 @@ public interface ParallelRunnerExecutor {
* @param key executor key
* @param childStatement child statement
*/
- void execute(String key, Runnable childStatement);
+ public void execute(final String key, final Runnable childStatement) {
+ futures.add(getExecutorService(key).submit(childStatement));
+ }
- /**
- * Override to implement any behavior that must occur after all children have been scheduled (for example, waiting for them all to finish).
- */
- void finished();
+ private ExecutorService getExecutorService(final String key) {
+ if (executorServices.containsKey(key)) {
+ return executorServices.get(key);
+ }
+ String threadPoolNameFormat = String.join("-", "TestThread", key, "%d");
+ ExecutorService executorService = Executors.newFixedThreadPool(
+ Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat(threadPoolNameFormat).build());
+ if (null != executorServices.putIfAbsent(key, executorService)) {
+ executorService.shutdownNow();
+ }
+ return executorServices.get(key);
+ }
/**
- * Get parallel level.
- *
- * @return parallel level
+ * Finish tasks.
*/
- ParallelLevel getParallelLevel();
+ public void finished() {
+ futures.forEach(each -> {
+ try {
+ each.get();
+ } catch (final InterruptedException | ExecutionException ignored) {
+ }
+ });
+ executorServices.values().forEach(ExecutorService::shutdownNow);
+ }
}
diff --git a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutorFactory.java b/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutorFactory.java
deleted file mode 100644
index ae97e5d796b..00000000000
--- a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutorFactory.java
+++ /dev/null
@@ -1,53 +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.test.runner.executor;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import lombok.SneakyThrows;
-import org.apache.shardingsphere.test.runner.ParallelRunningStrategy.ParallelLevel;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.ServiceLoader;
-
-/**
- * Parallel runner executor factory.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ParallelRunnerExecutorFactory {
-
- private static final Map<ParallelLevel, Class<? extends ParallelRunnerExecutor>> EXECUTORS = new HashMap<>();
-
- static {
- for (ParallelRunnerExecutor each : ServiceLoader.load(ParallelRunnerExecutor.class)) {
- EXECUTORS.put(each.getParallelLevel(), each.getClass());
- }
- }
-
- /**
- * Create new instance of parallel runner executor.
- *
- * @param parallelLevel parallel level
- * @return created instance
- */
- @SneakyThrows(ReflectiveOperationException.class)
- public static ParallelRunnerExecutor newInstance(final ParallelLevel parallelLevel) {
- return EXECUTORS.get(parallelLevel).getDeclaredConstructor().newInstance();
- }
-}
diff --git a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutors.java b/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutors.java
index 16b6cf3a4a4..af839f87e8a 100644
--- a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutors.java
+++ b/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/ParallelRunnerExecutors.java
@@ -17,8 +17,6 @@
package org.apache.shardingsphere.test.runner.executor;
-import org.apache.shardingsphere.test.runner.ParallelRunningStrategy.ParallelLevel;
-
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -33,14 +31,13 @@ public final class ParallelRunnerExecutors {
* Get executor.
*
* @param key key bind to the executor
- * @param parallelLevel parallel level
* @return got executor
*/
- public ParallelRunnerExecutor getExecutor(final String key, final ParallelLevel parallelLevel) {
+ public ParallelRunnerExecutor getExecutor(final String key) {
if (executors.containsKey(key)) {
return executors.get(key);
}
- ParallelRunnerExecutor newExecutor = ParallelRunnerExecutorFactory.newInstance(parallelLevel);
+ ParallelRunnerExecutor newExecutor = new ParallelRunnerExecutor();
if (null != executors.putIfAbsent(key, newExecutor)) {
newExecutor.finished();
}
diff --git a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/impl/NormalParallelRunnerExecutor.java b/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/impl/NormalParallelRunnerExecutor.java
deleted file mode 100644
index 8366e17a86e..00000000000
--- a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/executor/impl/NormalParallelRunnerExecutor.java
+++ /dev/null
@@ -1,76 +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.test.runner.executor.impl;
-
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import lombok.Getter;
-import org.apache.shardingsphere.test.runner.ParallelRunningStrategy.ParallelLevel;
-import org.apache.shardingsphere.test.runner.executor.ParallelRunnerExecutor;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-
-/**
- * Normal parallel runner executor.
- */
-public class NormalParallelRunnerExecutor implements ParallelRunnerExecutor {
-
- private final Collection<Future<?>> futures = new LinkedList<>();
-
- @Getter
- private final Map<Object, ExecutorService> executorServices = new ConcurrentHashMap<>();
-
- @Override
- public void execute(final String key, final Runnable childStatement) {
- futures.add(getExecutorService(key).submit(childStatement));
- }
-
- protected ExecutorService getExecutorService(final String key) {
- if (executorServices.containsKey(key)) {
- return executorServices.get(key);
- }
- String threadPoolNameFormat = String.join("-", "ShardingSphere-KeyedParallelTestThread", key, "%d");
- ExecutorService executorService = Executors.newFixedThreadPool(
- Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat(threadPoolNameFormat).build());
- if (null != executorServices.putIfAbsent(key, executorService)) {
- executorService.shutdownNow();
- }
- return executorServices.get(key);
- }
-
- @Override
- public void finished() {
- futures.forEach(each -> {
- try {
- each.get();
- } catch (final InterruptedException | ExecutionException ignored) {
- }
- });
- executorServices.values().forEach(ExecutorService::shutdownNow);
- }
-
- @Override
- public ParallelLevel getParallelLevel() {
- return ParallelLevel.NORMAL;
- }
-}
diff --git a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/scheduler/ParallelRunnerScheduler.java b/test/runner/src/main/java/org/apache/shardingsphere/test/runner/scheduler/ParallelRunnerScheduler.java
index 4393d128f3d..5c921db2bdb 100644
--- a/test/runner/src/main/java/org/apache/shardingsphere/test/runner/scheduler/ParallelRunnerScheduler.java
+++ b/test/runner/src/main/java/org/apache/shardingsphere/test/runner/scheduler/ParallelRunnerScheduler.java
@@ -36,7 +36,7 @@ public class ParallelRunnerScheduler implements RunnerScheduler {
@Override
public void schedule(final Runnable childStatement) {
- executorEngine.getExecutor("", parallelLevel).execute("", childStatement);
+ executorEngine.getExecutor("").execute("", childStatement);
}
@Override
diff --git a/test/runner/src/main/resources/META-INF/services/org.apache.shardingsphere.test.runner.executor.ParallelRunnerExecutor b/test/runner/src/main/resources/META-INF/services/org.apache.shardingsphere.test.runner.executor.ParallelRunnerExecutor
deleted file mode 100644
index 57835bbe662..00000000000
--- a/test/runner/src/main/resources/META-INF/services/org.apache.shardingsphere.test.runner.executor.ParallelRunnerExecutor
+++ /dev/null
@@ -1,18 +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.
-#
-
-org.apache.shardingsphere.test.runner.executor.impl.NormalParallelRunnerExecutor