Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize shutdown hook #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.meituan.dorado.trace.TraceFactory;
import com.meituan.dorado.util.ClazzUtil;

public abstract class AbstractConfig {
public abstract class AbstractConfig implements Disposable {

protected String serviceName;
// 服务接口, 调用端必配, 服务端若没有配置则取实现类的第一个接口
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.meituan.dorado.config.service;

public interface Disposable {

void destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.List;
import java.util.concurrent.BlockingQueue;

public class ProviderConfig {
public class ProviderConfig implements Disposable {

private static final Logger LOGGER = LoggerFactory.getLogger(ProviderConfig.class);

Expand Down Expand Up @@ -66,8 +66,6 @@ public class ProviderConfig {
private List<Filter> filters = Collections.emptyList();
private String env = Constants.EnvType.TEST.getEnvName();

private volatile ShutDownHook hook;

private volatile boolean destroyed;

public void init() {
Expand Down Expand Up @@ -104,10 +102,7 @@ public synchronized void destroy() {
}

protected synchronized void addShutDownHook() {
if (hook == null) {
hook = new ShutDownHook(this);
Runtime.getRuntime().addShutdownHook(hook);
}
ShutdownHook.register(this);
}

public List<String> getServiceList() {
Expand Down Expand Up @@ -250,17 +245,4 @@ public void setThreadPoolQueue(BlockingQueue<Runnable> threadPoolQueue) {
this.threadPoolQueue = threadPoolQueue;
}

class ShutDownHook extends Thread {
private ProviderConfig config;

public ShutDownHook(ProviderConfig config) {
this.config = config;
}

@Override
public void run() {
hook = null;
config.destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.List;
import java.util.Map;

public class ReferenceConfig<T> extends AbstractConfig {
public class ReferenceConfig<T> extends AbstractConfig implements Disposable {

// 必配项 服务端 appkey
private String remoteAppkey;
Expand Down Expand Up @@ -84,8 +84,6 @@ public class ReferenceConfig<T> extends AbstractConfig {
private transient ClusterHandler<?> clusterHandler;

private volatile boolean destroyed;
private volatile ShutDownHook hook;


public synchronized T get() {
if (destroyed) {
Expand Down Expand Up @@ -141,10 +139,7 @@ private void configRouter() {
}

protected synchronized void addShutDownHook() {
if (hook == null) {
hook = new ShutDownHook(this);
Runtime.getRuntime().addShutdownHook(hook);
}
ShutdownHook.register(this);
}

/**
Expand Down Expand Up @@ -345,17 +340,4 @@ public void setEnv(String env) {
this.env = env;
}

class ShutDownHook extends Thread {
private ReferenceConfig config;

public ShutDownHook(ReferenceConfig config) {
this.config = config;
}

@Override
public void run() {
hook = null;
config.destroy();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.meituan.dorado.config.service;

import java.util.LinkedList;
import java.util.List;

public class ShutdownHook extends Thread {

private static final List<Disposable> configs = new LinkedList<>();

static {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
}

public static synchronized void register(Disposable config) {
configs.add(config);
}

private ShutdownHook() {
super("DoradoShutdownHook-Thread");
}
@Override
public void run() {
for (Disposable config : configs) {
config.destroy();
}
}
}
30 changes: 18 additions & 12 deletions dorado/dorado-doc/manual-user/ShutdownGracefully.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ Dorado的优雅关闭通过ShutDownHook方式实现,调用端和服务端通

```java
protected synchronized void addShutDownHook() {
if (hook == null) {
hook = new ShutDownHook(this);
Runtime.getRuntime().addShutdownHook(hook);
}
ShutdownHook.register(this);
}

class ShutDownHook extends Thread {
private ReferenceConfig config;

public ShutDownHook(ReferenceConfig config) {
this.config = config;
public class ShutdownHook extends Thread {

private static final List<Disposable> configs = new LinkedList<>();

static {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
}

public static synchronized void register(Disposable config) {
configs.add(config);
}

private ShutdownHook() {
super("DoradoShutdownHook-Thread");
}

@Override
public void run() {
hook = null;
config.destroy();
for (Disposable config : configs) {
config.destroy();
}
}
}
```