Search in sources :

Example 11 with IMetric

use of org.apache.storm.metric.api.IMetric in project heron by twitter.

the class KafkaSpout method open.

@Override
@SuppressWarnings("unchecked")
public void open(Map<String, Object> conf, final TopologyContext context, final SpoutOutputCollector aCollector) {
    collector = aCollector;
    String topologyInstanceId = context.getStormId();
    Map<String, Object> stateConf = new HashMap<>(conf);
    List<String> zkServers = spoutConfig.zkServers;
    if (zkServers == null) {
        zkServers = (List<String>) conf.get(Config.STORM_ZOOKEEPER_SERVERS);
    }
    Integer zkPort = spoutConfig.zkPort;
    if (zkPort == null) {
        zkPort = ((Number) conf.get(Config.STORM_ZOOKEEPER_PORT)).intValue();
    }
    stateConf.put(Config.TRANSACTIONAL_ZOOKEEPER_SERVERS, zkServers);
    stateConf.put(Config.TRANSACTIONAL_ZOOKEEPER_PORT, zkPort);
    stateConf.put(Config.TRANSACTIONAL_ZOOKEEPER_ROOT, spoutConfig.zkRoot);
    state = new ZkState(stateConf);
    connections = new DynamicPartitionConnections(spoutConfig, KafkaUtils.makeBrokerReader(conf, spoutConfig));
    // using TransactionalState like this is a hack
    int totalTasks = context.getComponentTasks(context.getThisComponentId()).size();
    if (spoutConfig.hosts instanceof StaticHosts) {
        coordinator = new StaticCoordinator(connections, conf, spoutConfig, state, context.getThisTaskIndex(), totalTasks, topologyInstanceId);
    } else {
        coordinator = new ZkCoordinator(connections, conf, spoutConfig, state, context.getThisTaskIndex(), totalTasks, topologyInstanceId);
    }
    context.registerMetric("kafkaOffset", new IMetric() {

        private KafkaUtils.KafkaOffsetMetric kafkaOffsetMetric = new KafkaUtils.KafkaOffsetMetric(connections);

        @Override
        public Object getValueAndReset() {
            List<PartitionManager> pms = coordinator.getMyManagedPartitions();
            Set<Partition> latestPartitions = new HashSet<>();
            for (PartitionManager pm : pms) {
                latestPartitions.add(pm.getPartition());
            }
            kafkaOffsetMetric.refreshPartitions(latestPartitions);
            for (PartitionManager pm : pms) {
                kafkaOffsetMetric.setOffsetData(pm.getPartition(), pm.getOffsetData());
            }
            return kafkaOffsetMetric.getValueAndReset();
        }
    }, spoutConfig.metricsTimeBucketSizeInSecs);
    context.registerMetric("kafkaPartition", new IMetric() {

        @Override
        public Object getValueAndReset() {
            List<PartitionManager> pms = coordinator.getMyManagedPartitions();
            Map<String, Object> concatMetricsDataMaps = new HashMap<>();
            for (PartitionManager pm : pms) {
                concatMetricsDataMaps.putAll(pm.getMetricsDataMap());
            }
            return concatMetricsDataMaps;
        }
    }, spoutConfig.metricsTimeBucketSizeInSecs);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) IMetric(org.apache.storm.metric.api.IMetric) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IMetric (org.apache.storm.metric.api.IMetric)11 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IStatefulObject (org.apache.storm.metric.api.IStatefulObject)2 IShellMetric (org.apache.storm.metric.api.rpc.IShellMetric)2 IOException (java.io.IOException)1 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 RuntimeMXBean (java.lang.management.RuntimeMXBean)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 IMetricsConsumer (org.apache.storm.metric.api.IMetricsConsumer)1 StateMetric (org.apache.storm.metric.api.StateMetric)1 MultiCountStatAndMetric (org.apache.storm.metric.internal.MultiCountStatAndMetric)1 MultiLatencyStatAndMetric (org.apache.storm.metric.internal.MultiLatencyStatAndMetric)1 ReportedFailedException (org.apache.storm.topology.ReportedFailedException)1 Values (org.apache.storm.tuple.Values)1