Search in sources :

Example 11 with LogEvent

use of com.taobao.tddl.dbsync.binlog.LogEvent 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);
    try (FileLogFetcher fetcher = new FileLogFetcher(bufferSize)) {
        LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT);
        LogContext context = new LogContext();
        fetcher.open(current, binlogPosition);
        context.setLogPosition(new LogPosition(binlogfilename, binlogPosition));
        while (running) {
            boolean needContinue = true;
            LogEvent event = null;
            while (fetcher.fetch()) {
                event = decoder.decode(fetcher, context);
                if (event == null) {
                    continue;
                }
                checkServerId(event);
                if (!func.sink(event)) {
                    needContinue = false;
                    break;
                }
            }
            // 关闭上一个文件
            fetcher.close();
            parserFinish(current.getName());
            if (needContinue) {
                // 读取下一个
                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");
    }
}
Also used : LogEvent(com.taobao.tddl.dbsync.binlog.LogEvent) 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) FileLogFetcher(com.taobao.tddl.dbsync.binlog.FileLogFetcher) LogPosition(com.taobao.tddl.dbsync.binlog.LogPosition)

Example 12 with LogEvent

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

the class LocalBinLogConnection method dump.

@Override
public void dump(long timestampMills, MultiStageCoprocessor coprocessor) 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;
            while (fetcher.fetch()) {
                LogEvent event = decoder.decode(fetcher, context);
                if (event != null) {
                    checkServerId(event);
                    if (event.getWhen() > timestampSeconds) {
                        break;
                    }
                    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();
                    } else if (LogEvent.FORMAT_DESCRIPTION_EVENT == event.getHeader().getType()) {
                        lastXidLogFilename = current.getName();
                        lastXidLogFileOffset = event.getLogPos();
                    }
                }
            }
            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, coprocessor);
}
Also used : LogEvent(com.taobao.tddl.dbsync.binlog.LogEvent) 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)

Aggregations

LogEvent (com.taobao.tddl.dbsync.binlog.LogEvent)12 LogContext (com.taobao.tddl.dbsync.binlog.LogContext)9 LogDecoder (com.taobao.tddl.dbsync.binlog.LogDecoder)9 CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)6 QueryLogEvent (com.taobao.tddl.dbsync.binlog.event.QueryLogEvent)5 DirectLogFetcher (com.alibaba.otter.canal.parse.inbound.mysql.dbsync.DirectLogFetcher)4 FileLogFetcher (com.taobao.tddl.dbsync.binlog.FileLogFetcher)4 LogPosition (com.taobao.tddl.dbsync.binlog.LogPosition)4 FormatDescriptionLogEvent (com.taobao.tddl.dbsync.binlog.event.FormatDescriptionLogEvent)4 File (java.io.File)4 IOException (java.io.IOException)4 CanalEntry (com.alibaba.otter.canal.protocol.CanalEntry)3 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)3 EntryPosition (com.alibaba.otter.canal.protocol.position.EntryPosition)2 DeleteRowsLogEvent (com.taobao.tddl.dbsync.binlog.event.DeleteRowsLogEvent)2 RowsLogEvent (com.taobao.tddl.dbsync.binlog.event.RowsLogEvent)2 TableMapLogEvent (com.taobao.tddl.dbsync.binlog.event.TableMapLogEvent)2 UpdateRowsLogEvent (com.taobao.tddl.dbsync.binlog.event.UpdateRowsLogEvent)2 WriteRowsLogEvent (com.taobao.tddl.dbsync.binlog.event.WriteRowsLogEvent)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2