|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hdfs.server.federation.router; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import org.apache.hadoop.conf.Configuration; |
| 23 | +import org.apache.hadoop.hdfs.server.federation.RouterConfigBuilder; |
| 24 | +import org.apache.hadoop.hdfs.server.federation.StateStoreDFSCluster; |
| 25 | +import org.apache.hadoop.hdfs.server.federation.resolver.MultipleDestinationMountTableResolver; |
| 26 | +import org.apache.hadoop.io.IOUtils; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import java.io.ByteArrayOutputStream; |
| 31 | +import java.net.HttpURLConnection; |
| 32 | +import java.net.URL; |
| 33 | +import java.util.Iterator; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_HTTP_ENABLE; |
| 37 | +import static org.junit.Assert.assertEquals; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | + |
| 40 | +public class TestRouterNetworkTopologyServlet { |
| 41 | + |
| 42 | + private static StateStoreDFSCluster clusterWithDatanodes; |
| 43 | + private static StateStoreDFSCluster clusterNoDatanodes; |
| 44 | + |
| 45 | + @BeforeClass |
| 46 | + public static void setUp() throws Exception { |
| 47 | + // Builder configuration |
| 48 | + Configuration routerConf = |
| 49 | + new RouterConfigBuilder().stateStore().admin().quota().rpc().build(); |
| 50 | + routerConf.set(DFS_ROUTER_HTTP_ENABLE, "true"); |
| 51 | + Configuration hdfsConf = new Configuration(false); |
| 52 | + |
| 53 | + // Build and start a federated cluster |
| 54 | + clusterWithDatanodes = new StateStoreDFSCluster(false, 2, |
| 55 | + MultipleDestinationMountTableResolver.class); |
| 56 | + clusterWithDatanodes.addNamenodeOverrides(hdfsConf); |
| 57 | + clusterWithDatanodes.addRouterOverrides(routerConf); |
| 58 | + clusterWithDatanodes.setNumDatanodesPerNameservice(9); |
| 59 | + clusterWithDatanodes.setIndependentDNs(); |
| 60 | + clusterWithDatanodes.setRacks( |
| 61 | + new String[] {"/rack1", "/rack1", "/rack1", "/rack2", "/rack2", |
| 62 | + "/rack2", "/rack3", "/rack3", "/rack3", "/rack4", "/rack4", |
| 63 | + "/rack4", "/rack5", "/rack5", "/rack5", "/rack6", "/rack6", |
| 64 | + "/rack6"}); |
| 65 | + clusterWithDatanodes.startCluster(); |
| 66 | + clusterWithDatanodes.startRouters(); |
| 67 | + clusterWithDatanodes.waitClusterUp(); |
| 68 | + clusterWithDatanodes.waitActiveNamespaces(); |
| 69 | + |
| 70 | + // Build and start a federated cluster |
| 71 | + clusterNoDatanodes = new StateStoreDFSCluster(false, 2, |
| 72 | + MultipleDestinationMountTableResolver.class); |
| 73 | + clusterNoDatanodes.addNamenodeOverrides(hdfsConf); |
| 74 | + clusterNoDatanodes.addRouterOverrides(routerConf); |
| 75 | + clusterNoDatanodes.setNumDatanodesPerNameservice(0); |
| 76 | + clusterNoDatanodes.setIndependentDNs(); |
| 77 | + clusterNoDatanodes.startCluster(); |
| 78 | + clusterNoDatanodes.startRouters(); |
| 79 | + clusterNoDatanodes.waitClusterUp(); |
| 80 | + clusterNoDatanodes.waitActiveNamespaces(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testPrintTopologyTextFormat() throws Exception { |
| 85 | + // get http Address |
| 86 | + String httpAddress = clusterWithDatanodes.getRandomRouter().getRouter() |
| 87 | + .getHttpServerAddress().toString(); |
| 88 | + |
| 89 | + // send http request |
| 90 | + URL url = new URL("http:/" + httpAddress + "/topology"); |
| 91 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 92 | + conn.setReadTimeout(20000); |
| 93 | + conn.setConnectTimeout(20000); |
| 94 | + conn.connect(); |
| 95 | + |
| 96 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 97 | + IOUtils.copyBytes(conn.getInputStream(), out, 4096, true); |
| 98 | + StringBuilder sb = |
| 99 | + new StringBuilder("-- Network Topology -- \n"); |
| 100 | + sb.append(out); |
| 101 | + sb.append("\n-- Network Topology -- "); |
| 102 | + String topology = sb.toString(); |
| 103 | + |
| 104 | + // assert rack info |
| 105 | + assertTrue(topology.contains("/ns0/rack1")); |
| 106 | + assertTrue(topology.contains("/ns0/rack2")); |
| 107 | + assertTrue(topology.contains("/ns0/rack3")); |
| 108 | + assertTrue(topology.contains("/ns1/rack4")); |
| 109 | + assertTrue(topology.contains("/ns1/rack5")); |
| 110 | + assertTrue(topology.contains("/ns1/rack6")); |
| 111 | + |
| 112 | + // assert node number |
| 113 | + assertEquals(18, |
| 114 | + topology.split("127.0.0.1").length - 1); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testPrintTopologyJsonFormat() throws Exception { |
| 119 | + // get http Address |
| 120 | + String httpAddress = clusterWithDatanodes.getRandomRouter().getRouter() |
| 121 | + .getHttpServerAddress().toString(); |
| 122 | + |
| 123 | + // send http request |
| 124 | + URL url = new URL("http:/" + httpAddress + "/topology"); |
| 125 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 126 | + conn.setReadTimeout(20000); |
| 127 | + conn.setConnectTimeout(20000); |
| 128 | + conn.setRequestProperty("Accept", "application/json"); |
| 129 | + conn.connect(); |
| 130 | + |
| 131 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 132 | + IOUtils.copyBytes(conn.getInputStream(), out, 4096, true); |
| 133 | + String topology = out.toString(); |
| 134 | + |
| 135 | + // parse json |
| 136 | + JsonNode racks = new ObjectMapper().readTree(topology); |
| 137 | + |
| 138 | + // assert rack number |
| 139 | + assertEquals(6, racks.size()); |
| 140 | + |
| 141 | + // assert rack info |
| 142 | + assertTrue(topology.contains("/ns0/rack1")); |
| 143 | + assertTrue(topology.contains("/ns0/rack2")); |
| 144 | + assertTrue(topology.contains("/ns0/rack3")); |
| 145 | + assertTrue(topology.contains("/ns1/rack4")); |
| 146 | + assertTrue(topology.contains("/ns1/rack5")); |
| 147 | + assertTrue(topology.contains("/ns1/rack6")); |
| 148 | + |
| 149 | + // assert node number |
| 150 | + Iterator<JsonNode> elements = racks.elements(); |
| 151 | + int dataNodesCount = 0; |
| 152 | + while(elements.hasNext()){ |
| 153 | + JsonNode rack = elements.next(); |
| 154 | + Iterator<Map.Entry<String, JsonNode>> fields = rack.fields(); |
| 155 | + while (fields.hasNext()) { |
| 156 | + dataNodesCount += fields.next().getValue().size(); |
| 157 | + } |
| 158 | + } |
| 159 | + assertEquals(18, dataNodesCount); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testPrintTopologyNoDatanodesTextFormat() throws Exception { |
| 164 | + // get http Address |
| 165 | + String httpAddress = clusterNoDatanodes.getRandomRouter().getRouter() |
| 166 | + .getHttpServerAddress().toString(); |
| 167 | + |
| 168 | + // send http request |
| 169 | + URL url = new URL("http:/" + httpAddress + "/topology"); |
| 170 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 171 | + conn.setReadTimeout(20000); |
| 172 | + conn.setConnectTimeout(20000); |
| 173 | + conn.connect(); |
| 174 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 175 | + IOUtils.copyBytes(conn.getInputStream(), out, 4096, true); |
| 176 | + StringBuilder sb = |
| 177 | + new StringBuilder("-- Network Topology -- \n"); |
| 178 | + sb.append(out); |
| 179 | + sb.append("\n-- Network Topology -- "); |
| 180 | + String topology = sb.toString(); |
| 181 | + |
| 182 | + // assert node number |
| 183 | + assertTrue(topology.contains("No DataNodes")); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void testPrintTopologyNoDatanodesJsonFormat() throws Exception { |
| 188 | + // get http Address |
| 189 | + String httpAddress = clusterNoDatanodes.getRandomRouter().getRouter() |
| 190 | + .getHttpServerAddress().toString(); |
| 191 | + |
| 192 | + // send http request |
| 193 | + URL url = new URL("http:/" + httpAddress + "/topology"); |
| 194 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 195 | + conn.setReadTimeout(20000); |
| 196 | + conn.setConnectTimeout(20000); |
| 197 | + conn.setRequestProperty("Accept", "application/json"); |
| 198 | + conn.connect(); |
| 199 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 200 | + IOUtils.copyBytes(conn.getInputStream(), out, 4096, true); |
| 201 | + StringBuilder sb = |
| 202 | + new StringBuilder("-- Network Topology -- \n"); |
| 203 | + sb.append(out); |
| 204 | + sb.append("\n-- Network Topology -- "); |
| 205 | + String topology = sb.toString(); |
| 206 | + |
| 207 | + // assert node number |
| 208 | + assertTrue(topology.contains("No DataNodes")); |
| 209 | + } |
| 210 | +} |
0 commit comments