Search in sources :

Example 1 with LogPosition

use of com.taobao.tddl.dbsync.binlog.LogPosition in project canal by alibaba.

the class LocalBinLogConnection method dump.

public void dump(long timestampMills, SinkFunction func) throws IOException {
    List<File> currentBinlogs = binlogs.currentBinlogs();
    File current = currentBinlogs.get(currentBinlogs.size() - 1);
    long timestampSeconds = timestampMills / 1000;
    String binlogFilename = null;
    long binlogFileOffset = 0;
    FileLogFetcher fetcher = new FileLogFetcher(bufferSize);
    LogDecoder decoder = new LogDecoder();
    decoder.handle(LogEvent.FORMAT_DESCRIPTION_EVENT);
    decoder.handle(LogEvent.QUERY_EVENT);
    decoder.handle(LogEvent.XID_EVENT);
    LogContext context = new LogContext();
    try {
        fetcher.open(current);
        context.setLogPosition(new LogPosition(current.getName()));
        while (running) {
            boolean needContinue = true;
            String lastXidLogFilename = current.getName();
            long lastXidLogFileOffset = 0;
            binlogFilename = lastXidLogFilename;
            binlogFileOffset = lastXidLogFileOffset;
            L: while (fetcher.fetch()) {
                LogEvent event;
                do {
                    event = decoder.decode(fetcher, context);
                    if (event != null) {
                        if (event.getWhen() > timestampSeconds) {
                            break L;
                        }
                        needContinue = false;
                        if (LogEvent.QUERY_EVENT == event.getHeader().getType()) {
                            if (StringUtils.endsWithIgnoreCase(((QueryLogEvent) event).getQuery(), "BEGIN")) {
                                binlogFilename = lastXidLogFilename;
                                binlogFileOffset = lastXidLogFileOffset;
                            } else if (StringUtils.endsWithIgnoreCase(((QueryLogEvent) event).getQuery(), "COMMIT")) {
                                lastXidLogFilename = current.getName();
                                lastXidLogFileOffset = event.getLogPos();
                            }
                        } else if (LogEvent.XID_EVENT == event.getHeader().getType()) {
                            lastXidLogFilename = current.getName();
                            lastXidLogFileOffset = event.getLogPos();
                        }
                    }
                } while (event != null);
            }
            if (needContinue) {
                // 读取下一个
                // 关闭上一个文件
                fetcher.close();
                File nextFile = binlogs.getBefore(current);
                if (nextFile == null) {
                    break;
                }
                current = nextFile;
                fetcher.open(current);
                context.setLogPosition(new LogPosition(current.getName()));
            } else {
                // 跳出
                break;
            }
        }
    } finally {
        if (fetcher != null) {
            fetcher.close();
        }
    }
    dump(binlogFilename, binlogFileOffset, func);
}
Also used : LogEvent(com.taobao.tddl.dbsync.binlog.LogEvent) RotateLogEvent(com.taobao.tddl.dbsync.binlog.event.RotateLogEvent) QueryLogEvent(com.taobao.tddl.dbsync.binlog.event.QueryLogEvent) LogContext(com.taobao.tddl.dbsync.binlog.LogContext) LogDecoder(com.taobao.tddl.dbsync.binlog.LogDecoder) File(java.io.File) QueryLogEvent(com.taobao.tddl.dbsync.binlog.event.QueryLogEvent) FileLogFetcher(com.taobao.tddl.dbsync.binlog.FileLogFetcher) LogPosition(com.taobao.tddl.dbsync.binlog.LogPosition)

Example 2 with LogPosition

use of com.taobao.tddl.dbsync.binlog.LogPosition in project canal by alibaba.

the class MysqlConnection method dump.

public void dump(String binlogfilename, Long binlogPosition, SinkFunction func) throws IOException {
    updateSettings();
    loadBinlogChecksum();
    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();
    context.setLogPosition(new LogPosition(binlogfilename));
    context.setFormatDescription(new FormatDescriptionLogEvent(4, binlogChecksum));
    while (fetcher.fetch()) {
        LogEvent event = null;
        event = decoder.decode(fetcher, context);
        if (event == null) {
            throw new CanalParseException("parse failed");
        }
        if (!func.sink(event)) {
            break;
        }
    }
}
Also used : FormatDescriptionLogEvent(com.taobao.tddl.dbsync.binlog.event.FormatDescriptionLogEvent) DirectLogFetcher(com.alibaba.otter.canal.parse.inbound.mysql.dbsync.DirectLogFetcher) LogEvent(com.taobao.tddl.dbsync.binlog.LogEvent) FormatDescriptionLogEvent(com.taobao.tddl.dbsync.binlog.event.FormatDescriptionLogEvent) LogContext(com.taobao.tddl.dbsync.binlog.LogContext) LogDecoder(com.taobao.tddl.dbsync.binlog.LogDecoder) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException) LogPosition(com.taobao.tddl.dbsync.binlog.LogPosition)

Example 3 with LogPosition

use of com.taobao.tddl.dbsync.binlog.LogPosition in project canal by alibaba.

the class MysqlConnection method seek.

/**
     * 加速主备切换时的查找速度,做一些特殊优化,比如只解析事务头或者尾
     */
public void seek(String binlogfilename, Long binlogPosition, SinkFunction func) throws IOException {
    updateSettings();
    loadBinlogChecksum();
    sendBinlogDump(binlogfilename, binlogPosition);
    DirectLogFetcher fetcher = new DirectLogFetcher(connector.getReceiveBufferSize());
    fetcher.start(connector.getChannel());
    LogDecoder decoder = new LogDecoder();
    decoder.handle(LogEvent.ROTATE_EVENT);
    decoder.handle(LogEvent.FORMAT_DESCRIPTION_EVENT);
    decoder.handle(LogEvent.QUERY_EVENT);
    decoder.handle(LogEvent.XID_EVENT);
    LogContext context = new LogContext();
    context.setLogPosition(new LogPosition(binlogfilename));
    context.setFormatDescription(new FormatDescriptionLogEvent(4, binlogChecksum));
    while (fetcher.fetch()) {
        LogEvent event = null;
        event = decoder.decode(fetcher, context);
        if (event == null) {
            throw new CanalParseException("parse failed");
        }
        if (!func.sink(event)) {
            break;
        }
    }
}
Also used : FormatDescriptionLogEvent(com.taobao.tddl.dbsync.binlog.event.FormatDescriptionLogEvent) DirectLogFetcher(com.alibaba.otter.canal.parse.inbound.mysql.dbsync.DirectLogFetcher) LogEvent(com.taobao.tddl.dbsync.binlog.LogEvent) FormatDescriptionLogEvent(com.taobao.tddl.dbsync.binlog.event.FormatDescriptionLogEvent) LogContext(com.taobao.tddl.dbsync.binlog.LogContext) LogDecoder(com.taobao.tddl.dbsync.binlog.LogDecoder) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException) LogPosition(com.taobao.tddl.dbsync.binlog.LogPosition)

Example 4 with LogPosition

use of com.taobao.tddl.dbsync.binlog.LogPosition in project canal by alibaba.

the class LocalBinLogConnection method dump.

public void dump(String binlogfilename, Long binlogPosition, SinkFunction func) throws IOException {
    File current = new File(directory, binlogfilename);
    FileLogFetcher fetcher = new FileLogFetcher(bufferSize);
    LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT);
    LogContext context = new LogContext();
    try {
        fetcher.open(current, binlogPosition);
        context.setLogPosition(new LogPosition(binlogfilename, binlogPosition));
        while (running) {
            boolean needContinue = true;
            LogEvent event = null;
            L: while (fetcher.fetch()) {
                do {
                    if (event == null) {
                        event = new RotateLogEvent(context.getLogPosition().getFileName(), context.getLogPosition().getPosition());
                    } else {
                        event = decoder.decode(fetcher, context);
                    }
                    if (event != null && !func.sink(event)) {
                        needContinue = false;
                        break L;
                    }
                } while (event != null);
            }
            if (needContinue) {
                // 读取下一个
                // 关闭上一个文件
                fetcher.close();
                File nextFile;
                if (needWait) {
                    nextFile = binlogs.waitForNextFile(current);
                } else {
                    nextFile = binlogs.getNextFile(current);
                }
                if (nextFile == null) {
                    break;
                }
                current = nextFile;
                fetcher.open(current);
                context.setLogPosition(new LogPosition(nextFile.getName()));
            } else {
                // 跳出
                break;
            }
        }
    } catch (InterruptedException e) {
        logger.warn("LocalBinLogConnection dump interrupted");
    } finally {
        if (fetcher != null) {
            fetcher.close();
        }
    }
}
Also used : LogEvent(com.taobao.tddl.dbsync.binlog.LogEvent) RotateLogEvent(com.taobao.tddl.dbsync.binlog.event.RotateLogEvent) QueryLogEvent(com.taobao.tddl.dbsync.binlog.event.QueryLogEvent) LogContext(com.taobao.tddl.dbsync.binlog.LogContext) RotateLogEvent(com.taobao.tddl.dbsync.binlog.event.RotateLogEvent) LogDecoder(com.taobao.tddl.dbsync.binlog.LogDecoder) File(java.io.File) FileLogFetcher(com.taobao.tddl.dbsync.binlog.FileLogFetcher) LogPosition(com.taobao.tddl.dbsync.binlog.LogPosition)

Aggregations

LogContext (com.taobao.tddl.dbsync.binlog.LogContext)4 LogDecoder (com.taobao.tddl.dbsync.binlog.LogDecoder)4 LogEvent (com.taobao.tddl.dbsync.binlog.LogEvent)4 LogPosition (com.taobao.tddl.dbsync.binlog.LogPosition)4 CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)2 DirectLogFetcher (com.alibaba.otter.canal.parse.inbound.mysql.dbsync.DirectLogFetcher)2 FileLogFetcher (com.taobao.tddl.dbsync.binlog.FileLogFetcher)2 FormatDescriptionLogEvent (com.taobao.tddl.dbsync.binlog.event.FormatDescriptionLogEvent)2 QueryLogEvent (com.taobao.tddl.dbsync.binlog.event.QueryLogEvent)2 RotateLogEvent (com.taobao.tddl.dbsync.binlog.event.RotateLogEvent)2 File (java.io.File)2