Source:
private void monitorMetadata() { if (isAlive && (metadataRefreshRetryLimit == -1 || retryCounter.get() < metadataRefreshRetryLimit)) { logger.debug("{}: Update metadata for topic {}", Thread.currentThread().getName(), topic); Map<String, List<PartitionMetadata>> pms = KafkaMetadataUtil.getPartitionsForTopic(brokers, topic); if (pms == null) { retryCounter.getAndAdd(1); return; } for (Entry<String, List<PartitionMetadata>> pmLEntry : pms.entrySet()) { if (pmLEntry.getValue() == null) continue; for (PartitionMetadata pm : pmLEntry.getValue()) { KafkaPartition kp = new KafkaPartition(pmLEntry.getKey(), topic, pm.partitionId()); if (!kps.contains(kp)) { continue; } Broker b = pm.leader(); if (b == null) { continue; } Broker oldB = partitionToBroker.put(kp, b); if (b.equals(oldB)) { continue; } deltaPositive.put(b, kp); stats.updatePartitionStats(kp, pm.leader().id(), pm.leader().host() + ":" + pm.leader().port()); } } for (Iterator<Entry<Broker, ConsumerThread>> iterator = simpleConsumerThreads.entrySet().iterator(); iterator.hasNext(); ) { Entry<Broker, ConsumerThread> item = iterator.next(); if (item.getValue().getThreadItSelf().isDone()) { iterator.remove(); } } for (Broker b : deltaPositive.keySet()) { if (!simpleConsumerThreads.containsKey(b)) { ConsumerThread ct = new ConsumerThread(b, deltaPositive.get(b), ref); ct.setThreadItSelf(kafkaConsumerExecutor.submit(ct)); simpleConsumerThreads.put(b, ct); } else { simpleConsumerThreads.get(b).addPartitions(deltaPositive.get(b)); } } deltaPositive.clear(); retryCounter.set(0); } }

Target:
private void monitorMetadata() { if (isAlive && (metadataRefreshRetryLimit == -1 || retryCounter.get() < metadataRefreshRetryLimit)) { logger.debug("{}: Update metadata for topic {}", Thread.currentThread().getName(), topic); Map<String, List<PartitionMetadata>> pms = KafkaMetadataUtil.getPartitionsForTopic(brokers, topic); if (pms == null) { retryCounter.getAndAdd(1); return; } for (Entry<String, List<PartitionMetadata>> pmLEntry : pms.entrySet()) { if (pmLEntry.getValue() == null) continue; for (PartitionMetadata pm : pmLEntry.getValue()) { KafkaPartition kp = new KafkaPartition(pmLEntry.getKey(), topic, pm.partitionId()); if (!kps.contains(kp)) { continue; } Broker b = pm.leader(); if (b == null) { logger.info("No Leader broker for Kafka Partition {}. Skipping it for time until new leader is elected", kp.getPartitionId()); continue; } Broker oldB = partitionToBroker.put(kp, b); if (b.equals(oldB)) { continue; } deltaPositive.put(b, kp); stats.updatePartitionStats(kp, pm.leader().id(), pm.leader().host() + ":" + pm.leader().port()); } } for (Iterator<Entry<Broker, ConsumerThread>> iterator = simpleConsumerThreads.entrySet().iterator(); iterator.hasNext(); ) { Entry<Broker, ConsumerThread> item = iterator.next(); if (item.getValue().getThreadItSelf().isDone()) { iterator.remove(); } } for (Broker b : deltaPositive.keySet()) { if (!simpleConsumerThreads.containsKey(b)) { ConsumerThread ct = new ConsumerThread(b, deltaPositive.get(b), ref); ct.setThreadItSelf(kafkaConsumerExecutor.submit(ct)); simpleConsumerThreads.put(b, ct); } else { simpleConsumerThreads.get(b).addPartitions(deltaPositive.get(b)); } } deltaPositive.clear(); retryCounter.set(0); } }