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

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

@@ -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();
}
}