Example #1
0
 public static function checkZkConf($zk_conf)
 {
     $schema = self::_getSchema();
     $zk_conf = $schema->validate('server_list', $zk_conf, $ret);
     if ($ret != true) {
         Ak_Log::warning("zk conf format error");
     }
     return $zk_conf;
 }
Example #2
0
 public static function checkAClientConf($aclient_conf)
 {
     $schema = self::_getSchema();
     $aclient_conf = $schema->validate('aclient_conf', $aclient_conf, $ret);
     if ($ret != true) {
         Ak_Log::warning("aclient conf format error");
         return null;
     }
     return $aclient_conf;
 }
Example #3
0
 public static function create($conf)
 {
     $op = new Ak_Op();
     $op->conf = $conf;
     Ak_Zookeeper::setHost($conf['zk_host']);
     Ak_Log::setHandler('warning', 'Ak_Op::error');
     //mysqli_report(MYSQLI_REPORT_ALL ^ MYSQLI_REPORT_INDEX);
     //foreach ($conf['db'] as $name => $info) {
     //    $db = new mysqli($info['db_host'], $info['db_user'], $info['db_pass'], $info['db_name'], $info['db_port']);
     //    $db->set_charset("utf8");
     //    $op->db[$name] = $db;
     //}
     return $op;
 }
Example #4
0
 protected static function _keyToFile($key)
 {
     $dir = self::$dir;
     if ($dir == null) {
         if (defined('IS_ODP')) {
             $dir = DATA_PATH . '/ak';
         } else {
             $dir = dirname(__FILE__) . '/var';
         }
     }
     $md5 = md5($key);
     for ($i = 0; $i < self::$depth; $i++) {
         $dir .= '/' . $md5[$i];
     }
     if (!file_exists($dir)) {
         $ret = mkdir($dir, 0755, true);
         if ($ret != true) {
             Ak_Log::warning("make dir[{$dir}] failed");
             return null;
         }
     }
     return $dir . '/' . $md5 . '.php';
 }
Example #5
0
 public static function setLimit($limit)
 {
     self::$limit = $limit;
 }
Example #6
0
 public static function enumKeys($ip, $port, $callback, $step = 0)
 {
     $sock = fsockopen($ip, $port, $err_no, $err_msg, 2);
     if (!is_resource($sock)) {
         Ak_Log::warning("connect ip[{$ip}] port[{$port}] failed");
         return null;
     }
     $slabs = self::_sendCmd($sock, 'stats items');
     $lines = explode("\n", $slabs);
     $nslab = (count($lines) - 2) / 8;
     $nkey = array();
     for ($i = 0; $i < $nslab; $i++) {
         $line = $lines[$i * 8];
         list($v1, $v2, $num) = explode(' ', $line);
         list($v1, $slab, $v3) = explode(':', $v2);
         $nkey[$slab] = (int) $num;
     }
     foreach ($nkey as $slab => $n) {
         $start = 0;
         while ($start < $n) {
             $keys = self::_sendCmd($sock, "stats cachedump {$slab} {$step}");
             $keys = explode("\n", $keys);
             $c = count($keys) - 2;
             if ($c == 0) {
                 break;
             }
             $key_arr = array();
             for ($i = 0; $i < $c; $i++) {
                 $key = $keys[$i];
                 $key = explode(' ', $key);
                 $key = $key[1];
                 $key_arr[] = $key;
             }
             call_user_func($callback, $key_arr);
             $start += $c;
         }
     }
     //reset last item
     self::_sendCmd($sock, "stats cachedump 0 0");
 }
