From 728bf7f6988e13f17f03d2e3a4037b7238c4fd45 Mon Sep 17 00:00:00 2001 From: Zhe Zhang Date: Wed, 13 Jul 2016 16:37:40 -0700 Subject: [PATCH] HADOOP-13290. Appropriate use of generics in FairCallQueue. Contributed by Jonathan Hung. --- .../org/apache/hadoop/ipc/FairCallQueue.java | 15 ++++++------- .../apache/hadoop/ipc/TestFairCallQueue.java | 21 ++++++++++++++++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/FairCallQueue.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/FairCallQueue.java index 38b196dc808..c2d3cd80ca7 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/FairCallQueue.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/FairCallQueue.java @@ -300,7 +300,7 @@ public class FairCallQueue extends AbstractQueue @Override public int size() { int size = 0; - for (BlockingQueue q : this.queues) { + for (BlockingQueue q : this.queues) { size += q.size(); } return size; @@ -346,7 +346,7 @@ public class FairCallQueue extends AbstractQueue @Override public int remainingCapacity() { int sum = 0; - for (BlockingQueue q : this.queues) { + for (BlockingQueue q : this.queues) { sum += q.remainingCapacity(); } return sum; @@ -362,7 +362,7 @@ public class FairCallQueue extends AbstractQueue new HashMap(); // Weakref for delegate, so we don't retain it forever if it can be GC'd - private WeakReference delegate; + private WeakReference> delegate; // Keep track of how many objects we registered private int revisionNumber = 0; @@ -381,14 +381,15 @@ public class FairCallQueue extends AbstractQueue return mp; } - public void setDelegate(FairCallQueue obj) { - this.delegate = new WeakReference(obj); + public void setDelegate(FairCallQueue obj) { + this.delegate + = new WeakReference>(obj); this.revisionNumber++; } @Override public int[] getQueueSizes() { - FairCallQueue obj = this.delegate.get(); + FairCallQueue obj = this.delegate.get(); if (obj == null) { return new int[]{}; } @@ -398,7 +399,7 @@ public class FairCallQueue extends AbstractQueue @Override public long[] getOverflowedCalls() { - FairCallQueue obj = this.delegate.get(); + FairCallQueue obj = this.delegate.get(); if (obj == null) { return new long[]{}; } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestFairCallQueue.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestFairCallQueue.java index 8c96c2e500a..d5e5f90b2f1 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestFairCallQueue.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestFairCallQueue.java @@ -23,6 +23,9 @@ import static org.mockito.Mockito.when; import junit.framework.TestCase; +import javax.management.MBeanServer; +import javax.management.ObjectName; +import java.lang.management.ManagementFactory; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.BlockingQueue; @@ -392,4 +395,20 @@ public class TestFairCallQueue extends TestCase { assertEquals(call, fcq.take()); assertEquals(0, fcq.size()); } -} \ No newline at end of file + + public void testFairCallQueueMXBean() throws Exception { + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + ObjectName mxbeanName = new ObjectName( + "Hadoop:service=ns,name=FairCallQueue"); + + Schedulable call = mockCall("c"); + fcq.put(call); + int[] queueSizes = (int[]) mbs.getAttribute(mxbeanName, "QueueSizes"); + assertEquals(1, queueSizes[0]); + assertEquals(0, queueSizes[1]); + fcq.take(); + queueSizes = (int[]) mbs.getAttribute(mxbeanName, "QueueSizes"); + assertEquals(0, queueSizes[0]); + assertEquals(0, queueSizes[1]); + } +}