Skip to content

Commit

Permalink
Merge pull request #297 from didi/dev
Browse files Browse the repository at this point in the history
split op util controller to topic controller and leader controller, and add authority controller, quota controller
  • Loading branch information
ZQKC authored May 18, 2021
2 parents c2ad2d7 + 7719339 commit c6bcc0e
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.xiaojukeji.kafka.manager.common.entity.ao.gateway;

import com.xiaojukeji.kafka.manager.common.entity.dto.gateway.TopicQuotaDTO;

/**
* @author zhongyuankai
* @date 2020/4/27
Expand Down Expand Up @@ -65,4 +67,15 @@ public String toString() {
", consumeQuota=" + consumeQuota +
'}';
}

public static TopicQuota buildFrom(TopicQuotaDTO dto) {
TopicQuota topicQuota = new TopicQuota();
topicQuota.setAppId(dto.getAppId());
topicQuota.setClusterId(dto.getClusterId());
topicQuota.setTopicName(dto.getTopicName());
topicQuota.setProduceQuota(dto.getProduceQuota());
topicQuota.setConsumeQuota(dto.getConsumeQuota());
return topicQuota;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.xiaojukeji.kafka.manager.common.entity.dto.gateway;

import com.xiaojukeji.kafka.manager.common.entity.dto.ClusterTopicDTO;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(description = "配额调整")
public class TopicQuotaDTO extends ClusterTopicDTO {
@ApiModelProperty(value = "appId")
private String appId;

@ApiModelProperty(value = "发送数据速率B/s")
private Long produceQuota;

@ApiModelProperty(value = "消费数据速率B/s")
private Long consumeQuota;

public String getAppId() {
return appId;
}

public void setAppId(String appId) {
this.appId = appId;
}

public Long getProduceQuota() {
return produceQuota;
}

public void setProduceQuota(Long produceQuota) {
this.produceQuota = produceQuota;
}

public Long getConsumeQuota() {
return consumeQuota;
}

public void setConsumeQuota(Long consumeQuota) {
this.consumeQuota = consumeQuota;
}

@Override
public boolean paramLegal() {
return !ValidateUtils.isNullOrLessThanZero(clusterId) && !ValidateUtils.isBlank(topicName) && !ValidateUtils.isBlank(appId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicExpiredDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicStatisticsDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AuthorityDO;

import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -122,5 +123,12 @@ public interface TopicManagerService {
List<TopicStatisticsDO> getTopicStatistic(Long clusterId, String topicName, Date startTime, Date endTime);

TopicBusinessInfo getTopicBusinessInfo(Long physicalClusterId, String topicName);

/**
* topic权限调整
* @param authorityDO topic权限
* @return
*/
ResultStatus addAuthority(AuthorityDO authorityDO);
}

Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ List<String> fetchTopicData(KafkaConsumer kafkaConsumer,
List<TopicBrokerDTO> getTopicBrokerList(Long clusterId, String topicName);

Result<TopicOffsetChangedEnum> checkTopicOffsetChanged(Long physicalClusterId, String topicName, Long latestTime);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaojukeji.kafka.manager.service.service.gateway;

import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.gateway.TopicQuota;

/**
Expand Down Expand Up @@ -34,4 +35,11 @@ public interface QuotaService {
TopicQuota getQuotaFromZk(Long clusterId, String topicName, String appId);

Boolean modifyProduceQuota(Long clusterId, String topicName, String appId, Long produceQuota);

/**
* topic配额调整
* @param topicQuota topic配额
* @return
*/
ResultStatus addTopicQuotaByAuthority(TopicQuota topicQuota);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.xiaojukeji.kafka.manager.service.service.gateway.impl;

import com.xiaojukeji.kafka.manager.common.bizenum.TopicAuthorityEnum;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.gateway.TopicQuota;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AuthorityDO;
import com.xiaojukeji.kafka.manager.common.utils.NumberUtils;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.config.TopicQuotaData;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicDO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.cache.PhysicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.gateway.AuthorityService;
import com.xiaojukeji.kafka.manager.service.service.gateway.QuotaService;
import com.xiaojukeji.kafka.manager.service.service.TopicManagerService;
import com.xiaojukeji.kafka.manager.service.strategy.AbstractAllocateQuotaStrategy;
Expand All @@ -28,6 +33,12 @@ public class QuotaServiceImpl implements QuotaService {
@Autowired
private AbstractAllocateQuotaStrategy allocateQuotaStrategy;

@Autowired
private LogicalClusterMetadataManager logicalClusterMetadataManager;

@Autowired
private AuthorityService authorityService;

@Override
public int addTopicQuota(TopicQuota topicQuotaDO) {
return KafkaZookeeperUtils.setTopicQuota(
Expand Down Expand Up @@ -78,4 +89,34 @@ public Boolean modifyProduceQuota(Long clusterId, String topicName, String appId
}
return Boolean.TRUE;
}

@Override
public ResultStatus addTopicQuotaByAuthority(TopicQuota topicQuota) {
// 获取物理集群id
Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(topicQuota.getClusterId());
if (ValidateUtils.isNull(physicalClusterId)) {
return ResultStatus.CLUSTER_NOT_EXIST;
}
// 权限判断(access 0:无权限, 1:读, 2:写, 3:读写,4:可管理)
AuthorityDO authority = authorityService.getAuthority(physicalClusterId,
topicQuota.getTopicName(), topicQuota.getAppId());
if (ValidateUtils.isNull(authority) || authority.getAccess() == TopicAuthorityEnum.DENY.getCode()) {
return ResultStatus.USER_WITHOUT_AUTHORITY;
}
if (authority.getAccess() == TopicAuthorityEnum.READ.getCode()) {
// 可以消费
topicQuota.setProduceQuota(null);
}
if (authority.getAccess() == TopicAuthorityEnum.WRITE.getCode()) {
// 可以生产
topicQuota.setConsumeQuota(null);
}
// 设置物理集群id
topicQuota.setClusterId(physicalClusterId);
// 添加配额
if (addTopicQuota(topicQuota) > 0) {
return ResultStatus.SUCCESS;
}
return ResultStatus.ZOOKEEPER_WRITE_FAILED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.xiaojukeji.kafka.manager.common.utils.DateUtils;
import com.xiaojukeji.kafka.manager.common.utils.JsonUtils;
import com.xiaojukeji.kafka.manager.common.utils.NumberUtils;
import com.xiaojukeji.kafka.manager.common.utils.SpringTool;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.brokers.TopicMetadata;
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.config.TopicQuotaData;
Expand Down Expand Up @@ -618,6 +619,38 @@ public TopicBusinessInfo getTopicBusinessInfo(Long clusterId, String topicName)
return topicBusinessInfo;
}

@Override
public ResultStatus addAuthority(AuthorityDO authorityDO) {
// 查询该用户拥有的应用
List<AppDO> appDOs = appService.getByPrincipal(SpringTool.getUserName());
if (ValidateUtils.isEmptyList(appDOs)) {
// 该用户无应用,需要先申请应用
return ResultStatus.APP_NOT_EXIST;
}
List<Long> appIds = appDOs.stream().map(AppDO::getId).collect(Collectors.toList());
if (!appIds.contains(authorityDO.getAppId())) {
// 入参中的appId,该用户未拥有
return ResultStatus.APP_NOT_EXIST;
}
// 获取物理集群id
Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(authorityDO.getClusterId());
if (ValidateUtils.isNull(physicalClusterId)) {
// 集群不存在
return ResultStatus.CLUSTER_NOT_EXIST;
}
TopicDO topic = getByTopicName(physicalClusterId, authorityDO.getTopicName());
if (ValidateUtils.isNull(topic)) {
// topic不存在
return ResultStatus.TOPIC_NOT_EXIST;
}
// 设置物理集群id
authorityDO.setClusterId(physicalClusterId);
if (authorityService.addAuthority(authorityDO) > 0) {
return ResultStatus.SUCCESS;
}
return ResultStatus.MYSQL_ERROR;
}

private RdTopicBasic convert2RdTopicBasic(ClusterDO clusterDO,
String topicName,
TopicDO topicDO,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.xiaojukeji.kafka.manager.openapi.common.dto;

import com.xiaojukeji.kafka.manager.common.entity.dto.ClusterTopicDTO;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(description = "权限调整")
public class TopicAuthorityDTO extends ClusterTopicDTO {
@ApiModelProperty(value = "appId")
private String appId;

@ApiModelProperty(value = "0:无权限, 1:读, 2:写, 3:读写, 4:可管理")
private Integer access;

public String getAppId() {
return appId;
}

public void setAppId(String appId) {
this.appId = appId;
}

public Integer getAccess() {
return access;
}

public void setAccess(Integer access) {
this.access = access;
}

@Override
public boolean paramLegal() {
return !ValidateUtils.isNullOrLessThanZero(clusterId)
&& !ValidateUtils.isBlank(topicName)
&& !ValidateUtils.isBlank(appId)
&& !ValidateUtils.isNullOrLessThanZero(access);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public Result<List<TopicMyAppVO>> getTopicMyApps(@PathVariable Long clusterId,
}

return new Result<>(TopicModelConverter.convert2TopicMineAppVOList(
topicManagerService.getTopicMineApps(physicalClusterId, topicName, SpringTool.getUserName()))
topicManagerService.getTopicMineApps(physicalClusterId, topicName, SpringTool.getUserName()))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.xiaojukeji.kafka.manager.web.api.versionone.op;

import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import com.xiaojukeji.kafka.manager.openapi.common.dto.TopicAuthorityDTO;
import com.xiaojukeji.kafka.manager.service.service.TopicManagerService;
import com.xiaojukeji.kafka.manager.web.converters.AuthorityConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* Authority操作相关接口
* @author zengqiao
* @date 21/5/18
*/
@Api(tags = "OP-Authority操作相关接口(REST)")
@RestController
public class OpAuthorityController {
@Autowired
private TopicManagerService topicManagerService;

@ApiOperation(value = "权限调整",notes = "权限调整")
@PostMapping(value = "topic-authorities")
@ResponseBody
public Result addAuthority(@RequestBody TopicAuthorityDTO dto) {
//非空校验
if (ValidateUtils.isNull(dto) || !dto.paramLegal()) {
return Result.buildFrom(ResultStatus.PARAM_ILLEGAL);
}
return Result.buildFrom(topicManagerService.addAuthority(AuthorityConverter.convert2AuthorityDO(dto)));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.xiaojukeji.kafka.manager.web.api.versionone.thirdpart;
package com.xiaojukeji.kafka.manager.web.api.versionone.op;

import com.xiaojukeji.kafka.manager.common.bizenum.RebalanceDimensionEnum;
import com.xiaojukeji.kafka.manager.common.bizenum.TaskStatusEnum;
import com.xiaojukeji.kafka.manager.common.constant.ApiPrefix;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.dto.op.RebalanceDTO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.utils.JsonUtils;
import com.xiaojukeji.kafka.manager.common.utils.SpringTool;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import com.xiaojukeji.kafka.manager.service.service.AdminService;
Expand All @@ -16,22 +18,35 @@
import org.springframework.web.bind.annotation.*;

/**
* Leader操作[选举|切换]相关接口
* @author zengqiao
* @date 20/9/23
* @date 21/5/18
*/
@Api(tags = "开放接口-OP相关接口(REST)")
@Api(tags = "OP-Leader操作相关接口(REST)")
@RestController
@RequestMapping(ApiPrefix.API_V1_THIRD_PART_OP_PREFIX)
public class ThirdPartOpUtilController {

@RequestMapping(ApiPrefix.API_V1_OP_PREFIX)
public class OpLeaderController {
@Autowired
private AdminService adminService;

@Autowired
private ClusterService clusterService;

@ApiOperation(value = "优先副本选举状态")
@RequestMapping(value = {"leaders/preferred-replica-election-status", "utils/rebalance-status"}, method = RequestMethod.GET)
@ResponseBody
public Result preferredReplicaElectStatus(@RequestParam("clusterId") Long clusterId) {
ClusterDO clusterDO = clusterService.getById(clusterId);
if (ValidateUtils.isNull(clusterDO)) {
return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
}

TaskStatusEnum statusEnum = adminService.preferredReplicaElectionStatus(clusterDO);
return new Result<>(JsonUtils.toJson(statusEnum));
}

@ApiOperation(value = "优先副本选举")
@RequestMapping(value = "op/rebalance", method = RequestMethod.POST)
@RequestMapping(value = {"leaders/preferred-replica-election", "utils/rebalance"}, method = RequestMethod.POST)
@ResponseBody
public Result preferredReplicaElect(@RequestBody RebalanceDTO reqObj) {
if (!reqObj.paramLegal()) {
Expand Down
Loading

0 comments on commit c6bcc0e

Please sign in to comment.