Example #7
0
 public function validate($type, $val, &$return)
 {
     $return = false;
     if (self::_isBasicType($type)) {
         $def = array('base' => $type);
     } else {
         $def = @$this->struct_def[$type];
     }
     if (!is_array($def)) {
         Ak_Log::warning("type[{$type}] not exists");
         return null;
     }
     //check if value is null
     if (is_null($val)) {
         if (isset($def['default'])) {
             $val = $def['default'];
         } elseif (@$def['optional']) {
             $return = true;
             return null;
         } else {
             Ak_Log::warning("value is null");
             return null;
         }
     }
     //check if value matches the type
     switch ($def['base']) {
         case 'bool':
             if (!is_bool($val)) {
                 Ak_Log::warning("value[{$val}] is not bool");
                 return null;
             }
             break;
         case 'int':
             if (!is_int($val)) {
                 Ak_Log::warning("value[{$val}] is not int");
                 return null;
             }
             break;
         case 'float':
             if (!is_float($val)) {
                 Ak_Log::warning("value[{$val}] is not float");
                 return null;
             }
             break;
         case 'string':
             if (!is_string($val)) {
                 Ak_Log::warning("value[{$val}] is not string");
                 return null;
             }
             break;
         case 'array':
             if (!is_array($val)) {
                 Ak_Log::warning("value[{$val}] is not array");
                 return null;
             }
             break;
         case 'dict':
             if (!is_array($val)) {
                 Ak_Log::warning("value[{$val}] is not dict");
                 return null;
             }
             break;
         case 'object':
             if (!is_array($val)) {
                 Ak_Log::warning("value[{$val}] is not object");
                 return null;
             }
             break;
         case 'any':
             break;
         case 'multi':
             $ret_all = false;
             Ak_Log::beginSub();
             foreach ($def['types'] as $t) {
                 $temp_val = $this->validate($t, $val, $ret);
                 if ($ret != true) {
                     Ak_Log::warning("value[{$val}] is not type[{$t}]");
                 } else {
                     $val = $temp_val;
                     $ret_all = true;
                     break;
                 }
             }
             if ($ret_all == true) {
                 Ak_Log::endSub(false);
             } else {
                 Ak_Log::endSub(true);
                 return null;
             }
             break;
         default:
             Ak_Log::warning("unknown type[{$def['base']}]");
             return null;
     }
     //check if value in the array
     if (is_array(@$def['in'])) {
         $ret = in_array($val, $def['in']);
         if ($ret != true) {
             Ak_Log::warning("value[{$val}] not in array");
             return null;
         }
     }
     //check if the value in range
     switch ($def['base']) {
         case 'int':
         case 'float':
             if (!self::_checkRange(@$def['min'], @$def['max'], $val)) {
                 Ak_Log::warning("value[{$val}] is not valid");
                 return null;
             }
             break;
         case 'string':
             $len = strlen($val);
             if (!self::_checkRange(@$def['min_size'], @$def['max_size'], $len)) {
                 Ak_Log::warning("string len[{$len}] is not valid");
                 return null;
             }
             if (isset($def['regex']) && !preg_match($def['regex'], $val)) {
                 Ak_Log::warning("value[{$val}] not match regex[{$def['regex']}]");
                 return null;
             }
             break;
         case 'array':
         case 'dict':
             $len = count($val);
             if (!self::_checkRange(@$def['min_size'], @$def['max_size'], $len)) {
                 Ak_Log::warning("item count[{$len}] is not valid");
                 return null;
             }
             break;
     }
     //check children
     switch ($def['base']) {
         case 'array':
             $i = 0;
             foreach ($val as $k => &$v) {
                 if (!is_int($k) || $k != $i) {
                     Ak_Log::warning("array index[{$i}] != key[{$k}]");
                     return null;
                 }
                 $v = $this->validate($def['item_type'], $v, $ret);
                 if (!$ret) {
                     Ak_Log::warning("item[{$k}] not valid");
                     return null;
                 }
                 $i++;
             }
             break;
         case 'dict':
             foreach ($val as $k => &$v) {
                 $this->validate($def['key_type'], $k, $ret);
                 if (!$ret) {
                     Ak_Log::warning("key[{$k}] is not type[{$def['key_type']}]");
                     return null;
                 }
                 $v = $this->validate($def['value_type'], $v, $ret);
                 if (!$ret) {
                     Ak_Log::warning("value of key[{$k}] is not type[{$def['value_type']}]");
                     return null;
                 }
             }
             break;
         case 'object':
             if (!is_array(@$def['alias'])) {
                 $def['alias'] = array();
             }
             foreach ($def['alias'] as $k1 => $k2) {
                 if (!isset($val[$k2])) {
                     $val[$k2] = @$val[$k1];
                     unset($val[$k1]);
                 }
             }
             foreach ($def['members'] as $k => $t) {
                 $val[$k] = $this->validate($t, @$val[$k], $ret);
                 if (!$ret) {
                     Ak_Log::warning("key[{$k}] is not type[{$t}]");
                     return null;
                 }
             }
             break;
     }
     //user defined check
     $func = @$def['user_def'];
     if (is_callable($func)) {
         $val = $func($val, $ret);
         if ($ret != true) {
             Ak_Log::warning("user check[{$func}] failed");
             return null;
         }
     }
     $return = true;
     return $val;
 }
Example #8
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 #9
0
 public static function add_error($msg)
 {
     Ak_Log::warning($msg);
     self::$err_info['message'] .= "{$msg}; ";
 }