canal 源码解析(2)-数据流转篇(1)

1) 上一篇 只是正常启动,但是线程是等待中,没有数据接入处理。现在开始模拟同步数据,并分析其中原理  如上一片线程阻塞点位于:AbstractEventParser类的start方法2)mysql主从复制重点就在这里了,当前因为 startposition里部位null,// 4. 开始dump数据// 判断所属instance是否启用GTID模式,是的话调用ErosaConnection中G...
摘要由CSDN通过智能技术生成

一、组装数据


       上一篇 只是正常启动,但是线程是等待中,没有数据接入处理。现在开始模拟同步数据,并分析其中原理


AbstractEventParser类的start方法



2、mysql主从复制重点就在这里了,当前因为 startposition里部位空对象,

EntryPosition[included=false,journalName=mysql-bin.000023,position=123,serverId=1,gtid=<null>,timestamp=1528939448000]

3、

// 重新链接,因为在找position过程中可能有状态,需要断开后重建
erosaConnection.reconnect();

改变一下状态:connect为true、

4、dum数据

// 4. 开始dump数据
// 判断所属instance是否启用GTID模式,是的话调用ErosaConnection中GTID对应方法dump数据
if (isGTIDMode()) {
    erosaConnection.dump(MysqlGTIDSet.parse(startPosition.getGtid()), sinkHandler);
} else {
    if (StringUtils.isEmpty(startPosition.getJournalName()) && startPosition.getTimestamp() != null) {
        erosaConnection.dump(startPosition.getTimestamp(), sinkHandler);
    } else {
        erosaConnection.dump(startPosition.getJournalName(),
                startPosition.getPosition(),
                sinkHandler);
    }
}

 4.1 dump详细内容

public void dump(String binlogfilename, Long binlogPosition, SinkFunction func) throws IOException {
    updateSettings();
    sendRegisterSlave();
    sendBinlogDump(binlogfilename, binlogPosition);
    DirectLogFetcher fetcher = new DirectLogFetcher(connector.getReceiveBufferSize());
    fetcher.start(connector.getChannel());
    LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT);
    LogContext context = new LogContext();
    while (fetcher.fetch()) {
        LogEvent event = null;
        event = decoder.decode(fetcher, context);

        if (event == null) {
            throw new CanalParseException("parse failed");
        }

        if (!func.sink(event)) {
            break;
        }

        if (event.getSemival() == 1) {
            sendSemiAck(context.getLogPosition().getFileName(), binlogPosition);
        }
    }
}

4.1.1  设置mysql的通用参数,心跳间隔时间,内存参数

private void updateSettings() throws IOException {
    try {
        update("set wait_timeout=9999999");
    } catch (Exception e) {
        logger.warn("update wait_timeout failed", e);
    }
    try {
        update("set net_write_timeout=1800");
    } catch (Exception e) {
        logger.warn("update net_write_timeout failed", e);
    }
 
 构造器初始化第一个参数
public QueryCommandPacket(){
    setCommand((byte) 0x03);
}
public byte[] toBytes() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write(getCommand());
    out.write(getQueryString().getBytes("UTF-8"));// 链接建立时默认指定编码为UTF-8
    return out.toByteArray();
}


二、发送数据

 

三、接受数据

mysql协议规定了,通用接受数据的每个类型;参考链接

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值