Przeglądaj źródła

HDFS-14279. [SBN read] Fix race condition in ObserverReadProxyProvider. Contributed by Erik Krogen.

Erik Krogen 6 lat temu
rodzic
commit
2fe15053ab

+ 5 - 9
hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/ObserverReadProxyProvider.java

@@ -233,10 +233,7 @@ public class ObserverReadProxyProvider<T extends ClientProtocol>
    * {@link #changeProxy(NNProxyInfo)} to initialize one.
    * {@link #changeProxy(NNProxyInfo)} to initialize one.
    */
    */
   private NNProxyInfo<T> getCurrentProxy() {
   private NNProxyInfo<T> getCurrentProxy() {
-    if (currentProxy == null) {
-      changeProxy(null);
-    }
-    return currentProxy;
+    return changeProxy(null);
   }
   }
 
 
   /**
   /**
@@ -247,21 +244,20 @@ public class ObserverReadProxyProvider<T extends ClientProtocol>
    * returning.
    * returning.
    *
    *
    * @param initial The expected current proxy
    * @param initial The expected current proxy
+   * @return The new proxy that should be used.
    */
    */
-  private synchronized void changeProxy(NNProxyInfo<T> initial) {
+  private synchronized NNProxyInfo<T> changeProxy(NNProxyInfo<T> initial) {
     if (currentProxy != initial) {
     if (currentProxy != initial) {
       // Must have been a concurrent modification; ignore the move request
       // Must have been a concurrent modification; ignore the move request
-      return;
+      return currentProxy;
     }
     }
-    // Attempt to force concurrent callers of getCurrentProxy to wait for the
-    // new proxy; best-effort by setting currentProxy to null
-    currentProxy = null;
     currentIndex = (currentIndex + 1) % nameNodeProxies.size();
     currentIndex = (currentIndex + 1) % nameNodeProxies.size();
     currentProxy = createProxyIfNeeded(nameNodeProxies.get(currentIndex));
     currentProxy = createProxyIfNeeded(nameNodeProxies.get(currentIndex));
     currentProxy.setCachedState(getHAServiceState(currentProxy));
     currentProxy.setCachedState(getHAServiceState(currentProxy));
     LOG.debug("Changed current proxy from {} to {}",
     LOG.debug("Changed current proxy from {} to {}",
         initial == null ? "none" : initial.proxyInfo,
         initial == null ? "none" : initial.proxyInfo,
         currentProxy.proxyInfo);
         currentProxy.proxyInfo);
+    return currentProxy;
   }
   }
 
 
   /**
   /**