Search in sources :

Example 11 with EntryPosition

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

the class MysqlEventParserTest method test_timestamp.

@Test
public void test_timestamp() throws InterruptedException {
    final TimeoutChecker timeoutChecker = new TimeoutChecker(3000 * 1000);
    final AtomicLong entryCount = new AtomicLong(0);
    final EntryPosition entryPosition = new EntryPosition();
    final MysqlEventParser controller = new MysqlEventParser();
    final EntryPosition defaultPosition = buildPosition(null, null, 1475116855000L);
    controller.setSlaveId(3344L);
    controller.setDetectingEnable(false);
    controller.setDetectingSQL(DETECTING_SQL);
    controller.setMasterInfo(buildAuthentication());
    controller.setMasterPosition(defaultPosition);
    controller.setEventSink(new AbstractCanalEventSinkTest<List<Entry>>() {

        @Override
        public boolean sink(List<Entry> entrys, InetSocketAddress remoteAddress, String destination) throws CanalSinkException {
            for (Entry entry : entrys) {
                if (entry.getEntryType() != EntryType.HEARTBEAT) {
                    entryCount.incrementAndGet();
                    String logfilename = entry.getHeader().getLogfileName();
                    long logfileoffset = entry.getHeader().getLogfileOffset();
                    long executeTime = entry.getHeader().getExecuteTime();
                    entryPosition.setJournalName(logfilename);
                    entryPosition.setPosition(logfileoffset);
                    entryPosition.setTimestamp(executeTime);
                    break;
                }
            }
            if (entryCount.get() > 0) {
                controller.stop();
                timeoutChecker.stop();
                timeoutChecker.touch();
            }
            return true;
        }
    });
    controller.setLogPositionManager(new AbstractCanalLogPositionManager() {

        public void persistLogPosition(String destination, LogPosition logPosition) {
            System.out.println(logPosition);
        }

        public LogPosition getLatestIndexBy(String destination) {
            return null;
        }
    });
    controller.start();
    timeoutChecker.waitForIdle();
    if (controller.isStart()) {
        controller.stop();
    }
    // check
    Assert.assertTrue(entryCount.get() > 0);
    // 对比第一条数据和起始的position相同
    Assert.assertEquals(entryPosition.getJournalName(), "mysql-bin.000001");
    Assert.assertTrue(entryPosition.getPosition() <= 6163L);
    Assert.assertTrue(entryPosition.getTimestamp() <= defaultPosition.getTimestamp());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) AtomicLong(java.util.concurrent.atomic.AtomicLong) Entry(com.alibaba.otter.canal.protocol.CanalEntry.Entry) TimeoutChecker(com.alibaba.otter.canal.parse.helper.TimeoutChecker) List(java.util.List) EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition) AbstractCanalLogPositionManager(com.alibaba.otter.canal.parse.stub.AbstractCanalLogPositionManager) CanalSinkException(com.alibaba.otter.canal.sink.exception.CanalSinkException) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition) Test(org.junit.Test) AbstractCanalEventSinkTest(com.alibaba.otter.canal.parse.stub.AbstractCanalEventSinkTest)

Example 12 with EntryPosition

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

the class AbstractLogPositionManagerTest method buildPosition.

protected LogPosition buildPosition(int number) {
    LogPosition position = new LogPosition();
    position.setIdentity(new LogIdentity(new InetSocketAddress(MYSQL_ADDRESS, 3306), 1234L));
    position.setPostion(new EntryPosition("mysql-bin.000000" + number, 106L, new Date().getTime()));
    return position;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) 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 13 with EntryPosition

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

the class CanalEventUtils method checkPosition.

/**
     * 判断当前的entry和position是否相同
     */
public static boolean checkPosition(Event event, LogPosition logPosition) {
    EntryPosition position = logPosition.getPostion();
    CanalEntry.Entry entry = event.getEntry();
    boolean result = position.getTimestamp().equals(entry.getHeader().getExecuteTime());
    boolean exactely = (StringUtils.isBlank(position.getJournalName()) && position.getPosition() == null);
    if (!exactely) {
        // 精确匹配
        result &= StringUtils.equals(entry.getHeader().getLogfileName(), position.getJournalName());
        result &= position.getPosition().equals(entry.getHeader().getLogfileOffset());
    }
    return result;
}
Also used : CanalEntry(com.alibaba.otter.canal.protocol.CanalEntry) EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition)

Example 14 with EntryPosition

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

the class CanalEventUtils method createPosition.

/**
     * 根据entry创建对应的Position对象
     */
public static LogPosition createPosition(Event event, boolean included) {
    EntryPosition position = new EntryPosition();
    position.setJournalName(event.getEntry().getHeader().getLogfileName());
    position.setPosition(event.getEntry().getHeader().getLogfileOffset());
    position.setTimestamp(event.getEntry().getHeader().getExecuteTime());
    position.setIncluded(included);
    LogPosition logPosition = new LogPosition();
    logPosition.setPostion(position);
    logPosition.setIdentity(event.getLogIdentity());
    return logPosition;
}
Also used : EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition)

Example 15 with EntryPosition

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

the class MysqlEventParser method findEndPosition.

protected EntryPosition findEndPosition(ErosaConnection connection) throws IOException {
    MysqlConnection mysqlConnection = (MysqlConnection) connection;
    EntryPosition endPosition = findEndPosition(mysqlConnection);
    return endPosition;
}
Also used : EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition)

Aggregations

EntryPosition (com.alibaba.otter.canal.protocol.position.EntryPosition)26 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)19 InetSocketAddress (java.net.InetSocketAddress)12 AbstractCanalLogPositionManager (com.alibaba.otter.canal.parse.stub.AbstractCanalLogPositionManager)10 Entry (com.alibaba.otter.canal.protocol.CanalEntry.Entry)10 AbstractCanalEventSinkTest (com.alibaba.otter.canal.parse.stub.AbstractCanalEventSinkTest)9 CanalSinkException (com.alibaba.otter.canal.sink.exception.CanalSinkException)9 List (java.util.List)9 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 Test (org.junit.Test)9 TimeoutChecker (com.alibaba.otter.canal.parse.helper.TimeoutChecker)7 LogIdentity (com.alibaba.otter.canal.protocol.position.LogIdentity)5 IOException (java.io.IOException)5 Date (java.util.Date)5 CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)4 AuthenticationInfo (com.alibaba.otter.canal.parse.support.AuthenticationInfo)3 CanalEntry (com.alibaba.otter.canal.protocol.CanalEntry)3 ResultSetPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket)2 MysqlEventParser (com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser)2 EventType (com.alibaba.otter.canal.protocol.CanalEntry.EventType)2