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; }
/** * @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; }