Unverified Commit 861e49b7 authored by 张亮's avatar 张亮 Committed by GitHub
Browse files

Merge pull request #1102 from tuohai666/dev

change FrontendExecutorContext to UserGroupContext
parents 7c5f16dd 8dadd1f8
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -42,16 +42,27 @@ public enum ShardingPropertiesConstant {
     */
    SQL_SHOW("sql.show", Boolean.FALSE.toString(), boolean.class),
    
    /**
     * Worker group or user group thread max size.
     *
     * <p>
     * Worker group accept tcp connection.
     * User group accept MySQL command.
     * Default: CPU cores * 2.
     * </p>
     */
    ACCEPTOR_SIZE("acceptor.size", String.valueOf(Runtime.getRuntime().availableProcessors() * 2), int.class),
    
    /**
     * Worker thread max size.
     * 
     * <p>
     * Execute SQL Statement and PrepareStatement will use this thread pool.
     * One sharding data source will use a independent thread pool, it does not share thread pool even different data source in same JVM.
     * Default: same with CPU cores.
     * Default: infinite.
     * </p>
     */
    EXECUTOR_SIZE("executor.size", String.valueOf(Runtime.getRuntime().availableProcessors()), int.class),
    EXECUTOR_SIZE("executor.size", String.valueOf(0), int.class),
    
    /**
     * Connection mode of connected to databases.
+7 −1
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@@ -66,8 +67,13 @@ public final class ExecutorEngine implements AutoCloseable {
    private final ListeningExecutorService executorService;
    
    public ExecutorEngine(final int executorSize) {
        if (0 == executorSize) {
            executorService = MoreExecutors.listeningDecorator(new ThreadPoolExecutor(
                0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Sharding-JDBC-%d").build()));
        } else {
            executorService = MoreExecutors.listeningDecorator(new ThreadPoolExecutor(
                executorSize, executorSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Sharding-JDBC-%d").build()));
        }
        MoreExecutors.addDelayedShutdownHook(executorService, 60, TimeUnit.SECONDS);
    }
    
+7 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package io.shardingsphere.proxy.backend;

import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import io.shardingsphere.proxy.config.RuleRegistry;
import lombok.Getter;

import java.util.concurrent.Executors;
@@ -33,7 +34,12 @@ public final class BackendExecutorContext {
    private static final BackendExecutorContext INSTANCE = new BackendExecutorContext();
    
    @Getter
    private final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    private final ListeningExecutorService executorService;
    
    private BackendExecutorContext() {
        int executorSize = RuleRegistry.getInstance().getExecutorSize();
        executorService = 0 == executorSize ? MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()) : MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(executorSize));
    }
    
    /**
     * Get backend executor context instance.
+3 −0
Original line number Diff line number Diff line
@@ -75,6 +75,8 @@ public final class RuleRegistry {
    
    private ConnectionMode connectionMode;
    
    private int acceptorSize;
    
    private int executorSize;
    
    private BackendNIOConfiguration backendNIOConfig;
@@ -108,6 +110,7 @@ public final class RuleRegistry {
        connectionMode = ConnectionMode.valueOf(shardingProperties.<String>getValue(ShardingPropertiesConstant.CONNECTION_MODE));
        transactionType = TransactionType.valueOf(shardingProperties.<String>getValue(ShardingPropertiesConstant.PROXY_TRANSACTION_MODE));
        transactionManager = ProxyTransactionLoader.load(transactionType);
        acceptorSize = shardingProperties.getValue(ShardingPropertiesConstant.ACCEPTOR_SIZE);
        executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
        // TODO :jiaqi force off use NIO for backend, this feature is not complete yet
        boolean useNIO = false;
+0 −47
Original line number Diff line number Diff line
/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.proxy.frontend;

import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import io.shardingsphere.proxy.config.RuleRegistry;
import lombok.Getter;

import java.util.concurrent.Executors;

/**
 * Frontend executor context.
 *
 * @author zhangyonglun
 */
public final class FrontendExecutorContext {
    
    private static final FrontendExecutorContext INSTANCE = new FrontendExecutorContext();
    
    @Getter
    private final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(RuleRegistry.getInstance().getExecutorSize()));
    
    /**
     * Get frontend executor context instance.
     * 
     * @return instance of frontend executor context
     */
    public static FrontendExecutorContext getInstance() {
        return INSTANCE;
    }
}
Loading