Search in sources :

Example 11 with PositionRange

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

the class ZooKeeperMetaManager method removeBatch.

public PositionRange removeBatch(ClientIdentity clientIdentity, Long batchId) throws CanalMetaManagerException {
    String batchsPath = ZookeeperPathUtils.getBatchMarkPath(clientIdentity.getDestination(), clientIdentity.getClientId());
    List<String> nodes = zkClientx.getChildren(batchsPath);
    if (CollectionUtils.isEmpty(nodes)) {
        // 没有batch记录
        return null;
    }
    // 找到最小的Id
    ArrayList<Long> batchIds = new ArrayList<Long>(nodes.size());
    for (String batchIdString : nodes) {
        batchIds.add(Long.valueOf(batchIdString));
    }
    Long minBatchId = Collections.min(batchIds);
    if (!minBatchId.equals(batchId)) {
        // 检查一下提交的ack/rollback,必须按batchId分出去的顺序提交,否则容易出现丢数据
        throw new CanalMetaManagerException(String.format("batchId:%d is not the firstly:%d", batchId, minBatchId));
    }
    if (!batchIds.contains(batchId)) {
        // 不存在对应的batchId
        return null;
    }
    PositionRange positionRange = getBatch(clientIdentity, batchId);
    if (positionRange != null) {
        String path = ZookeeperPathUtils.getBatchMarkWithIdPath(clientIdentity.getDestination(), clientIdentity.getClientId(), batchId);
        zkClientx.delete(path);
    }
    return positionRange;
}
Also used : ArrayList(java.util.ArrayList) PositionRange(com.alibaba.otter.canal.protocol.position.PositionRange) CanalMetaManagerException(com.alibaba.otter.canal.meta.exception.CanalMetaManagerException)

Example 12 with PositionRange

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

the class AbstractMetaManagerTest method doCursorTest.

public Position doCursorTest(CanalMetaManager metaManager) {
    metaManager.subscribe(clientIdentity);
    Position position1 = metaManager.getCursor(clientIdentity);
    Assert.assertNull(position1);
    PositionRange range = buildRange(1);
    metaManager.updateCursor(clientIdentity, range.getStart());
    Position position2 = metaManager.getCursor(clientIdentity);
    Assert.assertEquals(range.getStart(), position2);
    metaManager.updateCursor(clientIdentity, range.getEnd());
    Position position3 = metaManager.getCursor(clientIdentity);
    Assert.assertEquals(range.getEnd(), position3);
    return position3;
}
Also used : Position(com.alibaba.otter.canal.protocol.position.Position) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition) EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition) PositionRange(com.alibaba.otter.canal.protocol.position.PositionRange)

Example 13 with PositionRange

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

the class MetaLogPositionManagerTest method testAll.

@Test
public void testAll() {
    MixedMetaManager metaManager = new MixedMetaManager();
    ZooKeeperMetaManager zooKeeperMetaManager = new ZooKeeperMetaManager();
    zooKeeperMetaManager.setZkClientx(zkclientx);
    metaManager.setZooKeeperMetaManager(zooKeeperMetaManager);
    metaManager.start();
    MetaLogPositionManager logPositionManager = new MetaLogPositionManager();
    logPositionManager.setMetaManager(metaManager);
    logPositionManager.start();
    // 构建meta信息
    ClientIdentity client1 = new ClientIdentity(destination, (short) 1);
    metaManager.subscribe(client1);
    PositionRange range1 = buildRange(1);
    metaManager.updateCursor(client1, range1.getEnd());
    PositionRange range2 = buildRange(2);
    metaManager.updateCursor(client1, range2.getEnd());
    ClientIdentity client2 = new ClientIdentity(destination, (short) 2);
    metaManager.subscribe(client2);
    PositionRange range3 = buildRange(3);
    metaManager.updateCursor(client2, range3.getEnd());
    PositionRange range4 = buildRange(4);
    metaManager.updateCursor(client2, range4.getEnd());
    LogPosition logPosition = logPositionManager.getLatestIndexBy(destination);
    Assert.assertEquals(range2.getEnd(), logPosition);
    metaManager.stop();
    logPositionManager.stop();
}
Also used : ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) PositionRange(com.alibaba.otter.canal.protocol.position.PositionRange) MixedMetaManager(com.alibaba.otter.canal.meta.MixedMetaManager) ZooKeeperMetaManager(com.alibaba.otter.canal.meta.ZooKeeperMetaManager) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition) Test(org.junit.Test)

Example 14 with PositionRange

use of com.alibaba.otter.canal.protocol.position.PositionRange 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<LogPosition>(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 15 with PositionRange

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

the class CanalServerWithEmbedded method listBatchIds.

/**
     * 查询当前未被ack的batch列表,batchId会按照从小到大进行返回
     */
public List<Long> listBatchIds(ClientIdentity clientIdentity) throws CanalServerException {
    checkStart(clientIdentity.getDestination());
    checkSubscribe(clientIdentity);
    CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
    Map<Long, PositionRange> batchs = canalInstance.getMetaManager().listAllBatchs(clientIdentity);
    List<Long> result = new ArrayList<Long>(batchs.keySet());
    Collections.sort(result);
    return result;
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) PositionRange(com.alibaba.otter.canal.protocol.position.PositionRange) ArrayList(java.util.ArrayList)

Aggregations

PositionRange (com.alibaba.otter.canal.protocol.position.PositionRange)15 ArrayList (java.util.ArrayList)6 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)5 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)3 EntryPosition (com.alibaba.otter.canal.protocol.position.EntryPosition)3 Position (com.alibaba.otter.canal.protocol.position.Position)3 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)3 LogIdentity (com.alibaba.otter.canal.protocol.position.LogIdentity)2 Function (com.google.common.base.Function)2 MigrateMap (com.google.common.collect.MigrateMap)2 InetSocketAddress (java.net.InetSocketAddress)2 Date (java.util.Date)2 Map (java.util.Map)2 CanalInstance (com.alibaba.otter.canal.instance.core.CanalInstance)1 MixedMetaManager (com.alibaba.otter.canal.meta.MixedMetaManager)1 ZooKeeperMetaManager (com.alibaba.otter.canal.meta.ZooKeeperMetaManager)1 CanalMetaManagerException (com.alibaba.otter.canal.meta.exception.CanalMetaManagerException)1 Event (com.alibaba.otter.canal.store.model.Event)1 Events (com.alibaba.otter.canal.store.model.Events)1 HashSet (java.util.HashSet)1