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();
沒明白爲什麼這麼分配,難道可用達到權重越大,分配可能性越小???

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

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章