
org.joyqueue.service.impl.BrokerTopicMonitorServiceImpl Maven / Gradle / Ivy
/**
* Copyright 2019 The JoyQueue Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joyqueue.service.impl;
import org.joyqueue.convert.CodeConverter;
import org.joyqueue.manage.PartitionGroupMetric;
import org.joyqueue.model.PageResult;
import org.joyqueue.model.Pagination;
import org.joyqueue.model.QPageQuery;
import org.joyqueue.model.domain.Broker;
import org.joyqueue.model.domain.BrokerTopicMonitor;
import org.joyqueue.model.domain.BrokerTopicMonitorRecord;
import org.joyqueue.model.domain.Consumer;
import org.joyqueue.model.domain.Producer;
import org.joyqueue.model.domain.SubscribeType;
import org.joyqueue.model.query.QMonitor;
import org.joyqueue.monitor.BrokerMonitorInfo;
import org.joyqueue.monitor.BrokerStartupInfo;
import org.joyqueue.monitor.Client;
import org.joyqueue.monitor.ConnectionMonitorDetailInfo;
import org.joyqueue.monitor.ConsumerMonitorInfo;
import org.joyqueue.monitor.ProducerMonitorInfo;
import org.joyqueue.monitor.RestResponse;
import org.joyqueue.other.HttpRestService;
import org.joyqueue.service.BrokerService;
import org.joyqueue.service.BrokerTopicMonitorService;
import org.joyqueue.service.ConsumerService;
import org.joyqueue.service.ProducerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by wangxiaofei1 on 2019/3/13.
*/
@Service("brokerTopicMonitorService")
public class BrokerTopicMonitorServiceImpl implements BrokerTopicMonitorService {
public static final Logger logger = LoggerFactory.getLogger(BrokerTopicMonitorServiceImpl.class);
@Autowired
private HttpRestService httpRestService;
@Autowired
protected BrokerService brokerService;
@Autowired
private ConsumerService consumerService;
@Autowired
private ProducerService producerService;
@Override
public PageResult queryTopicsPartitionMointor(QPageQuery qPageQuery) {
PageResult pageResult = new PageResult<>();
try {
Pagination pagination = qPageQuery.getPagination();
QMonitor qMonitor = qPageQuery.getQuery();
Broker broker = brokerService.findById(Integer.valueOf(String.valueOf(qMonitor.getBrokerId())));
List toplicList = queryTopicList(broker);
pagination.setTotalRecord(toplicList.size());
int toIndex= pagination.getStart()+pagination.getSize();
if (toIndex >pagination.getTotalRecord()) {
toIndex = pagination.getTotalRecord();
}
List brokerTopicMonitorList = new ArrayList<>();
for (String topic: toplicList.subList(pagination.getStart(),toIndex)) {
BrokerTopicMonitor brokerTopicMonitor = new BrokerTopicMonitor();
List partitionGroupMetricList = getPartitionGroup(topic,broker);
brokerTopicMonitor.setTopic(topic);
brokerTopicMonitor.setPartitionGroupMetricList(partitionGroupMetricList);
brokerTopicMonitorList.add(brokerTopicMonitor);
}
pageResult.setPagination(pagination);
pageResult.setResult(brokerTopicMonitorList);
} catch (Exception e) {
logger.error("queryTopicsPartitionMointor exception",e);
}
return pageResult;
}
/**
*
* 查询连接详情
* @param qPageQuery
* @return
*/
@Override
public PageResult queryClientConnectionDetail(QPageQuery qPageQuery) {
PageResult pageResult = new PageResult<>();
try {
Pagination pagination = qPageQuery.getPagination();
QMonitor qMonitor = qPageQuery.getQuery();
Broker broker = brokerService.findById(Integer.valueOf(String.valueOf(qMonitor.getBrokerId())));
ConnectionMonitorDetailInfo connectionMonitorDetailInfo = getConnectMonitorDetail(broker);
if (connectionMonitorDetailInfo != null) {
List clients=connectionMonitorDetailInfo.getClients();
pagination.setTotalRecord(clients.size());
int toIndex= pagination.getStart()+pagination.getSize();
if (toIndex >pagination.getTotalRecord()) {
toIndex = pagination.getTotalRecord();
}
pageResult.setPagination(pagination);
pageResult.setResult(clients.subList(pagination.getStart(),toIndex));
}
} catch (Exception e) {
logger.error("queryClientConnectionDetail exception",e);
}
return pageResult;
}
/**
* 查询topic,app 详情
* @param qPageQuery
* @return
* @throws Exception
*/
@Override
public PageResult queryTopicsMointor(QPageQuery qPageQuery) {
PageResult pageResult = new PageResult<>();
try {
Pagination pagination = qPageQuery.getPagination();
QMonitor qMonitor = qPageQuery.getQuery();
Broker broker = brokerService.findById(Integer.valueOf(String.valueOf(qMonitor.getBrokerId())));
List toplicList = queryTopicList(broker);
pagination.setTotalRecord(toplicList.size());
int fromIndx= pagination.getStart()+pagination.getSize();
if (fromIndx >pagination.getTotalRecord()) {
fromIndx = pagination.getTotalRecord();
}
List brokerTopicMonitors = new ArrayList<>(pagination.getSize());
for (String topic: toplicList.subList(pagination.getStart(),fromIndx)) {
List appList = getAppByTopic(qMonitor.getType(),topic);
BrokerTopicMonitor brokerTopicMonitor = getMonitorByAppAndTopic(topic,appList,broker,qMonitor.getType());
brokerTopicMonitors.add(brokerTopicMonitor);
}
pageResult.setPagination(pagination);
pageResult.setResult(brokerTopicMonitors);
} catch (Exception e) {
logger.error("queryTopicsMointor exception",e);
}
return pageResult;
}
/**
* 查询broker详情
* @param brokerId
* @return
*/
public BrokerMonitorInfo findBrokerMonitor(Long brokerId){
try {
Broker broker = brokerService.findById(Integer.valueOf(String.valueOf(brokerId)));
return queryBrokerMonitor(broker);
} catch (Exception e) {
logger.error("findBrokerMonitor exception",e);
}
return null;
}
/**
* 查询启动信息
* @param brokerId
* @return
*/
public BrokerStartupInfo getStartupInfo(Long brokerId) throws Exception {
Broker broker = brokerService.findById(Integer.valueOf(String.valueOf(brokerId)));
return getStartInfo(broker);
}
private BrokerTopicMonitor getMonitorByAppAndTopic(String topic,List appList,Broker broker,SubscribeType type) throws Exception {
BrokerTopicMonitor brokerTopicMonitor = new BrokerTopicMonitor();
List brokerMonitorRecordList = new ArrayList<>();
for (String app:appList) {
BrokerTopicMonitorRecord brokerTopicMonitorRecord = new BrokerTopicMonitorRecord();
if (type == SubscribeType.CONSUMER) {
ConsumerMonitorInfo consumerMonitorInfo = queryMonitorConsumer(topic,app,broker);
brokerTopicMonitorRecord.setConnections(consumerMonitorInfo.getConnections());
brokerTopicMonitorRecord.setCount(consumerMonitorInfo.getDeQueue().getCount());
brokerTopicMonitorRecord.setTotalSize(consumerMonitorInfo.getDeQueue().getTotalSize());
} else if (type == SubscribeType.PRODUCER) {
ProducerMonitorInfo producerMonitorInfo = queryMonitorProducer(topic,app,broker);
brokerTopicMonitorRecord.setConnections(producerMonitorInfo.getConnections());
brokerTopicMonitorRecord.setCount(producerMonitorInfo.getEnQueue().getCount());
brokerTopicMonitorRecord.setTotalSize(producerMonitorInfo.getEnQueue().getTotalSize());
}
brokerTopicMonitorRecord.setApp(app);
brokerMonitorRecordList.add(brokerTopicMonitorRecord);
}
brokerTopicMonitor.setBrokerTopicMonitorRecordList(brokerMonitorRecordList);
brokerTopicMonitor.setTopic(topic);
return brokerTopicMonitor;
}
private List getAppByTopic(SubscribeType subscribeType,String topic) throws Exception {
if (subscribeType == SubscribeType.CONSUMER) {
List consumerList = consumerService.findByTopic(topic, null);
return consumerList.stream().map(consumer -> CodeConverter.convertApp(consumer.getApp(),consumer.getSubscribeGroup())).collect(Collectors.toList());
} else if (subscribeType == SubscribeType.PRODUCER) {
List producerList = producerService.findByTopic(null, topic);
return producerList.stream().map(producer -> producer.getApp().getCode()).collect(Collectors.toList());
}
return new ArrayList<>();
}
private List getPartitionGroup(String topic, Broker broker){
String path="partitionGroupDetail";
String[] args=new String[3];
args[0]=broker.getIp();
args[1]=String.valueOf(broker.getMonitorPort());
args[2]=topic;
RestResponse> restResponse = httpRestService.get(path,PartitionGroupMetric.class,true,args);
if (restResponse != null && restResponse.getData() != null) {
return restResponse.getData();
}
return null;
}
/**
* 查询消费者详情
* @return
*/
private ConsumerMonitorInfo queryMonitorConsumer(String topic, String app, Broker broker) throws Exception {
String path="appTopicMonitorConsumer";
String[] args=new String[4];
args[0]=broker.getIp();
args[1]=String.valueOf(broker.getMonitorPort());
args[2]=topic;
args[3]=app;
RestResponse restResponse = httpRestService.get(path,ConsumerMonitorInfo.class,false,args);
if (restResponse != null && restResponse.getData() != null) {
return restResponse.getData();
}
return null;
}
/**
* 查询连接详情
* @return
*/
private ConnectionMonitorDetailInfo getConnectMonitorDetail(Broker broker) throws Exception {
String path="appConnectionDetail";
String[] args=new String[4];
args[0]=broker.getIp();
args[1]=String.valueOf(broker.getMonitorPort());
RestResponse restResponse = httpRestService.get(path,ConnectionMonitorDetailInfo.class,false,args);
if (restResponse != null && restResponse.getData() != null) {
return restResponse.getData();
}
return null;
}
/**
* 查询生产者详情
* @return
*/
private ProducerMonitorInfo queryMonitorProducer(String topic,String app,Broker broker) throws Exception {
String path="appTopicMonitorProducer";
String[] args=new String[4];
args[0]=broker.getIp();
args[1]=String.valueOf(broker.getMonitorPort());
args[2]=topic;
args[3]=app;
RestResponse restResponse = httpRestService.get(path,ProducerMonitorInfo.class,false,args);
if (restResponse != null && restResponse.getData() != null) {
return restResponse.getData();
}
return null;
}
/**
* 查询topicList
* @return
*/
private List queryTopicList(Broker broker) throws Exception {
String path="topicList";
String[] args=new String[2];
args[0]=broker.getIp();
args[1]=String.valueOf(broker.getMonitorPort());
RestResponse> restResponse = httpRestService.get(path,String.class,true,args);
if (restResponse != null && restResponse.getData() != null) {
return restResponse.getData();
}
return null;
}
/**
* 查询broker监控
* @return
*/
private BrokerMonitorInfo queryBrokerMonitor(Broker broker) throws Exception {
String path="brokerMonitor";
String[] args=new String[2];
args[0]=broker.getIp();
args[1]=String.valueOf(broker.getMonitorPort());
RestResponse restResponse = httpRestService.get(path,BrokerMonitorInfo.class,false,args);
if (restResponse != null && restResponse.getData() != null) {
return restResponse.getData();
}
return null;
}
/**
* 查询broker监控
* @return
*/
private BrokerStartupInfo getStartInfo(Broker broker) throws Exception {
String path="startupInfo";
String[] args=new String[2];
args[0]=broker.getIp();
args[1]=String.valueOf(broker.getMonitorPort());
RestResponse restResponse = httpRestService.get(path,BrokerStartupInfo.class,false,args);
if (restResponse != null && restResponse.getData() != null) {
return restResponse.getData();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy