/** * Get the hostname and port of a broker. * * @param int the id of the brother to get the address of * * @return string the hostname and port of the broker, separated by a colon: host:port */ public function address($broker) { $data = sprintf(self::BROKER_PATH, (int) $broker); $result = $this->zookeeper->get($data); if (empty($result)) { $result = null; } else { $parts = explode(":", $result); $result = $parts[1] . ':' . $parts[2]; } return $result; }
public function testGetBrokerDetailWithEmptyResponse() { $zookeeperMock = $this->getMockBuilder('\\ZooKeeper')->disableOriginalConstructor()->getMock(); $zookeeperMock->expects($this->once())->method('exists')->with($this->equalTo(sprintf(Zookeeper::BROKER_DETAIL_PATH, 1)))->will($this->returnValue(true)); $zookeeperMock->expects($this->once())->method('get')->with($this->equalTo(sprintf(Zookeeper::BROKER_DETAIL_PATH, 1)))->will($this->returnValue(null)); $zookeeper = new Zookeeper(); $zookeeper->setZookeeper($zookeeperMock); $this->assertEquals([], $zookeeper->getBrokerDetail(1)); }
/** * Get the currently active brokers participating in a particular topic. * * @param string $topic the topic to get the brokers for * * @return array an array of brokers (int) that are participating in the topic */ public function brokers($topic) { $topicPath = sprintf(self::TOPIC_PATH, $topic); if (!$this->zookeeper->exists($topicPath)) { return array(); } $children = $this->zookeeper->getChildren($topicPath); if (empty($children)) { return array(); } $results = array(); foreach ($children as $child) { $results[] = intval(str_replace($topicPath, '', $child)); } return $results; }
/** * List the children of the given path, i.e. the name of the directories * within the current node, if any * * @param string $path the path to the node * * @return array the subpaths within the given node */ public function getChildren($path) { if (strlen($path) > 1 && preg_match('@/$@', $path)) { // remove trailing / $path = substr($path, 0, -1); } return $this->zookeeper->getChildren($path); }
function formList($askingInstance = null) { $zookeepers = Zookeeper::orderBy("name", "ASC")->get(); $tab = []; foreach ($zookeepers as $zk) { $tab[$zk->id] = $zk->name; } return $tab; }
/** * Get the currently active brokers participating in a particular topic. * * @param string $topic the topic to get the brokers for * * @return array an array of brokers (int) that are participating in the topic */ public function brokers($topic) { $topicPath = sprintf(self::TOPIC_PATH, $topic); if (!@$this->zookeeper->exists($topicPath)) { if ($this->zookeeper->getState() != Zookeeper::CONNECTED_STATE) { $msg = 'Cannot connect to Zookeeper to fetch brokers for topic ' . $topic; throw new Kafka_Exception_ZookeeperConnection($msg); } return array(); } $children = $this->zookeeper->getChildren($topicPath); if (empty($children)) { return array(); } $results = array(); foreach ($children as $child) { $results[] = intval(str_replace($topicPath, '', $child)); } return $results; }
function commitOffset($groupId, $topic, $brokerId, $partition, \Kafka\Offset $offset) { $this->zkConnect(); $path = "/consumers/{$groupId}/offsets/{$topic}"; if (!$this->zk->exists($path)) { $this->createPermaNode($path); } if (!$this->zk->exists("{$path}/{$brokerId}-{$partition}")) { $this->createPermaNode("{$path}/{$brokerId}-{$partition}", $offset->__toString()); } else { $this->zk->set("{$path}/{$brokerId}-{$partition}", $offset->__toString()); } }
/** * Gets all the partitions for a given topic. * * @param string $topic the topic to get the partitions for * * @return array an associative array of the broker (int) to the number of partitions (int) */ public function partitions($topic) { $offsetsPath = sprintf(self::OFFSETS_PATH, $this->group, $topic); if (!$this->zookeeper->exists($offsetsPath)) { return array(); } $children = $this->zookeeper->getChildren($offsetsPath); $partitions = array(); foreach ($children as $child) { list($broker, $partition) = explode('-', str_replace($offsetsPath, '', $child), 2); $partitions[intval($broker)] = intval($partition); } return $partitions; }
/** * Delete watch callback on a node, delete all callback when $callback is null * @param string $path * @param callable $callback * @return boolean|NULL */ public function cancelWatch($path, $callback = null) { if (isset($this->callback[$path])) { if (empty($callback)) { unset($this->callback[$path]); $this->zookeeper->get($path); //reset the callback return true; } else { $key = array_search($callback, $this->callback[$path]); if ($key !== false) { unset($this->callback[$path][$key]); return true; } else { return null; } } } else { return null; } }
if ($animal->foodType == 'carnivore') { $this->meat = $this->meat - 2; } elseif ($animal->foodType == "herbivore") { $this->veggies -= 2; //$this->veggies - 2; } elseif ($animal->foodType == 'omnivore') { $this->veggies -= 2; $this->meat -= 2; } } public function getFoodStatus() { return "There are " . $this->meat . " units of meat, " . $this->veggies . " units of veggies."; } } $kevin = new Zookeeper(10, 10); $lion = new Animal('carnivore'); $cow = new Animal('herbivore'); $pig = new Animal('omnivore'); print_r($cow); echo "<br>"; $kevin->feed($lion); print_r($kevin); echo $kevin->getFoodStatus(); echo "<br>"; $kevin->feed($cow); echo $kevin->getFoodStatus(); echo "<br>"; $kevin->feed($pig); echo $kevin->getFoodStatus(); echo "<br>";
/** * Create parent node, if needed * * @param string $nodePath */ private function createParentIfNeeded($nodePath) { if (!$this->zk->exists($nodePath)) { $this->zk->create($nodePath, 'Cruftflake machines', array(array('perms' => \Zookeeper::PERM_ALL, 'scheme' => 'world', 'id' => 'anyone'))); } }
<?php /** * https://github.com/andreiz/php-zookeeper/blob/master/examples/Zookeeper_Example.php */ $zk = new Zookeeper('127.0.0.1:port,...,ip:port/path'); $brokersPath = '/brokers/ids'; $ids = $zk->getChildren($brokersPath); $brokers = array(); foreach ($ids as $id) { $path = $brokersPath . "/{$id}"; $val = $zk->get($path); $val = json_decode($val, true); $brokers[] = $val['host'] . ":" . $val['port']; } $brokersAddr = implode(',', $brokers); //begin to consume $conf = new RdKafka\Conf(); // Set the group id. This is required when storing offsets on the broker $conf->set('group.id', 'my-alarm-group'); $conf->set('broker.version.fallback', '0.8.2.2'); // socket请求的超时时间。实际的超时时间为max.fetch.wait + socket.timeout.ms。 $conf->set('socket.timeout.ms', '400'); $consumer = new RdKafka\Consumer($conf); $consumer->addBrokers($brokersAddr); $consumer->setLogLevel(LOG_DEBUG); $topicConf = new RdKafka\TopicConf(); $topicConf->set('auto.commit.interval.ms', 1000); // Set the offset store method to 'file' $topicConf->set('offset.store.method', 'file'); $topicConf->set('offset.store.path', sys_get_temp_dir());
private $veggies = 10; public function feed($animal) { if ($animal->foodType == 'carnivore') { $this->meat -= 2; } elseif ($animal->foodType == 'herbivore') { $this->veggies -= 2; } elseif ($animal->foodType == 'omnivore') { $this->meat--; $this->veggies--; } else { die("Invalid animal type: " . $animal->foodType); } } public function getFoodStatus() { return "meat: " . $this->meat . "veggies: " . $this->veggies; } } $lion = new Animal('carnivore'); $sloth = new Animal('herbivore'); $bear = new Animal('omnivore'); $lorax = new Animal('candivore'); $zookeeper = new Zookeeper(); echo $zookeeper->getFoodStatus(), "<br>"; $zookeeper->feed($lion); echo $zookeeper->getFoodStatus(), "<br>"; $zookeeper->feed($sloth); echo $zookeeper->getFoodStatus(), "<br>"; $zookeeper->feed($bear); echo $zookeeper->getFoodStatus(), "<br>";
<?php /** * Created by PhpStorm. * User: bamdad * Date: 2/3/2016 * Time: 9:02 PM */ include_once './autoload.php'; //ZooKeeper $animal1 = new Rabbit('bunbun'); $animal2 = new Fish('nemo'); $zookeeper = new Zookeeper(); $zookeeper->takeCareOf($animal1); $zookeeper->takeCareOf($animal2); $zookeeper->feedTheAnimals(); $zookeeper->soundOff(); //Dice $diceTrhow = new DiceThrow(); $count = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); for ($x = 1; $x <= 1000; $x++) { $res = $diceTrhow->throww(); $count[$res]++; } echo "Result for Normal Dice\n"; for ($x = 2; $x <= 12; $x++) { print_r('sum=' . $x . ' , count=' . $count[$x] . "\n"); } echo "Result for Sicherman Dice\n"; $sichermanDiceTrhow = new SichermanDiceThrow(); $count = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
<?php /** * Created by PhpStorm. * User: bamdad * Date: 2/3/2016 * Time: 9:02 PM */ include_once './autoload.php'; //Animals $animal1 = new Rabbit('bunbun'); $animal2 = new Fish('nemo'); $zookeeper = new Zookeeper(); $zookeeper->takeCareOf($animal1); $zookeeper->takeCareOf($animal2); echo "This zoo is taking care of " . sizeof($zookeeper) . " animals \n"; // $zookeeper->feedTheAnimals(); // $zookeeper->soundOff(); // $zookeeper->test(); $hungryCount = 0; $fullCount = 0; foreach ($zookeeper as $key => $value) { if ($value->isHungry()) { $hungryCount++; } else { $fullCount++; } } echo "Hungry: #" . $hungryCount . " and Full:#" . $fullCount . "\n"; foreach ($zookeeper as $key => $value) { echo $value->name . " is a " . get_class($value) . " who is ";