PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/external/storm-kafka/src/jvm/storm/kafka/DynamicBrokersReader.java

https://gitlab.com/kidaa/storm
Java | 178 lines | 120 code | 19 blank | 39 comment | 3 complexity | 2ddafd4269a76ab27ff777947ea1942c MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package storm.kafka;
  19. import backtype.storm.Config;
  20. import backtype.storm.utils.Utils;
  21. import com.google.common.base.Preconditions;
  22. import org.apache.curator.framework.CuratorFramework;
  23. import org.apache.curator.framework.CuratorFrameworkFactory;
  24. import org.apache.curator.retry.RetryNTimes;
  25. import org.json.simple.JSONValue;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import storm.kafka.trident.GlobalPartitionInformation;
  29. import java.io.UnsupportedEncodingException;
  30. import java.net.SocketTimeoutException;
  31. import java.util.List;
  32. import java.util.Map;
  33. public class DynamicBrokersReader {
  34. public static final Logger LOG = LoggerFactory.getLogger(DynamicBrokersReader.class);
  35. private CuratorFramework _curator;
  36. private String _zkPath;
  37. private String _topic;
  38. public DynamicBrokersReader(Map conf, String zkStr, String zkPath, String topic) {
  39. // Check required parameters
  40. Preconditions.checkNotNull(conf, "conf cannot be null");
  41. validateConfig(conf);
  42. Preconditions.checkNotNull(zkStr,"zkString cannot be null");
  43. Preconditions.checkNotNull(zkPath, "zkPath cannot be null");
  44. Preconditions.checkNotNull(topic, "topic cannot be null");
  45. _zkPath = zkPath;
  46. _topic = topic;
  47. try {
  48. _curator = CuratorFrameworkFactory.newClient(
  49. zkStr,
  50. Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_SESSION_TIMEOUT)),
  51. Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_CONNECTION_TIMEOUT)),
  52. new RetryNTimes(Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_TIMES)),
  53. Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_INTERVAL))));
  54. _curator.start();
  55. } catch (Exception ex) {
  56. LOG.error("Couldn't connect to zookeeper", ex);
  57. throw new RuntimeException(ex);
  58. }
  59. }
  60. /**
  61. * Get all partitions with their current leaders
  62. */
  63. public GlobalPartitionInformation getBrokerInfo() throws SocketTimeoutException {
  64. GlobalPartitionInformation globalPartitionInformation = new GlobalPartitionInformation();
  65. try {
  66. int numPartitionsForTopic = getNumPartitions();
  67. String brokerInfoPath = brokerPath();
  68. for (int partition = 0; partition < numPartitionsForTopic; partition++) {
  69. int leader = getLeaderFor(partition);
  70. String path = brokerInfoPath + "/" + leader;
  71. try {
  72. byte[] brokerData = _curator.getData().forPath(path);
  73. Broker hp = getBrokerHost(brokerData);
  74. globalPartitionInformation.addPartition(partition, hp);
  75. } catch (org.apache.zookeeper.KeeperException.NoNodeException e) {
  76. LOG.error("Node {} does not exist ", path);
  77. }
  78. }
  79. } catch (SocketTimeoutException e) {
  80. throw e;
  81. } catch (Exception e) {
  82. throw new RuntimeException(e);
  83. }
  84. LOG.info("Read partition info from zookeeper: " + globalPartitionInformation);
  85. return globalPartitionInformation;
  86. }
  87. private int getNumPartitions() {
  88. try {
  89. String topicBrokersPath = partitionPath();
  90. List<String> children = _curator.getChildren().forPath(topicBrokersPath);
  91. return children.size();
  92. } catch (Exception e) {
  93. throw new RuntimeException(e);
  94. }
  95. }
  96. public String partitionPath() {
  97. return _zkPath + "/topics/" + _topic + "/partitions";
  98. }
  99. public String brokerPath() {
  100. return _zkPath + "/ids";
  101. }
  102. /**
  103. * get /brokers/topics/distributedTopic/partitions/1/state
  104. * { "controller_epoch":4, "isr":[ 1, 0 ], "leader":1, "leader_epoch":1, "version":1 }
  105. *
  106. * @param partition
  107. * @return
  108. */
  109. private int getLeaderFor(long partition) {
  110. try {
  111. String topicBrokersPath = partitionPath();
  112. byte[] hostPortData = _curator.getData().forPath(topicBrokersPath + "/" + partition + "/state");
  113. Map<Object, Object> value = (Map<Object, Object>) JSONValue.parse(new String(hostPortData, "UTF-8"));
  114. Integer leader = ((Number) value.get("leader")).intValue();
  115. if (leader == -1) {
  116. throw new RuntimeException("No leader found for partition " + partition);
  117. }
  118. return leader;
  119. } catch (RuntimeException e) {
  120. throw e;
  121. } catch (Exception e) {
  122. throw new RuntimeException(e);
  123. }
  124. }
  125. public void close() {
  126. _curator.close();
  127. }
  128. /**
  129. * [zk: localhost:2181(CONNECTED) 56] get /brokers/ids/0
  130. * { "host":"localhost", "jmx_port":9999, "port":9092, "version":1 }
  131. *
  132. * @param contents
  133. * @return
  134. */
  135. private Broker getBrokerHost(byte[] contents) {
  136. try {
  137. Map<Object, Object> value = (Map<Object, Object>) JSONValue.parse(new String(contents, "UTF-8"));
  138. String host = (String) value.get("host");
  139. Integer port = ((Long) value.get("port")).intValue();
  140. return new Broker(host, port);
  141. } catch (UnsupportedEncodingException e) {
  142. throw new RuntimeException(e);
  143. }
  144. }
  145. /**
  146. * Validate required parameters in the input configuration Map
  147. * @param conf
  148. */
  149. private void validateConfig(final Map conf) {
  150. Preconditions.checkNotNull(conf.get(Config.STORM_ZOOKEEPER_SESSION_TIMEOUT),
  151. "%s cannot be null", Config.STORM_ZOOKEEPER_SESSION_TIMEOUT);
  152. Preconditions.checkNotNull(conf.get(Config.STORM_ZOOKEEPER_CONNECTION_TIMEOUT),
  153. "%s cannot be null", Config.STORM_ZOOKEEPER_CONNECTION_TIMEOUT);
  154. Preconditions.checkNotNull(conf.get(Config.STORM_ZOOKEEPER_RETRY_TIMES),
  155. "%s cannot be null", Config.STORM_ZOOKEEPER_RETRY_TIMES);
  156. Preconditions.checkNotNull(conf.get(Config.STORM_ZOOKEEPER_RETRY_INTERVAL),
  157. "%s cannot be null", Config.STORM_ZOOKEEPER_RETRY_INTERVAL);
  158. }
  159. }