use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class CommonDispatcher method dispatchCollectData.
@Override
public void dispatchCollectData(Timeout timeout, Metrics metrics, CollectRep.MetricsData metricsData) {
WheelTimerTask timerJob = (WheelTimerTask) timeout.task();
Job job = timerJob.getJob();
metricsTimeoutMonitorMap.remove(job.getId() + "-" + metrics.getName());
Set<Metrics> metricsSet = job.getNextCollectMetrics(metrics, false);
if (job.isCyclic()) {
// If it is an asynchronous periodic cyclic task, directly send the collected data of the indicator group to the message middleware
// 若是异步的周期性循环任务,直接发送指标组的采集数据到消息中间件
kafkaDataExporter.send(metricsData);
// 或判断采集指标组是否优先级为0,即为可用性采集指标组 若可用性采集失败 则取消后面的指标组调度直接进入下一轮调度
if (metricsSet == null || (metrics.getPriority() == (byte) 0 && metricsData.getCode() != CollectRep.Code.SUCCESS)) {
// 先判断此次任务执行时间与任务采集间隔时间
if (timeout.isCancelled()) {
return;
}
long spendTime = System.currentTimeMillis() - job.getDispatchTime();
long interval = job.getInterval() - spendTime / 1000;
interval = interval <= 0 ? 0 : interval;
// Reset Construction Execution Metrics Group View 重置构造执行指标组视图
job.constructPriorMetrics();
timerDispatch.cyclicJob(timerJob, interval, TimeUnit.SECONDS);
} else if (!metricsSet.isEmpty()) {
// The execution of the current level indicator group is completed, and the execution of the next level indicator group starts
// 当前级别指标组执行完成,开始执行下一级别的指标组
metricsSet.forEach(metricItem -> {
MetricsCollect metricsCollect = new MetricsCollect(metricItem, timeout, this);
jobRequestQueue.addJob(metricsCollect);
metricsTimeoutMonitorMap.put(job.getId() + "-" + metricItem.getName(), new MetricsTime(System.currentTimeMillis(), metricItem, timeout));
});
} else {
// The list of indicator groups at the current execution level has not been fully executed.
// It needs to wait for the execution of other indicator groups of the same level to complete the execution and enter the next level for execution.
// 当前执行级别的指标组列表未全执行完成,
// 需等待其它同级别指标组执行完成后进入下一级别执行
}
} else {
// If it is a temporary one-time task, you need to wait for the collected data of all indicator groups to be packaged and returned.
// Insert the current indicator group data into the job for unified assembly
// 若是临时性一次任务,需等待所有指标组的采集数据统一包装返回
// 将当前指标组数据插入job里统一组装
job.addCollectMetricsData(metricsData);
if (metricsSet == null) {
// The collection and execution of all indicator groups of this job are completed
// and the result listener is notified of the combination of all indicator group data
// 此Job所有指标组采集执行完成
// 将所有指标组数据组合一起通知结果监听器
timerDispatch.responseSyncJobData(job.getId(), job.getResponseDataTemp());
} else if (!metricsSet.isEmpty()) {
// The execution of the current level indicator group is completed, and the execution of the next level indicator group starts
// 当前级别指标组执行完成,开始执行下一级别的指标组
metricsSet.forEach(metricItem -> {
MetricsCollect metricsCollect = new MetricsCollect(metricItem, timeout, this);
jobRequestQueue.addJob(metricsCollect);
metricsTimeoutMonitorMap.put(job.getId() + "-" + metricItem.getName(), new MetricsTime(System.currentTimeMillis(), metricItem, timeout));
});
} else {
// The list of indicator groups at the current execution level has not been fully executed.
// It needs to wait for the execution of other indicator groups of the same level to complete the execution and enter the next level for execution.
// 当前执行级别的指标组列表未全执行完成,
// 需等待其它同级别指标组执行完成后进入下一级别执行
}
}
}
use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class CommonDispatcher method dispatchMetricsTask.
@Override
public void dispatchMetricsTask(Timeout timeout) {
// Divide the collection task of a single application into corresponding collection tasks of the indicator group according to the indicator group under it. AbstractCollect
// Put each indicator group into the thread pool for scheduling
// 将单个应用的采集任务根据其下的指标组拆分为对应的指标组采集任务 AbstractCollect
// 将每个指标组放入线程池进行调度
WheelTimerTask timerTask = (WheelTimerTask) timeout.task();
Job job = timerTask.getJob();
job.constructPriorMetrics();
Set<Metrics> metricsSet = job.getNextCollectMetrics(null, true);
metricsSet.forEach(metrics -> {
MetricsCollect metricsCollect = new MetricsCollect(metrics, timeout, this);
jobRequestQueue.addJob(metricsCollect);
metricsTimeoutMonitorMap.put(job.getId() + "-" + metrics.getName(), new MetricsTime(System.currentTimeMillis(), metrics, timeout));
});
}
use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class AppServiceImpl method run.
@Override
public void run(String... args) throws Exception {
boolean loadFromFile = true;
final List<InputStream> inputStreams = new LinkedList<>();
// 读取app定义配置加载到内存中 define/app/*.yml
Yaml yaml = new Yaml();
String classpath = this.getClass().getClassLoader().getResource("").getPath();
String defineAppPath = classpath + File.separator + "define" + File.separator + "app";
File directory = new File(defineAppPath);
if (!directory.exists() || directory.listFiles() == null) {
classpath = this.getClass().getResource(File.separator).getPath();
defineAppPath = classpath + File.separator + "define" + File.separator + "app";
directory = new File(defineAppPath);
if (!directory.exists() || directory.listFiles() == null) {
// load define app yml in jar
log.info("load define app yml in internal jar");
loadFromFile = false;
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath:define/app/*.yml");
for (Resource resource : resources) {
inputStreams.add(resource.getInputStream());
}
} catch (Exception e) {
log.error("define app yml not exist");
throw e;
}
}
}
if (loadFromFile) {
log.info("load define path {}", defineAppPath);
for (File appFile : Objects.requireNonNull(directory.listFiles())) {
if (appFile.exists()) {
try (FileInputStream fileInputStream = new FileInputStream(appFile)) {
Job app = yaml.loadAs(fileInputStream, Job.class);
appDefines.put(app.getApp().toLowerCase(), app);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new IOException(e);
}
}
}
} else {
if (inputStreams.isEmpty()) {
throw new IllegalArgumentException("define app directory not exist");
} else {
inputStreams.forEach(stream -> {
try {
Job app = yaml.loadAs(stream, Job.class);
appDefines.put(app.getApp().toLowerCase(), app);
stream.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
});
}
}
// 读取监控参数定义配置加载到数据库中 define/param/*.yml
if (loadFromFile) {
String defineParamPath = classpath + File.separator + "define" + File.separator + "param";
directory = new File(defineParamPath);
if (!directory.exists() || directory.listFiles() == null) {
throw new IllegalArgumentException("define param directory not exist: " + defineParamPath);
}
for (File appFile : Objects.requireNonNull(directory.listFiles())) {
if (appFile.exists()) {
try (FileInputStream fileInputStream = new FileInputStream(appFile)) {
ParamDefineDto paramDefine = yaml.loadAs(fileInputStream, ParamDefineDto.class);
paramDefines.put(paramDefine.getApp().toLowerCase(), paramDefine.getParam());
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new IOException(e);
}
}
}
} else {
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath:define/param/*.yml");
for (Resource resource : resources) {
InputStream stream = resource.getInputStream();
ParamDefineDto paramDefine = yaml.loadAs(stream, ParamDefineDto.class);
paramDefines.put(paramDefine.getApp().toLowerCase(), paramDefine.getParam());
stream.close();
}
} catch (Exception e) {
log.error("define param yml not exist");
throw e;
}
}
}
use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class AppServiceImpl method getAllAppHierarchy.
@Override
public List<Hierarchy> getAllAppHierarchy(String lang) {
List<Hierarchy> hierarchies = new LinkedList<>();
for (Job job : appDefines.values()) {
Hierarchy hierarchyApp = new Hierarchy();
hierarchyApp.setCategory(job.getCategory());
hierarchyApp.setValue(job.getApp());
Map<String, String> nameMap = job.getName();
if (nameMap != null) {
String i18nName = nameMap.get(lang);
if (i18nName == null) {
i18nName = nameMap.values().stream().findFirst().get();
}
hierarchyApp.setLabel(i18nName);
}
List<Hierarchy> hierarchyMetricList = new LinkedList<>();
if (job.getMetrics() != null) {
for (Metrics metrics : job.getMetrics()) {
Hierarchy hierarchyMetric = new Hierarchy();
hierarchyMetric.setValue(metrics.getName());
hierarchyMetric.setLabel(metrics.getName());
List<Hierarchy> hierarchyFieldList = new LinkedList<>();
if (metrics.getFields() != null) {
for (Metrics.Field field : metrics.getFields()) {
Hierarchy hierarchyField = new Hierarchy();
hierarchyField.setValue(field.getField());
hierarchyField.setLabel(field.getField());
hierarchyField.setIsLeaf(true);
hierarchyFieldList.add(hierarchyField);
}
hierarchyMetric.setChildren(hierarchyFieldList);
}
hierarchyMetricList.add(hierarchyMetric);
}
}
hierarchyApp.setChildren(hierarchyMetricList);
hierarchies.add(hierarchyApp);
}
return hierarchies;
}
use of com.usthe.common.entity.job.Job in project hertzbeat by dromara.
the class MonitorServiceImpl method detectMonitor.
@Override
@Transactional(readOnly = true)
public void detectMonitor(Monitor monitor, List<Param> params) throws MonitorDetectException {
Long monitorId = monitor.getId();
if (monitorId == null || monitorId == 0) {
monitorId = MONITOR_ID_TMP;
}
Job appDefine = appService.getAppDefine(monitor.getApp());
appDefine.setMonitorId(monitorId);
appDefine.setCyclic(false);
appDefine.setTimestamp(System.currentTimeMillis());
List<Configmap> configmaps = params.stream().map(param -> new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
appDefine.setConfigmap(configmaps);
// To detect availability, you only need to collect the set of availability indicators with a priority of 0.
// 探测可用性只需要采集优先级为0的可用性指标集合
List<Metrics> availableMetrics = appDefine.getMetrics().stream().filter(item -> item.getPriority() == 0).collect(Collectors.toList());
appDefine.setMetrics(availableMetrics);
List<CollectRep.MetricsData> collectRep = collectJobService.collectSyncJobData(appDefine);
// 判断探测结果 失败则抛出探测异常
if (collectRep == null || collectRep.isEmpty()) {
throw new MonitorDetectException("No collector response");
}
if (collectRep.get(0).getCode() != CollectRep.Code.SUCCESS) {
throw new MonitorDetectException(collectRep.get(0).getMsg());
}
}
Aggregations