Skip to content

Commit

Permalink
HDFS-16547. [SBN read] Namenode in safe mode should not be transfer t…
Browse files Browse the repository at this point in the history
…o observer state
  • Loading branch information
tomscut committed Apr 19, 2022
1 parent ec0ff1d commit 0ed4aa1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ && isInStartupSafeMode()) {
}
}

private SafeModeException newSafemodeException(String errorMsg) {
public SafeModeException newSafemodeException(String errorMsg) {
return new SafeModeException(errorMsg + ". Name node is in safe " +
"mode.\n" + getSafeModeTip() + " NamenodeHostName:" + nameNodeHostName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,10 @@ synchronized void transitionToStandby() throws IOException {
synchronized void transitionToObserver() throws IOException {
String operationName = "transitionToObserver";
namesystem.checkSuperuserPrivilege(operationName);
if (namesystem.isInSafeMode()) {
throw namesystem.newSafemodeException("Cannot transition to " +
OBSERVER_STATE);
}
if (!haEnabled) {
throw new ServiceFailedException("HA for namenode is not enabled");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,13 @@ private int transitionToObserver(final CommandLine cmd)
if (!checkManualStateManagementOK(target)) {
return -1;
}
HAServiceProtocol proto = target.getProxy(getConf(), 0);
HAServiceProtocolHelper.transitionToObserver(proto, createReqInfo());
try {
HAServiceProtocol proto = target.getProxy(getConf(), 0);
HAServiceProtocolHelper.transitionToObserver(proto, createReqInfo());
} catch (IOException e) {
errOut.println("TransitionToObserver failed! " + e.getMessage());
return -1;
}
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_SERVICE_RPC_BIND_HOST_KEY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
Expand All @@ -40,9 +41,11 @@
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.hdfs.MiniDFSNNTopology;
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
import org.apache.hadoop.hdfs.server.namenode.EditLogFileOutputStream;
import org.apache.hadoop.hdfs.server.namenode.MockNameNodeResourceChecker;
import org.apache.hadoop.hdfs.server.namenode.NameNode;
import org.apache.hadoop.hdfs.server.namenode.SafeModeException;
import org.apache.hadoop.hdfs.server.namenode.ha.HATestUtil;
import org.apache.hadoop.hdfs.server.protocol.NamenodeProtocols;
import org.apache.hadoop.net.ServerSocketUtil;
Expand Down Expand Up @@ -301,6 +304,40 @@ public void testManualFailoverWithDFSHAAdmin() throws Exception {
waitForHAState(1, HAServiceState.STANDBY);
}

/**
* Tests that a Namenode in safe mode should not be transfer to observer state.
*/
@Test
public void testManualFailoverWithDFSHAAdminInSafemode() throws Exception {
startCluster();
NamenodeProtocols nn1 = cluster.getNameNode(1).getRpcServer();

// Enter safe mode.
nn1.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_ENTER, false);
// Test NameNodeRpcServer.
LambdaTestUtils.intercept(SafeModeException.class,
"Cannot transition to observer. Name node is in safe mode",
() -> nn1.transitionToObserver(
new StateChangeRequestInfo(RequestSource.REQUEST_BY_USER_FORCED)));

// Test DFSHAAdmin.
DFSHAAdmin tool = new DFSHAAdmin();
tool.setConf(conf);
System.setIn(new ByteArrayInputStream("yes\n".getBytes()));
int result = tool.run(
new String[]{"-transitionToObserver", "-forcemanual", "nn2"});
assertEquals("State transition returned: " + result, -1, result);

// Leave safe mode.
nn1.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE, false);
// Just need to test DFSHAAdmin.
System.setIn(new ByteArrayInputStream("yes\n".getBytes()));
int result1 = tool.run(
new String[]{"-transitionToObserver", "-forcemanual", "nn2"});
assertEquals("State transition returned: " + result1, 0, result1);
assertFalse(cluster.getNameNode(1).isInSafeMode());
}

@Test(timeout=30000)
public void testElectionOnObserver() throws Exception{
startCluster();
Expand Down

0 comments on commit 0ed4aa1

Please sign in to comment.