
org.joyqueue.service.impl.BrokerServiceImpl 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.apache.commons.collections.CollectionUtils;
import org.joyqueue.domain.TopicName;
import org.joyqueue.model.ListQuery;
import org.joyqueue.model.PageResult;
import org.joyqueue.model.QPageQuery;
import org.joyqueue.model.domain.Broker;
import org.joyqueue.model.domain.BrokerGroupRelated;
import org.joyqueue.model.domain.PartitionGroupReplica;
import org.joyqueue.model.query.QBroker;
import org.joyqueue.model.query.QBrokerGroupRelated;
import org.joyqueue.nsr.BrokerNameServerService;
import org.joyqueue.service.BrokerGroupRelatedService;
import org.joyqueue.service.BrokerService;
import org.joyqueue.service.PartitionGroupReplicaService;
import org.joyqueue.util.NullUtil;
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.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
import static java.util.Comparator.comparing;
/**
* @author wylixiaobin
* Date: 2018/10/17
*/
@Service("brokerService")
public class BrokerServiceImpl implements BrokerService {
private final Logger logger = LoggerFactory.getLogger(BrokerServiceImpl.class);
@Autowired
protected BrokerNameServerService brokerNameServerService;
@Autowired
private BrokerGroupRelatedService brokerGroupRelatedService;
@Autowired
private PartitionGroupReplicaService partitionGroupReplicaService;
@Override
public Broker findById(Integer id) throws Exception {
return brokerNameServerService.findById(id);
}
@Override
public List findByTopic(String topic) throws Exception {
TopicName topicName = TopicName.parse(topic);
List replicas = partitionGroupReplicaService.getByTopic(topicName.getCode(), topicName.getNamespace());
if (NullUtil.isEmpty(replicas)) {
logger.error(String.format("can not find partitionGroup by topic, topic is %s", topic));
return null;
}
return replicas.stream().map(replica -> {
try {
return this.findById(replica.getBrokerId());
} catch (Exception e) {
logger.error(String.format("can not find broker with id %s"), replica.getBrokerId());
return null;
}
}).collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(comparing(Broker::getId))), ArrayList::new));
}
@Override
public List findByGroup(long group) throws Exception {
QBrokerGroupRelated qBrokerGroupRelated = new QBrokerGroupRelated();
qBrokerGroupRelated.setBrokerGroupId(group);
List brokerGroupRelateds = brokerGroupRelatedService.findByQuery(new ListQuery<>(qBrokerGroupRelated));
List brokerIds = new ArrayList<>();
if (CollectionUtils.isNotEmpty(brokerGroupRelateds)) {
for (BrokerGroupRelated brokerGroupRelated : brokerGroupRelateds) {
brokerIds.add(Integer.valueOf(String.valueOf(brokerGroupRelated.getId())));
}
}
return getByIdsBroker(brokerIds);
}
@Override
public int add(Broker model) {
try {
return brokerNameServerService.add(model);
} catch (Exception e) {
logger.error("add error",e);
}
return 0;
}
@Override
public int delete(Broker model) {
try {
return brokerNameServerService.delete(model);
} catch (Exception e) {
logger.error("delete error",e);
}
return 0;
}
@Override
public int update(Broker model) {
try {
return brokerNameServerService.update(model);
} catch (Exception e) {
logger.error("update error",e);
}
return 0;
}
@Override
public List queryBrokerList(QBroker qBroker) throws Exception {
ListQuery brokerListQuery = new ListQuery<>();
QBrokerGroupRelated qBrokerGroupRelated = new QBrokerGroupRelated();
qBrokerGroupRelated.setGroup(qBroker.getGroup());
brokerListQuery.setQuery(qBrokerGroupRelated);
List brokerList = brokerGroupRelatedService.findByQuery(brokerListQuery);
if (brokerList != null && brokerList.size() > 0) {
List brokerIdList = brokerList.stream().map(broker -> Integer.valueOf(String.valueOf(broker.getId()))).collect(Collectors.toList());
qBroker.setInBrokerIds(brokerIdList);
} else {
if (qBrokerGroupRelated.getGroup() != null) {
return new ArrayList<>();
}
}
return brokerNameServerService.getByIdsBroker(qBroker.getInBrokerIds());
}
/**
* 通过组获取brokerIds
* @param query
* @return
*/
private List getBrokerIdByGroupIds(QBroker query) {
if (query.getBrokerGroupId() > 0) {
Integer brokerGroupId = Math.toIntExact(query.getBrokerGroupId());
if (query.getBrokerGroupIds() != null) {
query.getBrokerGroupIds().add(brokerGroupId);
} else {
query.setBrokerGroupIds(Arrays.asList(brokerGroupId));
}
}
if(query.getBrokerGroupIds() != null && query.getBrokerGroupIds().size() > 0) {
ListQuery brokerListQuery = new ListQuery<>();
QBrokerGroupRelated qBrokerGroupRelated = new QBrokerGroupRelated();
//数据库查询
qBrokerGroupRelated.setBrokerGroupIds(query.getBrokerGroupIds());
qBrokerGroupRelated.setNotInBrokerIds(query.getNotInBrokerIds());
brokerListQuery.setQuery(qBrokerGroupRelated);
List brokerList = brokerGroupRelatedService.findByQuery(brokerListQuery);
if (brokerList != null && brokerList.size() > 0) {
List brokerIdList = brokerList.stream().map(broker -> broker.getId()).collect(Collectors.toList());
return brokerIdList;
} else {
//查询到0个 broker 则直接返回
return null;
}
}
return new ArrayList<>();
}
@Override
public List getByIdsBroker(List ids) throws Exception {
return brokerNameServerService.getByIdsBroker(ids);
}
@Override
public List syncBrokers() throws Exception {
return brokerNameServerService.syncBrokers();
}
@Override
public PageResult search(QPageQuery qPageQuery) throws Exception {
PageResult pageResult = brokerNameServerService.search(qPageQuery);
if (pageResult !=null && pageResult.getResult() != null && pageResult.getResult().size() >0) {
List brokerList = pageResult.getResult();
Iterator iterator = brokerList.iterator();
while (iterator.hasNext()) {
Broker broker = iterator.next();
BrokerGroupRelated brokerRelated = brokerGroupRelatedService.findById(broker.getId());
if (brokerRelated != null && brokerRelated.getGroup() != null) {
broker.setGroup(brokerRelated.getGroup());
broker.setStatus(0);
}
}
}
return pageResult;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy