Browse Source

MINOR: Replace some Java 7 style code with Java 8 style (#7623)

Reviewers: Mickael Maison <mickael.maison@gmail.com>, Manikumar Reddy <manikumar.reddy@gmail.com>
pull/7634/head
Viktor Somogyi 5 years ago committed by Manikumar Reddy
parent
commit
5fa2de43ec
  1. 39
      clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java
  2. 32
      clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java

39
clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java

@ -16,30 +16,26 @@ @@ -16,30 +16,26 @@
*/
package org.apache.kafka.common.metrics;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.utils.Sanitizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.JMException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.utils.Sanitizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Register metrics in JMX as dynamic mbeans based on the metric names
@ -49,7 +45,7 @@ public class JmxReporter implements MetricsReporter { @@ -49,7 +45,7 @@ public class JmxReporter implements MetricsReporter {
private static final Logger log = LoggerFactory.getLogger(JmxReporter.class);
private static final Object LOCK = new Object();
private String prefix;
private final Map<String, KafkaMbean> mbeans = new HashMap<String, KafkaMbean>();
private final Map<String, KafkaMbean> mbeans = new HashMap<>();
public JmxReporter() {
this("");
@ -175,7 +171,7 @@ public class JmxReporter implements MetricsReporter { @@ -175,7 +171,7 @@ public class JmxReporter implements MetricsReporter {
private final ObjectName objectName;
private final Map<String, KafkaMetric> metrics;
public KafkaMbean(String mbeanName) throws MalformedObjectNameException {
KafkaMbean(String mbeanName) throws MalformedObjectNameException {
this.metrics = new HashMap<>();
this.objectName = new ObjectName(mbeanName);
}
@ -184,12 +180,12 @@ public class JmxReporter implements MetricsReporter { @@ -184,12 +180,12 @@ public class JmxReporter implements MetricsReporter {
return objectName;
}
public void setAttribute(String name, KafkaMetric metric) {
void setAttribute(String name, KafkaMetric metric) {
this.metrics.put(name, metric);
}
@Override
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
public Object getAttribute(String name) throws AttributeNotFoundException {
if (this.metrics.containsKey(name))
return this.metrics.get(name).metricValue();
else
@ -209,7 +205,7 @@ public class JmxReporter implements MetricsReporter { @@ -209,7 +205,7 @@ public class JmxReporter implements MetricsReporter {
return list;
}
public KafkaMetric removeAttribute(String name) {
KafkaMetric removeAttribute(String name) {
return this.metrics.remove(name);
}
@ -232,15 +228,12 @@ public class JmxReporter implements MetricsReporter { @@ -232,15 +228,12 @@ public class JmxReporter implements MetricsReporter {
}
@Override
public Object invoke(String name, Object[] params, String[] sig) throws MBeanException, ReflectionException {
public Object invoke(String name, Object[] params, String[] sig) {
throw new UnsupportedOperationException("Set not allowed.");
}
@Override
public void setAttribute(Attribute attribute) throws AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException {
public void setAttribute(Attribute attribute) {
throw new UnsupportedOperationException("Set not allowed.");
}

32
clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java

@ -38,7 +38,6 @@ import java.util.TreeMap; @@ -38,7 +38,6 @@ import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import static java.util.Collections.emptyList;
@ -137,29 +136,20 @@ public class Metrics implements Closeable { @@ -137,29 +136,20 @@ public class Metrics implements Closeable {
this.reporters = Objects.requireNonNull(reporters);
this.time = time;
for (MetricsReporter reporter : reporters)
reporter.init(new ArrayList<KafkaMetric>());
reporter.init(new ArrayList<>());
// Create the ThreadPoolExecutor only if expiration of Sensors is enabled.
if (enableExpiration) {
this.metricsScheduler = new ScheduledThreadPoolExecutor(1);
// Creating a daemon thread to not block shutdown
this.metricsScheduler.setThreadFactory(new ThreadFactory() {
public Thread newThread(Runnable runnable) {
return KafkaThread.daemon("SensorExpiryThread", runnable);
}
});
this.metricsScheduler.setThreadFactory(runnable -> KafkaThread.daemon("SensorExpiryThread", runnable));
this.metricsScheduler.scheduleAtFixedRate(new ExpireSensorTask(), 30, 30, TimeUnit.SECONDS);
} else {
this.metricsScheduler = null;
}
addMetric(metricName("count", "kafka-metrics-count", "total number of registered metrics"),
new Measurable() {
@Override
public double measure(MetricConfig config, long now) {
return metrics.size();
}
});
(config, now) -> metrics.size());
}
/**
@ -186,7 +176,7 @@ public class Metrics implements Closeable { @@ -186,7 +176,7 @@ public class Metrics implements Closeable {
* @param description A human-readable description to include in the metric
*/
public MetricName metricName(String name, String group, String description) {
return metricName(name, group, description, new HashMap<String, String>());
return metricName(name, group, description, new HashMap<>());
}
/**
@ -196,7 +186,7 @@ public class Metrics implements Closeable { @@ -196,7 +186,7 @@ public class Metrics implements Closeable {
* @param group logical group name of the metrics to which this metric belongs
*/
public MetricName metricName(String name, String group) {
return metricName(name, group, "", new HashMap<String, String>());
return metricName(name, group, "", new HashMap<>());
}
/**
@ -227,7 +217,7 @@ public class Metrics implements Closeable { @@ -227,7 +217,7 @@ public class Metrics implements Closeable {
private static Map<String, String> getTags(String... keyValue) {
if ((keyValue.length % 2) != 0)
throw new IllegalArgumentException("keyValue needs to be specified in pairs");
Map<String, String> tags = new LinkedHashMap<String, String>();
Map<String, String> tags = new LinkedHashMap<>();
for (int i = 0; i < keyValue.length; i += 2)
tags.put(keyValue[i], keyValue[i + 1]);
@ -245,7 +235,7 @@ public class Metrics implements Closeable { @@ -245,7 +235,7 @@ public class Metrics implements Closeable {
* @return the string containing the HTML table; never null
*/
public static String toHtmlTable(String domain, Iterable<MetricNameTemplate> allMetrics) {
Map<String, Map<String, String>> beansAndAttributes = new TreeMap<String, Map<String, String>>();
Map<String, Map<String, String>> beansAndAttributes = new TreeMap<>();
try (Metrics metrics = new Metrics()) {
for (MetricNameTemplate template : allMetrics) {
@ -257,7 +247,7 @@ public class Metrics implements Closeable { @@ -257,7 +247,7 @@ public class Metrics implements Closeable {
MetricName metricName = metrics.metricName(template.name(), template.group(), template.description(), tags);
String mBeanName = JmxReporter.getMBeanName(domain, metricName);
if (!beansAndAttributes.containsKey(mBeanName)) {
beansAndAttributes.put(mBeanName, new TreeMap<String, String>());
beansAndAttributes.put(mBeanName, new TreeMap<>());
}
Map<String, String> attrAndDesc = beansAndAttributes.get(mBeanName);
if (!attrAndDesc.containsKey(template.name())) {
@ -405,11 +395,7 @@ public class Metrics implements Closeable { @@ -405,11 +395,7 @@ public class Metrics implements Closeable {
this.sensors.put(name, s);
if (parents != null) {
for (Sensor parent : parents) {
List<Sensor> children = childrenSensors.get(parent);
if (children == null) {
children = new ArrayList<>();
childrenSensors.put(parent, children);
}
List<Sensor> children = childrenSensors.computeIfAbsent(parent, k -> new ArrayList<>());
children.add(s);
}
}

Loading…
Cancel
Save