mirror of
https://github.com/alibaba/arthas.git
synced 2024-04-21 10:21:39 +00:00
improve arthas stop logic. #1514
This commit is contained in:
@@ -2,23 +2,17 @@ package com.taobao.arthas.core.command.basic1000;
|
||||
|
||||
import com.alibaba.arthas.deps.org.slf4j.Logger;
|
||||
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
|
||||
import com.taobao.arthas.core.advisor.Enhancer;
|
||||
import com.taobao.arthas.core.command.model.MessageModel;
|
||||
import com.taobao.arthas.core.command.model.ResetModel;
|
||||
import com.taobao.arthas.core.command.model.ShutdownModel;
|
||||
import com.taobao.arthas.core.server.ArthasBootstrap;
|
||||
import com.taobao.arthas.core.shell.ShellServer;
|
||||
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
|
||||
import com.taobao.arthas.core.shell.command.CommandProcess;
|
||||
import com.taobao.arthas.core.shell.session.SessionManager;
|
||||
import com.taobao.arthas.core.util.affect.EnhancerAffect;
|
||||
import com.taobao.arthas.core.util.matcher.WildcardMatcher;
|
||||
import com.taobao.middleware.cli.annotations.Hidden;
|
||||
import com.taobao.middleware.cli.annotations.Name;
|
||||
import com.taobao.middleware.cli.annotations.Summary;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
/**
|
||||
* 关闭命令
|
||||
*
|
||||
@@ -38,11 +32,11 @@ public class ShutdownCommand extends AnnotatedCommand {
|
||||
}
|
||||
|
||||
public static void shutdown(CommandProcess process) {
|
||||
ArthasBootstrap arthasBootstrap = ArthasBootstrap.getInstance();
|
||||
try {
|
||||
// 退出之前需要重置所有的增强类
|
||||
process.appendResult(new MessageModel("Resetting all enhanced classes ..."));
|
||||
Instrumentation inst = process.session().getInstrumentation();
|
||||
EnhancerAffect enhancerAffect = Enhancer.reset(inst, new WildcardMatcher("*"));
|
||||
EnhancerAffect enhancerAffect = arthasBootstrap.reset();
|
||||
process.appendResult(new ResetModel(enhancerAffect));
|
||||
process.appendResult(new ShutdownModel(true, "Arthas Server is going to shut down..."));
|
||||
} catch (Throwable e) {
|
||||
@@ -50,23 +44,7 @@ public class ShutdownCommand extends AnnotatedCommand {
|
||||
process.appendResult(new ShutdownModel(false, "An error occurred when stopping arthas server."));
|
||||
} finally {
|
||||
process.end();
|
||||
ShellServer server = ArthasBootstrap.getInstance().getShellServer();
|
||||
if (server != null) {
|
||||
try {
|
||||
server.close();
|
||||
} catch (Throwable e) {
|
||||
logger.error("close shell server failure", e);
|
||||
}
|
||||
}
|
||||
|
||||
SessionManager sessionManager = ArthasBootstrap.getInstance().getSessionManager();
|
||||
if (sessionManager != null){
|
||||
try {
|
||||
sessionManager.close();
|
||||
} catch (Throwable e) {
|
||||
logger.error("close session manager failure", e);
|
||||
}
|
||||
}
|
||||
arthasBootstrap.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import java.arthas.SpyAPI;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.lang.instrument.UnmodifiableClassException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.security.CodeSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -28,6 +28,7 @@ import com.alibaba.arthas.tunnel.client.TunnelClient;
|
||||
import com.taobao.arthas.common.AnsiLog;
|
||||
import com.taobao.arthas.common.PidUtils;
|
||||
import com.taobao.arthas.common.SocketUtils;
|
||||
import com.taobao.arthas.core.advisor.Enhancer;
|
||||
import com.taobao.arthas.core.advisor.TransformerManager;
|
||||
import com.taobao.arthas.core.command.BuiltinCommandPack;
|
||||
import com.taobao.arthas.core.command.view.ResultViewResolver;
|
||||
@@ -54,6 +55,9 @@ import com.taobao.arthas.core.util.ArthasBanner;
|
||||
import com.taobao.arthas.core.util.FileUtils;
|
||||
import com.taobao.arthas.core.util.LogUtil;
|
||||
import com.taobao.arthas.core.util.UserStatUtil;
|
||||
import com.taobao.arthas.core.util.affect.EnhancerAffect;
|
||||
import com.taobao.arthas.core.util.matcher.WildcardMatcher;
|
||||
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.util.concurrent.DefaultThreadFactory;
|
||||
@@ -316,7 +320,7 @@ public class ArthasBootstrap {
|
||||
options.setWelcomeMessage(ArthasBanner.welcome(welcomeInfos));
|
||||
}
|
||||
|
||||
shellServer = new ShellServerImpl(options, this);
|
||||
shellServer = new ShellServerImpl(options);
|
||||
BuiltinCommandPack builtinCommands = new BuiltinCommandPack();
|
||||
List<CommandResolver> resolvers = new ArrayList<CommandResolver>();
|
||||
resolvers.add(builtinCommands);
|
||||
@@ -350,7 +354,7 @@ public class ArthasBootstrap {
|
||||
shellServer.listen(new BindHandler(isBindRef));
|
||||
|
||||
//http api session manager
|
||||
sessionManager = new SessionManagerImpl(options, this, shellServer.getCommandManager(), shellServer.getJobController());
|
||||
sessionManager = new SessionManagerImpl(options, shellServer.getCommandManager(), shellServer.getJobController());
|
||||
//http api handler
|
||||
httpApiHandler = new HttpApiHandler(historyManager, sessionManager);
|
||||
|
||||
@@ -400,6 +404,13 @@ public class ArthasBootstrap {
|
||||
return isBindRef.get();
|
||||
}
|
||||
|
||||
public EnhancerAffect reset() throws UnmodifiableClassException {
|
||||
return Enhancer.reset(this.instrumentation, new WildcardMatcher("*"));
|
||||
}
|
||||
|
||||
/**
|
||||
* call reset() before destroy()
|
||||
*/
|
||||
public void destroy() {
|
||||
timer.cancel();
|
||||
if (this.tunnelClient != null) {
|
||||
@@ -409,10 +420,8 @@ public class ArthasBootstrap {
|
||||
logger().error("stop tunnel client error", e);
|
||||
}
|
||||
}
|
||||
executorService.shutdownNow();
|
||||
transformerManager.destroy();
|
||||
UserStatUtil.destroy();
|
||||
shutdownWorkGroup();
|
||||
// clear the reference in Spy class.
|
||||
cleanUpSpyReference();
|
||||
try {
|
||||
@@ -424,8 +433,28 @@ public class ArthasBootstrap {
|
||||
if (loggerContext != null) {
|
||||
loggerContext.stop();
|
||||
}
|
||||
|
||||
if (sessionManager != null){
|
||||
try {
|
||||
sessionManager.close();
|
||||
} catch (Throwable e) {
|
||||
logger().error("close session manager failure", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (shellServer != null) {
|
||||
try {
|
||||
shellServer.close();
|
||||
} catch (Throwable e) {
|
||||
logger().error("close shell server failure", e);
|
||||
}
|
||||
}
|
||||
|
||||
shellServer = null;
|
||||
sessionManager = null;
|
||||
|
||||
executorService.shutdownNow();
|
||||
shutdownWorkGroup();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -48,7 +48,6 @@ public class ShellServerImpl extends ShellServer {
|
||||
private final long timeoutMillis;
|
||||
private final long reaperInterval;
|
||||
private String welcomeMessage;
|
||||
private ArthasBootstrap bootstrap;
|
||||
private Instrumentation instrumentation;
|
||||
private long pid;
|
||||
private boolean closed = true;
|
||||
@@ -58,10 +57,6 @@ public class ShellServerImpl extends ShellServer {
|
||||
private JobControllerImpl jobController = new GlobalJobControllerImpl();
|
||||
|
||||
public ShellServerImpl(ShellServerOptions options) {
|
||||
this(options, null);
|
||||
}
|
||||
|
||||
public ShellServerImpl(ShellServerOptions options, ArthasBootstrap bootstrap) {
|
||||
this.welcomeMessage = options.getWelcomeMessage();
|
||||
this.termServers = new ArrayList<TermServer>();
|
||||
this.timeoutMillis = options.getSessionTimeout();
|
||||
@@ -70,7 +65,6 @@ public class ShellServerImpl extends ShellServer {
|
||||
this.resolvers = new CopyOnWriteArrayList<CommandResolver>();
|
||||
this.commandManager = new InternalCommandManager(resolvers);
|
||||
this.instrumentation = options.getInstrumentation();
|
||||
this.bootstrap = bootstrap;
|
||||
this.pid = options.getPid();
|
||||
|
||||
// Register builtin commands so they are listed in help
|
||||
@@ -240,7 +234,6 @@ public class ShellServerImpl extends ShellServer {
|
||||
}
|
||||
jobController.close();
|
||||
sessionsClosed.setHandler(handler);
|
||||
bootstrap.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-6
@@ -5,8 +5,6 @@ import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
|
||||
import com.taobao.arthas.core.command.model.MessageModel;
|
||||
import com.taobao.arthas.core.distribution.ResultConsumer;
|
||||
import com.taobao.arthas.core.distribution.SharingResultDistributor;
|
||||
import com.taobao.arthas.core.distribution.impl.SharingResultDistributorImpl;
|
||||
import com.taobao.arthas.core.server.ArthasBootstrap;
|
||||
import com.taobao.arthas.core.shell.ShellServerOptions;
|
||||
import com.taobao.arthas.core.shell.session.Session;
|
||||
import com.taobao.arthas.core.shell.session.SessionManager;
|
||||
@@ -25,7 +23,6 @@ import java.util.concurrent.*;
|
||||
*/
|
||||
public class SessionManagerImpl implements SessionManager {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SessionManagerImpl.class);
|
||||
private final ArthasBootstrap bootstrap;
|
||||
private final InternalCommandManager commandManager;
|
||||
private final Instrumentation instrumentation;
|
||||
private final JobController jobController;
|
||||
@@ -37,9 +34,8 @@ public class SessionManagerImpl implements SessionManager {
|
||||
private boolean closed = false;
|
||||
private ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
public SessionManagerImpl(ShellServerOptions options, ArthasBootstrap bootstrap, InternalCommandManager commandManager,
|
||||
public SessionManagerImpl(ShellServerOptions options, InternalCommandManager commandManager,
|
||||
JobController jobController) {
|
||||
this.bootstrap = bootstrap;
|
||||
this.commandManager = commandManager;
|
||||
this.jobController = jobController;
|
||||
this.sessions = new ConcurrentHashMap<String, Session>();
|
||||
@@ -101,7 +97,6 @@ public class SessionManagerImpl implements SessionManager {
|
||||
}
|
||||
|
||||
jobController.close();
|
||||
bootstrap.destroy();
|
||||
}
|
||||
|
||||
private synchronized void setEvictTimer() {
|
||||
|
||||
Reference in New Issue
Block a user