mysql semi support and mariadb gtid parse

This commit is contained in:
lucifax301
2017-12-18 14:10:53 +08:00
parent ac773009f8
commit d66839c965
8 changed files with 164 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ import com.alibaba.otter.canal.parse.driver.mysql.MysqlQueryExecutor;
import com.alibaba.otter.canal.parse.driver.mysql.MysqlUpdateExecutor;
import com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket;
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.BinlogDumpCommandPacket;
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.SemiAckCommandPacket;
import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket;
import com.alibaba.otter.canal.parse.driver.mysql.utils.PacketManager;
import com.alibaba.otter.canal.parse.exception.CanalParseException;
@@ -24,6 +25,7 @@ import com.alibaba.otter.canal.parse.support.AuthenticationInfo;
import com.taobao.tddl.dbsync.binlog.LogContext;
import com.taobao.tddl.dbsync.binlog.LogDecoder;
import com.taobao.tddl.dbsync.binlog.LogEvent;
import com.taobao.tddl.dbsync.binlog.event.RotateLogEvent;
public class MysqlConnection implements ErosaConnection {
@@ -35,6 +37,8 @@ public class MysqlConnection implements ErosaConnection {
private BinlogFormat binlogFormat;
private BinlogImage binlogImage;
private String binlogfilename;
// tsdb releated
private AuthenticationInfo authInfo;
protected int connTimeout = 5 * 1000; // 5秒
@@ -141,9 +145,18 @@ public class MysqlConnection implements ErosaConnection {
if (event == null) {
throw new CanalParseException("parse failed");
}
//binlog日志文件发生变化
if(event.getHeader().getType()==LogEvent.ROTATE_EVENT){
binlogfilename=((RotateLogEvent)event).getFilename();
}
if (!func.sink(event)) {
break;
}else{
if(event.getSemival()==1){
sendSemiAck(binlogfilename, binlogPosition);
}
}
}
}
@@ -167,6 +180,21 @@ public class MysqlConnection implements ErosaConnection {
PacketManager.writePkg(connector.getChannel(), binlogDumpHeader.toBytes(), cmdBody);
connector.setDumping(true);
}
private void sendSemiAck(String binlogfilename, Long binlogPosition) throws IOException {
SemiAckCommandPacket semiAckCmd = new SemiAckCommandPacket();
semiAckCmd.binlogFileName = binlogfilename;
semiAckCmd.binlogPosition = binlogPosition;
byte[] cmdBody = semiAckCmd.toBytes();
logger.info("SEMI ACK with position:{}", semiAckCmd);
HeaderPacket semiAckHeader = new HeaderPacket();
semiAckHeader.setPacketBodyLength(cmdBody.length);
semiAckHeader.setPacketSequenceNumber((byte) 0x00);
PacketManager.writePkg(connector.getChannel(), semiAckHeader.toBytes(), cmdBody);
}
public MysqlConnection fork() {
MysqlConnection connection = new MysqlConnection();

View File

@@ -36,6 +36,8 @@ public class DirectLogFetcher extends LogFetcher {
public static final int MAX_PACKET_LENGTH = (256 * 256 * 256 - 1);
private SocketChannel channel;
private boolean issemi=false;
// private BufferedInputStream input;
@@ -53,6 +55,10 @@ public class DirectLogFetcher extends LogFetcher {
public void start(SocketChannel channel) throws IOException {
this.channel = channel;
String dbsemi= System.getProperty("db.semi");
if("1".equals(dbsemi)){
issemi=true;
}
// 和mysql driver一样,提供buffer机制,提升读取binlog速度
// this.input = new
// BufferedInputStream(channel.socket().getInputStream(), 16384);
@@ -105,7 +111,15 @@ public class DirectLogFetcher extends LogFetcher {
+ ", len = " + netlen);
}
}
//if mysql is in semi mode
if(issemi){
//parse semi mark
int semimark=getUint8(NET_HEADER_SIZE+1);
int semival=getUint8(NET_HEADER_SIZE+2);
this.semival=semival;
}
// The first packet is a multi-packet, concatenate the packets.
while (netlen == MAX_PACKET_LENGTH) {
if (!fetch0(0, NET_HEADER_SIZE)) {
@@ -122,7 +136,11 @@ public class DirectLogFetcher extends LogFetcher {
}
// Preparing buffer variables to decoding.
origin = NET_HEADER_SIZE + 1;
if(issemi){
origin = NET_HEADER_SIZE + 3;
}else{
origin = NET_HEADER_SIZE + 1;
}
position = origin;
limit -= origin;
return true;