You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2014/06/16 10:18:51 UTC
git commit: [SSHD-328] Customize thread pools to have nicer names
Repository: mina-sshd
Updated Branches:
refs/heads/master 2aed686bd -> b8c21ebc1
[SSHD-328] Customize thread pools to have nicer names
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/b8c21ebc
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/b8c21ebc
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/b8c21ebc
Branch: refs/heads/master
Commit: b8c21ebc1aaea60f029c301500159bc18e446bd2
Parents: 2aed686
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Jun 16 10:18:42 2014 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Jun 16 10:18:42 2014 +0200
----------------------------------------------------------------------
.../main/java/org/apache/sshd/SshClient.java | 10 +-
.../main/java/org/apache/sshd/SshServer.java | 11 +-
.../sshd/common/io/mina/MinaServiceFactory.java | 8 +-
.../sshd/common/io/nio2/Nio2ServiceFactory.java | 12 +--
.../apache/sshd/common/util/ThreadUtils.java | 100 +++++++++++++++++++
.../sshd/server/shell/InvertedShellWrapper.java | 11 +-
.../sshd/sftp/subsystem/SftpSubsystem.java | 3 +-
7 files changed, 133 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b8c21ebc/sshd-core/src/main/java/org/apache/sshd/SshClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/SshClient.java b/sshd-core/src/main/java/org/apache/sshd/SshClient.java
index 967d020..ce8b335 100644
--- a/sshd-core/src/main/java/org/apache/sshd/SshClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/SshClient.java
@@ -115,6 +115,7 @@ import org.apache.sshd.common.util.CloseableUtils;
import org.apache.sshd.common.util.NoCloseInputStream;
import org.apache.sshd.common.util.NoCloseOutputStream;
import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.common.util.ThreadUtils;
import org.bouncycastle.openssl.PasswordFinder;
/**
@@ -213,7 +214,9 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
throw new IllegalArgumentException("KeyExchangeFactories not set");
}
if (getScheduledExecutorService() == null) {
- setScheduledExecutorService(Executors.newSingleThreadScheduledExecutor(), true);
+ setScheduledExecutorService(
+ ThreadUtils.newSingleThreadScheduledExecutor(this.toString() + "-timer"),
+ true);
}
if (getCipherFactories() == null) {
throw new IllegalArgumentException("CipherFactories not set");
@@ -355,6 +358,11 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
return new SessionFactory();
}
+ @Override
+ public String toString() {
+ return "SshClient[" + Integer.toHexString(hashCode()) + "]";
+ }
+
/**
* Setup a default client. The client does not require any additional setup.
*
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b8c21ebc/sshd-core/src/main/java/org/apache/sshd/SshServer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/SshServer.java b/sshd-core/src/main/java/org/apache/sshd/SshServer.java
index 69a5033..7d1b439 100644
--- a/sshd-core/src/main/java/org/apache/sshd/SshServer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/SshServer.java
@@ -30,7 +30,6 @@ import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
-import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -87,6 +86,7 @@ import org.apache.sshd.common.signature.SignatureRSA;
import org.apache.sshd.common.util.CloseableUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.common.util.ThreadUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.PasswordAuthenticator;
@@ -278,7 +278,9 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
}
}
if (getScheduledExecutorService() == null) {
- setScheduledExecutorService(Executors.newSingleThreadScheduledExecutor(), true);
+ setScheduledExecutorService(
+ ThreadUtils.newSingleThreadScheduledExecutor(this.toString() + "-timer"),
+ true);
}
if (getCipherFactories() == null) {
throw new IllegalArgumentException("CipherFactories not set");
@@ -436,6 +438,11 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
sessionTimeoutListener = null;
}
+ @Override
+ public String toString() {
+ return "SshServer[" + Integer.toHexString(hashCode()) + "]";
+ }
+
public static SshServer setUpDefaultServer() {
SshServer sshd = new SshServer();
// DHG14 uses 2048 bits key which are not supported by the default JCE provider
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b8c21ebc/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java
index 268323f..2e5276f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java
@@ -19,8 +19,6 @@
package org.apache.sshd.common.io.mina;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.mina.core.service.IoProcessor;
@@ -28,12 +26,12 @@ import org.apache.mina.core.service.SimpleIoProcessorPool;
import org.apache.mina.transport.socket.nio.NioProcessor;
import org.apache.mina.transport.socket.nio.NioSession;
import org.apache.sshd.common.FactoryManager;
-import org.apache.sshd.common.future.CloseFuture;
import org.apache.sshd.common.io.IoAcceptor;
import org.apache.sshd.common.io.IoConnector;
import org.apache.sshd.common.io.IoHandler;
import org.apache.sshd.common.io.IoServiceFactory;
import org.apache.sshd.common.util.CloseableUtils;
+import org.apache.sshd.common.util.ThreadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -48,9 +46,7 @@ public class MinaServiceFactory extends CloseableUtils.AbstractCloseable impleme
public MinaServiceFactory(FactoryManager manager) {
this.manager = manager;
- this.executor = Executors.newCachedThreadPool();
- // Set a default reject handler
- ((ThreadPoolExecutor) this.executor).setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+ this.executor = ThreadUtils.newCachedThreadPool(manager.toString() + "-mina");
this.ioProcessor = new SimpleIoProcessorPool<NioSession>(NioProcessor.class, getNioWorkers());
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b8c21ebc/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2ServiceFactory.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2ServiceFactory.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2ServiceFactory.java
index 07eb627..c488436 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2ServiceFactory.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2ServiceFactory.java
@@ -21,21 +21,16 @@ package org.apache.sshd.common.io.nio2;
import java.io.IOException;
import java.nio.channels.AsynchronousChannelGroup;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.RuntimeSshException;
-import org.apache.sshd.common.future.CloseFuture;
-import org.apache.sshd.common.future.SshFuture;
import org.apache.sshd.common.io.IoAcceptor;
import org.apache.sshd.common.io.IoConnector;
import org.apache.sshd.common.io.IoHandler;
import org.apache.sshd.common.io.IoServiceFactory;
import org.apache.sshd.common.util.CloseableUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.sshd.common.util.ThreadUtils;
/**
*/
@@ -47,8 +42,9 @@ public class Nio2ServiceFactory extends CloseableUtils.AbstractCloseable impleme
public Nio2ServiceFactory(FactoryManager manager) {
this.manager = manager;
try {
- ExecutorService executor = Executors.newFixedThreadPool(getNioWorkers());
- ((ThreadPoolExecutor) executor).setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+ ExecutorService executor = ThreadUtils.newFixedThreadPool(
+ manager.toString() + "-nio2",
+ getNioWorkers());
group = AsynchronousChannelGroup.withThreadPool(executor);
} catch (IOException e) {
throw new RuntimeSshException(e);
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b8c21ebc/sshd-core/src/main/java/org/apache/sshd/common/util/ThreadUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/ThreadUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/ThreadUtils.java
new file mode 100644
index 0000000..81a31ad
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/ThreadUtils.java
@@ -0,0 +1,100 @@
+/*
+ * 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.sshd.common.util;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Utility class for thread pools.
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class ThreadUtils {
+
+ public static ExecutorService newFixedThreadPool(
+ String poolName,
+ int nThreads
+ ) {
+ return new ThreadPoolExecutor(nThreads, nThreads,
+ 0L, TimeUnit.MILLISECONDS,
+ new LinkedBlockingQueue<Runnable>(),
+ new SshdThreadFactory(poolName),
+ new ThreadPoolExecutor.CallerRunsPolicy());
+ }
+
+ public static ExecutorService newCachedThreadPool(
+ String poolName
+ ) {
+ return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
+ 60L, TimeUnit.SECONDS,
+ new SynchronousQueue<Runnable>(),
+ new SshdThreadFactory(poolName),
+ new ThreadPoolExecutor.CallerRunsPolicy());
+ }
+
+ public static ScheduledExecutorService newSingleThreadScheduledExecutor(
+ String poolName
+ ) {
+ return new ScheduledThreadPoolExecutor(
+ 1,
+ new SshdThreadFactory(poolName));
+ }
+
+ public static ExecutorService newSingleThreadExecutor(
+ String poolName
+ ) {
+ return newFixedThreadPool(poolName, 1);
+ }
+
+ public static class SshdThreadFactory implements ThreadFactory {
+ private final ThreadGroup group;
+ private final AtomicInteger threadNumber = new AtomicInteger(1);
+ private final String namePrefix;
+
+ public SshdThreadFactory(String name) {
+ SecurityManager s = System.getSecurityManager();
+ group = (s != null) ? s.getThreadGroup() :
+ Thread.currentThread().getThreadGroup();
+ namePrefix = "sshd-" + name + "-thread-";
+ }
+
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
+ if (t.isDaemon())
+ t.setDaemon(false);
+ if (t.getPriority() != Thread.NORM_PRIORITY)
+ t.setPriority(Thread.NORM_PRIORITY);
+ return t;
+ }
+
+ }
+
+ private ThreadUtils() {
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b8c21ebc/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java b/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
index c00ce77..397f111 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
@@ -23,8 +23,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import org.apache.sshd.common.util.ThreadUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
@@ -58,15 +58,18 @@ public class InvertedShellWrapper implements Command, SessionAware {
private boolean shutdownExecutor;
public InvertedShellWrapper(InvertedShell shell) {
- this(shell, Executors.newSingleThreadExecutor(), true, DEFAULT_BUFFER_SIZE);
+ this(shell, DEFAULT_BUFFER_SIZE);
}
public InvertedShellWrapper(InvertedShell shell, Executor executor) {
- this(shell, executor, false, DEFAULT_BUFFER_SIZE);
+ this(shell, executor, DEFAULT_BUFFER_SIZE);
}
public InvertedShellWrapper(InvertedShell shell, int bufferSize) {
- this(shell, Executors.newSingleThreadExecutor(), true, bufferSize);
+ this(shell,
+ ThreadUtils.newSingleThreadExecutor("shell[" + Integer.toHexString(shell.hashCode()) + "]"),
+ true,
+ bufferSize);
}
public InvertedShellWrapper(InvertedShell shell, Executor executor, int bufferSize) {
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b8c21ebc/sshd-sftp/src/main/java/org/apache/sshd/sftp/subsystem/SftpSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/subsystem/SftpSubsystem.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/subsystem/SftpSubsystem.java
index 60f0bcd..3e9c8bc 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/subsystem/SftpSubsystem.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/subsystem/SftpSubsystem.java
@@ -25,6 +25,7 @@ import org.apache.sshd.common.file.FileSystemView;
import org.apache.sshd.common.file.SshFile;
import org.apache.sshd.common.util.Buffer;
import org.apache.sshd.common.util.SelectorUtils;
+import org.apache.sshd.common.util.ThreadUtils;
import org.apache.sshd.server.*;
import org.apache.sshd.server.channel.ChannelDataReceiver;
import org.apache.sshd.server.channel.ChannelSession;
@@ -96,7 +97,7 @@ public class SftpSubsystem implements Command, SessionAware, FileSystemAware, Sf
private final ExecutorService executor;
public SftpSubsystem() {
- executor = Executors.newSingleThreadExecutor();
+ executor = ThreadUtils.newSingleThreadExecutor("sftp[" + Integer.toHexString(hashCode()) + "]");
}
public void setSftpLet(final Sftplet sftpLet) {