gerrit 服务器 sshd 服务的队列详解 gerrit sshd 相关的配置 一般的 gerrit 会配上下面的类似的, 但是其中各个都代表什么意思呢?
1 2 3 4 5 6 7 8 9 [sshd] listenAddress = *:29418 maxConnectionsPerUser = 128 batchThreads = 30 threads = 120 streamThreads = 30 commandStartThreads = 30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 简单的讲gerrit的ssh队列可以分为2个大的队列,一个是SSH-Interactive-Worker, 一个是SSH-Batch-Worker. 其中的 batchThreads = 30 就是控制 SSH-Batch-Worker队列的个数的. 其中使用命令 ssh gerrit.example.com gerrit show-queue -w -q ,加上-q可以按照不同队列显示 Task State StartTime Command ------------------------------------------------------------------------------ Queue: SSH-Interactive-Worker ------------------------------------------------------------------------------ 88 tasks, 90 worker threads Queue: SSH-Batch-Worker ------------------------------------------------------------------------------ 1 tasks, 30 worker threads Queue: WorkQueue 787bdb02 Log File Compressor b885d313 change cleanup runner ------------------------------------------------------------------------------ 2 tasks, 1 worker threads Queue: ReplicateTo-pubmirror ------------------------------------------------------------------------------ 8 tasks, 3 worker threads
ThreadSettingsConfig 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 @Singleton public class ThreadSettingsConfig { private final int sshdThreads; private final int httpdMaxThreads; private final int sshdBatchThreads; private final int databasePoolLimit; @Inject ThreadSettingsConfig(@GerritServerConfig Config cfg) { int cores = Runtime.getRuntime().availableProcessors(); sshdThreads = cfg.getInt("sshd", "threads", Math.max(4, 2 * cores)); httpdMaxThreads = cfg.getInt("httpd", "maxThreads", 25); int defaultDatabasePoolLimit = sshdThreads + httpdMaxThreads + 2; databasePoolLimit = cfg.getInt("database", "poolLimit", defaultDatabasePoolLimit); sshdBatchThreads = cores == 1 ? 1 : 2; } } sshdThreads 最小默认有个4, 如果服务器cpu核心比较多 就是 cpu核心的2倍这个数字. 如果配置文件中配置了 sshd.threads 的个数,就取配置文件的 linux 实现 Runtime.getRuntime().availableProcessors() int os::active_processor_count() { // Linux doesn't yet have a (official) notion of processor sets, // so just return the number of online processors. int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check"); return online_cpus; } httpdMaxThreads 默认取25, 如果配置文件中配置了 httpd.maxThreads 的个数,就取配置文件的 databasePoolLimit 默认取 sshdThreads + httpdMaxThreads + 2 这么多, 感觉数据库默认取得挺多呀. 如果配置文件中配置了 database.poolLimit 就取配置文件中的. sshdBatchThreads 根据cpu是1个核心还是多个的, 分别取 1 或者 2. ThreadSettingsConfig类就是 配置 sshdThreads, httpdMaxThreads, sshdBatchThreads, databasePoolLimit 这4个数字的.
CommandExecutorQueueProvider 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 @Singleton public class CommandExecutorQueueProvider implements QueueProvider { private int poolSize; private final int batchThreads; private final ScheduledThreadPoolExecutor interactiveExecutor; private final ScheduledThreadPoolExecutor batchExecutor; @Inject public CommandExecutorQueueProvider( @GerritServerConfig Config config, ThreadSettingsConfig threadsSettingsConfig, WorkQueue queues) { poolSize = threadsSettingsConfig.getSshdThreads(); batchThreads = config.getInt("sshd", "batchThreads", threadsSettingsConfig.getSshdBatchTreads()); if (batchThreads > poolSize) { poolSize += batchThreads; } int interactiveThreads = Math.max(1, poolSize - batchThreads); interactiveExecutor = queues.createQueue(interactiveThreads, "SSH-Interactive-Worker", Thread.MIN_PRIORITY, true); if (batchThreads != 0) { batchExecutor = queues.createQueue(batchThreads, "SSH-Batch-Worker", Thread.MIN_PRIORITY, true); } else { batchExecutor = interactiveExecutor; } } } 通过代码分析可以知道, poolSize 是 sshd.threads = 120 这个配置控制的. batchThreads 默认取 ThreadSettingsConfig 中 的 sshdBatchThreads值. 如果配置文件中配置了sshd.batchThreads, 就用配置文件中的. 这里我们配置了 sshd.batchThreads = 30 如果 batchThreads 大于 poolSize的个数, 那就把 poolSize 增大, 加上 batchThreads的个数.得到新的 poolSize 的大小. interactiveThreads 的 值等于 1 或者 poolSize 减去 batchThreads 的值, 我们这里配置的 就是 120 - 30 = 90, 所以 interactiveThreads 队列的大小是90. batchThreads 队列的大小就是 30. 可以看到 如果 batchThreads 不等于0 就会分开2个队列的. interactiveExecutor = queues.createQueue(interactiveThreads, "SSH-Interactive-Worker", Thread.MIN_PRIORITY, true); batchExecutor = queues.createQueue(batchThreads, "SSH-Batch-Worker", Thread.MIN_PRIORITY, true);