国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 基礎知識 > 正文

Mycat 連接池模型源碼

2024-09-12 20:30:04
字體:
來源:轉載
供稿:網友

  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<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;
     }

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 榆树市| 玛曲县| 剑河县| 左权县| 栾川县| 蓝田县| 吉林市| 博湖县| 望江县| 察雅县| 佳木斯市| 金昌市| 大城县| 顺平县| 抚顺市| 双牌县| 五莲县| 寿宁县| 收藏| 宜州市| 日照市| 富宁县| 禹城市| 岑溪市| 永清县| 临江市| 闵行区| 苍溪县| 和平区| 宣化县| 疏勒县| 永寿县| 吉隆县| 宣威市| 盐池县| 阳信县| 西畴县| 镇雄县| 金秀| 吴桥县| 长宁区|