HADOOP-13290. Appropriate use of generics in FairCallQueue. Contributed by Jonathan Hung.

This commit is contained in:
Zhe Zhang 2016-07-13 16:37:40 -07:00
parent d18050522c
commit 728bf7f698
2 changed files with 28 additions and 8 deletions

View File

@ -300,7 +300,7 @@ public E peek() {
@Override
public int size() {
int size = 0;
for (BlockingQueue q : this.queues) {
for (BlockingQueue<E> q : this.queues) {
size += q.size();
}
return size;
@ -346,7 +346,7 @@ public int drainTo(Collection<? super E> c) {
@Override
public int remainingCapacity() {
int sum = 0;
for (BlockingQueue q : this.queues) {
for (BlockingQueue<E> q : this.queues) {
sum += q.remainingCapacity();
}
return sum;
@ -362,7 +362,7 @@ private static final class MetricsProxy implements FairCallQueueMXBean {
new HashMap<String, MetricsProxy>();
// Weakref for delegate, so we don't retain it forever if it can be GC'd
private WeakReference<FairCallQueue> delegate;
private WeakReference<FairCallQueue<? extends Schedulable>> delegate;
// Keep track of how many objects we registered
private int revisionNumber = 0;
@ -381,14 +381,15 @@ public static synchronized MetricsProxy getInstance(String namespace) {
return mp;
}
public void setDelegate(FairCallQueue obj) {
this.delegate = new WeakReference<FairCallQueue>(obj);
public void setDelegate(FairCallQueue<? extends Schedulable> obj) {
this.delegate
= new WeakReference<FairCallQueue<? extends Schedulable>>(obj);
this.revisionNumber++;
}
@Override
public int[] getQueueSizes() {
FairCallQueue obj = this.delegate.get();
FairCallQueue<? extends Schedulable> obj = this.delegate.get();
if (obj == null) {
return new int[]{};
}
@ -398,7 +399,7 @@ public int[] getQueueSizes() {
@Override
public long[] getOverflowedCalls() {
FairCallQueue obj = this.delegate.get();
FairCallQueue<? extends Schedulable> obj = this.delegate.get();
if (obj == null) {
return new long[]{};
}

View File

@ -23,6 +23,9 @@
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 void testTakeTriesNextQueue() throws InterruptedException {
assertEquals(call, fcq.take());
assertEquals(0, fcq.size());
}
}
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]);
}
}