Search in sources :

Example 11 with CanalInstance

use of com.alibaba.otter.canal.instance.core.CanalInstance in project canal by alibaba.

the class CanalServerWithEmbedded method ack.

/**
 * 进行 batch id 的确认。确认之后,小于等于此 batchId 的 Message 都会被确认。
 *
 * <pre>
 * 注意:进行反馈时必须按照batchId的顺序进行ack(需有客户端保证)
 * </pre>
 */
@Override
public void ack(ClientIdentity clientIdentity, long batchId) throws CanalServerException {
    checkStart(clientIdentity.getDestination());
    checkSubscribe(clientIdentity);
    CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
    PositionRange<LogPosition> positionRanges = null;
    // 更新位置
    positionRanges = canalInstance.getMetaManager().removeBatch(clientIdentity, batchId);
    if (positionRanges == null) {
        // 说明是重复的ack/rollback
        throw new CanalServerException(String.format("ack error , clientId:%s batchId:%d is not exist , please check", clientIdentity.getClientId(), batchId));
    }
    // 更新cursor
    if (positionRanges.getAck() != null) {
        canalInstance.getMetaManager().updateCursor(clientIdentity, positionRanges.getAck());
        if (logger.isInfoEnabled()) {
            logger.info("ack successfully, clientId:{} batchId:{} position:{}", clientIdentity.getClientId(), batchId, positionRanges);
        }
    }
    // 可定时清理数据
    canalInstance.getEventStore().ack(positionRanges.getEnd(), positionRanges.getEndSeq());
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) CanalServerException(com.alibaba.otter.canal.server.exception.CanalServerException) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition)

Example 12 with CanalInstance

use of com.alibaba.otter.canal.instance.core.CanalInstance in project canal by alibaba.

the class CanalServerWithNettyTest method setUp.

@Before
public void setUp() {
    CanalServerWithEmbedded embeddedServer = new CanalServerWithEmbedded();
    embeddedServer.setCanalInstanceGenerator(new CanalInstanceGenerator() {

        public CanalInstance generate(String destination) {
            Canal canal = buildCanal();
            return new CanalInstanceWithManager(canal, FILTER);
        }
    });
    nettyServer = CanalServerWithNetty.instance();
    nettyServer.setEmbeddedServer(embeddedServer);
    nettyServer.setPort(1088);
    nettyServer.start();
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) CanalServerWithEmbedded(com.alibaba.otter.canal.server.embedded.CanalServerWithEmbedded) Canal(com.alibaba.otter.canal.instance.manager.model.Canal) CanalInstanceGenerator(com.alibaba.otter.canal.instance.core.CanalInstanceGenerator) CanalInstanceWithManager(com.alibaba.otter.canal.instance.manager.CanalInstanceWithManager) Before(org.junit.Before)

Example 13 with CanalInstance

use of com.alibaba.otter.canal.instance.core.CanalInstance in project canal by alibaba.

the class DefaultSpringInstanceTest method testInstance.

@Test
public void testInstance() {
    CanalInstanceGenerator generator = (CanalInstanceGenerator) context.getBean("canalInstanceGenerator");
    CanalInstance canalInstance = generator.generate("instance");
    Assert.notNull(canalInstance);
    canalInstance.start();
    try {
        Thread.sleep(10 * 1000);
    } catch (InterruptedException e) {
    }
    canalInstance.stop();
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) CanalInstanceGenerator(com.alibaba.otter.canal.instance.core.CanalInstanceGenerator) Test(org.junit.Test)

Example 14 with CanalInstance

use of com.alibaba.otter.canal.instance.core.CanalInstance in project canal by alibaba.

the class SpringCanalInstanceGenerator method generate.

public CanalInstance generate(String destination) {
    synchronized (CanalEventParser.class) {
        try {
            // 设置当前正在加载的通道,加载spring查找文件时会用到该变量
            System.setProperty("canal.instance.destination", destination);
            this.beanFactory = getBeanFactory(springXml);
            String beanName = destination;
            if (!beanFactory.containsBean(beanName)) {
                beanName = defaultName;
            }
            return (CanalInstance) beanFactory.getBean(beanName);
        } catch (Throwable e) {
            logger.error("generator instance failed.", e);
            throw new CanalException(e);
        } finally {
            System.setProperty("canal.instance.destination", "");
        }
    }
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) CanalEventParser(com.alibaba.otter.canal.parse.CanalEventParser) CanalException(com.alibaba.otter.canal.common.CanalException)

Example 15 with CanalInstance

use of com.alibaba.otter.canal.instance.core.CanalInstance 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<>(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

CanalInstance (com.alibaba.otter.canal.instance.core.CanalInstance)22 CanalInstanceGenerator (com.alibaba.otter.canal.instance.core.CanalInstanceGenerator)6 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)5 CanalServerException (com.alibaba.otter.canal.server.exception.CanalServerException)5 CanalException (com.alibaba.otter.canal.common.CanalException)3 CanalInstanceWithManager (com.alibaba.otter.canal.instance.manager.CanalInstanceWithManager)3 Canal (com.alibaba.otter.canal.instance.manager.model.Canal)3 CanalEventParser (com.alibaba.otter.canal.parse.CanalEventParser)3 Message (com.alibaba.otter.canal.protocol.Message)3 Position (com.alibaba.otter.canal.protocol.position.Position)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)2 Event (com.alibaba.otter.canal.store.model.Event)2 List (java.util.List)2 Before (org.junit.Before)2 MQDestination (com.alibaba.otter.canal.connector.core.producer.MQDestination)1 Callback (com.alibaba.otter.canal.connector.core.util.Callback)1 MediaHAController (com.alibaba.otter.canal.extend.ha.MediaHAController)1 CanalMQConfig (com.alibaba.otter.canal.instance.core.CanalMQConfig)1