|
@@ -18,13 +18,19 @@
|
|
|
package org.apache.hadoop.hdfs;
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
+import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.PrintStream;
|
|
|
+import java.nio.charset.Charset;
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
+import org.apache.hadoop.hdfs.protocol.ClientDatanodeProtocol;
|
|
|
import org.apache.hadoop.hdfs.server.datanode.DataNode;
|
|
|
+import org.apache.hadoop.hdfs.tools.DFSAdmin;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
/**
|
|
@@ -36,6 +42,9 @@ public class TestBalancerBandwidth {
|
|
|
final static private int NUM_OF_DATANODES = 2;
|
|
|
final static private int DEFAULT_BANDWIDTH = 1024*1024;
|
|
|
public static final Log LOG = LogFactory.getLog(TestBalancerBandwidth.class);
|
|
|
+ private static final Charset UTF8 = Charset.forName("UTF-8");
|
|
|
+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
|
|
+ private final PrintStream outStream = new PrintStream(outContent);
|
|
|
|
|
|
@Test
|
|
|
public void testBalancerBandwidth() throws Exception {
|
|
@@ -56,6 +65,23 @@ public class TestBalancerBandwidth {
|
|
|
// Ensure value from the configuration is reflected in the datanodes.
|
|
|
assertEquals(DEFAULT_BANDWIDTH, (long) datanodes.get(0).getBalancerBandwidth());
|
|
|
assertEquals(DEFAULT_BANDWIDTH, (long) datanodes.get(1).getBalancerBandwidth());
|
|
|
+ ClientDatanodeProtocol dn1Proxy = DFSUtilClient
|
|
|
+ .createClientDatanodeProtocolProxy(datanodes.get(0).getDatanodeId(),
|
|
|
+ conf, 60000, false);
|
|
|
+ ClientDatanodeProtocol dn2Proxy = DFSUtilClient
|
|
|
+ .createClientDatanodeProtocolProxy(datanodes.get(1).getDatanodeId(),
|
|
|
+ conf, 60000, false);
|
|
|
+ DFSAdmin admin = new DFSAdmin(conf);
|
|
|
+ String dn1Address = datanodes.get(0).ipcServer.getListenerAddress()
|
|
|
+ .getHostName() + ":" + datanodes.get(0).getIpcPort();
|
|
|
+ String dn2Address = datanodes.get(1).ipcServer.getListenerAddress()
|
|
|
+ .getHostName() + ":" + datanodes.get(1).getIpcPort();
|
|
|
+
|
|
|
+ // verifies the dfsadmin command execution
|
|
|
+ String[] args = new String[] { "-getBalancerBandwidth", dn1Address };
|
|
|
+ runGetBalancerBandwidthCmd(admin, args, dn1Proxy, DEFAULT_BANDWIDTH);
|
|
|
+ args = new String[] { "-getBalancerBandwidth", dn2Address };
|
|
|
+ runGetBalancerBandwidthCmd(admin, args, dn2Proxy, DEFAULT_BANDWIDTH);
|
|
|
|
|
|
// Dynamically change balancer bandwidth and ensure the updated value
|
|
|
// is reflected on the datanodes.
|
|
@@ -69,6 +95,11 @@ public class TestBalancerBandwidth {
|
|
|
|
|
|
assertEquals(newBandwidth, (long) datanodes.get(0).getBalancerBandwidth());
|
|
|
assertEquals(newBandwidth, (long) datanodes.get(1).getBalancerBandwidth());
|
|
|
+ // verifies the dfsadmin command execution
|
|
|
+ args = new String[] { "-getBalancerBandwidth", dn1Address };
|
|
|
+ runGetBalancerBandwidthCmd(admin, args, dn1Proxy, newBandwidth);
|
|
|
+ args = new String[] { "-getBalancerBandwidth", dn2Address };
|
|
|
+ runGetBalancerBandwidthCmd(admin, args, dn2Proxy, newBandwidth);
|
|
|
|
|
|
// Dynamically change balancer bandwidth to 0. Balancer bandwidth on the
|
|
|
// datanodes should remain as it was.
|
|
@@ -81,11 +112,33 @@ public class TestBalancerBandwidth {
|
|
|
|
|
|
assertEquals(newBandwidth, (long) datanodes.get(0).getBalancerBandwidth());
|
|
|
assertEquals(newBandwidth, (long) datanodes.get(1).getBalancerBandwidth());
|
|
|
- }finally {
|
|
|
+ // verifies the dfsadmin command execution
|
|
|
+ args = new String[] { "-getBalancerBandwidth", dn1Address };
|
|
|
+ runGetBalancerBandwidthCmd(admin, args, dn1Proxy, newBandwidth);
|
|
|
+ args = new String[] { "-getBalancerBandwidth", dn2Address };
|
|
|
+ runGetBalancerBandwidthCmd(admin, args, dn2Proxy, newBandwidth);
|
|
|
+ } finally {
|
|
|
cluster.shutdown();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void runGetBalancerBandwidthCmd(DFSAdmin admin, String[] args,
|
|
|
+ ClientDatanodeProtocol proxy, long expectedBandwidth) throws Exception {
|
|
|
+ PrintStream initialStdOut = System.out;
|
|
|
+ outContent.reset();
|
|
|
+ try {
|
|
|
+ System.setOut(outStream);
|
|
|
+ int exitCode = admin.run(args);
|
|
|
+ assertEquals("DFSAdmin should return 0", 0, exitCode);
|
|
|
+ String bandwidthOutMsg = "Balancer bandwidth is " + expectedBandwidth
|
|
|
+ + " bytes per second.";
|
|
|
+ String strOut = new String(outContent.toByteArray(), UTF8);
|
|
|
+ assertTrue("Wrong balancer bandwidth!", strOut.contains(bandwidthOutMsg));
|
|
|
+ } finally {
|
|
|
+ System.setOut(initialStdOut);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
new TestBalancerBandwidth().testBalancerBandwidth();
|
|
|
}
|