/** * Get the value for the node * * @param string $path the path to the node * * @return string|null */ public function get($path) { if (!$this->zookeeper->exists($path)) { return null; } return $this->zookeeper->get($path); }
/** * 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; }
/** * 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; }
/** * Wath a given path * @param string $path the path to node * @param callable $callback callback function * @return string|null */ public function watch($path, $callback) { if (!is_callable($callback)) { return null; } if ($this->zookeeper->exists($path)) { if (!isset($this->callback[$path])) { $this->callback[$path] = array(); } if (!in_array($callback, $this->callback[$path])) { $this->callback[$path][] = $callback; return $this->zookeeper->get($path, array($this, 'watchCallback')); } } }
/** * 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'))); } }