improve monitor creating, handle errors (#7866)

This commit is contained in:
Gong Dewei 2021-05-26 23:36:11 +08:00 committed by GitHub
parent 04f10d3dd3
commit 0fa05b8982
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 31 deletions

View File

@ -27,9 +27,7 @@ import org.apache.dubbo.monitor.MonitorService;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
@ -55,7 +53,7 @@ public abstract class AbstractMonitorFactory implements MonitorFactory {
*/
private static final Map<String, Monitor> MONITORS = new ConcurrentHashMap<String, Monitor>();
private static final Map<String, CompletableFuture<Monitor>> FUTURES = new ConcurrentHashMap<String, CompletableFuture<Monitor>>();
private static final Map<String, Future<Monitor>> FUTURES = new ConcurrentHashMap<String, Future<Monitor>>();
/**
* The monitor create executor
@ -85,10 +83,18 @@ public abstract class AbstractMonitorFactory implements MonitorFactory {
}
final URL monitorUrl = url;
final CompletableFuture<Monitor> completableFuture = CompletableFuture.supplyAsync(() -> AbstractMonitorFactory.this.createMonitor(monitorUrl));
FUTURES.put(key, completableFuture);
completableFuture.thenRunAsync(new MonitorListener(key), EXECUTOR);
future = EXECUTOR.submit(() -> {
try {
Monitor m = createMonitor(monitorUrl);
MONITORS.put(key, m);
FUTURES.remove(key);
return m;
} catch (Throwable e) {
logger.warn("Create monitor failed, monitor data will not be collected until you fix this problem. monitorUrl: " + monitorUrl, e);
return null;
}
});
FUTURES.put(key, future);
return null;
} finally {
// unlock
@ -98,28 +104,4 @@ public abstract class AbstractMonitorFactory implements MonitorFactory {
protected abstract Monitor createMonitor(URL url);
class MonitorListener implements Runnable {
private String key;
public MonitorListener(String key) {
this.key = key;
}
@Override
public void run() {
try {
CompletableFuture<Monitor> completableFuture = AbstractMonitorFactory.FUTURES.get(key);
AbstractMonitorFactory.MONITORS.put(key, completableFuture.get());
AbstractMonitorFactory.FUTURES.remove(key);
} catch (InterruptedException e) {
logger.warn("Thread was interrupted unexpectedly, monitor will never be got.");
AbstractMonitorFactory.FUTURES.remove(key);
} catch (ExecutionException e) {
logger.warn("Create monitor failed, monitor data will not be collected until you fix this problem. ", e);
}
}
}
}