Search in sources :

Example 6 with LogIdentity

use of com.alibaba.otter.canal.protocol.position.LogIdentity in project canal by alibaba.

the class MetaLogPositionManagerTest method buildRange.

private PositionRange<LogPosition> buildRange(int number) {
    LogPosition start = new LogPosition();
    start.setIdentity(new LogIdentity(new InetSocketAddress(MYSQL_ADDRESS, 3306), 1234L));
    start.setPostion(new EntryPosition("mysql-bin.000000" + number, 106L, new Date().getTime()));
    LogPosition end = new LogPosition();
    end.setIdentity(new LogIdentity(new InetSocketAddress(MYSQL_ADDRESS, 3306), 1234L));
    end.setPostion(new EntryPosition("mysql-bin.000000" + (number + 1), 106L, (new Date().getTime()) + 1000 * 1000L));
    return new PositionRange<>(start, end);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) PositionRange(com.alibaba.otter.canal.protocol.position.PositionRange) LogIdentity(com.alibaba.otter.canal.protocol.position.LogIdentity) EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition) Date(java.util.Date) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition)

Example 7 with LogIdentity

use of com.alibaba.otter.canal.protocol.position.LogIdentity in project canal by alibaba.

the class AbstractEventParser method buildLastPosition.

protected LogPosition buildLastPosition(CanalEntry.Entry entry) {
    // 初始化一下
    LogPosition logPosition = new LogPosition();
    EntryPosition position = new EntryPosition();
    position.setJournalName(entry.getHeader().getLogfileName());
    position.setPosition(entry.getHeader().getLogfileOffset());
    position.setTimestamp(entry.getHeader().getExecuteTime());
    // add serverId at 2016-06-28
    position.setServerId(entry.getHeader().getServerId());
    // set gtid
    position.setGtid(entry.getHeader().getGtid());
    logPosition.setPostion(position);
    LogIdentity identity = new LogIdentity(runningInfo.getAddress(), -1L);
    logPosition.setIdentity(identity);
    return logPosition;
}
Also used : LogIdentity(com.alibaba.otter.canal.protocol.position.LogIdentity) EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition)

Example 8 with LogIdentity

use of com.alibaba.otter.canal.protocol.position.LogIdentity in project canal by alibaba.

the class EntryEventSink method sinkData.

private boolean sinkData(List<CanalEntry.Entry> entrys, InetSocketAddress remoteAddress) throws InterruptedException {
    boolean hasRowData = false;
    boolean hasHeartBeat = false;
    List<Event> events = new ArrayList<>();
    for (CanalEntry.Entry entry : entrys) {
        if (!doFilter(entry)) {
            continue;
        }
        if (filterTransactionEntry && (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND)) {
            long currentTimestamp = entry.getHeader().getExecuteTime();
            // 基于一定的策略控制,放过空的事务头和尾,便于及时更新数据库位点,表明工作正常
            if (lastTransactionCount.incrementAndGet() <= emptyTransctionThresold && Math.abs(currentTimestamp - lastTransactionTimestamp) <= emptyTransactionInterval) {
                continue;
            } else {
                // 主要原因在于空事务只发送了begin,没有同步发送commit信息,这里修改为只对commit事件做计数更新,确保begin/commit成对出现
                if (entry.getEntryType() == EntryType.TRANSACTIONEND) {
                    lastTransactionCount.set(0L);
                    lastTransactionTimestamp = currentTimestamp;
                }
            }
        }
        hasRowData |= (entry.getEntryType() == EntryType.ROWDATA);
        hasHeartBeat |= (entry.getEntryType() == EntryType.HEARTBEAT);
        Event event = new Event(new LogIdentity(remoteAddress, -1L), entry, raw);
        events.add(event);
    }
    if (hasRowData || hasHeartBeat) {
        // 存在row记录 或者 存在heartbeat记录,直接跳给后续处理
        return doSink(events);
    } else {
        // 需要过滤的数据
        if (filterEmtryTransactionEntry && !CollectionUtils.isEmpty(events)) {
            long currentTimestamp = events.get(0).getExecuteTime();
            // 基于一定的策略控制,放过空的事务头和尾,便于及时更新数据库位点,表明工作正常
            if (Math.abs(currentTimestamp - lastEmptyTransactionTimestamp) > emptyTransactionInterval || lastEmptyTransactionCount.incrementAndGet() > emptyTransctionThresold) {
                lastEmptyTransactionCount.set(0L);
                lastEmptyTransactionTimestamp = currentTimestamp;
                return doSink(events);
            }
        }
        // 直接返回true,忽略空的事务头和尾
        return true;
    }
}
Also used : ArrayList(java.util.ArrayList) CanalEntry(com.alibaba.otter.canal.protocol.CanalEntry) Event(com.alibaba.otter.canal.store.model.Event) LogIdentity(com.alibaba.otter.canal.protocol.position.LogIdentity) Entry(com.alibaba.otter.canal.protocol.CanalEntry.Entry)

Example 9 with LogIdentity

use of com.alibaba.otter.canal.protocol.position.LogIdentity in project canal by alibaba.

the class MemoryEventStoreBase method buildEvent.

protected Event buildEvent(String binlogFile, long offset, long timestamp) {
    Header.Builder headerBuilder = Header.newBuilder();
    headerBuilder.setLogfileName(binlogFile);
    headerBuilder.setLogfileOffset(offset);
    headerBuilder.setExecuteTime(timestamp);
    headerBuilder.setEventLength(1024);
    Entry.Builder entryBuilder = Entry.newBuilder();
    entryBuilder.setHeader(headerBuilder.build());
    Entry entry = entryBuilder.build();
    return new Event(new LogIdentity(new InetSocketAddress(MYSQL_ADDRESS, 3306), 1234L), entry);
}
Also used : Entry(com.alibaba.otter.canal.protocol.CanalEntry.Entry) Header(com.alibaba.otter.canal.protocol.CanalEntry.Header) InetSocketAddress(java.net.InetSocketAddress) Event(com.alibaba.otter.canal.store.model.Event) LogIdentity(com.alibaba.otter.canal.protocol.position.LogIdentity)

Aggregations

LogIdentity (com.alibaba.otter.canal.protocol.position.LogIdentity)9 InetSocketAddress (java.net.InetSocketAddress)6 Entry (com.alibaba.otter.canal.protocol.CanalEntry.Entry)5 EntryPosition (com.alibaba.otter.canal.protocol.position.EntryPosition)5 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)5 Event (com.alibaba.otter.canal.store.model.Event)4 Date (java.util.Date)4 Header (com.alibaba.otter.canal.protocol.CanalEntry.Header)3 PositionRange (com.alibaba.otter.canal.protocol.position.PositionRange)2 TimeoutChecker (com.alibaba.otter.canal.parse.helper.TimeoutChecker)1 AbstractLogPositionManager (com.alibaba.otter.canal.parse.index.AbstractLogPositionManager)1 AbstractCanalEventSinkTest (com.alibaba.otter.canal.parse.stub.AbstractCanalEventSinkTest)1 CanalEntry (com.alibaba.otter.canal.protocol.CanalEntry)1 RowChange (com.alibaba.otter.canal.protocol.CanalEntry.RowChange)1 RowData (com.alibaba.otter.canal.protocol.CanalEntry.RowData)1 CanalSinkException (com.alibaba.otter.canal.sink.exception.CanalSinkException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.Test)1