Jelajahi Sumber

HADOOP-16345. Fix a potential NPE when instantiating FairCallQueue metrics. Contributed by Erik Krogen.

Erik Krogen 5 tahun lalu
induk
melakukan
76b94c274f

+ 14 - 2
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/FairCallQueue.java

@@ -377,9 +377,21 @@ public class FairCallQueue<E extends Schedulable> extends AbstractQueue<E>
       this.revisionNumber++;
     }
 
+    /**
+     * Fetch the current call queue from the weak reference delegate. If there
+     * is no delegate, or the delegate is empty, this will return null.
+     */
+    private FairCallQueue<? extends Schedulable> getCallQueue() {
+      WeakReference<FairCallQueue<? extends Schedulable>> ref = this.delegate;
+      if (ref == null) {
+        return null;
+      }
+      return ref.get();
+    }
+
     @Override
     public int[] getQueueSizes() {
-      FairCallQueue<? extends Schedulable> obj = this.delegate.get();
+      FairCallQueue<? extends Schedulable> obj = getCallQueue();
       if (obj == null) {
         return new int[]{};
       }
@@ -389,7 +401,7 @@ public class FairCallQueue<E extends Schedulable> extends AbstractQueue<E>
 
     @Override
     public long[] getOverflowedCalls() {
-      FairCallQueue<? extends Schedulable> obj = this.delegate.get();
+      FairCallQueue<? extends Schedulable> obj = getCallQueue();
       if (obj == null) {
         return new long[]{};
       }