public function getNext() { if (time() - $this->pTime > 1) { $this->pTime = time(); // MetaQ::$log->logDebug("current status:", array( // 'id' => $this->id, // 'group' => $this->group, // 'topic' => $this->topic, // 'offset' => $this->offset, // 'sockets' => $this->sockets, // 'partitionList' => $this->partitionList // )); $this->checkBalance(); } if (sizeof($this->partitionList) == 0) { return array(); } $arr = $this->getCurrentPartition(); list($partition, $sleep, $master, $partId) = $arr; list($host, $port, $pid) = explode('-', $partition); $offset = $this->getOffset($master); if (!isset($this->sockets[$host . ':' . $port])) { $this->sockets[$host . ':' . $port] = new Socket($host, $port); try { $this->sockets[$host . ':' . $port]->connect(); } catch (\Exception $e) { echo $e->getMessage(); } } $socket = $this->sockets[$host . ':' . $port]; $data = Codec::getEncode($this->topic, $this->group, $pid, $offset); $socket->write($data); $data = $socket->read(Codec::MAX_MSG_LENGTH + 14); list($msgs, $offset, $_offset) = Codec::getResultDecode($data); // 301, We can turn off this feature at server side if ($_offset) { $sleep += $sleep * 20; $this->partitionList[] = array($partition, $sleep, $master, $partId); $this->offset[$master] = $_offset; $this->commitOffset($this->group, $master, $_offset, 0); return array(); } else { if ($offset) { $this->partitionList[] = array($partition, $sleep, $master, $partId); $this->offset[$master] += $offset; $lastMsg = array_pop(array_values($msgs)); $lastId = $lastMsg['id']; $this->commitOffset($this->group, $master, $this->offset[$master], $lastId); if ($msgs) { return $msgs; } } else { $sleep += $sleep * 2; $this->partitionList[] = array($partition, $sleep, $master, $partId); // Reorder the partition list, sort and get the smallest sleep value $parts = array_values($this->partitionList); uasort($parts, array($this, 'cmp')); $smallest = array_shift($parts); if ($smallest[1] >= 2000000) { //echo "sleep 2s\n"; //echo "offset ". $this->offset[$master]. "\n"; usleep(2000000); foreach ($this->partitionList as $key => $value) { $this->partitionList[$key][1] -= 2000000; } } return array(); } } }