Author: todd
Date: Sat Mar 24 23:48:17 2012
New Revision: 1304968
URL: http://svn.apache.org/viewvc?rev=1304968&view=rev
Log:
HADOOP-8193. Refactor FailoverController/HAAdmin code to add an abstract class
for "target" services. Contributed by Todd Lipcon.
Added:
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/NNHAServiceTarget.java
Modified:
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSHAAdmin.java
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSHAAdmin.java
Modified:
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSHAAdmin.java
URL:
http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSHAAdmin.java?rev=1304968&r1=1304967&r2=1304968&view=diff
==============================================================================
---
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSHAAdmin.java
(original)
+++
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSHAAdmin.java
Sat Mar 24 23:48:17 2012
@@ -25,8 +25,8 @@ import org.apache.commons.logging.LogFac
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.ha.HAAdmin;
+import org.apache.hadoop.ha.HAServiceTarget;
import org.apache.hadoop.hdfs.DFSConfigKeys;
-import org.apache.hadoop.hdfs.DFSUtil;
import org.apache.hadoop.hdfs.HdfsConfiguration;
import org.apache.hadoop.util.ToolRunner;
@@ -65,15 +65,9 @@ public class DFSHAAdmin extends HAAdmin
* Try to map the given namenode ID to its service address.
*/
@Override
- protected String getServiceAddr(String nnId) {
+ protected HAServiceTarget resolveTarget(String nnId) {
HdfsConfiguration conf = (HdfsConfiguration)getConf();
- String serviceAddr =
- DFSUtil.getNamenodeServiceAddr(conf, nameserviceId, nnId);
- if (serviceAddr == null) {
- throw new IllegalArgumentException(
- "Unable to determine service address for namenode '" + nnId + "'");
- }
- return serviceAddr;
+ return new NNHAServiceTarget(conf, nameserviceId, nnId);
}
@Override
Added:
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/NNHAServiceTarget.java
URL:
http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/NNHAServiceTarget.java?rev=1304968&view=auto
==============================================================================
---
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/NNHAServiceTarget.java
(added)
+++
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/NNHAServiceTarget.java
Sat Mar 24 23:48:17 2012
@@ -0,0 +1,84 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hdfs.tools;
+
+import java.net.InetSocketAddress;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.ha.BadFencingConfigurationException;
+import org.apache.hadoop.ha.HAServiceTarget;
+import org.apache.hadoop.ha.NodeFencer;
+import org.apache.hadoop.hdfs.DFSUtil;
+import org.apache.hadoop.hdfs.HdfsConfiguration;
+import org.apache.hadoop.hdfs.server.namenode.NameNode;
+import org.apache.hadoop.net.NetUtils;
+
+/**
+ * One of the NN NameNodes acting as the target of an administrative command
+ * (e.g. failover).
+ */
[email protected]
+public class NNHAServiceTarget extends HAServiceTarget {
+
+ private final InetSocketAddress addr;
+ private NodeFencer fencer;
+ private BadFencingConfigurationException fenceConfigError;
+
+ public NNHAServiceTarget(HdfsConfiguration conf,
+ String nsId, String nnId) {
+ String serviceAddr =
+ DFSUtil.getNamenodeServiceAddr(conf, nsId, nnId);
+ if (serviceAddr == null) {
+ throw new IllegalArgumentException(
+ "Unable to determine service address for namenode '" + nnId + "'");
+ }
+ this.addr = NetUtils.createSocketAddr(serviceAddr,
+ NameNode.DEFAULT_PORT);
+ try {
+ this.fencer = NodeFencer.create(conf);
+ } catch (BadFencingConfigurationException e) {
+ this.fenceConfigError = e;
+ }
+ }
+
+ /**
+ * @return the NN's IPC address.
+ */
+ @Override
+ public InetSocketAddress getAddress() {
+ return addr;
+ }
+
+ @Override
+ public void checkFencingConfigured() throws BadFencingConfigurationException
{
+ if (fenceConfigError != null) {
+ throw fenceConfigError;
+ }
+ }
+
+ @Override
+ public NodeFencer getFencer() {
+ return fencer;
+ }
+
+ @Override
+ public String toString() {
+ return "NameNode at " + addr;
+ }
+
+}
Modified:
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSHAAdmin.java
URL:
http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSHAAdmin.java?rev=1304968&r1=1304967&r2=1304968&view=diff
==============================================================================
---
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSHAAdmin.java
(original)
+++
hadoop/common/branches/branch-0.23/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSHAAdmin.java
Sat Mar 24 23:48:17 2012
@@ -32,6 +32,7 @@ import org.apache.hadoop.hdfs.HdfsConfig
import org.apache.hadoop.ha.HAServiceProtocol;
import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState;
import org.apache.hadoop.ha.HAServiceStatus;
+import org.apache.hadoop.ha.HAServiceTarget;
import org.apache.hadoop.ha.HealthCheckFailedException;
import org.apache.hadoop.ha.NodeFencer;
@@ -79,10 +80,18 @@ public class TestDFSHAAdmin {
public void setup() throws IOException {
mockProtocol = Mockito.mock(HAServiceProtocol.class);
tool = new DFSHAAdmin() {
+
@Override
- protected HAServiceProtocol getProtocol(String serviceId) throws
IOException {
- getServiceAddr(serviceId);
- return mockProtocol;
+ protected HAServiceTarget resolveTarget(String nnId) {
+ HAServiceTarget target = super.resolveTarget(nnId);
+ HAServiceTarget spy = Mockito.spy(target);
+ // OVerride the target to return our mock protocol
+ try {
+ Mockito.doReturn(mockProtocol).when(spy).getProxy();
+ } catch (IOException e) {
+ throw new AssertionError(e); // mock setup doesn't really throw
+ }
+ return spy;
}
};
tool.setConf(getHAConf());