protected function _initPipeEvents($process, $workerType) { \Daemon\Library\Ipc\Shared::setCurrentTaskTable($process->pid, array('taskNum' => 0, 'workerType' => $workerType)); $objMaster = \Daemon\Process\Master::getInstance(); swoole_event_add($process->pipe, function ($pipe) use($process, $objMaster) { $objMaster->dispatch($process); }); }
protected function _flowPredict($flow) { if (count($this->_arrFlow) >= 18) { array_shift($this->_arrFlow); } array_push($this->_arrFlow, $flow); $average = ceil(array_sum($this->_arrFlow) / count($this->_arrFlow)); $driveConf = $this->_objAha->getConfig()->get('aha', 'drive'); $maxFlow = $driveConf['max_process_num']; if ($average > $maxFlow * 0.85) { \Daemon\Library\Ipc\Shared::getMaxTaskNumAtomic()->set(0); //根据流量预测 可能已经堵住了 清零自恢复 Log::monitor()->error(array(Monitor::KEY => Monitor::FLOW_OVER_PREDICT_RESET, 'average' => $average, 'maxFlow' => $maxFlow * 0.85)); } Log::statsLog()->debug(array('type' => 'flowPredict', 'average' => $average, 'maxFlow' => $maxFlow, 'point' => $maxFlow * 0.85)); return $average; }
protected function _send($package) { $ret = $this->_worker->write(json_encode($package) . Constant::PACKAGE_EOF); if (false === $ret) { Log::monitor()->error(array(Monitor::KEY => Monitor::IPC_PIPE_WRITE_ERR, '%package' => $package)); Log::redoLog()->debug(array('redo' => $package)); } else { \Daemon\Library\Ipc\Shared::getMaxTaskNumAtomic()->add(1); } Log::billLog()->debug(array('%package' => $package, 'type' => 'send')); }
protected function _taskWorkerComplete($package) { $pid = $package['content']['pid']; $res = \Daemon\Library\Ipc\Shared::getCurrentTaskTable()->decr($pid, 'taskNum', 1); if (false === $res) { Log::monitor()->error(array(Monitor::KEY => Monitor::TABLE_DECR_ERR, '#package' => $package)); } $driveWorker = $this->_objManager->getDriveWorker(); list($drivePid, $driveProcess) = each($driveWorker); $ret = $driveProcess->write(json_encode($package) . Constant::PACKAGE_EOF); if (false === $ret) { Log::monitor()->error(array(Monitor::KEY => Monitor::IPC_PIPE_WRITE_ERR, '#package' => $package)); Log::redoLog(array('redo' => $package)); return false; } Log::billLog()->debug(array('#package' => $package, 'type' => 'complete')); return true; }
protected function _waitChildProcess() { //必须为false,非阻塞模式 //信号发生时可能同时有多个子进程退出 //必须循环执行wait直到返回false $objManager = \Daemon\Library\Ipc\Manager::getInstance(); while ($result = \swoole_process::wait(false)) { $pid = $result['pid']; if ($objManager->hasDriveWorker($pid)) { $objManager->delDriveWorker($pid); \Daemon\Library\Ipc\Shared::delCurrentTaskTable($pid); if (!$this->_sigterm) { $objManager->createDriveWorker(\Daemon\Process\Master::getInstance()->getAha(), 1); } } elseif ($objManager->hasTaskWorker($pid)) { $objManager->delTaskWorker($pid); \Daemon\Library\Ipc\Shared::delCurrentTaskTable($pid); if (!$this->_sigterm) { $objManager->createDirtributeWorker(\Daemon\Process\Master::getInstance()->getAha(), 1); } } elseif ($objManager->hasRedoWorker($pid)) { $objManager->delRedoWorker($pid); \Daemon\Library\Ipc\Shared::delCurrentTaskTable($pid); if (!$this->_sigterm) { $objManager->createRedoWorker(\Daemon\Process\Master::getInstance()->getAha(), 1); } } elseif ($objManager->hasStatsWorker($pid)) { $objManager->delStatsWorker($pid); \Daemon\Library\Ipc\Shared::delCurrentTaskTable($pid); if (!$this->_sigterm) { $objManager->createStatsWorker(\Daemon\Process\Master::getInstance()->getAha(), 1); } } else { } } //如果是SIGTERM信号 并且子进程都已经退出了 父进程终止 $workers = \Daemon\Library\Ipc\Shared::getCurrentTaskTable(); if ($this->_sigterm && !$workers->count()) { \swoole_event_exit(); } }
protected function _send($package) { $ret = $this->_worker->write(json_encode($package) . Constant::PACKAGE_EOF); if (false === $ret) { Log::monitor()->error(array(Monitor::KEY => Monitor::IPC_PIPE_WRITE_ERR, '^package' => $package)); Log::redoLog()->debug(array('redo' => $package)); } \Daemon\Library\Ipc\Shared::getMaxTaskNumAtomic()->sub(1); //在distribute进程减1有最短路径 if (\Daemon\Library\Ipc\Shared::getMaxTaskNumAtomic()->get() > 2 << 16) { \Daemon\Library\Ipc\Shared::getMaxTaskNumAtomic()->set(0); } Log::billLog()->debug(array('^package' => $package, 'type' => '_distributeResponse')); }