mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
1. Add MariaDB gtid support 2. fix issue:#561 增加一个外部参数控制,允许位点不存在时自动重置到当前位点 (#3025)
* fix issue:#561 增加一个外部参数控制,允许位点不存在时自动重置到当前位点 * Add MariaDB GTID support
This commit is contained in:
@@ -49,6 +49,8 @@ public class MysqlConnector {
|
||||
// mysql connectinnId
|
||||
private long connectionId = -1;
|
||||
private AtomicBoolean connected = new AtomicBoolean(false);
|
||||
// serverVersion
|
||||
private String serverVersion;
|
||||
|
||||
public static final int timeout = 5 * 1000; // 5s
|
||||
|
||||
@@ -179,6 +181,7 @@ public class MysqlConnector {
|
||||
}
|
||||
|
||||
connectionId = handshakePacket.threadId; // 记录一下connection
|
||||
serverVersion = handshakePacket.serverVersion; // 记录serverVersion
|
||||
logger.info("handshake initialization packet received, prepare the client authentication packet to send");
|
||||
ClientAuthenticationPacket clientAuth = new ClientAuthenticationPacket();
|
||||
clientAuth.setCharsetNumber(charsetNumber);
|
||||
@@ -412,4 +415,8 @@ public class MysqlConnector {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getServerVersion() {
|
||||
return serverVersion;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.alibaba.otter.canal.parse.driver.mysql.packets;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 类 MariaGTIDSet.java 的实现
|
||||
*
|
||||
* @author winger 2020/9/24 10:31 上午
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class MariaGTIDSet implements GTIDSet {
|
||||
//MariaDB 10.0.2+ representation of Gtid
|
||||
Map<Long, MariaGtid> gtidMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public byte[] encode() throws IOException {
|
||||
return this.toString().getBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(String str) {
|
||||
MariaGtid mariaGtid = MariaGtid.parse(str);
|
||||
gtidMap.put(mariaGtid.getDomainId(), mariaGtid);
|
||||
}
|
||||
|
||||
public void add(MariaGtid mariaGtid) {
|
||||
gtidMap.put(mariaGtid.getDomainId(), mariaGtid);
|
||||
}
|
||||
|
||||
public static MariaGTIDSet parse(String gtidData) {
|
||||
Map<Long, MariaGtid> gtidMap = new HashMap<>();
|
||||
if (StringUtils.isNotEmpty(gtidData)) {
|
||||
// 存在多个GTID时会有回车符
|
||||
String[] gtidStrs = gtidData.replaceAll("\n", "").split(",");
|
||||
for (String gtid : gtidStrs) {
|
||||
MariaGtid mariaGtid = MariaGtid.parse(gtid);
|
||||
gtidMap.put(mariaGtid.getDomainId(), mariaGtid);
|
||||
}
|
||||
}
|
||||
MariaGTIDSet gtidSet = new MariaGTIDSet();
|
||||
gtidSet.gtidMap = gtidMap;
|
||||
return gtidSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (MariaGtid gtid : gtidMap.values()) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(gtid.toString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.alibaba.otter.canal.parse.driver.mysql.packets;
|
||||
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
|
||||
/**
|
||||
* 类 MariaGtid.java 的实现
|
||||
*
|
||||
* @author winger 2020/9/24 11:30 上午
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class MariaGtid {
|
||||
|
||||
// {domainId}-{serverId}-{sequence}
|
||||
private long domainId;
|
||||
private long serverId;
|
||||
private long sequence;
|
||||
|
||||
public MariaGtid(long domainId, long serverId, long sequence) {
|
||||
this.domainId = domainId;
|
||||
this.serverId = serverId;
|
||||
this.sequence = sequence;
|
||||
}
|
||||
|
||||
public MariaGtid(String gtid) {
|
||||
String[] gtidArr = gtid.split("-");
|
||||
this.domainId = NumberUtils.toLong(gtidArr[0]);
|
||||
this.serverId = NumberUtils.toLong(gtidArr[1]);
|
||||
this.sequence = NumberUtils.toLong(gtidArr[2]);
|
||||
}
|
||||
|
||||
public static MariaGtid parse(String gtid) {
|
||||
return new MariaGtid(gtid);
|
||||
}
|
||||
|
||||
public long getDomainId() {
|
||||
return domainId;
|
||||
}
|
||||
|
||||
public void setDomainId(long domainId) {
|
||||
this.domainId = domainId;
|
||||
}
|
||||
|
||||
public long getServerId() {
|
||||
return serverId;
|
||||
}
|
||||
|
||||
public void setServerId(long serverId) {
|
||||
this.serverId = serverId;
|
||||
}
|
||||
|
||||
public long getSequence() {
|
||||
return sequence;
|
||||
}
|
||||
|
||||
public void setSequence(long sequence) {
|
||||
this.sequence = sequence;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MariaGtid mariaGtid = (MariaGtid) o;
|
||||
return domainId == mariaGtid.domainId &&
|
||||
serverId == mariaGtid.serverId &&
|
||||
sequence == mariaGtid.sequence;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s-%s-%s", domainId, serverId, sequence);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.alibaba.otter.canal.parse.driver.mysql.utils;
|
||||
|
||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.GTIDSet;
|
||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.MariaGTIDSet;
|
||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.MysqlGTIDSet;
|
||||
|
||||
/**
|
||||
* 类 GtidUtil.java 的实现
|
||||
*
|
||||
* @author winger 2020/9/24 1:25 下午
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class GtidUtil {
|
||||
|
||||
public static GTIDSet parseGtidSet(String gtid, boolean isMariaDB) {
|
||||
if (isMariaDB) {
|
||||
return MariaGTIDSet.parse(gtid);
|
||||
}
|
||||
return MysqlGTIDSet.parse(gtid);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user