/** * 并行渲染 * @param unknown_type $data * @param unknown_type $smarty * @param unknown_type uiData中阿拉丁的数据 * @param unknown_type uiData中普通结果的数据 */ function multi_render($data, $smarty, $arrAlaData, $arrAsData, $arrMultiRenderConf) { $time_start = Volatile::microtime(true) * 1000; require VUI_APP_PATH . '/plugins/modifier.common_render.php'; $arrNeedRenderSingleResult = array_merge($arrAlaData, $arrAsData); $time_multi_start = Volatile::microtime(true) * 1000; $mh = curl_multi_init(); $url = $arrMultiRenderConf['MULTI_RENDER_URL']; $arrRenderMultiReq = array(); $arrMultiPublicData = array(); $arrMultiPublicData['smarty_conf'] = CSmarty::getConf(); $arrMultiPublicData['DISPLAY'] = $GLOBALS['DISPLAY']; $arrMultiPublicData['LOG'] = $GLOBALS['LOG']; $arrMultiPublicData['DEFAULT_TEMPLATE_NAME'] = $GLOBALS['DEFAULT_TEMPLATE_NAME']; $arrMultiPublicData['DEFAULT_TEMPLATE_TYPE'] = $GLOBALS['DEFAULT_TEMPLATE_TYPE']; $arrMultiPublicData['ALADDIN_TEMPLATE_TYPE'] = $GLOBALS['ALADDIN_TEMPLATE_TYPE']; $arrMultiPublicData['STRUCT_TEMPLATE_TYPE'] = $GLOBALS['STRUCT_TEMPLATE_TYPE']; $arrMultiPublicData['ECOM_TEMPLATE_TYPE'] = $GLOBALS['ECOM_TEMPLATE_TYPE']; $arrMultiPublicData['randTime'] = CSmarty::getRandState(); $arrMultiPublicData['TplLOG'] = $GLOBALS['TplLOG']; $arrMultiPublicData['ALADDIN_FIELD'] = $GLOBALS['ALADDIN_FIELD']; $arrMultiPublicData['globalConf'] = $GLOBALS['globalConf']; $data['uiData']['multi_public_data'] = $arrMultiPublicData; //使用logid来计算存放在共享内存中的key $intLogId = CLog::logId(); $memsize = intval($arrMultiRenderConf['SHMOP']['MEM_SIZE']); // 共享内存的大小,单位byte $perm = 0666; // 共享内存访问权限,参考linux的权限 $shmid = shmop_open($intLogId, "n", $perm, $memsize); // 创建一个共享内存,第二个参数c表示创建 $shm_bytes_written = shmop_write($shmid, addslashes(serialize($data['uiData'])), 0); $arrShmData['id'] = $intLogId; $arrShmData['size'] = $shm_bytes_written; unset($data['uiData']['multi_public_data']); //若向共享内存中存放数据失败,则直接跳出并行渲染,回归正常渲染 if ($arrShmData['id'] === false || empty($arrShmData['size'])) { CLog::warning("put data to shmop fail,back to nomarl render,data_size:" . strlen(addslashes(serialize($data['uiData']))), MULTI_RENDER_FAIL); return false; } $GLOBALS['multi_render_shmid'] = $shmid; //主模板加入并发队列 $strReqKey = 'req_main'; $arrMainPostData = array(); $intRandTime = CSmarty::getRandState(); $arrMainPostData['tempName'] = $data['uiControl']['templateName']; $arrMainPostData['tempSwitch'] = $data['uiControl']['templateSwitch']; $arrMainPostData['tempVersion'] = empty($data['uiControl']['templateVersion']) ? "" : $data['uiControl']['templateVersion']; $arrMainPostData['dataId'] = $arrShmData['id']; $arrMainPostData['dataSize'] = $arrShmData['size']; $arrMainPostData['randTime'] = $intRandTime; $hexQid = $data['uiData']['queryInfo']['queryId']; $strMainUrl = $url . '?renderType=' . $strReqKey . '&logId=' . $intLogId . '&qid=' . $hexQid; $intMainTplTimeout = intval($arrMultiRenderConf['MAIN_TPL_TIMEOUT_MS']); $arrRenderMultiReq[$strReqKey] = curl_init($strMainUrl); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_HEADER, 0); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_NOSIGNAL, true); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_RETURNTRANSFER, 1); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_POST, 1); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_POSTFIELDS, http_build_query($arrMainPostData)); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_TIMEOUT_MS, $intMainTplTimeout); curl_multi_add_handle($mh, $arrRenderMultiReq[$strReqKey]); //按时间分包(尽可能均分),时间参数来自于进生的模板耗时报表 //分包数量,主模板单独占用一个包 $intSplitNum = intval($arrMultiRenderConf['PACK_NUM']) - 1; $arrTplTime = array(); $intTimeCount = 0; $arrSplitReqData = array(); // 分阿拉丁 if (!empty($arrAlaData) && is_array($arrAlaData)) { $intDefaultAladdinTime = $arrMultiRenderConf['DEFAULT_ALADDIN_TIME']; $bHhvm = $GLOBALS['globalConf']['FEATURE_LIST']['HHVM_BRANCH']['TURN'] === 'ON' ? true : false; if ($bHhvm) { $getDict = new GetDict(); $getDict->init(DATA_PATH . "/shmdict/dict/tpl_time.php"); } foreach ($arrAlaData as $strKey => $arrOneAlaData) { if ($bHhvm) { //hhvm 临时方案,提前手动分发tpl_time.php文件到data/shmdict/dict/下 $intOneTplTime = $getDict->getValueFromDict($arrOneAlaData['render_template']); } else { $intOneTplTime = shmdict_getValueFromDict($arrOneAlaData['render_template'], 'tpl_time'); } $intOneTplTime = empty($intOneTplTime) ? $intDefaultAladdinTime : $intOneTplTime; $intTimeCount += $intOneTplTime; $arrTplTime[$strKey] = $intOneTplTime; } arsort($arrTplTime); $intSplitTplTime = ceil($intTimeCount / $intSplitNum); $arrGroupTime = array(); $intIndex = 0; foreach ($arrTplTime as $strKey => $arrOneTplTime) { $intIndex = $intIndex % $intSplitNum; $intCount = 0; while ($intCount < $intSplitNum) { if ($arrGroupTime[$intIndex] >= $intSplitTplTime) { $intIndex = ($intIndex + 1) % $intSplitNum; } else { $arrSplitReqData[$intIndex][$strKey] = $arrAlaData[$strKey]; $arrGroupTime[$intIndex] += $arrOneTplTime; $intIndex++; break; } $intCount++; } } } // 分AS结果 if (!empty($arrAsData) && is_array($arrAsData)) { $intDefaultAsTime = $arrMultiRenderConf['DEFAULT_AS_TIME']; $intAsCount = count($arrAsData); $intNewTimeCount = $intTimeCount + $intAsCount * $intDefaultAsTime; $intNewSplitTplTime = $intNewTimeCount / $intSplitNum; foreach ($arrAsData as $strKey => $arrOneAsData) { $intIndex = $intIndex % $intSplitNum; $intCount = 0; while ($intCount < $intSplitNum) { if ($arrGroupTime[$intIndex] >= $intNewSplitTplTime) { $intIndex = ($intIndex + 1) % $intSplitNum; } else { $arrSplitReqData[$intIndex][$strKey] = $arrOneAsData; $arrGroupTime[$intIndex] += $intDefaultAsTime; $intIndex++; break; } $intCount++; } } } //将单条结果包加入并行 if (empty($arrSplitReqData) || !is_array($arrSplitReqData)) { CLog::warning("split pack result is empty,back to nomarl render!", MULTI_RENDER_FAIL); $bolDelRst = shmop_delete($shmid); $bolCloseRst = shmop_close($shmid); if ($bolDelRst && $bolCloseRst) { unset($GLOBALS['multi_render_shmid']); } return false; } foreach ($arrSplitReqData as $intPackIndex => $arrOneSplitReqData) { if (!empty($arrOneSplitReqData) && is_array($arrOneSplitReqData)) { $strReqKey = 'req_' . $intPackIndex; $arrReqPostData = array(); $arrReqPostData['dataId'] = $arrShmData['id']; $arrReqPostData['dataSize'] = $arrShmData['size']; $arrReqPostData['randTime'] = $intRandTime; $arrReqPostData['dataInfo'] = $arrOneSplitReqData; $strReqUrl = $url . '?renderType=' . $strReqKey . '&logId=' . $intLogId . '&qid=' . $hexQid; $arrRenderMultiReq[$strReqKey] = curl_init($strReqUrl); $intReqNum = count($arrOneSplitReqData); $intReqNumLine = intval($arrMultiRenderConf['SINGLE_TIMEOUT_NUM_LINE']); $intMoreNumPackTimeout = intval($arrMultiRenderConf['SINGLE_TPL_PACK_MAX_TIMEOUT_MS']); $intLessNumPackTimeout = intval($arrMultiRenderConf['SINGLE_TPL_PACK_MIN_TIMEOUT_MS']); $intTimeOut = $intReqNum > $intReqNumLine ? $intMoreNumPackTimeout : $intLessNumPackTimeout; curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_HEADER, 0); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_NOSIGNAL, true); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_RETURNTRANSFER, 1); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_POST, 1); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_POSTFIELDS, http_build_query($arrReqPostData)); curl_setopt($arrRenderMultiReq[$strReqKey], CURLOPT_TIMEOUT_MS, $intTimeOut); curl_multi_add_handle($mh, $arrRenderMultiReq[$strReqKey]); } } $GLOBALS['logArr']['time_multi_render_ready'] = round(Volatile::microtime(true) * 1000 - $time_start, 2); //执行并行渲染 $time_exe_start = Volatile::microtime(true) * 1000; $mrc = curl_multi_exec($mh, $active); if ($mrc == CURLM_CALL_MULTI_PERFORM) { CLog::warning("curl_multi connect fail,back to nomarl render!", MULTI_RENDER_FAIL); return false; } $intMaxCurlExeTimes = intval($arrMultiRenderConf['CURL_MAX_EXE_TIMES']); $intCurlExeCount = 0; while ($active && $mrc == CURLM_OK && $intCurlExeCount < $intMaxCurlExeTimes) { $intCurlExeCount++; if (curl_multi_select($mh) != -1) { $mrc = curl_multi_exec($mh, $active); if ($mrc == CURLM_CALL_MULTI_PERFORM) { CLog::warning("curl_multi connect fail,back to nomarl render!", MULTI_RENDER_FAIL); return false; } } } $GLOBALS['logArr']['time_multi_render_exec'] = round(Volatile::microtime(true) * 1000 - $time_exe_start, 2); $time_start = Volatile::microtime(true) * 1000; //接收数据 foreach ($arrRenderMultiReq as $key => $value) { $arrRenderResult[$key] = curl_multi_getcontent($arrRenderMultiReq[$key]); curl_close($arrRenderMultiReq[$key]); curl_multi_remove_handle($mh, $arrRenderMultiReq[$key]); } curl_multi_close($mh); $isDebug = $bolPreRenderFeature = $arrMultiRenderConf['DEBUG'] == 'ON' ? true : false; $arrRenderedData = array(); $intChildLastIn = 0; $intChildLastOut = 0; $intChildFirstIn = 0; $intTCount = 1; $strAllOneTime = ''; $arrTplLog = array(); $arrResultTplTm = array(); $intTimeRenderStructTpl = 0; $intRenderStructTplNum = 0; $intTimeAladdinTotal = 0; $intRenderAladdinNum = 0; $intTimeUrlencryptTotal = 0; $arrOtherLog = array(); foreach ($arrRenderResult as $key => $oneResult) { $arrUnseriResult = unserialize($oneResult); if (!empty($arrUnseriResult)) { $arrLogInfo = $arrUnseriResult['pack_log']; unset($arrUnseriResult['pack_log']); if (!empty($arrLogInfo) && is_array($arrLogInfo)) { $intChildLastIn = $arrLogInfo['start_time'] > $intChildLastIn ? $arrLogInfo['start_time'] : $intChildLastIn; if ($key != 'req_main') { $GLOBALS['logArr']['time_pack_' . $intTCount] = round($arrLogInfo['end_time'] - $arrLogInfo['start_time'], 2); $intTCount++; } else { $GLOBALS['logArr']['time_render_main_tpl'] = $arrLogInfo['time_main_tpl']; $GLOBALS['logArr']['time_pack_main'] = round($arrLogInfo['end_time'] - $arrLogInfo['start_time'], 2); } $arrTplLog = empty($arrTplLog) ? $arrLogInfo['tpl_log'] : array_merge($arrTplLog, $arrLogInfo['tpl_log']); $arrResultTplTm = empty($arrResultTplTm) ? $arrLogInfo['result_tpl_tm'] : array_merge($arrResultTplTm, $arrLogInfo['result_tpl_tm']); $intTimeRenderStructTpl += $arrLogInfo['time_render_struct_tpl']; $intRenderStructTplNum += $arrLogInfo['render_struct_tpl_num']; $intTimeAladdinTotal += $arrLogInfo['time_aladdin_total']; $intRenderAladdinNum += $arrLogInfo['render_aladdin_num']; $intTimeUrlencryptTotal += $arrLogInfo['time_urlencrypt_total']; if (!empty($arrLogInfo['logArr'])) { $arrOtherLog = array_merge($arrOtherLog, $arrLogInfo['logArr']); } // debug,更为详细的日志信息 if ($isDebug) { if ($intChildFirstIn == 0) { $intChildFirstIn = $arrLogInfo['start_time']; } else { $intChildFirstIn = $arrLogInfo['start_time'] < $intChildFirstIn ? $arrLogInfo['start_time'] : $intChildFirstIn; } $intChildLastOut = $arrLogInfo['end_time'] > $intChildLastOut ? $arrLogInfo['end_time'] : $intChildLastOut; foreach ($arrLogInfo['one_time'] as $key => $intOneTime) { $strAllOneTime .= $key . ':' . $intOneTime . '|'; } } } $arrRenderedData = array_merge($arrRenderedData, $arrUnseriResult); } else { CLog::warning("whole pack render fail!pack name:" . $key, MULTI_RENDER_FAIL); } } $GLOBALS['logArr'] = array_merge($GLOBALS['logArr'], $arrOtherLog); $GLOBALS['logArr']['time_multi_render_web'] = round($intChildLastIn - $time_exe_start, 2); $GLOBALS['logArr']['time_multi_render_recdata'] = round(Volatile::microtime(true) * 1000 - $time_start, 2); CSmarty::addStructTplRenderNumCount($intRenderStructTplNum); CSmarty::addStructTplRenderTimeCount($intTimeRenderStructTpl); $GLOBALS['time_count_temp']['time_aladdin_total'] += $intTimeAladdinTotal; $GLOBALS['time_count_temp']['render_aladdin_num'] += $intRenderAladdinNum; $GLOBALS['time_count_temp']['time_urlencrypt_total'] += $intTimeUrlencryptTotal; if (!empty($arrResultTplTm) && is_array($arrResultTplTm)) { foreach ($arrResultTplTm as $intOneTplTm) { CSmarty::recordResultTplRenderTime($intOneTplTm); } } if (!empty($arrTplLog) && is_array($arrTplLog)) { $objTplLog = TplLog::getInstance(); $objTplLog->setTplData($arrTplLog); } if ($isDebug) { $GLOBALS['logArr']['time_one_detail_time'] = $strAllOneTime; $GLOBALS['logArr']['time_web_curl'] = round($intChildFirstIn - $time_exe_start, 2); $GLOBALS['logArr']['time_multi_recdata'] = round(Volatile::microtime(true) * 1000 - $intChildLastOut, 2); $GLOBALS['logArr']['time_child_max'] = round($intChildLastOut - $intChildFirstIn, 2); } $time_start = Volatile::microtime(true) * 1000; $bolDelRst = shmop_delete($shmid); shmop_close($shmid); if (!$bolDelRst) { CLog::warning("clean shmop fail!id:" . $shmid, MULTI_RENDER_FAIL); } else { unset($GLOBALS['multi_render_shmid']); } //主模板并行渲染未成功 if ($arrRenderedData['req_main']['status'] !== 0) { CLog::warning("multi render main tpl fail,try local render once!", MULTI_RENDER_FAIL); $GLOBALS['mulit_render_flag'] = 1; $page = $smarty->do_render($data['uiData'], $data['uiControl']['templateName'], $data['uiControl']['templateSwitch'], $data['uiControl']['templateVersion']); if (empty($page)) { CLog::warning("local render main tpl fail too,abandon treatment,back to nomarl render", MULTI_RENDER_FAIL); return false; } } else { $page = $arrRenderedData['req_main']['content']; } unset($arrRenderedData['req_main']); $arrReplaceKeys = array(); $arrReplaceValues = array(); $intFailNum = 0; $strFailKeys = ''; foreach ($arrRenderedData as $strKey => $strRenderContent) { //单条模板未渲染成功 if ($strRenderContent['status'] !== 0) { //CLog::warning("multi render single tpl fail,try local render once!key:" . $strKey, MULTI_RENDER_FAIL); $intFailNum++; $strFailKeys = empty($strFailKeys) ? $strKey : $strFailKeys . '#' . $strKey; $strRenderContent['content'] = render_single_result($strKey, $arrNeedRenderSingleResult, $data); } if (empty($strRenderContent['content'])) { CLog::warning("local render single tpl fail too,abandon treatment,remove this single result!key:" . $strKey, MULTI_RENDER_FAIL); } $arrReplaceKeys[] = $strKey; $arrReplaceValues[] = empty($strRenderContent['content']) ? '' : $strRenderContent['content']; unset($arrNeedRenderSingleResult[$strKey]); } if (!empty($arrNeedRenderSingleResult)) { foreach ($arrNeedRenderSingleResult as $strKey => $arrContent) { //CLog::warning("multi render single tpl fail,try local render once!key:" . $strKey, MULTI_RENDER_FAIL); $intFailNum++; $strFailKeys = empty($strFailKeys) ? $strKey : $strFailKeys . '#' . $strKey; $rst = render_single_result($strKey, $arrNeedRenderSingleResult, $data); if (empty($rst)) { CLog::warning("local render single tpl fail too,abandon treatment,remove this single result!key:" . $strKey, MULTI_RENDER_FAIL); } $arrReplaceKeys[] = $strKey; $arrReplaceValues[] = empty($rst) ? '' : $rst; } } if ($intFailNum != 0) { CLog::warning("local render single tpl, num[{$intFailNum}] keys[{$strFailKeys}]", MULTI_RENDER_FAIL); } $GLOBALS['logArr']['time_multi_render_retry'] = round(Volatile::microtime(true) * 1000 - $time_start, 2); $time_start = Volatile::microtime(true) * 1000; $page = Util::replace($page, $arrReplaceKeys, $arrReplaceValues, 0); $GLOBALS['logArr']['time_multi_render_repdata'] = round(Volatile::microtime(true) * 1000 - $time_start, 2); return $page; }
function encryptUrl($url, $maxRandomStrLen = '', $encKeyStr = '', $encIvecStr = '') { $status = -1; $logArr['func'] = "encryptUrl"; /** * encryptUrl random string length * @var long */ if (empty($maxRandomStrLen) || intval($maxRandomStrLen) < 1) { $maxRandomStrLen = CSmarty::getMaxRandomStrLen(); } /** * encryptUrl key string, ie: token * @var string */ if (empty($encKeyStr)) { $encKeyStr = CSmarty::getEncKeyStr(); } if (empty($encIvecStr)) { $encIvecStr = CSmarty::getEncIvecStr(); } $randState = CSmarty::getRandState(); $flag = CSmarty::getFlag(); $logArr['url'] = $url; $logArr['encKeyStr'] = $encKeyStr; $logArr['maxRandomStrLen'] = $maxRandomStrLen; $logArr['randState'] = $randState; $logArr['flag'] = $flag; $url = trim($url); if (strlen($url) == 0) { return false; } $result = hilight_encryptUrl($url, $encKeyStr, $encIvecStr, $maxRandomStrLen, $randState, $flag); $logArr['result'] = $result; if ($GLOBALS['ENC_LOG']) { CLog::notice("hilight_encryptUrl end", $status, $logArr, 1); } if (false == $result) { CLog::warning("fail to call hilight_encryptUrl", $status, $logArr, 1); } $maxEncWarningLen = CSmarty::getMaxEncWarningLen(); $length = strlen($result); /** * if $length is greater than the max length of url, then return false,ie fail to encrypt * */ if ($maxEncWarningLen > 0 && $length > $maxEncWarningLen) { $logArr['length'] = $length; $logArr['maxEncWarningLen'] = $maxEncWarningLen; CLog::warning("encryption url len is {$length}, limit {$maxEncWarningLen}", $status, $logArr, 1); } return $result; }