Kafka\Client::getHostByPartition PHP Method

getHostByPartition() public method

get broker host by topic partition
public getHostByPartition ( string $topicName, integer $partitionId ) : string
$topicName string
$partitionId integer
return string
    public function getHostByPartition($topicName, $partitionId = 0)
    {
        $retryAttempts = 0;
        do {
            $partitionInfo = $this->metadata->getPartitionState($topicName, $partitionId);
            if (!$partitionInfo) {
                throw new \Kafka\Exception('topic:' . $topicName . ', partition id: ' . $partitionId . ' is not exists.');
            }
            $hostList = $this->getBrokers();
            if (isset($partitionInfo['leader']) && isset($hostList[$partitionInfo['leader']])) {
                return $hostList[$partitionInfo['leader']];
            } elseif ($retryAttempts == 0) {
                // Its possible a broker dropped out of the cluster during the lifetime of our process
                // We should refresh our cached metadata and retry once.
                $this->metadata->refreshMetadata();
                $retryAttempts++;
            } else {
                throw new \Kafka\Exception('can\'t find broker host for topic ' . $topicName . ' partitionId ' . $partitionId);
            }
        } while ($retryAttempts <= 1);
    }