Exemplo n.º 1
0
 protected static function _get_server_lists(&$conf, &$zk_conf)
 {
     $conf_file = $conf['pid'] . '_conf_file';
     $cached_conf_file = Ak_LocalCache::get($conf_file);
     if ($conf != null && $conf === $cached_conf_file['raw_conf']) {
         $conf = $cached_conf_file['validated_conf'];
     } else {
         $schema = self::_getSchema();
         $raw_conf = $conf;
         $conf = $schema->validate('mc_conf', $conf, $ret);
         if ($ret != true) {
             self::_warning("conf format error");
             return null;
         }
         $cached_conf_file['raw_conf'] = $raw_conf;
         $cached_conf_file['validated_conf'] = $conf;
         Ak_LocalCache::set($conf_file, $cached_conf_file);
     }
     $pid = $conf['pid'];
     $zk_path = $conf['zk_path'];
     $zk_expire = $conf['zk_expire'];
     $zk_global_path = "{$zk_path}/product";
     $zk_global_conf = Ak_Zookeeper::getCached($zk_global_path, 0, $zk_expire, 'Ak_McClient::checkZkGlobalConf');
     $zk_conf_path = "{$zk_path}/product/{$pid}";
     $zk_conf = Ak_Zookeeper::getCached($zk_conf_path, 2, $zk_expire, 'Ak_McClient::checkZkConf');
     if (!is_array($zk_conf)) {
         return null;
     }
     $def_conf_items = array('connect_timeout', 'poll_timeout', 'send_timeout', 'recv_timeout');
     foreach ($def_conf_items as $item) {
         if (is_null($conf[$item])) {
             $conf[$item] = is_null($zk_conf[$item]) ? $zk_global_conf[$item] : $zk_conf[$item];
         }
     }
     $idc_type = $zk_conf['idc_type'];
     $curr_idc = $conf['curr_idc'] - 1;
     if ($idc_type == "none") {
         $zk_conf['idc_num'] = 1;
         $curr_idc = 0;
     }
     $idc_num = $zk_conf['idc_num'];
     $server_lists = array();
     for ($i = 0; $i < $idc_num; $i++) {
         //$idc = $cur_mc_first ? ($i + $curr_idc) % $idc_num + 1 : $i + 1;
         $idc = $i + 1;
         $server_list = @$zk_conf['children']['server_list' . $idc];
         if (!is_array($server_list)) {
             self::_warning("server list[{$idc}] error");
             return null;
         }
         $server_lists[] = $server_list;
     }
     return $server_lists;
 }
Exemplo n.º 2
0
 public static function getCached($path, $depth = 0, $expire = 60, $user_def = null)
 {
     $zk_key = "Ak_Zookeeper." . $path . "." . $depth;
     $zk_bak_key = $zk_key . ".bak";
     if (!(self::$backup_dir === null)) {
         AK_LocalCache::setDir(self::$backup_dir);
         $zk_lock_dir = self::$backup_dir;
     } else {
         $zk_lock_dir = defined('IS_ODP') ? DATA_PATH . '/ak' : dirname(__FILE__) . '/var';
     }
     $res = Ak_LocalCache::get($zk_key);
     if (is_array($res)) {
         return $res;
     }
     if (!file_exists($zk_lock_dir)) {
         mkdir($zk_lock_dir);
     }
     $zk_lock = $zk_lock_dir . '/zk_lock';
     $fd = fopen($zk_lock, 'a+b');
     if (!flock($fd, LOCK_EX)) {
         Ak_Log::warning("zk lock failed");
         fclose($fd);
         return null;
     }
     $res = Ak_LocalCache::get($zk_key);
     if (!is_array($res)) {
         $bak = Ak_LocalCache::get($zk_bak_key);
         if (!empty($bak) && is_array($bak)) {
             Ak_LocalCache::set($zk_key, $bak, $expire);
         } else {
             $bak = null;
         }
         flock($fd, LOCK_UN);
         if (self::$use_backup_only) {
             $res = null;
         } else {
             // for null bak, add by cdz
             if ($bak === null) {
                 Ak_Log::warning("local-cached file is empty, waiting to get from zk...");
                 $bak_zk_lock = $zk_lock_dir . '/' . 'bak_zk_lock';
                 $bak_fd = fopen($bak_zk_lock, 'a+b');
                 // non-block lock
                 if (!flock($bak_fd, LOCK_EX | LOCK_NB, $eWouldBlock) || $eWouldBlock) {
                     Ak_Log::warning("zk lock failed, path[{$path}], eWouldBlock[{$eWouldBlock}]");
                     fclose($bak_fd);
                     return null;
                 }
                 $res = Ak_LocalCache::get($zk_key);
                 if (!is_array($res)) {
                     $bak = Ak_LocalCache::get($zk_bak_key);
                     if (!empty($bak) && is_array($bak)) {
                         Ak_LocalCache::set($zk_key, $bak, $expire);
                         flock($bak_fd, LOCK_UN);
                         fclose($bak_fd);
                     } else {
                         $bak = null;
                     }
                     $res = self::get($path, $depth, self::$retry_times);
                     if (is_callable($user_def)) {
                         $res = call_user_func($user_def, $res);
                     }
                 }
             } else {
                 // still need to get
                 $res = self::get($path, $depth, self::$retry_times);
                 if (is_callable($user_def)) {
                     $res = call_user_func($user_def, $res);
                 }
             }
         }
         if (!empty($res) && is_array($res)) {
             Ak_LocalCache::set($zk_bak_key, $res, -1);
             Ak_LocalCache::set($zk_key, $res, $expire);
             if ($bak === null) {
                 Ak_Log::warning("getting data from zk done, local-cached file created");
                 if (!self::$use_backup_only) {
                     flock($bak_fd, LOCK_UN);
                     fclose($bak_fd);
                 }
             }
         } else {
             self::$zk = null;
             if (empty($bak)) {
                 if (self::$use_backup_only) {
                     Ak_Log::warning("FATAL ERROR: zk is disabled and cached file is empty");
                 } else {
                     Ak_Log::warning("FATAL ERROR: get from zk failed and cached file is empty too, data[NULL]");
                     flock($bak_fd, LOCK_UN);
                     fclose($bak_fd);
                 }
                 fclose($fd);
                 return null;
             }
             if (!self::$use_backup_only) {
                 Ak_Log::warning("Using cached data[" . var_export($bak, true) . "] instead");
             }
             $res = $bak;
         }
     } else {
         flock($fd, LOCK_UN);
     }
     fclose($fd);
     return $res;
 }