集群同步-》最小位移对齐

This commit is contained in:
许晓东
2021-10-26 20:20:46 +08:00
parent 8545cba28e
commit a06b6dbb5f
10 changed files with 158 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
package com.xuxd.kafka.console.beans.dos;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* kafka-console-ui.
*
* @author xuxd
* @date 2021-10-26 10:32:05
**/
@Data
@TableName("t_min_offset_alignment")
public class MinOffsetAlignmentDO {
@TableId(type = IdType.AUTO)
private Long id;
private String groupId;
private String topic;
private String thatOffset;
private String thisOffset;
private String updateTime;
}

View File

@@ -27,4 +27,10 @@ public class OperationController {
dto.getProperties().put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, dto.getAddress());
return operationService.syncConsumerOffset(dto.getGroupId(), dto.getTopic(), dto.getProperties());
}
@PostMapping("/sync/min/offset/alignment")
public Object minOffsetAlignment(@RequestBody SyncDataDTO dto) {
dto.getProperties().put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, dto.getAddress());
return operationService.minOffsetAlignment(dto.getGroupId(), dto.getTopic(), dto.getProperties());
}
}

View File

@@ -0,0 +1,13 @@
package com.xuxd.kafka.console.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xuxd.kafka.console.beans.dos.MinOffsetAlignmentDO;
/**
* kafka-console-ui.
*
* @author xuxd
* @date 2021-10-26 10:33:55
**/
public interface MinOffsetAlignmentMapper extends BaseMapper<MinOffsetAlignmentDO> {
}

View File

@@ -12,4 +12,6 @@ import java.util.Properties;
public interface OperationService {
ResponseData syncConsumerOffset(String groupId, String topic, Properties thatProps);
ResponseData minOffsetAlignment(String groupId, String topic, Properties thatProps);
}

View File

@@ -1,9 +1,17 @@
package com.xuxd.kafka.console.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.xuxd.kafka.console.beans.ResponseData;
import com.xuxd.kafka.console.beans.dos.MinOffsetAlignmentDO;
import com.xuxd.kafka.console.dao.MinOffsetAlignmentMapper;
import com.xuxd.kafka.console.service.OperationService;
import java.util.Map;
import java.util.Properties;
import kafka.console.OperationConsole;
import org.apache.kafka.common.TopicPartition;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scala.Tuple2;
@@ -17,13 +25,52 @@ import scala.Tuple2;
@Service
public class OperationServiceImpl implements OperationService {
private Gson gson = new Gson();
@Autowired
private OperationConsole operationConsole;
private MinOffsetAlignmentMapper minOffsetAlignmentMapper;
public OperationServiceImpl(ObjectProvider<MinOffsetAlignmentMapper> minOffsetAlignmentMapper) {
this.minOffsetAlignmentMapper = minOffsetAlignmentMapper.getIfAvailable();
}
@Override public ResponseData syncConsumerOffset(String groupId, String topic, Properties thatProps) {
Tuple2<Object, String> tuple2 = operationConsole.syncConsumerOffset(groupId, topic, thatProps);
return (boolean) tuple2._1() ? ResponseData.create().success() : ResponseData.create().failed(tuple2._2());
}
@Override public ResponseData minOffsetAlignment(String groupId, String topic, Properties thatProps) {
Tuple2<Map<TopicPartition, Object>, Map<TopicPartition, Object>> tuple2 = operationConsole.checkAndFetchMinOffset(groupId, topic, thatProps);
Map<TopicPartition, Object> thisMinOffset = tuple2._1();
Map<TopicPartition, Object> thatMinOffset = tuple2._2();
JsonObject thisJson = new JsonObject(), thatJson = new JsonObject();
thisMinOffset.forEach((k, v) -> {
thisJson.addProperty(String.valueOf(k.partition()), v.toString());
});
thatMinOffset.forEach((k, v) -> {
thatJson.addProperty(String.valueOf(k.partition()), v.toString());
});
MinOffsetAlignmentDO alignmentDO = new MinOffsetAlignmentDO();
alignmentDO.setGroupId(groupId);
alignmentDO.setTopic(topic);
QueryWrapper<MinOffsetAlignmentDO> wrapper = new QueryWrapper<>();
wrapper.eq("group_id", groupId);
wrapper.eq("topic", topic);
if (minOffsetAlignmentMapper.selectCount(wrapper) > 0) {
minOffsetAlignmentMapper.delete(wrapper);
}
alignmentDO.setThisOffset(thisJson.toString());
alignmentDO.setThatOffset(thatJson.toString());
minOffsetAlignmentMapper.insert(alignmentDO);
return ResponseData.create().success();
}
}