Search in sources :

Example 1 with CanalInstance

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

the class BaseCanalServerWithEmbededTest method setUp.

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

        public CanalInstance generate(String destination) {
            Canal canal = buildCanal();
            return new CanalInstanceWithManager(canal, FILTER);
        }
    });
    server.start();
    server.start(DESTINATION);
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) 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 2 with CanalInstance

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

the class CanalEmbedSelector method start.

public void start() {
    if (running) {
        return;
    }
    // 获取destination/filter参数
    Pipeline pipeline = configClientService.findPipeline(pipelineId);
    filter = CanalFilterSupport.makeFilterExpression(pipeline);
    destination = pipeline.getParameters().getDestinationName();
    batchSize = pipeline.getParameters().getMainstemBatchsize();
    batchTimeout = pipeline.getParameters().getBatchTimeout();
    ddlSync = pipeline.getParameters().getDdlSync();
    final boolean syncFull = pipeline.getParameters().getSyncMode().isRow() || pipeline.getParameters().isEnableRemedy();
    // 暂时使用skip load代替
    filterTableError = pipeline.getParameters().getSkipSelectException();
    if (pipeline.getParameters().getDumpSelector() != null) {
        dump = pipeline.getParameters().getDumpSelector();
    }
    if (pipeline.getParameters().getDumpSelectorDetail() != null) {
        dumpDetail = pipeline.getParameters().getDumpSelectorDetail();
    }
    canalServer.setCanalInstanceGenerator(new CanalInstanceGenerator() {

        public CanalInstance generate(String destination) {
            Canal canal = canalConfigClient.findCanal(destination);
            final OtterAlarmHandler otterAlarmHandler = new OtterAlarmHandler();
            otterAlarmHandler.setPipelineId(pipelineId);
            // 注入一下spring资源
            OtterContextLocator.autowire(otterAlarmHandler);
            // 设置下slaveId,保证多个piplineId下重复引用时不重复
            // 默认基数
            long slaveId = 10000;
            if (canal.getCanalParameter().getSlaveId() != null) {
                slaveId = canal.getCanalParameter().getSlaveId();
            }
            canal.getCanalParameter().setSlaveId(slaveId + pipelineId);
            canal.getCanalParameter().setDdlIsolation(ddlSync);
            canal.getCanalParameter().setFilterTableError(filterTableError);
            canal.getCanalParameter().setMemoryStorageRawEntry(false);
            CanalInstanceWithManager instance = new CanalInstanceWithManager(canal, filter) {

                protected CanalHAController initHaController() {
                    HAMode haMode = parameters.getHaMode();
                    if (haMode.isMedia()) {
                        return new MediaHAController(parameters.getMediaGroup(), parameters.getDbUsername(), parameters.getDbPassword(), parameters.getDefaultDatabaseName());
                    } else {
                        return super.initHaController();
                    }
                }

                protected void startEventParserInternal(CanalEventParser parser, boolean isGroup) {
                    super.startEventParserInternal(parser, isGroup);
                    if (eventParser instanceof MysqlEventParser) {
                        // 设置支持的类型
                        ((MysqlEventParser) eventParser).setSupportBinlogFormats("ROW");
                        if (syncFull) {
                            ((MysqlEventParser) eventParser).setSupportBinlogImages("FULL");
                        } else {
                            ((MysqlEventParser) eventParser).setSupportBinlogImages("FULL,MINIMAL");
                        }
                        MysqlEventParser mysqlEventParser = (MysqlEventParser) eventParser;
                        // otter先使用简单的模式
                        mysqlEventParser.setParallel(false);
                        CanalHAController haController = mysqlEventParser.getHaController();
                        if (haController instanceof MediaHAController) {
                            if (isGroup) {
                                throw new CanalException("not support group database use media HA");
                            }
                            ((MediaHAController) haController).setCanalHASwitchable(mysqlEventParser);
                        }
                        if (!haController.isStart()) {
                            haController.start();
                        }
                        // 基于media的Ha,直接从tddl中获取数据库信息
                        if (haController instanceof MediaHAController) {
                            AuthenticationInfo authenticationInfo = ((MediaHAController) haController).getAvailableAuthenticationInfo();
                            ((MysqlEventParser) eventParser).setMasterInfo(authenticationInfo);
                        }
                    }
                }
            };
            instance.setAlarmHandler(otterAlarmHandler);
            CanalEventSink eventSink = instance.getEventSink();
            if (eventSink instanceof AbstractCanalEventSink) {
                handler = new OtterDownStreamHandler();
                handler.setPipelineId(pipelineId);
                handler.setDetectingIntervalInSeconds(canal.getCanalParameter().getDetectingIntervalInSeconds());
                // 注入一下spring资源
                OtterContextLocator.autowire(handler);
                // 添加到开头
                ((AbstractCanalEventSink) eventSink).addHandler(handler, 0);
                handler.start();
            }
            return instance;
        }
    });
    canalServer.start();
    canalServer.start(destination);
    this.clientIdentity = new ClientIdentity(destination, pipeline.getParameters().getMainstemClientId(), filter);
    // 发起一次订阅
    canalServer.subscribe(clientIdentity);
    running = true;
}
Also used : Canal(com.alibaba.otter.canal.instance.manager.model.Canal) CanalEventParser(com.alibaba.otter.canal.parse.CanalEventParser) AbstractCanalEventSink(com.alibaba.otter.canal.sink.AbstractCanalEventSink) CanalEventSink(com.alibaba.otter.canal.sink.CanalEventSink) HAMode(com.alibaba.otter.canal.instance.manager.model.CanalParameter.HAMode) ByteString(com.google.protobuf.ByteString) CanalInstanceWithManager(com.alibaba.otter.canal.instance.manager.CanalInstanceWithManager) AuthenticationInfo(com.alibaba.otter.canal.parse.support.AuthenticationInfo) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) MediaHAController(com.alibaba.otter.canal.extend.ha.MediaHAController) MysqlEventParser(com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser) AbstractCanalEventSink(com.alibaba.otter.canal.sink.AbstractCanalEventSink) CanalInstanceGenerator(com.alibaba.otter.canal.instance.core.CanalInstanceGenerator) CanalHAController(com.alibaba.otter.canal.parse.ha.CanalHAController) CanalException(com.alibaba.otter.canal.common.CanalException)

