|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 shardingsphere.io. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * </p> |
| 16 | + */ |
| 17 | + |
| 18 | +package io.shardingsphere.core.executor; |
| 19 | + |
| 20 | +import com.google.common.collect.Lists; |
| 21 | +import com.google.common.util.concurrent.ListenableFuture; |
| 22 | +import com.google.common.util.concurrent.ListeningExecutorService; |
| 23 | +import com.google.common.util.concurrent.MoreExecutors; |
| 24 | +import lombok.Getter; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.Iterator; |
| 30 | +import java.util.LinkedList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.concurrent.Callable; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.ExecutorService; |
| 35 | +import java.util.concurrent.Executors; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | + |
| 38 | +/** |
| 39 | + * Sharding execute engine. |
| 40 | + * |
| 41 | + * @author zhangliang |
| 42 | + */ |
| 43 | +public final class ShardingExecuteEngine implements AutoCloseable { |
| 44 | + |
| 45 | + private static final ExecutorService SHUTDOWN_EXECUTOR = Executors.newSingleThreadExecutor(ShardingThreadFactoryBuilder.build("Executor-Engine-Closer")); |
| 46 | + |
| 47 | + @Getter |
| 48 | + private final ListeningExecutorService executorService; |
| 49 | + |
| 50 | + public ShardingExecuteEngine(final int executorSize) { |
| 51 | + executorService = MoreExecutors.listeningDecorator( |
| 52 | + 0 == executorSize ? Executors.newCachedThreadPool(ShardingThreadFactoryBuilder.build()) : Executors.newFixedThreadPool(executorSize, ShardingThreadFactoryBuilder.build())); |
| 53 | + MoreExecutors.addDelayedShutdownHook(executorService, 60, TimeUnit.SECONDS); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Execute all callbacks. |
| 58 | + * |
| 59 | + * @param inputs sharding execute callbacks |
| 60 | + * @param callback sharding execute callback |
| 61 | + * @param <I> type of input value |
| 62 | + * @param <O> type of return value |
| 63 | + * @return execute result |
| 64 | + * @throws Exception throw if execute failure |
| 65 | + */ |
| 66 | + public <I, O> List<O> execute(final Collection<I> inputs, final ShardingExecuteCallback<I, O> callback) throws Exception { |
| 67 | + if (inputs.isEmpty()) { |
| 68 | + return Collections.emptyList(); |
| 69 | + } |
| 70 | + Iterator<I> inputIterator = inputs.iterator(); |
| 71 | + I firstInput = inputIterator.next(); |
| 72 | + Collection<ListenableFuture<O>> restFutures = asyncExecute(Lists.newArrayList(inputIterator), callback); |
| 73 | + return getResults(callback.execute(firstInput), restFutures); |
| 74 | + } |
| 75 | + |
| 76 | + private <I, O> Collection<ListenableFuture<O>> asyncExecute(final Collection<I> inputs, final ShardingExecuteCallback<I, O> callback) { |
| 77 | + Collection<ListenableFuture<O>> result = new ArrayList<>(inputs.size()); |
| 78 | + for (final I each : inputs) { |
| 79 | + result.add(executorService.submit(new Callable<O>() { |
| 80 | + |
| 81 | + @Override |
| 82 | + public O call() throws Exception { |
| 83 | + return callback.execute(each); |
| 84 | + } |
| 85 | + })); |
| 86 | + } |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + private <O> List<O> getResults(final O firstResult, final Collection<ListenableFuture<O>> restFutures) throws ExecutionException, InterruptedException { |
| 91 | + List<O> result = new LinkedList<>(); |
| 92 | + result.add(firstResult); |
| 93 | + for (ListenableFuture<O> each : restFutures) { |
| 94 | + result.add(each.get()); |
| 95 | + } |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void close() { |
| 101 | + SHUTDOWN_EXECUTOR.execute(new Runnable() { |
| 102 | + |
| 103 | + @Override |
| 104 | + public void run() { |
| 105 | + try { |
| 106 | + executorService.shutdown(); |
| 107 | + while (!executorService.awaitTermination(5, TimeUnit.SECONDS)) { |
| 108 | + executorService.shutdownNow(); |
| 109 | + } |
| 110 | + } catch (final InterruptedException ex) { |
| 111 | + Thread.currentThread().interrupt(); |
| 112 | + } |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | +} |
0 commit comments