资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Mycat连接池模型源码-创新互联

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();
没明白为什么这么分配,难道可用达到权重越大,分配可能性越小???

创新互联建站是一家专业提供南安企业网站建设,专注与做网站、成都做网站、H5建站、小程序制作等业务。10年已为南安众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。
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;
   }

}

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:Mycat连接池模型源码-创新互联
路径分享:http://cdkjz.cn/article/ccigee.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220