Example 3 with CanalInstance

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

the class CanalMQStarter method startDestination.

public synchronized void startDestination(String destination) {
    CanalInstance canalInstance = canalServer.getCanalInstances().get(destination);
    if (canalInstance != null) {
        stopDestination(destination);
        CanalMQRunnable canalMQRunnable = new CanalMQRunnable(destination);
        canalMQWorks.put(canalInstance.getDestination(), canalMQRunnable);
        executorService.execute(canalMQRunnable);
        logger.info("## Start the MQ work of destination:" + destination);
    }
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance)

Example 4 with CanalInstance

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

the class PlainCanalInstanceGenerator method generate.

public CanalInstance generate(String destination) {
    synchronized (CanalEventParser.class) {
        try {
            PlainCanal canal = canalConfigClient.findInstance(destination, null);
            if (canal == null) {
                throw new CanalException("instance : " + destination + " config is not found");
            }
            Properties properties = canal.getProperties();
            // merge local
            properties.putAll(canalConfig);
            // 设置动态properties,替换掉本地properties
            com.alibaba.otter.canal.instance.spring.support.PropertyPlaceholderConfigurer.propertiesLocal.set(properties);
            // 设置当前正在加载的通道,加载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) PlainCanal(com.alibaba.otter.canal.instance.manager.plain.PlainCanal) CanalEventParser(com.alibaba.otter.canal.parse.CanalEventParser) Properties(java.util.Properties) CanalException(com.alibaba.otter.canal.common.CanalException)

Example 5 with CanalInstance

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

the class MemorySpringInstanceTest 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)

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