|
|
@ -11,7 +11,7 @@ import rition.service.collector.dao.entity.MetricRecordEntity; |
|
|
|
import rition.service.collector.dao.mapper.MetricRecordMapper; |
|
|
|
import rition.service.collector.dao.mapper.MetricRecordMapper; |
|
|
|
|
|
|
|
|
|
|
|
import java.time.Instant; |
|
|
|
import java.time.Instant; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* DataCollectorService 指标数据收集类,负责收集来自云主机探针上报的数据,并将其入库等 |
|
|
|
* DataCollectorService 指标数据收集类,负责收集来自云主机探针上报的数据,并将其入库等 |
|
|
@ -21,7 +21,6 @@ import java.util.ArrayList; |
|
|
|
public class MetricCollectingService { |
|
|
|
public class MetricCollectingService { |
|
|
|
|
|
|
|
|
|
|
|
private final KafkaTemplate<String, MetricDataDto> kafkaTemplate; |
|
|
|
private final KafkaTemplate<String, MetricDataDto> kafkaTemplate; |
|
|
|
private final KafkaConfigure kafkaConfigure; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String collectedDataTopic; |
|
|
|
private final String collectedDataTopic; |
|
|
|
private final MetricRecordMapper metricRecordMapper; |
|
|
|
private final MetricRecordMapper metricRecordMapper; |
|
|
@ -29,55 +28,27 @@ public class MetricCollectingService { |
|
|
|
public MetricCollectingService(KafkaTemplate<String, MetricDataDto> kafkaTemplate, |
|
|
|
public MetricCollectingService(KafkaTemplate<String, MetricDataDto> kafkaTemplate, |
|
|
|
KafkaConfigure kafkaConfigure, MetricRecordMapper metricRecordMapper) { |
|
|
|
KafkaConfigure kafkaConfigure, MetricRecordMapper metricRecordMapper) { |
|
|
|
this.kafkaTemplate = kafkaTemplate; |
|
|
|
this.kafkaTemplate = kafkaTemplate; |
|
|
|
this.kafkaConfigure = kafkaConfigure; |
|
|
|
|
|
|
|
this.collectedDataTopic = kafkaConfigure.getDataCollecting().getTopic(); |
|
|
|
this.collectedDataTopic = kafkaConfigure.getDataCollecting().getTopic(); |
|
|
|
this.metricRecordMapper = metricRecordMapper; |
|
|
|
this.metricRecordMapper = metricRecordMapper; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static final int MAX_BATCH_INSERT_SIZE = 512; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 接收处理好的监控指标数据 |
|
|
|
* 接收处理好的监控指标数据,以时间戳为分组入库和发送mq |
|
|
|
* |
|
|
|
* |
|
|
|
* @param collectedMetricData 监控指标数据,protobuf对象 |
|
|
|
* @param collectedMetricData 监控指标数据,以时间戳为组 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void receiveData(MetricDataDto collectedMetricData) { |
|
|
|
public void receiveData(Map<Long, MetricDataDto> collectedMetricData) { |
|
|
|
// 对大于MAX_BATCH_INSERT_SIZE大小的数据进行分组的批量插入
|
|
|
|
for (Long timestamp : collectedMetricData.keySet()) { |
|
|
|
var metricDataList = collectedMetricData.getDataList(); |
|
|
|
var metricDataDto = collectedMetricData.get(timestamp); |
|
|
|
if (metricDataList.size() <= MAX_BATCH_INSERT_SIZE) { |
|
|
|
var metricDataList = metricDataDto.getDataList(); |
|
|
|
var metricRecordEntityList = new ArrayList<MetricRecordEntity>(metricDataList.size()); |
|
|
|
var metricRecordEntityList = metricDataList.stream().map(this::convert).toList(); |
|
|
|
for (MetricDataDto.MetricData metricData : metricDataList) { |
|
|
|
|
|
|
|
MetricRecordEntity entity = this.convert(metricData); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
metricRecordEntityList.add(entity); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
kafkaTemplate.send(this.collectedDataTopic, collectedMetricData); |
|
|
|
kafkaTemplate.send(this.collectedDataTopic, metricDataDto); |
|
|
|
metricRecordMapper.insertBatchSomeColumn(metricRecordEntityList); |
|
|
|
metricRecordMapper.insertBatchSomeColumn(metricRecordEntityList); |
|
|
|
} else { |
|
|
|
|
|
|
|
var batch = metricDataList.size() / MAX_BATCH_INSERT_SIZE; |
|
|
|
|
|
|
|
if (metricDataList.size() % MAX_BATCH_INSERT_SIZE != 0) { |
|
|
|
|
|
|
|
batch++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var metricRecordEntityList = new ArrayList<MetricRecordEntity>(MAX_BATCH_INSERT_SIZE); |
|
|
|
|
|
|
|
for (int i = 0; i < batch; i++) { |
|
|
|
|
|
|
|
metricRecordEntityList.clear(); |
|
|
|
|
|
|
|
var from = i * MAX_BATCH_INSERT_SIZE; |
|
|
|
|
|
|
|
var to = (i + 1) * MAX_BATCH_INSERT_SIZE; |
|
|
|
|
|
|
|
var subList = metricDataList.subList(from, Math.min(metricDataList.size(), to)); |
|
|
|
|
|
|
|
for (MetricDataDto.MetricData metricData : subList) { |
|
|
|
|
|
|
|
MetricRecordEntity entity = this.convert(metricData); |
|
|
|
|
|
|
|
metricRecordEntityList.add(entity); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
kafkaTemplate.send(this.collectedDataTopic, MetricDataDto.builder().dataList(subList).build()); |
|
|
|
|
|
|
|
metricRecordMapper.insertBatchSomeColumn(metricRecordEntityList); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public MetricRecordEntity convert(MetricDataDto.MetricData metricData) { |
|
|
|
private MetricRecordEntity convert(MetricDataDto.MetricData metricData) { |
|
|
|
MetricRecordEntity entity = new MetricRecordEntity(); |
|
|
|
MetricRecordEntity entity = new MetricRecordEntity(); |
|
|
|
entity.setId(YitIdHelper.nextId()); |
|
|
|
entity.setId(YitIdHelper.nextId()); |
|
|
|
entity.setInstanceId(metricData.getTags().get("instanceId")); |
|
|
|
entity.setInstanceId(metricData.getTags().get("instanceId")); |
|
|
@ -85,7 +56,7 @@ public class MetricCollectingService { |
|
|
|
entity.setValue(metricData.getValue()); |
|
|
|
entity.setValue(metricData.getValue()); |
|
|
|
entity.setStatus(CommonEntityStatus.STATUS_NORMAL.getValue()); |
|
|
|
entity.setStatus(CommonEntityStatus.STATUS_NORMAL.getValue()); |
|
|
|
|
|
|
|
|
|
|
|
var time = Instant.ofEpochMilli(metricData.getTimestamp()); |
|
|
|
var time = Instant.ofEpochSecond(metricData.getTimestamp()); |
|
|
|
entity.setTime(time); |
|
|
|
entity.setTime(time); |
|
|
|
entity.setUpdateTime(time); |
|
|
|
entity.setUpdateTime(time); |
|
|
|
|
|
|
|
|
|
|
|