Mycat 连接池模型源码
admin
2023-05-02 13:05:40
0

PhysicalDBNode 是Mycat集群(Datanode)的对应,引用一个连接池对象 PhysicalDBPool,
PhysicalDBPool 里面引用了真正的连接池对象 PhysicalDatasource,PhysicalDBPool 里面把该
集群的读节点,写节点写入各自的 PhysicalDatasource 数组,通过负载均衡决定走哪个节点
负载均衡策略:随机选择,按权重设置随机概率
代码:randomSelect
节点权重计算公式String weightStr = node.getAttribute("weight");
int weight = "".equals(weightStr) ? PhysicalDBPool.WEIGHT : Integer.parseInt(weightStr) ;
负载均衡:offset -= okSources.get(i).getConfig().getWeight();
没明白为什么这么分配,难道可用达到权重越大,分配可能性越小???

public PhysicalDatasource randomSelect(ArrayList okSources) {

   if (okSources.isEmpty()) {
      return this.getSource();

   } else {      

      int length = okSources.size();     // 总个数
        int totalWeight = 0;         // 总权重
        boolean sameWeight = true;        // 权重是否都一样
        for (int i = 0; i < length; i++) {            
            int weight = okSources.get(i).getConfig().getWeight();
            totalWeight += weight;        // 累计总权重               
            if (sameWeight && i > 0 
                  && weight != okSources.get(i-1).getConfig().getWeight() ) {      // 计算所有权重是否一样                              
                sameWeight = false;    
            }
        }

        if (totalWeight > 0 && !sameWeight ) {

           // 如果权重不相同且权重大于0则按总权重数随机
            int offset = random.nextInt(totalWeight);

            // 并确定随机值落在哪个片断上
            for (int i = 0; i < length; i++) {
                offset -= okSources.get(i).getConfig().getWeight();
                if (offset < 0) {
                    return okSources.get(i);
                }
            }
        }

        // 如果权重相同或权重为0则均等随机
        return okSources.get( random.nextInt(length) );    

      //int index = Math.abs(random.nextInt()) % okSources.size();
      //return okSources.get(index);
   }
}

PhysicalDatasource 连接池对象保存该连接的可用连接使用的数据结构是,ConMap,主要功能是获取当前节点的可用连接,首先从当前database上获取可用连接,如果没有,则从其他 database 上获取可用连接

public BackendConnection tryTakeCon(final String schema, boolean autoCommit) {
   final ConQueue queue = items.get(schema);
   BackendConnection con = tryTakeCon(queue, autoCommit);
   if (con != null) {
      return con;
   } else {
      for (ConQueue queue2 : items.values()) {
         if (queue != queue2) {
            con = tryTakeCon(queue2, autoCommit);
            if (con != null) {
               return con;
            }
         }
      }
   }
   return null;

}
private BackendConnection tryTakeCon(ConQueue queue, boolean autoCommit) {

   BackendConnection con = null;
   if (queue != null && ((con = queue.takeIdleCon(autoCommit)) != null)) {
      return con;
   } else {
      return null;
   }

}

database的可用连接是存放在数据结构ConQueue中的,可用连接分为自动提交,手动提交,所以ConQueue由2个ConcurrentLinkedQueue组成,autoCommitCons 自动提交队列,manCommitCons 手动提交队列
分配可用连接:先从提交方式队列队首分配,分配失败,从另一个队列分配,分配失败,从其他databse 分配。猜想:此处分配完成应该不是最种的可用连接,还需要做事务隔离级别、事务模式、字符集、Database 等等处理和校验,才能执行具体的 sql 指令,这些应该是在MySQLConnection 类中进行的

public BackendConnection takeIdleCon(boolean autoCommit) {
   ConcurrentLinkedQueue f1 = autoCommitCons;
   ConcurrentLinkedQueue f2 = manCommitCons;

   if (!autoCommit) {
      f1 = manCommitCons;
      f2 = autoCommitCons;

   }
   BackendConnection con = f1.poll();
   if (con == null || con.isClosedOrQuit()) {
      con = f2.poll();
   }
   if (con == null || con.isClosedOrQuit()) {
      return null;
   } else {
      return con;
   }

}

相关内容

热门资讯

卖“毒蛋”的人,抓到了 作者 | 何国胜 编辑 | 向现“(人)抓到了,目前案件正在侦办中。”7月28日晚间,苏州禁毒部门有...
尺素金声丨实施零关税国家达63... 海关总署发布的数据显示,今年5月1日起,我国对53个非洲建交国全面实施零关税举措,目前,我国实施零关...
职业索赔盯上基层诊所,倒逼用药... 文 | 布丁基层诊所正在被职业索赔盯上。据新京报,去年夏天,一男子走进河南南阳一家诊所,要求购买三瓶...
“西瓜我全买了”就可以肆意妄为... 拿西瓜砸了人,把瓜都买了,就能一走了之吗?事实证明,这套逻辑在法治社会行不通。7月28日晚,据海峡都...
科学家在日本广岛发现新物质,系... 在美国对日本广岛进行原子弹轰炸近81年后,科学家们在广岛的沙滩上发现了一种奇异且从未被发现过的新物质...
AI失控,反噬开始 作者 | 贺一 编辑 | 阿树近期,中国开源模型在美国频繁引发热议。7月28日,月之暗面发布Kimi...
“总统千金天价离婚”,分到43... 2026年7月24日下午,首尔高等法院,一场持续近十年的司法拉锯战终于接近尾声。法庭裁定SK集团会长...
巴基斯坦,又拿下一个历史性协议 全世界都没想到,接连的中东大战,巴基斯坦正成为最大赢家。去年以色列追杀哈马斯,空袭卡塔尔首都,阿拉伯...
汇正财经贺峰的一对一指导服务怎...   对于考虑购买证券投资顾问服务的投资者来说,'一对一指导服务怎么样'是一个重要的考量维度。需要首先...
重庆失联00后网格员龚宝冬确认...   重庆失联00后网格员龚宝冬确认遇难  【重庆失联00后网格员龚宝冬确认遇难】2026年7月29日...