/** * implements Iterator function * * @access public * @return integer */ public function valid() { if (!$this->valid) { $this->partition->setMessageOffset($this->offset); // one partition iterator end \Kafka\Protocol\Fetch\Helper\Helper::onPartitionEof($this->partition); } return $this->valid; }
/** * fetch message to broker * * @access public * @return void */ public function fetch() { $data = $this->_formatPayload(); if (empty($data)) { return false; } $responseData = array(); $streams = array(); foreach ($data as $host => $requestData) { $connArr = $this->client->getStream($host); $conn = $connArr['stream']; $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->fetchRequest($requestData); $streams[$connArr['key']] = $conn; } $fetch = new \Kafka\Protocol\Fetch\Topic($streams, $data); // register fetch helper $freeStream = new \Kafka\Protocol\Fetch\Helper\FreeStream($this->client); $freeStream->setStreams($streams); \Kafka\Protocol\Fetch\Helper\Helper::registerHelper('freeStream', $freeStream); // register partition commit offset $commitOffset = new \Kafka\Protocol\Fetch\Helper\CommitOffset($this->client); $commitOffset->setGroup($this->group); \Kafka\Protocol\Fetch\Helper\Helper::registerHelper('commitOffset', $commitOffset); return $fetch; }
/** * load next topic * * @access public * @return void */ public function loadNextTopic() { if ($this->validCount >= $this->topicCount) { \Kafka\Protocol\Fetch\Helper\Helper::onStreamEof($this->currentStreamLockKey); return false; } if ($this->currentStreamCount >= $this->topicCounts[$this->currentStreamKey]) { \Kafka\Protocol\Fetch\Helper\Helper::onStreamEof($this->currentStreamLockKey); $this->currentStreamKey++; } $lockKeys = array_keys($this->streams); $streams = array_values($this->streams); if (!isset($streams[$this->currentStreamKey])) { return false; } $stream = $streams[$this->currentStreamKey]; $this->currentStreamLockKey = $lockKeys[$this->currentStreamKey]; try { $topicLen = $stream->read(2, true); $topicLen = Decoder::unpack(Decoder::BIT_B16, $topicLen); $topicLen = array_shift($topicLen); if ($topicLen <= 0) { return false; } // topic name $this->key = $stream->read($topicLen, true); $this->current = new Partition($this, $this->context); } catch (\Kafka\Exception $e) { return false; } $this->validCount++; $this->currentStreamCount++; return true; }