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;
}
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;
}
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();
}
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);
}
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;
}
Aggregations