otter源码解读(一)

概览

lib存放的是项目依赖包,由于项目用到的包比较杂,可能有的包已经不在maven仓库中提供了,所以提供了一个lib包,执行里面的install命令,就可以把包安装到本地maven仓库。

manager是管理端源码,otter是一个典型的管理系统架构,manager用来管理以及配置各种资源,node,zookeeper,canal,datasource以及表这些实体资源均在manager端配置,其中manager把各个资源的工作流抽象成pipeline和channel,一个pipeline对应一个单向同步用到的资源和配置的集合,pipeline在channel中设置,一个channel是一个完整独立的工作单元,一个channel中可以有多个pipeline,比如:单向同步只需一个pipeline,双向同步需要两个方向相反的pipeline。在manager配置好各种资源之后就可以开启channel,开始同步数据了,启动之后的工作就交给node和zookeeper完成,就算manager崩了也不会终止同步。可以在manager关闭同步流程。

node是工作组件的源码,内嵌canal,完成从数据库dump binglog的工作(Select),binlog经过node中的ETL流程,被按照定义的行为同步到目标数据源。node中的SETL流程是整个otter中最经典也最关键的部分,其中用到了大量zookeeper分布式协作以及HA的功能,还多处使用了java并发知识。node中通过SEDA模型简化了串并行程序的复杂度,通过zookeeper保证了多节点可以协同工作,提高工作效率和资源利用率。比如单向同步的两个node就可以分布式协作,S/E在源端,T/L在目标端,E/T可并行,S/L必须串行执行。

shared是共用代码,主要实现了仲裁器的逻辑,setl中自定义逻辑以及节点间通讯逻辑和zk集群状态监控。

细说

com.alibaba.otter.manager.biz.autokeeper包主要完成manager端zk状态监控和数据采集的功能,

AutoKeeperCollector实现了自InitializingBean(Spring中的一个接口)

@Overridepublic void afterPropertiesSet() throws Exception {    
    collectorExecutor = Executors.newScheduledThreadPool(singleSize, new                                                             NamedThreadFactory("collector-thread",true));
    startCollect();
}

并且被注册到spring容器中:

<bean id="autoKeeperCollector" class="com.alibaba.otter.manager.biz.autokeeper.impl.AutoKeeperCollector" >
    <property name="collectInterval">
        <value>300</value>
    </property>
</bean>

所以容器一启动就会实例化AutoKeeperCollector,在AutoKeeperCollector中就会自动调用采集zk数据的代码。

AutoKeeperCollector中主要是通过远程调用zk的四字指令来完成对zk状态的查看:

    private static final String      CMD_STAT                 = "echo stat | nc %s %s";
    private static final String      CMD_CONS                 = "echo cons | nc %s %s";
    private static final String      CMD_DUMP                 = "echo dump | nc %s %s";
    private static final String      CMD_WCHC                 = "echo wchc | nc %s %s";

脚本调用是通过一个外部脚本调用工具Exec类完成的。

AutoKeeperPersist是数据持久化接口,被AutoKeeperData实现,AutoKeeperData主要用来持有zk数据,AutoKeeperPersist中的persist()持久化方法,被AutoKeeperCollector定时调用。

AutoKeeperCollector的startCollect()方法:

 private void startCollect() {

        // 启动定时工作任务
        collectorExecutor.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                List<AutoKeeperCluster> autoKeeperClusters = autoKeeperClusterService.listAutoKeeperClusters();
                if (!autoKeeperClusters.isEmpty()) {
                    autoKeeperData.persist();
                    for (AutoKeeperCluster autoKeeperCluster : autoKeeperClusters) {
                        List<String> servers = autoKeeperCluster.getServerList();
                        for (String address : servers) {//调用四字命令api
                            collectorServerStat(address);
                            collectorConnectionStat(address);
                            collectorWatchStat(address);
                            collectorEphemeralStat(address);
                        }
                    }
                }
            }
        }, delay, collectInterval, TimeUnit.SECONDS);

    }

AutoKeeperStatService定义了查询zk信息的接口:

/**
     * 根据serverIp查询对应的统计信息,包括Connection/Watch/Ephemeral等统计信息
     * 
     * @param serverIp
     * @return
     */
    public AutoKeeperServerStat findServerStat(String serverIp);

    /**
     * 根据sessionId查询对应的统计信息,包括详细的Connection/Watch/Ephemeral等统计信息
     * 
     * @param sessionId
     * @return
     */
    public AutoKeeperServerStat findServerStatBySessionId(String sessionId);

    /**
     * 根据sessionId查询对应的connction链接
     * 
     * @param sessionId
     * @return
     */
    public AutoKeeperConnectionStat findConnectionBySessionId(String sessionId);

    /**
     * 根据临时节点路径查询对应的connection统计信息
     * 
     * @param path
     * @return
     */
    public AutoKeeperConnectionStat findConnectionByEphemeralPath(String path);

    /**
     * 根据watcher路径查询对应的connection统计信息
     * 
     * @param path
     * @return
     */
    public List<AutoKeeperConnectionStat> findConnectionByWatcherPath(String path);

AutoKeeperStatServiceImpl是AutoKeeperStatService的实现, AutoKeeperStatServiceImpl也被注入到spring中:

<bean id="autoKeeperStatService"
        class="com.alibaba.otter.manager.biz.autokeeper.impl.AutoKeeperStatServiceImpl">
</bean>   

猜你喜欢

转载自www.cnblogs.com/yanshaoshuai/p/12030810.html