这部分代码其实在ServerRunningMonitor的start()方法中。针对不一样的destination,启动不一样的CanalInstance。主要的方法在于initRunning()。java
private void initRunning() { if (!isStart()) { return; } String path = ZookeeperPathUtils.getDestinationServerRunning(destination); // 序列化 byte[] bytes = JsonUtils.marshalToByte(serverData); try { mutex.set(false); zkClient.create(path, bytes, CreateMode.EPHEMERAL); activeData = serverData; processActiveEnter();// 触发一下事件 mutex.set(true); } catch (ZkNodeExistsException e) { bytes = zkClient.readData(path, true); if (bytes == null) {// 若是不存在节点,当即尝试一次 initRunning(); } else { activeData = JsonUtils.unmarshalFromByte(bytes, ServerRunningData.class); } } catch (ZkNoNodeException e) { zkClient.createPersistent(ZookeeperPathUtils.getDestinationPath(destination), true); // 尝试建立父节点 initRunning(); } }
首先在zk中新增一个临时节点,表示的是正在运行destination的ip和端口,而后触发一下processActiveEnter()。咱们主要看下这个方法,在controller启动时定义的。mysql
public void processActiveEnter() { try { MDC.put(CanalConstants.MDC_DESTINATION, String.valueOf(destination)); embededCanalServer.start(destination); } finally { MDC.remove(CanalConstants.MDC_DESTINATION); } } public void start(final String destination) { final CanalInstance canalInstance = canalInstances.get(destination); if (!canalInstance.isStart()) { try { MDC.put("destination", destination); canalInstance.start(); logger.info("start CanalInstances[{}] successfully", destination); } finally { MDC.remove("destination"); } } }
主要在embededCanalServer.start中,咱们看下这个canalInstance.start(),跟踪到AbstractCanalInstance。spring
在默认的instance配置文件中,咱们选择的metaManager是PeriodMixedMetaManager,定时(默认1s)刷新数据到zk中,因此咱们主要关注这个类的start方法。这个类继承了MemoryMetaManager,首先启动一个MemoryMetaManager,而后再启动一个ZooKeeperMetaManager。sql
destinations = MigrateMap.makeComputingMap(new Function<String, List<ClientIdentity>>() { public List<ClientIdentity> apply(String destination) { return zooKeeperMetaManager.listAllSubscribeInfo(destination); } });
从/otter/canal/destinations/{destination}获取全部的client信息,返回的内容是List<ClientIdentity>,包括了destination、clientId、filter等等。缓存
根据ClientIdentity去zk获取指针,从zk的/otter/canal/destinations/{destination}/{clientId}/cursor下面去获取,返回的内容是个LogPosition。app
cursors = MigrateMap.makeComputingMap(new Function<ClientIdentity, Position>() { public Position apply(ClientIdentity clientIdentity) { Position position = zooKeeperMetaManager.getCursor(clientIdentity); if (position == null) { return nullCursor; // 返回一个空对象标识,避免出现异常 } else { return position; } } });
有可能返回一个空。ide
建立一个基于内存的MemoryClientIdentityBatch,包含位点的start、end、ack信息。而后从zk节点/otter/canal/destinations/{destination}/{clientId}/mark获取,取出来的数据进行排序,而后从/otter/canal/destinations/{destination}/{clientId}/mark/{batchId}中取出PositionRange这个类,描述的是一个position的范围。源码分析
batches = MigrateMap.makeComputingMap(new Function<ClientIdentity, MemoryClientIdentityBatch>() { public MemoryClientIdentityBatch apply(ClientIdentity clientIdentity) { // 读取一下zookeeper信息,初始化一次 MemoryClientIdentityBatch batches = MemoryClientIdentityBatch.create(clientIdentity); Map<Long, PositionRange> positionRanges = zooKeeperMetaManager.listAllBatchs(clientIdentity); for (Map.Entry<Long, PositionRange> entry : positionRanges.entrySet()) { batches.addPositionRange(entry.getValue(), entry.getKey()); // 添加记录到指定batchId } return batches; } });
// 启动定时工做任务 executor.scheduleAtFixedRate(new Runnable() { public void run() { List<ClientIdentity> tasks = new ArrayList<ClientIdentity>(updateCursorTasks); for (ClientIdentity clientIdentity : tasks) { try { // 定时将内存中的最新值刷到zookeeper中,屡次变动只刷一次 zooKeeperMetaManager.updateCursor(clientIdentity, getCursor(clientIdentity)); updateCursorTasks.remove(clientIdentity); } catch (Throwable e) { // ignore logger.error("period update" + clientIdentity.toString() + " curosr failed!", e); } } } }, period, period, TimeUnit.MILLISECONDS);
定时刷新position到zk后,从任务中删除。刷新的频率为1s。ui
这块比较简单。this
if (!alarmHandler.isStart()) { alarmHandler.start(); }
其实默认是LogAlarmHandler,用于发送告警信息的。
启动EventStore,默认是MemoryEventStoreWithBuffer。start方法也比较简单。
public void start() throws CanalStoreException { super.start(); if (Integer.bitCount(bufferSize) != 1) { throw new IllegalArgumentException("bufferSize must be a power of 2"); } indexMask = bufferSize - 1; entries = new Event[bufferSize]; }
这块默认是EntryEventSink。这块也不复杂。
public void start() { super.start(); Assert.notNull(eventStore); for (CanalEventDownStreamHandler handler : getHandlers()) { if (!handler.isStart()) { handler.start(); } } }
正常的启动,将running状态置为true。
if (!eventParser.isStart()) { beforeStartEventParser(eventParser); eventParser.start(); afterStartEventParser(eventParser); }
咱们分别看下。
protected void beforeStartEventParser(CanalEventParser eventParser) { boolean isGroup = (eventParser instanceof GroupEventParser); if (isGroup) { // 处理group的模式 List<CanalEventParser> eventParsers = ((GroupEventParser) eventParser).getEventParsers(); for (CanalEventParser singleEventParser : eventParsers) {// 须要遍历启动 startEventParserInternal(singleEventParser, true); } } else { startEventParserInternal(eventParser, false); } }
判断是否是集群的parser(用于分库),若是是GroupParser,须要一个个启动CanalEventParser。咱们主要看下startEventParserInternal方法。咱们只关注MysqlEventParser,由于他支持HA。
if (eventParser instanceof MysqlEventParser) { MysqlEventParser mysqlEventParser = (MysqlEventParser) eventParser; CanalHAController haController = mysqlEventParser.getHaController(); if (haController instanceof HeartBeatHAController) { ((HeartBeatHAController) haController).setCanalHASwitchable(mysqlEventParser); } if (!haController.isStart()) { haController.start(); } }
启动一个HeartBeatHAController。主要做用是用于当parser失败次数超过阈值时,执行mysql的主备切换。
这里也区分是GroupParser仍是单个的MysqlParser,其实最终都是启动Parser,不过前者是启动多个而已。咱们看下单个的start方法。具体实如今AbstractMysqlEventParser中
public void start() throws CanalParseException { if (enableTsdb) { if (tableMetaTSDB == null) { // 初始化 tableMetaTSDB = TableMetaTSDBBuilder.build(destination, tsdbSpringXml); } } super.start(); }
首先若是启用了Tsdb功能(也就是DDL后表结构的回溯),那么须要从xml中初始化表结构源数据,而后调用AbstractEventParser的start方法。
protected void afterStartEventParser(CanalEventParser eventParser) { // 读取一下历史订阅的filter信息 List<ClientIdentity> clientIdentitys = metaManager.listAllSubscribeInfo(destination); for (ClientIdentity clientIdentity : clientIdentitys) { subscribeChange(clientIdentity); } }
这块订阅的主要是filter的变化。
public boolean subscribeChange(ClientIdentity identity) { if (StringUtils.isNotEmpty(identity.getFilter())) { logger.info("subscribe filter change to " + identity.getFilter()); AviaterRegexFilter aviaterFilter = new AviaterRegexFilter(identity.getFilter()); boolean isGroup = (eventParser instanceof GroupEventParser); if (isGroup) { // 处理group的模式 List<CanalEventParser> eventParsers = ((GroupEventParser) eventParser).getEventParsers(); for (CanalEventParser singleEventParser : eventParsers) {// 须要遍历启动 ((AbstractEventParser) singleEventParser).setEventFilter(aviaterFilter); } } else { ((AbstractEventParser) eventParser).setEventFilter(aviaterFilter); } } // filter的处理规则 // a. parser处理数据过滤处理 // b. sink处理数据的路由&分发,一份parse数据通过sink后能够分发为多份,每份的数据能够根据本身的过滤规则不一样而有不一样的数据 // 后续内存版的一对多分发,能够考虑 return true; }
至此,CanalInstance启动成功。
一样的,中止的触发也是在ServerRunningMonitor中,中止的代码以下:
public void stop() { super.stop(); logger.info("stop CannalInstance for {}-{} ", new Object[] { canalId, destination }); if (eventParser.isStart()) { beforeStopEventParser(eventParser); eventParser.stop(); afterStopEventParser(eventParser); } if (eventSink.isStart()) { eventSink.stop(); } if (eventStore.isStart()) { eventStore.stop(); } if (metaManager.isStart()) { metaManager.stop(); } if (alarmHandler.isStart()) { alarmHandler.stop(); } logger.info("stop successful...."); }
和启动同样,在先后也能够作一些事情。
public void stop() { super.stop(); stopHeartBeat(); // 先中止心跳 parseThread.interrupt(); // 尝试中断 eventSink.interrupt(); try { parseThread.join();// 等待其结束 } catch (InterruptedException e) { // ignore } if (binlogParser.isStart()) { binlogParser.stop(); } if (transactionBuffer.isStart()) { transactionBuffer.stop(); } }
首先关闭心跳的定时器,而后中断解析线程,等待当前运行的任务结束后,中止binlogParser,清空transactionBuffer。这里看下怎么清空transactionBuffer的。
public void stop() throws CanalStoreException { putSequence.set(INIT_SQEUENCE); flushSequence.set(INIT_SQEUENCE); entries = null; super.stop(); }
将put和flush的序列置为初始序列,也就是再也不容许向队列中put数据。
中止parser后,中止位点管理和HAController。其实只是将running置为false。
相似于启动,中止也不复杂。
public void stop() { super.stop(); for (CanalEventDownStreamHandler handler : getHandlers()) { if (handler.isStart()) { handler.stop(); } } }
主要部分在这边
public void cleanAll() throws CanalStoreException { final ReentrantLock lock = this.lock; lock.lock(); try { putSequence.set(INIT_SQEUENCE); getSequence.set(INIT_SQEUENCE); ackSequence.set(INIT_SQEUENCE); putMemSize.set(0); getMemSize.set(0); ackMemSize.set(0); entries = null; // for (int i = 0; i < entries.length; i++) { // entries[i] = null; // } } finally { lock.unlock(); } }
其实也是将RingBuffer的指针置为初始值。
咱们看下PeriodMixedMetaManager。主要调用了两块的stop,一个是MemoryMetaManager,另外一个是ZooKeeperMetaManager。清理内存中的数据,而后让zk的管理器running状态改成false。
将running状态置为false。