1 | <?php |
||
27 | class ZooKeeperConfig implements ConfigInterface, LoggerAwareInterface |
||
28 | { |
||
29 | /** |
||
30 | * Parent path. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $parentPath; |
||
35 | |||
36 | /** |
||
37 | * ZK. |
||
38 | * |
||
39 | * @var \Zookeeper |
||
40 | */ |
||
41 | private $zk; |
||
42 | |||
43 | /** |
||
44 | * Process ID for a multi-process-single-machine setup. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | private $procesId = 1; |
||
49 | |||
50 | /** |
||
51 | * Logger. |
||
52 | * |
||
53 | * @var LoggerInterface |
||
54 | */ |
||
55 | private $logger; |
||
56 | |||
57 | /** |
||
58 | * Constructor. |
||
59 | * |
||
60 | * @param string $hostnames A comma separated list of hostnames (including |
||
61 | * port) |
||
62 | * @param int $processId If you want to run multiple server processes on a single machine, |
||
63 | * you have to provide each one an unique ID, |
||
64 | * so the zookeeper knows, which machine ID belongs to which process. |
||
65 | * @param string $zkPath The ZK path we look to find other machines under |
||
66 | * @param LoggerInterface $logger Logger class |
||
67 | */ |
||
68 | public function __construct($hostnames, $processId = 1, $zkPath = '/cruftflake', |
||
84 | |||
85 | /** |
||
86 | * Get machine identifier. |
||
87 | * |
||
88 | * @throws RuntimeException Thrown, when obtaining machine ID has failed. |
||
89 | * |
||
90 | * @return int Should be a 10-bit int (decimal 0 to 1023) |
||
91 | */ |
||
92 | public function getMachine() |
||
123 | |||
124 | /** |
||
125 | * Periodically re-syncs with zookeeper, to obtain new machine ID, if necessary. |
||
126 | * |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function heartbeat() |
||
133 | |||
134 | /** |
||
135 | * Compare found machine information with expected values. |
||
136 | * |
||
137 | * @param array $found |
||
138 | * @param array $expected |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | private function compareMachineInfo(array $found, array $expected) |
||
150 | |||
151 | /** |
||
152 | * Attempt to claim and create new machine ID in Zookeeper. |
||
153 | * |
||
154 | * @param array $children |
||
155 | * @param array $machineInfo |
||
156 | * |
||
157 | * @throws RuntimeException Thrown, when creation of machine ID has failed. |
||
158 | * |
||
159 | * @return int Machine ID. |
||
160 | */ |
||
161 | private function createMachineInfo(array $children, array $machineInfo) |
||
188 | |||
189 | /** |
||
190 | * Get mac address and hostname. |
||
191 | * |
||
192 | * @return array "hostname","processId", "time" keys |
||
193 | */ |
||
194 | private function getMachineInfo() |
||
208 | |||
209 | /** |
||
210 | * Create parent node, if needed. |
||
211 | * |
||
212 | * @param string $nodePath |
||
213 | */ |
||
214 | private function createParentIfNeeded($nodePath) |
||
227 | |||
228 | /** |
||
229 | * Machine ID to ZK node. |
||
230 | * |
||
231 | * @param int $id |
||
232 | * |
||
233 | * @return string The node path to use in ZK |
||
234 | */ |
||
235 | private function machineToNode($id) |
||
239 | |||
240 | /** |
||
241 | * Set logger. |
||
242 | * |
||
243 | * @param LoggerInterface $logger |
||
244 | */ |
||
245 | public function setLogger(LoggerInterface $logger) |
||
249 | } |
||
250 |