Mycat 连接池模型源码
2018-03-22 13:14
92 查看
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();
没明白为什么这么分配,难道可用达到权重越大,分配可能性越小???
PhysicalDatasource 连接池对象保存该连接的可用连接使用的数据结构是,ConMap,主要功能是获取当前节点的可用连接,首先从当前database上获取可用连接,如果没有,则从其他 database 上获取可用连接
database的可用连接是存放在数据结构ConQueue中的,可用连接分为自动提交,手动提交,所以ConQueue由2个ConcurrentLinkedQueue组成,autoCommitCons 自动提交队列,manCommitCons 手动提交队列
分配可用连接:先从提交方式队列队首分配,分配失败,从另一个队列分配,分配失败,从其他databse 分配。猜想:此处分配完成应该不是最种的可用连接,还需要做事务隔离级别、事务模式、字符集、Database 等等处理和校验,才能执行具体的 sql 指令,这些应该是在MySQLConnection 类中进行的
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<PhysicalDatasource> 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<BackendConnection> f1 = autoCommitCons; ConcurrentLinkedQueue<BackendConnection> 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; } }
相关文章推荐
- 源码研读Mycat1.6之网络篇---前端线程模型(应用程序与mycat交互篇)上篇
- Mycat开发实践---mycat的连接池模型
- 源码分析mycat1.6之网络篇---前端线程模型下篇(读写事件篇)
- 数据库中间件Mycat源码解析(四):Mycat的心跳管理
- CodeIgniter框架源码学习之模型类--Model.php
- Mongoose源码剖析:mongoose的工作模型
- 阅读Linux设备驱动模型源码之 device结构体成员详解
- 数据库中间件 为什么阅读 MyCAT 源码?
- Okhttp源码分析(五)连接池
- CI框架源码解析十五之模型类文件Model.php
- 深入浅出DOM模型(源码分析)!
- Memcached 源码分析--网络模型流程分析
- 数据库中间件 MyCAT源码分析:【单库单表】插入
- Semantic segmentation (with deconvolutional CNN) 作者公布源码和模型
- Google Chrome源码剖析【五】:插件模型
- Kafka源码深度解析-序列14 -Server核心组件之3-SocketServer与NIO- 1+N+M 模型
- MyCAT 源码解析合集
- Tensorflow cifar模型源码
- Tensorflow cifar模型源码
- 【图灵学院09】RPC底层通讯原理之Netty线程模型源码分析