Ejemplo n.º 1
0
 public function publish_message($topic, $message, $partition = 0)
 {
     $stime = microtime(true);
     $rtn = false;
     $offset = false;
     if (!is_string($message)) {
         $message = json_encode($message);
     }
     $p = $this->getProducer();
     $p->setRequireAck($this->producerRequireAck);
     $p->setMessages($topic, $partition, $message);
     try {
         $result = $p->send();
     } catch (\Exception $e) {
         $catched_exception = $e;
         \Libs\Log\LevelLogWriter::warning("send to kafka failed,msg:" . $e->getMessage(), $e->getCode());
         $rtn = false;
     }
     if ($result && isset($result[$topic][$partition]['errCode']) && $result[$topic][$partition]['errCode'] == 0) {
         $rtn = $offset = $result[$topic][$partition]['offset'];
     } else {
         $rtn = false;
     }
     $logData = array();
     $logData['zk'] = $this->zkHosts;
     $logData['topic'] = $topic;
     $logData['message'] = $message;
     $logData['partition'] = $partition;
     $logData['offset'] = $offset;
     $logData['exception'] = isset($catched_exception) ? json_encode($catched_exception) : '';
     $logData['timecost'] = number_format((microtime(true) - $stime) * 1000, 2);
     \Libs\Log\LevelLogWriter::selfLog("mqclient", "publish_message", $logData);
     return $rtn;
 }
Ejemplo n.º 2
0
 public static function log($tag = "")
 {
     Profiler::over();
     $data = array();
     $data['tag'] = $tag;
     $data['data'] = serialize(Profiler::getTopNodes());
     $data['memuseage'] = Profiler::getMemUsage();
     $data['memuseage'] = $data['memuseage']['num'] . ' ' . $data['memuseage']['unit'];
     $data['total_cost'] = Profiler::getGlobalDuration();
     \Libs\Log\LevelLogWriter::selfLog("profiler", "PROFILER", $data);
     return $data;
 }
Ejemplo n.º 3
0
 private function getHeaders()
 {
     $headers = RemoteHeaderCreator::getHeaders();
     $headerArr = array('Meilishuo:' . $headers['Meilishuo']);
     if (!empty($headers['X-REF'])) {
         $headerArr[] = 'XREF:' . $headers['X-REF'];
     }
     //transfer the logid http header between rpc
     if ($logid = \Libs\log\LevelLogWriter::logId()) {
         $headerArr[] = 'LOGID:' . $logid;
     }
     return $headerArr;
 }
Ejemplo n.º 4
0
 private function rpclog($ch)
 {
     $curlErrno = curl_errno($ch);
     $curlError = curl_error($ch);
     $info = curl_getinfo($ch);
     $logInfo = array();
     $logInfo['curl_errno'] = $curlErrno;
     $logInfo['curl_error'] = $curlError;
     $logInfo['url'] = $info['url'];
     $logInfo['http_code'] = $info['http_code'];
     $logInfo['total_time'] = number_format($info['total_time'] * 1000, 0);
     $logInfo['time_detail'] = number_format($info['namelookup_time'] * 1000, 0) . "," . number_format($info['connect_time'] * 1000, 0) . "," . number_format($info['pretransfer_time'] * 1000, 0) . "," . number_format($info['starttransfer_time'] * 1000, 0);
     \Libs\Log\LevelLogWriter::selfLog('rpc', "", $logInfo);
 }
Ejemplo n.º 5
0
 /**
  * Get an initialized Redis connection according to the key.
  */
 protected static function getRedis($key = NULL, $reconnect = FALSE)
 {
     $class = get_called_class();
     $config = $class::getConfig();
     $count = count($config->servers);
     $server_id = is_null($key) ? 0 : hexdec(substr(md5($key), 0, 2)) % $count;
     $host = $config->servers[$server_id]['host'];
     $port = $config->servers[$server_id]['port'];
     $connect_index = $host . ":" . $port;
     $isConnect = TRUE;
     if (!isset(self::$connections[$connect_index])) {
         self::$connections[$connect_index] = self::connect($host, $port, $class::$timeout);
         $isConnect = self::$connections[$connect_index]->isConnected();
         if (!$isConnect && !$reconnect) {
             unset(self::$connections[$connect_index]);
             //reconnect
             return self::getRedis($key, TRUE);
         }
     }
     if (!$isConnect) {
         \Libs\Log\LevelLogWriter::warning("redis server connect error:{$host}:{$port}:{$key}");
     }
     return self::$connections[$connect_index];
 }