|
|
@@ -4,6 +4,7 @@ import com.nju.edu.paasMonitorClient.config.Config;
|
|
|
import com.nju.edu.paasMonitorClient.entity.*;
|
|
|
import com.nju.edu.paasMonitorClient.entity.Error;
|
|
|
import com.nju.edu.paasMonitorClient.util.PostHelper;
|
|
|
+import com.nju.edu.paasMonitorClient.util.SystemInfoUtil;
|
|
|
import com.nju.edu.paasMonitorCommon.dto.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
@@ -12,25 +13,52 @@ import java.util.concurrent.*;
|
|
|
|
|
|
@Slf4j
|
|
|
public class MonitorConsumer implements Runnable {
|
|
|
- private final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 8,
|
|
|
- 10000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(40));
|
|
|
- private int errorCount = 0;
|
|
|
- private BlockingQueue<Message> messageQueue;
|
|
|
+ private static final int CORE_POOL_SIZE = SystemInfoUtil.getCpuCoreSize() * 2;
|
|
|
+ private final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,
|
|
|
+ CORE_POOL_SIZE, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
|
|
|
+ private int threadPoolErrorCount = 0;
|
|
|
+ private int mqErrorCount = 0;
|
|
|
+ private final BlockingQueue<Message> messageQueue = new LinkedBlockingQueue<>(2000);
|
|
|
private final String serverKey = "server.address.prefix";
|
|
|
- private static final int REJECT_SLEEP_TIME = 50;
|
|
|
+ private volatile static MonitorConsumer instance;
|
|
|
+ private boolean active = true;
|
|
|
|
|
|
- public MonitorConsumer(BlockingQueue<Message> queue) {
|
|
|
- this.messageQueue = queue;
|
|
|
+ public static MonitorConsumer getInstance() {
|
|
|
+ if (instance == null || !instance.active) {
|
|
|
+ synchronized (MonitorConsumer.class) {
|
|
|
+ if (instance == null || !instance.active) {
|
|
|
+ instance = new MonitorConsumer();
|
|
|
+ Thread thread = new Thread(instance);
|
|
|
+ thread.start();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void add(Message message) {
|
|
|
+ boolean success = messageQueue.offer(message);
|
|
|
+ if (!success) {
|
|
|
+ mqErrorCount++;
|
|
|
+ if (mqErrorCount == 1 || mqErrorCount % 50 == 0) {
|
|
|
+ log.error("Can't add message to queue");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private MonitorConsumer() {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void run() {
|
|
|
- while (true) {
|
|
|
+ while (active || !messageQueue.isEmpty()) {
|
|
|
Message backup = null;
|
|
|
try {
|
|
|
Message message = messageQueue.poll(10, TimeUnit.MILLISECONDS);
|
|
|
backup = message;
|
|
|
- if (message != null) {
|
|
|
+ if (message == null) {
|
|
|
+ active = false;
|
|
|
+ } else {
|
|
|
threadPoolExecutor.execute(() -> {
|
|
|
MessageDto send;
|
|
|
try {
|
|
|
@@ -38,21 +66,21 @@ public class MonitorConsumer implements Runnable {
|
|
|
if (message instanceof Call) {
|
|
|
address += "call";
|
|
|
send = new CallDto(message.getSystem(), message.getCategory(),
|
|
|
- message.getName(),message.getCreateTime(),((Call) message).getDuration(),
|
|
|
+ message.getName(), message.getCreateTime(), ((Call) message).getDuration(),
|
|
|
((Call) message).getStatus().getVal());
|
|
|
} else if (message instanceof Error) {
|
|
|
address += "error";
|
|
|
send = new ErrorDto(message.getSystem(), message.getCategory(),
|
|
|
- message.getName(),message.getCreateTime(),((Error) message).getMessage(),
|
|
|
+ message.getName(), message.getCreateTime(), ((Error) message).getMessage(),
|
|
|
((Error) message).getExceptionInfo(), ((Error) message).getStackTrace());
|
|
|
} else if (message instanceof Event) {
|
|
|
address += "event";
|
|
|
send = new EventDto(message.getSystem(), message.getCategory(),
|
|
|
- message.getName(),message.getCreateTime(),((Event) message).getStatus().getVal());
|
|
|
+ message.getName(), message.getCreateTime(), ((Event) message).getStatus().getVal());
|
|
|
} else if (message instanceof Metric) {
|
|
|
address += "metric";
|
|
|
send = new MetricDto(message.getSystem(), message.getCategory(),
|
|
|
- message.getName(),message.getCreateTime(),((Metric) message).getCount(),
|
|
|
+ message.getName(), message.getCreateTime(), ((Metric) message).getCount(),
|
|
|
((Metric) message).getType().getVal());
|
|
|
} else {
|
|
|
throw new Exception("Wrong type of message");
|
|
|
@@ -67,21 +95,18 @@ public class MonitorConsumer implements Runnable {
|
|
|
}
|
|
|
} catch (InterruptedException ignored) {
|
|
|
} catch (RejectedExecutionException e) {
|
|
|
- if(backup != null){
|
|
|
+ if (backup != null) {
|
|
|
boolean success = messageQueue.offer(backup);
|
|
|
- if(!success){
|
|
|
- log.error("Can't add message to queue");
|
|
|
+ if (!success) {
|
|
|
+ log.error("Can't add message to queue", e);
|
|
|
}
|
|
|
}
|
|
|
- errorCount++;
|
|
|
- if (errorCount == 1 || errorCount % 50 == 0) {
|
|
|
- log.error("ThreadPool is full", e);
|
|
|
- }
|
|
|
- try {
|
|
|
- Thread.sleep(REJECT_SLEEP_TIME);
|
|
|
- }catch (InterruptedException ignore){
|
|
|
+ threadPoolErrorCount++;
|
|
|
+ if (threadPoolErrorCount == 1 || threadPoolErrorCount % 50 == 0) {
|
|
|
+ log.error("ThreadPool is full");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ threadPoolExecutor.shutdown();
|
|
|
}
|
|
|
}
|