Example #1
0
 public static function SetGlobalConf($conf)
 {
     self::$global_conf = $conf;
     if (isset($conf['ZookeeperHost'])) {
         Ak_Zookeeper::setHost($conf['ZookeeperHost']);
     }
     if (isset($conf['ZookeeperBackupDir'])) {
         Ak_Zookeeper::setBackupDir($conf['ZookeeperBackupDir']);
     }
     if (isset($conf['ZookeeperDisabled']) && $conf['ZookeeperDisabled'] === true) {
         Ak_Zookeeper::useBackupOnly(true);
     }
 }
Example #2
0
 protected function call_acm($group_key, $key, $sub_key = null)
 {
     $acm_zk_path = $this->conf['zk_path'] . '/acm';
     $zk_expire = $this->conf['zk_expire'];
     $acm_conf = Ak_Zookeeper::getCached($acm_zk_path, 2, $zk_expire, 'Ak_McClient::checkAcmConf');
     if (!is_array($acm_conf)) {
         Ak_Log::warning("acm zk conf formate error!");
         return false;
     }
     for ($i = 1; $i <= $acm_conf['acm_idc_num']; ++$i) {
         $server_name = "server{$i}";
         if (!is_array($acm_conf['children'][$server_name])) {
             Ak_Log::warning("acm {$server_name} not exist!");
             continue;
         }
         $server_conf = $acm_conf['children'][$server_name];
         $this->span_idc->registerResource(self::ACM_RESOURCE, $server_conf['idc'], $server_name);
     }
     $resources = $this->span_idc->getResource($this->conf['span_idc_strategy_index'], self::ACM_RESOURCE, $this->conf['curr_idc']);
     if (count($resources) == 0) {
         Ak_Log::warning("get resources is empty!");
         return false;
     }
     foreach ($resources as $server_name) {
         $acm_path = $this->conf['zk_path'] . "/acm/{$server_name}";
         $aclient_conf = array('Source' => 'Galileo', 'Protocol' => 'Nshead', 'Scheduler' => 'Closest', 'GalileoConf' => array('Path' => $acm_path), 'ClosestConf' => array('AlwaysRetry' => true), 'NsheadConf' => array('ConnectTimeOut' => $this->conf['acm_connect_timeout'], 'WriteTimeOut' => $this->conf['acm_write_timeout'], 'ReadTimeOut' => $this->conf['acm_read_timeout']));
         $client = new Ak_AClient();
         $client->SetConf($aclient_conf);
         $idc_list = array();
         for ($i = 0; $i < $this->zk_conf['idc_num']; $i++) {
             $idc_list[] = $i;
         }
         $data = array('command_no' => $this->zk_conf['delete_cmd'], 'pid' => $this->conf['pid'], 'group_key' => (string) $group_key, 'key' => (string) $key, 'idc_list' => $idc_list);
         if ($sub_key !== null) {
             $data['has_sub_key'] = 1;
             $data['sub_key'] = $sub_key;
         }
         $input = array('body' => mc_pack_array2pack($data, PHP_MC_PACK_V2));
         $output = $client->Call($input);
         if ($output == null) {
             Ak_Log::warning("aclient call acm [{$server_name}] failed");
         } else {
             $res = mc_pack_pack2array($output['body']);
             if ($res['error_no'] !== 0) {
                 Ak_Log::warning("call acm [{$server_name}] output err_no is {$res['errno']}");
             } else {
                 return true;
             }
         }
     }
     return false;
 }
Example #3
0
 public function get_conf()
 {
     $this->zk_conf = Ak_Zookeeper::getCached($this->zk_path, 1, $this->zk_update_time, 'AClientGalileo::checkZkConf');
     return $this->zk_conf;
 }
Example #4
0
 /**
  * @brief 此接口封装了Ak_McClient初始化的过程
  *
  * @return  初始化成功返回Ak_McClient对象实例,失败则返回false
  * @retval  object/boolean
  * @author chenyijie
  * @date 2012/09/27 20:53:48
  **/
 public function create()
 {
     $zkHost = array();
     $zkhostFromConf = Bd_Conf::getConf('cacheproxy/AkCache/zkhost');
     if ($zkhostFromConf === false) {
         Bd_Log::warning('Get conf form cache.conf failed when create AkCacheClient');
         return false;
     }
     //装配ip和port
     foreach ($zkhostFromConf as $value) {
         $content = $value['ip'] . ':' . $value['port'];
         $zkHost[] = $content;
     }
     Ak_Zookeeper::setHost($zkHost);
     Ak_AClient::SetGlobalConf(array('ZookeeperHost' => $zkHost));
     //创建cache客户端实例
     $keyArray = array('pid', 'zk_path', 'default_expire', 'curr_idc', 'delete_directly', 'delete_delay', 'connect_timeout', 'poll_timeout', 'send_timeout', 'recv_timeout', 'acm_connect_timeout', 'acm_read_timeout', 'acm_write_timeout');
     $mcConf = array();
     $confArray = Bd_Conf::getConf('cacheproxy/AkCache/mc_conf/');
     //去除confArray中读取到的空项
     foreach ($keyArray as $key) {
         if (isset($confArray[$key]) && $confArray[$key] !== '') {
             $mcConf[$key] = $confArray[$key];
         }
     }
     if (empty($mcConf)) {
         //错误号参见CacheProxyAbstract.php中的定义
         Bd_Log::warning('Get conf form cache.conf failed when create AkCacheClient', 6, null);
         return false;
     }
     //去除不需要进行string -> int类型转换的项
     array_splice($keyArray, 0, 2);
     array_splice($keyArray, 1, 1);
     //记录需要进行string -> bool转换的项
     $boolArray = array_splice($keyArray, 1, 2);
     foreach ($keyArray as $key) {
         if (array_key_exists($key, $mcConf)) {
             $mcConf[$key] = intval($mcConf[$key]);
         }
     }
     foreach ($boolArray as $key) {
         if ($mcConf[$key] === 'true') {
             $mcConf[$key] = true;
         } else {
             $mcConf[$key] = false;
         }
     }
     /* 装配后的mcConf的具体示例
        $mcConf = array(
            'pid'     => $pid,
            'zk_path' => $zk_path,
            'default_expire' => 86400,
            'curr_idc' => 'tc',
            'delete_directly' => true,
            'delete_delay' => true,
            'connect_timeout' => 10,
            'poll_timeout' => 20,
            'send_timeout' => 100,
            'recv_timeout' => 500,
            'acm_connect_timeout' => 30,
            'acm_read_timeout' => 500,
            'acm_write_timeout' => 100,
        );
        */
     $objMcClient = Ak_McClient::create($mcConf);
     if (is_null($objMcClient)) {
         Bd_Log::debug("create AkCacheClient failed", 2, $mcConf);
         return false;
     }
     return $objMcClient;
 }
Example #5
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;
 }
Example #6
0
 protected function _zkDeleteChildren($path)
 {
     return Ak_Zookeeper::deleteChildren($this->zk_path . $path);
 }