[monitor-collector-scheduler] 监控探测接口,一次性临时任务调度编码
This commit is contained in:
@@ -11,6 +11,7 @@ import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.EventListener;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -162,10 +163,12 @@ public class CommonDispatcher implements MetricsTaskDispatch, CollectDataDispatc
|
||||
}
|
||||
} else {
|
||||
// 若是临时性一次任务,需等待所有指标组的采集数据统一包装返回
|
||||
// todo 将当前指标组数据插入job里统一组装
|
||||
// 将当前指标组数据插入job里统一组装
|
||||
job.addCollectMetricsData(metricsData);
|
||||
if (metricsSet == null) {
|
||||
// 此Job所有指标组采集执行完成
|
||||
// todo 将所有指标组数据组合一起发送到回调函数
|
||||
// 将所有指标组数据组合一起通知结果监听器
|
||||
timerDispatch.responseSyncJobData(job.getId(), job.getMetricsDataTemps());
|
||||
} else if (!metricsSet.isEmpty()) {
|
||||
// 当前级别指标组执行完成,开始执行下一级别的指标组
|
||||
metricsSet.forEach(metricItem -> {
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
package com.usthe.collector.dispatch.entrance.http;
|
||||
|
||||
import com.usthe.collector.dispatch.timer.TimerDispatch;
|
||||
import com.usthe.common.entity.job.Job;
|
||||
import com.usthe.common.entity.message.CollectRep;
|
||||
import com.usthe.common.util.ProtoJsonUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 采集job管理提供api接口
|
||||
* @author tomsun28
|
||||
@@ -13,13 +22,36 @@ import reactor.core.publisher.Mono;
|
||||
@RestController
|
||||
public class CollectJobController {
|
||||
|
||||
@Autowired
|
||||
private TimerDispatch timerDispatch;
|
||||
|
||||
/**
|
||||
* 执行一次性采集任务,获取采集数据响应
|
||||
* @return 采集结果
|
||||
*/
|
||||
@PostMapping("/job")
|
||||
public Mono<Object> collectJobData(Job job) {
|
||||
return null;
|
||||
@PostMapping(path = "/job/sync", consumes = MediaType.APPLICATION_JSON_VALUE,
|
||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public Mono<List<String>> collectSyncJobData(@RequestBody Job job) {
|
||||
return Mono.create(sink -> {
|
||||
CollectResponseEventListener listener = new CollectResponseEventListener() {
|
||||
@Override
|
||||
public void response(List<CollectRep.MetricsData> responseMetrics) {
|
||||
if (responseMetrics == null || responseMetrics.isEmpty()) {
|
||||
sink.success();
|
||||
} else {
|
||||
List<String> jsons = new ArrayList<>(responseMetrics.size());
|
||||
for (CollectRep.MetricsData metricsData : responseMetrics) {
|
||||
String json = ProtoJsonUtil.toJsonStr(metricsData);
|
||||
if (json != null) {
|
||||
jsons.add(json);
|
||||
}
|
||||
}
|
||||
sink.success(jsons);
|
||||
}
|
||||
}
|
||||
};
|
||||
timerDispatch.addJob(job, listener);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.usthe.collector.dispatch.entrance.http;
|
||||
|
||||
import com.usthe.common.entity.message.CollectRep;
|
||||
|
||||
import java.util.EventListener;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 一次性采集任务响应结果监听器
|
||||
* @author tomsun28
|
||||
* @date 2021/11/16 10:09
|
||||
*/
|
||||
public interface CollectResponseEventListener extends EventListener {
|
||||
|
||||
/**
|
||||
* 采集任务完成结果通知
|
||||
* @param responseMetrics 响应数据
|
||||
*/
|
||||
public default void response(List<CollectRep.MetricsData> responseMetrics) {}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.usthe.collector.dispatch.timer;
|
||||
|
||||
|
||||
import com.usthe.collector.dispatch.entrance.http.CollectResponseEventListener;
|
||||
import com.usthe.common.entity.job.Job;
|
||||
import com.usthe.common.entity.message.CollectRep;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -15,8 +18,9 @@ public interface TimerDispatch {
|
||||
/**
|
||||
* 增加新的job
|
||||
* @param addJob job
|
||||
* @param eventListener 一次性同步任务监听器,异步任务不需要
|
||||
*/
|
||||
void addJob(Job addJob);
|
||||
void addJob(Job addJob, CollectResponseEventListener eventListener);
|
||||
|
||||
/**
|
||||
* 调度循环周期性job
|
||||
@@ -32,4 +36,11 @@ public interface TimerDispatch {
|
||||
* @param isCyclic 是否是周期性任务,true是, false为临时性任务
|
||||
*/
|
||||
void deleteJob(long jobId, boolean isCyclic);
|
||||
|
||||
/**
|
||||
* 一次性同步采集任务采集结果通知监听器
|
||||
* @param jobId jobId
|
||||
* @param metricsDataTemps 采集结果数据
|
||||
*/
|
||||
void responseSyncJobData(long jobId, List<CollectRep.MetricsData> metricsDataTemps);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.usthe.collector.dispatch.timer;
|
||||
|
||||
import com.usthe.collector.dispatch.entrance.http.CollectResponseEventListener;
|
||||
import com.usthe.common.entity.job.Job;
|
||||
import com.usthe.common.entity.message.CollectRep;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.EventListener;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -26,6 +30,11 @@ public class TimerDispatcher implements TimerDispatch {
|
||||
* 已存在的临时性调度任务
|
||||
*/
|
||||
private Map<Long, Timeout> currentTempTaskMap;
|
||||
/**
|
||||
* 一次性任务响应监听器持有
|
||||
* jobId - listener
|
||||
*/
|
||||
private Map<Long, CollectResponseEventListener> eventListeners;
|
||||
|
||||
public TimerDispatcher() {
|
||||
this.wheelTimer = new HashedWheelTimer(r -> {
|
||||
@@ -34,17 +43,20 @@ public class TimerDispatcher implements TimerDispatch {
|
||||
return ret;
|
||||
}, 10, TimeUnit.SECONDS, 512);
|
||||
this.currentCyclicTaskMap = new ConcurrentHashMap<>(1024);
|
||||
this.currentTempTaskMap = new ConcurrentHashMap<>(1024);
|
||||
this.currentTempTaskMap = new ConcurrentHashMap<>(64);
|
||||
eventListeners = new ConcurrentHashMap<>(64);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addJob(Job addJob) {
|
||||
public void addJob(Job addJob, CollectResponseEventListener eventListener) {
|
||||
WheelTimerJob timerJob = new WheelTimerJob(addJob);
|
||||
Timeout timeout = wheelTimer.newTimeout(timerJob, addJob.getInterval(), TimeUnit.SECONDS);
|
||||
if (addJob.isCyclic()) {
|
||||
Timeout timeout = wheelTimer.newTimeout(timerJob, addJob.getInterval(), TimeUnit.SECONDS);
|
||||
currentCyclicTaskMap.put(addJob.getId(), timeout);
|
||||
} else {
|
||||
Timeout timeout = wheelTimer.newTimeout(timerJob, 0, TimeUnit.SECONDS);
|
||||
currentTempTaskMap.put(addJob.getId(), timeout);
|
||||
eventListeners.put(addJob.getId(), eventListener);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,4 +84,11 @@ public class TimerDispatcher implements TimerDispatch {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void responseSyncJobData(long jobId, List<CollectRep.MetricsData> metricsDataTemps) {
|
||||
currentTempTaskMap.remove(jobId);
|
||||
CollectResponseEventListener eventListener = eventListeners.remove(jobId);
|
||||
eventListener.response(metricsDataTemps);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 8081
|
||||
port: 1157
|
||||
spring:
|
||||
application:
|
||||
name: ${HOSTNAME:@collecor@}${PID}
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
|
||||
<!-- 开发环境配置 -->
|
||||
<springProfile name="dev">
|
||||
<root level="INFO">
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="ConsoleAppender"/>
|
||||
<appender-ref ref="SystemOutFileAppender"/>
|
||||
<appender-ref ref="ErrOutFileAppender"/>
|
||||
|
||||
Reference in New Issue
Block a user