You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by ay...@apache.org on 2019/12/05 13:32:15 UTC

[hadoop] branch trunk updated: HDFS-15023. [SBN read] ZKFC should check the state before joining the election. Contributed by Fei Hui.

This is an automated email from the ASF dual-hosted git repository.

ayushsaxena pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 83a1455  HDFS-15023. [SBN read] ZKFC should check the state before joining the election. Contributed by Fei Hui.
83a1455 is described below

commit 83a14559e594b0e918d04cafd8c7c6ac57715b22
Author: Ayush Saxena <ay...@apache.org>
AuthorDate: Thu Dec 5 18:52:01 2019 +0530

    HDFS-15023. [SBN read] ZKFC should check the state before joining the election. Contributed by Fei Hui.
---
 .../org/apache/hadoop/ha/ActiveStandbyElector.java |  5 ++++
 .../org/apache/hadoop/ha/ZKFailoverController.java | 11 +++++--
 .../hdfs/tools/TestDFSZKFailoverController.java    | 34 ++++++++++++++++++++++
 3 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ActiveStandbyElector.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ActiveStandbyElector.java
index 12de2ef..828a17b 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ActiveStandbyElector.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ActiveStandbyElector.java
@@ -579,6 +579,11 @@ public class ActiveStandbyElector implements StatCallback, StringCallback {
     fatalError(errorMessage);
   }
 
+  @VisibleForTesting
+  public boolean getWantToBeInElection() {
+    return wantToBeInElection;
+  }
+
   /**
    * We failed to become active. Re-join the election, but
    * sleep for a few seconds after terminating our existing
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ZKFailoverController.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ZKFailoverController.java
index ee4ca1a..943d53d 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ZKFailoverController.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ZKFailoverController.java
@@ -157,7 +157,10 @@ public abstract class ZKFailoverController {
     return localTarget;
   }
 
-  HAServiceState getServiceState() { return serviceState; }
+  @VisibleForTesting
+  public HAServiceState getServiceState() {
+    return serviceState;
+  }
 
   public int run(final String[] args) throws Exception {
     if (!localTarget.isAutoFailoverEnabled()) {
@@ -799,7 +802,9 @@ public abstract class ZKFailoverController {
     
         switch (lastHealthState) {
         case SERVICE_HEALTHY:
-          elector.joinElection(targetToData(localTarget));
+          if(serviceState != HAServiceState.OBSERVER) {
+            elector.joinElection(targetToData(localTarget));
+          }
           if (quitElectionOnBadState) {
             quitElectionOnBadState = false;
           }
@@ -909,7 +914,7 @@ public abstract class ZKFailoverController {
   }
   
   @VisibleForTesting
-  ActiveStandbyElector getElectorForTests() {
+  public ActiveStandbyElector getElectorForTests() {
     return elector;
   }
   
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSZKFailoverController.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSZKFailoverController.java
index 8ff68dc..6e88196 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSZKFailoverController.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSZKFailoverController.java
@@ -253,6 +253,32 @@ public class TestDFSZKFailoverController extends ClientBaseWithFixes {
     waitForHAState(1, HAServiceState.STANDBY);
   }
 
+  @Test(timeout=30000)
+  public void testElectionOnObserver() throws Exception{
+    InputStream inOriginial = System.in;
+    try {
+      DFSHAAdmin tool = new DFSHAAdmin();
+      tool.setConf(conf);
+
+      // Transition nn2 to Observer
+      System.setIn(new ByteArrayInputStream("yes\n".getBytes()));
+      int result = tool.run(
+          new String[]{"-transitionToObserver", "-forcemanual", "nn2"});
+      assertEquals("State transition returned: " + result, 0, result);
+      waitForHAState(1, HAServiceState.OBSERVER);
+      waitForZKFCState(thr2.zkfc, HAServiceState.OBSERVER);
+
+      // Call recheckElectability
+      thr2.zkfc.getLocalTarget().getZKFCProxy(conf, 15000).cedeActive(-1);
+
+      // This namenode is in observer state, it shouldn't join election
+      assertEquals(false,
+          thr2.zkfc.getElectorForTests().getWantToBeInElection());
+    } finally {
+      System.setIn(inOriginial);
+    }
+  }
+
   private void waitForHAState(int nnidx, final HAServiceState state)
       throws TimeoutException, InterruptedException {
     final NameNode nn = cluster.getNameNode(nnidx);
@@ -269,6 +295,14 @@ public class TestDFSZKFailoverController extends ClientBaseWithFixes {
     }, 50, 15000);
   }
 
+  private void waitForZKFCState(DFSZKFailoverController zkfc,
+      final HAServiceState state)
+      throws TimeoutException, InterruptedException{
+    GenericTestUtils.waitFor(
+        () -> zkfc.getServiceState() == state,
+        50, 15000);
+  }
+
   /**
    * Test-thread which runs a ZK Failover Controller corresponding
    * to a given NameNode in the minicluster.


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org