-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-7136: Avoid deadlocks in synchronized metrics reporters #5341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
guozhangwang
merged 2 commits into
apache:trunk
from
rajinisivaram:KAFKA-7136-metrics-deadlock
Jul 6, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,13 +26,16 @@ | |
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Deque; | ||
| import java.util.List; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Random; | ||
| import java.util.concurrent.ConcurrentLinkedDeque; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import org.apache.kafka.common.Metric; | ||
|
|
@@ -54,9 +57,12 @@ | |
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
| public class MetricsTest { | ||
| private static final Logger log = LoggerFactory.getLogger(MetricsTest.class); | ||
|
|
||
| private static final double EPS = 0.000001; | ||
| private MockTime time = new MockTime(); | ||
|
|
@@ -604,25 +610,21 @@ public void testMetricInstances() { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that concurrent sensor add, remove, updates and read don't result | ||
| * in errors or deadlock. | ||
| */ | ||
| @Test | ||
| public void testConcurrentAccess() throws Exception { | ||
| public void testConcurrentReadUpdate() throws Exception { | ||
| final Random random = new Random(); | ||
| final Deque<Sensor> sensors = new ConcurrentLinkedDeque<>(); | ||
| metrics = new Metrics(new MockTime(10)); | ||
| SensorCreator sensorCreator = new SensorCreator(metrics); | ||
|
|
||
| final AtomicBoolean alive = new AtomicBoolean(true); | ||
| executorService = Executors.newSingleThreadExecutor(); | ||
| executorService.submit(new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| while (alive.get()) { | ||
| for (Sensor sensor : sensors) { | ||
| sensor.record(random.nextInt(10000)); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| executorService.submit(new ConcurrentMetricOperation(alive, "record", | ||
| () -> sensors.forEach(sensor -> sensor.record(random.nextInt(10000))))); | ||
|
|
||
| for (int i = 0; i < 10000; i++) { | ||
| if (sensors.size() > 5) { | ||
|
|
@@ -640,6 +642,97 @@ public void run() { | |
| alive.set(false); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that concurrent sensor add, remove, updates and read with a metrics reporter | ||
| * that synchronizes on every reporter method doesn't result in errors or deadlock. | ||
| */ | ||
| @Test | ||
| public void testConcurrentReadUpdateReport() throws Exception { | ||
|
|
||
| class LockingReporter implements MetricsReporter { | ||
| Map<MetricName, KafkaMetric> activeMetrics = new HashMap<>(); | ||
| @Override | ||
| public synchronized void init(List<KafkaMetric> metrics) { | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void metricChange(KafkaMetric metric) { | ||
| activeMetrics.put(metric.metricName(), metric); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void metricRemoval(KafkaMetric metric) { | ||
| activeMetrics.remove(metric.metricName(), metric); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
| } | ||
|
|
||
| @Override | ||
| public void configure(Map<String, ?> configs) { | ||
| } | ||
|
|
||
| synchronized void processMetrics() { | ||
| for (KafkaMetric metric : activeMetrics.values()) { | ||
| assertNotNull("Invalid metric value", metric.metricValue()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final LockingReporter reporter = new LockingReporter(); | ||
| this.metrics.close(); | ||
| this.metrics = new Metrics(config, Arrays.asList((MetricsReporter) reporter), new MockTime(10), true); | ||
| final Deque<Sensor> sensors = new ConcurrentLinkedDeque<>(); | ||
| SensorCreator sensorCreator = new SensorCreator(metrics); | ||
|
|
||
| final Random random = new Random(); | ||
| final AtomicBoolean alive = new AtomicBoolean(true); | ||
| executorService = Executors.newFixedThreadPool(3); | ||
|
|
||
| Future<?> writeFuture = executorService.submit(new ConcurrentMetricOperation(alive, "record", | ||
| () -> sensors.forEach(sensor -> sensor.record(random.nextInt(10000))))); | ||
| Future<?> readFuture = executorService.submit(new ConcurrentMetricOperation(alive, "read", | ||
| () -> sensors.forEach(sensor -> sensor.metrics().forEach(metric -> | ||
| assertNotNull("Invalid metric value", metric.metricValue()))))); | ||
| Future<?> reportFuture = executorService.submit(new ConcurrentMetricOperation(alive, "report", | ||
| () -> reporter.processMetrics())); | ||
|
|
||
| for (int i = 0; i < 10000; i++) { | ||
| if (sensors.size() > 10) { | ||
| Sensor sensor = random.nextBoolean() ? sensors.removeFirst() : sensors.removeLast(); | ||
| metrics.removeSensor(sensor.name()); | ||
| } | ||
| StatType statType = StatType.forId(random.nextInt(StatType.values().length)); | ||
| sensors.add(sensorCreator.createSensor(statType, i)); | ||
| } | ||
| assertFalse("Read failed", readFuture.isDone()); | ||
| assertFalse("Write failed", writeFuture.isDone()); | ||
| assertFalse("Report failed", reportFuture.isDone()); | ||
|
|
||
| alive.set(false); | ||
| } | ||
|
|
||
| private class ConcurrentMetricOperation implements Runnable { | ||
| private final AtomicBoolean alive; | ||
| private final String opName; | ||
| private final Runnable op; | ||
| ConcurrentMetricOperation(AtomicBoolean alive, String opName, Runnable op) { | ||
| this.alive = alive; | ||
| this.opName = opName; | ||
| this.op = op; | ||
| } | ||
| public void run() { | ||
| try { | ||
| while (alive.get()) { | ||
| op.run(); | ||
| } | ||
| } catch (Throwable t) { | ||
| log.error("Metric {} failed with exception", opName, t); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we get an exception, should we fail the test?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test fails if there is an exception since it verifies that the task is still running. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| enum StatType { | ||
| AVG(0), | ||
| TOTAL(1), | ||
|
|
@@ -676,7 +769,7 @@ private static class SensorCreator { | |
| } | ||
|
|
||
| private Sensor createSensor(StatType statType, int index) { | ||
| Sensor sensor = metrics.sensor("kafka.requests"); | ||
| Sensor sensor = metrics.sensor("kafka.requests." + index); | ||
| Map<String, String> tags = Collections.singletonMap("tag", "tag" + index); | ||
| switch (statType) { | ||
| case AVG: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: do we still need this function? Could we just reference the
metricLockobject directly? Since it is private my understanding is that it was not intended to be used outside this class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@guozhangwang Thanks for the review. I left the method in since it gives a good place to document :-) I think the method would get optimized away at runtime anyway. I don't mind changing it if you think it may be confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. MVN then :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can document the field too, right? If there's no reason to have a method, I'd remove it.