public static function get_db_host_roll($db_read_host_array, $db)
 {
     $key = md5(implode("_", array_keys($db_read_host_array) . implode(array_values($db_read_host_array))));
     $cache_array = cls_shmop::readArray($key);
     if ($cache_array) {
         $host = array_shift($cache_array);
         cls_shmop::writeArray($key, $cache_array);
     } else {
         $host_str = $db_read_host_array[$db];
         if ($host_str) {
             $host_array = explode(',', $host_str);
             $read_host_list = self::get_read_host_list($host_array);
             $host = array_shift($read_host_list);
             cls_shmop::writeArray($key, $read_host_list);
         } else {
             $host = '';
         }
     }
     return $host;
 }
Beispiel #2
0
 private function initNodeList()
 {
     $str = $this->getConsistentHashSeparateString();
     if ($str) {
         $str = strtolower($str);
         $list = explode(';', $str);
         if (!empty($list)) {
             $max = 0;
             $i = 0;
             $nodeList = array();
             foreach ($list as $value) {
                 $one_db_config = explode('=', $value);
                 $one_db_config[0] = str_replace('[', '', $one_db_config[0]);
                 $one_db_config[0] = str_replace(']', '', $one_db_config[0]);
                 $start_end_list = explode(',', $one_db_config[0]);
                 if ($max <= $start_end_list[1]) {
                     $max = $start_end_list[1];
                 }
                 $node = new Node();
                 $node->setStart(str_replace('w', '0000', $start_end_list[0]));
                 $node->setEnd(str_replace('w', '0000', $start_end_list[1]));
                 $node->setDbName($one_db_config[1]);
                 if ($i == 0 && !parent::getConsistentHashOneDbOneTable()) {
                     $node->setIsDefaultDb(true);
                 }
                 $i++;
                 $nodeList[] = $node;
             }
             $this->node_list = $nodeList;
             cls_shmop::writeArray(self::INIT_CONSISTENT_HASH_SECTION_CACHE_KEY . parent::getLogicTable(), $nodeList);
         }
     }
     /*if($max !=$this->consistent_hash_separate_mod_max_value){
     	 throw new DBRouteException('一致性hash字符串设置错误');
     	 }*/
 }
 public function nextValue($logic_table)
 {
     $file = fopen(ROOT_PATH . 'includes/cls/' . $logic_table . '_seq.txt', "w+");
     if (flock($file, LOCK_EX)) {
         //独占锁
         $value = 0;
         $is_write = false;
         for (;;) {
             $new_array = cls_shmop::readArray($logic_table);
             if ($new_array) {
                 $start = $new_array['start'];
                 $end = $new_array['end'];
                 $value = $start;
                 if ($start < $end) {
                     $new_array['start'] = $start + 1;
                 } else {
                     $new_array = array();
                 }
                 $is_write = cls_shmop::writeArray($logic_table, $new_array);
             } else {
                 $range = $this->getLastSeq($logic_table);
                 if ($range) {
                     $min = $range->getMin();
                     $max = $range->getMax();
                     $array = array();
                     $array['start'] = $min + 1;
                     $array['end'] = $max;
                     $value = $min;
                     $is_write = cls_shmop::writeArray($logic_table, $array);
                 }
             }
             if ($is_write && $value) {
                 break;
             }
         }
         flock($file, LOCK_UN);
     }
     fclose($file);
     return $value;
 }