PageRenderTime 4454ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodes.java

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 719 lines | 615 code | 82 blank | 22 comment | 6 complexity | 59cd8fb25067d06a35a0b679b6691c5a MD5 | raw file
  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.yarn.server.resourcemanager.webapp;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertTrue;
  21. import static org.junit.Assert.fail;
  22. import java.io.StringReader;
  23. import java.util.ArrayList;
  24. import java.util.EnumSet;
  25. import javax.ws.rs.core.MediaType;
  26. import javax.xml.parsers.DocumentBuilder;
  27. import javax.xml.parsers.DocumentBuilderFactory;
  28. import org.apache.hadoop.conf.Configuration;
  29. import org.apache.hadoop.yarn.api.records.ContainerStatus;
  30. import org.apache.hadoop.yarn.api.records.NodeState;
  31. import org.apache.hadoop.yarn.server.api.records.NodeHealthStatus;
  32. import org.apache.hadoop.yarn.server.resourcemanager.MockNM;
  33. import org.apache.hadoop.yarn.server.resourcemanager.MockRM;
  34. import org.apache.hadoop.yarn.server.resourcemanager.ResourceManager;
  35. import org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNode;
  36. import org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNodeImpl;
  37. import org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNodeStatusEvent;
  38. import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler;
  39. import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerNodeReport;
  40. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  41. import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
  42. import org.codehaus.jettison.json.JSONArray;
  43. import org.codehaus.jettison.json.JSONException;
  44. import org.codehaus.jettison.json.JSONObject;
  45. import org.junit.Before;
  46. import org.junit.Test;
  47. import org.w3c.dom.Document;
  48. import org.w3c.dom.Element;
  49. import org.w3c.dom.NodeList;
  50. import org.xml.sax.InputSource;
  51. import com.google.common.base.Joiner;
  52. import com.google.inject.Guice;
  53. import com.google.inject.Injector;
  54. import com.google.inject.servlet.GuiceServletContextListener;
  55. import com.google.inject.servlet.ServletModule;
  56. import com.sun.jersey.api.client.ClientResponse;
  57. import com.sun.jersey.api.client.ClientResponse.Status;
  58. import com.sun.jersey.api.client.UniformInterfaceException;
  59. import com.sun.jersey.api.client.WebResource;
  60. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  61. import com.sun.jersey.test.framework.JerseyTest;
  62. import com.sun.jersey.test.framework.WebAppDescriptor;
  63. public class TestRMWebServicesNodes extends JerseyTest {
  64. private static MockRM rm;
  65. private Injector injector = Guice.createInjector(new ServletModule() {
  66. @Override
  67. protected void configureServlets() {
  68. bind(JAXBContextResolver.class);
  69. bind(RMWebServices.class);
  70. bind(GenericExceptionHandler.class);
  71. rm = new MockRM(new Configuration());
  72. rm.getRMContext().getContainerTokenSecretManager().rollMasterKey();
  73. rm.getRMContext().getNMTokenSecretManager().rollMasterKey();
  74. bind(ResourceManager.class).toInstance(rm);
  75. serve("/*").with(GuiceContainer.class);
  76. }
  77. });
  78. public class GuiceServletConfig extends GuiceServletContextListener {
  79. @Override
  80. protected Injector getInjector() {
  81. return injector;
  82. }
  83. }
  84. @Before
  85. @Override
  86. public void setUp() throws Exception {
  87. super.setUp();
  88. }
  89. public TestRMWebServicesNodes() {
  90. super(new WebAppDescriptor.Builder(
  91. "org.apache.hadoop.yarn.server.resourcemanager.webapp")
  92. .contextListenerClass(GuiceServletConfig.class)
  93. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  94. .contextPath("jersey-guice-filter").servletPath("/").build());
  95. }
  96. @Test
  97. public void testNodes() throws JSONException, Exception {
  98. testNodesHelper("nodes", MediaType.APPLICATION_JSON);
  99. }
  100. @Test
  101. public void testNodesSlash() throws JSONException, Exception {
  102. testNodesHelper("nodes/", MediaType.APPLICATION_JSON);
  103. }
  104. @Test
  105. public void testNodesDefault() throws JSONException, Exception {
  106. testNodesHelper("nodes/", "");
  107. }
  108. @Test
  109. public void testNodesDefaultWithUnHealthyNode() throws JSONException,
  110. Exception {
  111. WebResource r = resource();
  112. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  113. MockNM nm2 = rm.registerNode("h2:1235", 5121);
  114. rm.sendNodeStarted(nm1);
  115. rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  116. rm.NMwaitForState(nm2.getNodeId(), NodeState.NEW);
  117. MockNM nm3 = rm.registerNode("h3:1236", 5122);
  118. rm.NMwaitForState(nm3.getNodeId(), NodeState.NEW);
  119. rm.sendNodeStarted(nm3);
  120. rm.NMwaitForState(nm3.getNodeId(), NodeState.RUNNING);
  121. RMNodeImpl node = (RMNodeImpl) rm.getRMContext().getRMNodes()
  122. .get(nm3.getNodeId());
  123. NodeHealthStatus nodeHealth = NodeHealthStatus.newInstance(false,
  124. "test health report", System.currentTimeMillis());
  125. node.handle(new RMNodeStatusEvent(nm3.getNodeId(), nodeHealth,
  126. new ArrayList<ContainerStatus>(), null, null));
  127. rm.NMwaitForState(nm3.getNodeId(), NodeState.UNHEALTHY);
  128. ClientResponse response =
  129. r.path("ws").path("v1").path("cluster").path("nodes")
  130. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  131. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  132. JSONObject json = response.getEntity(JSONObject.class);
  133. assertEquals("incorrect number of elements", 1, json.length());
  134. JSONObject nodes = json.getJSONObject("nodes");
  135. assertEquals("incorrect number of elements", 1, nodes.length());
  136. JSONArray nodeArray = nodes.getJSONArray("node");
  137. // 3 nodes, including the unhealthy node and the new node.
  138. assertEquals("incorrect number of elements", 3, nodeArray.length());
  139. }
  140. @Test
  141. public void testNodesQueryNew() throws JSONException, Exception {
  142. WebResource r = resource();
  143. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  144. MockNM nm2 = rm.registerNode("h2:1235", 5121);
  145. rm.sendNodeStarted(nm1);
  146. rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  147. rm.NMwaitForState(nm2.getNodeId(), NodeState.NEW);
  148. ClientResponse response = r.path("ws").path("v1").path("cluster")
  149. .path("nodes").queryParam("states", NodeState.NEW.toString())
  150. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  151. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  152. JSONObject json = response.getEntity(JSONObject.class);
  153. assertEquals("incorrect number of elements", 1, json.length());
  154. JSONObject nodes = json.getJSONObject("nodes");
  155. assertEquals("incorrect number of elements", 1, nodes.length());
  156. JSONArray nodeArray = nodes.getJSONArray("node");
  157. assertEquals("incorrect number of elements", 1, nodeArray.length());
  158. JSONObject info = nodeArray.getJSONObject(0);
  159. verifyNodeInfo(info, nm2);
  160. }
  161. @Test
  162. public void testNodesQueryStateNone() throws JSONException, Exception {
  163. WebResource r = resource();
  164. rm.registerNode("h1:1234", 5120);
  165. rm.registerNode("h2:1235", 5121);
  166. ClientResponse response = r.path("ws").path("v1").path("cluster")
  167. .path("nodes")
  168. .queryParam("states", NodeState.DECOMMISSIONED.toString())
  169. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  170. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  171. JSONObject json = response.getEntity(JSONObject.class);
  172. assertEquals("incorrect number of elements", 1, json.length());
  173. assertEquals("nodes is not null", JSONObject.NULL, json.get("nodes"));
  174. }
  175. @Test
  176. public void testNodesQueryStateInvalid() throws JSONException, Exception {
  177. WebResource r = resource();
  178. rm.registerNode("h1:1234", 5120);
  179. rm.registerNode("h2:1235", 5121);
  180. try {
  181. r.path("ws").path("v1").path("cluster").path("nodes")
  182. .queryParam("states", "BOGUSSTATE").accept(MediaType.APPLICATION_JSON)
  183. .get(JSONObject.class);
  184. fail("should have thrown exception querying invalid state");
  185. } catch (UniformInterfaceException ue) {
  186. ClientResponse response = ue.getResponse();
  187. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  188. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  189. JSONObject msg = response.getEntity(JSONObject.class);
  190. JSONObject exception = msg.getJSONObject("RemoteException");
  191. assertEquals("incorrect number of elements", 3, exception.length());
  192. String message = exception.getString("message");
  193. String type = exception.getString("exception");
  194. String classname = exception.getString("javaClassName");
  195. WebServicesTestUtils
  196. .checkStringContains(
  197. "exception message",
  198. "org.apache.hadoop.yarn.api.records.NodeState.BOGUSSTATE",
  199. message);
  200. WebServicesTestUtils.checkStringMatch("exception type",
  201. "IllegalArgumentException", type);
  202. WebServicesTestUtils.checkStringMatch("exception classname",
  203. "java.lang.IllegalArgumentException", classname);
  204. } finally {
  205. rm.stop();
  206. }
  207. }
  208. @Test
  209. public void testNodesQueryStateLost() throws JSONException, Exception {
  210. WebResource r = resource();
  211. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  212. MockNM nm2 = rm.registerNode("h2:1234", 5120);
  213. rm.sendNodeStarted(nm1);
  214. rm.sendNodeStarted(nm2);
  215. rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  216. rm.NMwaitForState(nm2.getNodeId(), NodeState.RUNNING);
  217. rm.sendNodeLost(nm1);
  218. rm.sendNodeLost(nm2);
  219. ClientResponse response = r.path("ws").path("v1").path("cluster")
  220. .path("nodes").queryParam("states", NodeState.LOST.toString())
  221. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  222. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  223. JSONObject json = response.getEntity(JSONObject.class);
  224. JSONObject nodes = json.getJSONObject("nodes");
  225. assertEquals("incorrect number of elements", 1, nodes.length());
  226. JSONArray nodeArray = nodes.getJSONArray("node");
  227. assertEquals("incorrect number of elements", 2, nodeArray.length());
  228. for (int i = 0; i < nodeArray.length(); ++i) {
  229. JSONObject info = nodeArray.getJSONObject(i);
  230. String host = info.get("id").toString().split(":")[0];
  231. RMNode rmNode = rm.getRMContext().getInactiveRMNodes().get(host);
  232. WebServicesTestUtils.checkStringMatch("nodeHTTPAddress", "",
  233. info.getString("nodeHTTPAddress"));
  234. WebServicesTestUtils.checkStringMatch("state", rmNode.getState()
  235. .toString(), info.getString("state"));
  236. }
  237. }
  238. @Test
  239. public void testSingleNodeQueryStateLost() throws JSONException, Exception {
  240. WebResource r = resource();
  241. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  242. MockNM nm2 = rm.registerNode("h2:1234", 5120);
  243. rm.sendNodeStarted(nm1);
  244. rm.sendNodeStarted(nm2);
  245. rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  246. rm.NMwaitForState(nm2.getNodeId(), NodeState.RUNNING);
  247. rm.sendNodeLost(nm1);
  248. rm.sendNodeLost(nm2);
  249. ClientResponse response = r.path("ws").path("v1").path("cluster")
  250. .path("nodes").path("h2:1234").accept(MediaType.APPLICATION_JSON)
  251. .get(ClientResponse.class);
  252. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  253. JSONObject json = response.getEntity(JSONObject.class);
  254. JSONObject info = json.getJSONObject("node");
  255. String id = info.get("id").toString();
  256. assertEquals("Incorrect Node Information.", "h2:1234", id);
  257. RMNode rmNode = rm.getRMContext().getInactiveRMNodes().get("h2");
  258. WebServicesTestUtils.checkStringMatch("nodeHTTPAddress", "",
  259. info.getString("nodeHTTPAddress"));
  260. WebServicesTestUtils.checkStringMatch("state",
  261. rmNode.getState().toString(), info.getString("state"));
  262. }
  263. @Test
  264. public void testNodesQueryRunning() throws JSONException, Exception {
  265. WebResource r = resource();
  266. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  267. MockNM nm2 = rm.registerNode("h2:1235", 5121);
  268. rm.sendNodeStarted(nm1);
  269. rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  270. rm.NMwaitForState(nm2.getNodeId(), NodeState.NEW);
  271. ClientResponse response = r.path("ws").path("v1").path("cluster")
  272. .path("nodes").queryParam("states", "running")
  273. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  274. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  275. JSONObject json = response.getEntity(JSONObject.class);
  276. assertEquals("incorrect number of elements", 1, json.length());
  277. JSONObject nodes = json.getJSONObject("nodes");
  278. assertEquals("incorrect number of elements", 1, nodes.length());
  279. JSONArray nodeArray = nodes.getJSONArray("node");
  280. assertEquals("incorrect number of elements", 1, nodeArray.length());
  281. }
  282. @Test
  283. public void testNodesQueryHealthyFalse() throws JSONException, Exception {
  284. WebResource r = resource();
  285. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  286. MockNM nm2 = rm.registerNode("h2:1235", 5121);
  287. rm.sendNodeStarted(nm1);
  288. rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  289. rm.NMwaitForState(nm2.getNodeId(), NodeState.NEW);
  290. ClientResponse response = r.path("ws").path("v1").path("cluster")
  291. .path("nodes").queryParam("states", "UNHEALTHY")
  292. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  293. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  294. JSONObject json = response.getEntity(JSONObject.class);
  295. assertEquals("incorrect number of elements", 1, json.length());
  296. assertEquals("nodes is not null", JSONObject.NULL, json.get("nodes"));
  297. }
  298. public void testNodesHelper(String path, String media) throws JSONException,
  299. Exception {
  300. WebResource r = resource();
  301. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  302. MockNM nm2 = rm.registerNode("h2:1235", 5121);
  303. rm.sendNodeStarted(nm1);
  304. rm.sendNodeStarted(nm2);
  305. rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  306. rm.NMwaitForState(nm2.getNodeId(), NodeState.RUNNING);
  307. ClientResponse response = r.path("ws").path("v1").path("cluster")
  308. .path(path).accept(media).get(ClientResponse.class);
  309. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  310. JSONObject json = response.getEntity(JSONObject.class);
  311. assertEquals("incorrect number of elements", 1, json.length());
  312. JSONObject nodes = json.getJSONObject("nodes");
  313. assertEquals("incorrect number of elements", 1, nodes.length());
  314. JSONArray nodeArray = nodes.getJSONArray("node");
  315. assertEquals("incorrect number of elements", 2, nodeArray.length());
  316. JSONObject info = nodeArray.getJSONObject(0);
  317. String id = info.get("id").toString();
  318. if (id.matches("h1:1234")) {
  319. verifyNodeInfo(info, nm1);
  320. verifyNodeInfo(nodeArray.getJSONObject(1), nm2);
  321. } else {
  322. verifyNodeInfo(info, nm2);
  323. verifyNodeInfo(nodeArray.getJSONObject(1), nm1);
  324. }
  325. }
  326. @Test
  327. public void testSingleNode() throws JSONException, Exception {
  328. rm.registerNode("h1:1234", 5120);
  329. MockNM nm2 = rm.registerNode("h2:1235", 5121);
  330. testSingleNodeHelper("h2:1235", nm2, MediaType.APPLICATION_JSON);
  331. }
  332. @Test
  333. public void testSingleNodeSlash() throws JSONException, Exception {
  334. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  335. rm.registerNode("h2:1235", 5121);
  336. testSingleNodeHelper("h1:1234/", nm1, MediaType.APPLICATION_JSON);
  337. }
  338. @Test
  339. public void testSingleNodeDefault() throws JSONException, Exception {
  340. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  341. rm.registerNode("h2:1235", 5121);
  342. testSingleNodeHelper("h1:1234/", nm1, "");
  343. }
  344. public void testSingleNodeHelper(String nodeid, MockNM nm, String media)
  345. throws JSONException, Exception {
  346. WebResource r = resource();
  347. ClientResponse response = r.path("ws").path("v1").path("cluster")
  348. .path("nodes").path(nodeid).accept(media).get(ClientResponse.class);
  349. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  350. JSONObject json = response.getEntity(JSONObject.class);
  351. assertEquals("incorrect number of elements", 1, json.length());
  352. JSONObject info = json.getJSONObject("node");
  353. verifyNodeInfo(info, nm);
  354. }
  355. @Test
  356. public void testNonexistNode() throws JSONException, Exception {
  357. rm.registerNode("h1:1234", 5120);
  358. rm.registerNode("h2:1235", 5121);
  359. WebResource r = resource();
  360. try {
  361. r.path("ws").path("v1").path("cluster").path("nodes")
  362. .path("node_invalid:99").accept(MediaType.APPLICATION_JSON)
  363. .get(JSONObject.class);
  364. fail("should have thrown exception on non-existent nodeid");
  365. } catch (UniformInterfaceException ue) {
  366. ClientResponse response = ue.getResponse();
  367. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  368. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  369. JSONObject msg = response.getEntity(JSONObject.class);
  370. JSONObject exception = msg.getJSONObject("RemoteException");
  371. assertEquals("incorrect number of elements", 3, exception.length());
  372. String message = exception.getString("message");
  373. String type = exception.getString("exception");
  374. String classname = exception.getString("javaClassName");
  375. verifyNonexistNodeException(message, type, classname);
  376. } finally {
  377. rm.stop();
  378. }
  379. }
  380. // test that the exception output defaults to JSON
  381. @Test
  382. public void testNonexistNodeDefault() throws JSONException, Exception {
  383. rm.registerNode("h1:1234", 5120);
  384. rm.registerNode("h2:1235", 5121);
  385. WebResource r = resource();
  386. try {
  387. r.path("ws").path("v1").path("cluster").path("nodes")
  388. .path("node_invalid:99").get(JSONObject.class);
  389. fail("should have thrown exception on non-existent nodeid");
  390. } catch (UniformInterfaceException ue) {
  391. ClientResponse response = ue.getResponse();
  392. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  393. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  394. JSONObject msg = response.getEntity(JSONObject.class);
  395. JSONObject exception = msg.getJSONObject("RemoteException");
  396. assertEquals("incorrect number of elements", 3, exception.length());
  397. String message = exception.getString("message");
  398. String type = exception.getString("exception");
  399. String classname = exception.getString("javaClassName");
  400. verifyNonexistNodeException(message, type, classname);
  401. } finally {
  402. rm.stop();
  403. }
  404. }
  405. // test that the exception output works in XML
  406. @Test
  407. public void testNonexistNodeXML() throws JSONException, Exception {
  408. rm.registerNode("h1:1234", 5120);
  409. rm.registerNode("h2:1235", 5121);
  410. WebResource r = resource();
  411. try {
  412. r.path("ws").path("v1").path("cluster").path("nodes")
  413. .path("node_invalid:99").accept(MediaType.APPLICATION_XML)
  414. .get(JSONObject.class);
  415. fail("should have thrown exception on non-existent nodeid");
  416. } catch (UniformInterfaceException ue) {
  417. ClientResponse response = ue.getResponse();
  418. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  419. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  420. String msg = response.getEntity(String.class);
  421. System.out.println(msg);
  422. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  423. DocumentBuilder db = dbf.newDocumentBuilder();
  424. InputSource is = new InputSource();
  425. is.setCharacterStream(new StringReader(msg));
  426. Document dom = db.parse(is);
  427. NodeList nodes = dom.getElementsByTagName("RemoteException");
  428. Element element = (Element) nodes.item(0);
  429. String message = WebServicesTestUtils.getXmlString(element, "message");
  430. String type = WebServicesTestUtils.getXmlString(element, "exception");
  431. String classname = WebServicesTestUtils.getXmlString(element,
  432. "javaClassName");
  433. verifyNonexistNodeException(message, type, classname);
  434. } finally {
  435. rm.stop();
  436. }
  437. }
  438. private void verifyNonexistNodeException(String message, String type, String classname) {
  439. assertTrue("exception message incorrect",
  440. "java.lang.Exception: nodeId, node_invalid:99, is not found"
  441. .matches(message));
  442. assertTrue("exception type incorrect", "NotFoundException".matches(type));
  443. assertTrue("exception className incorrect",
  444. "org.apache.hadoop.yarn.webapp.NotFoundException".matches(classname));
  445. }
  446. @Test
  447. public void testInvalidNode() throws JSONException, Exception {
  448. rm.registerNode("h1:1234", 5120);
  449. rm.registerNode("h2:1235", 5121);
  450. WebResource r = resource();
  451. try {
  452. r.path("ws").path("v1").path("cluster").path("nodes")
  453. .path("node_invalid_foo").accept(MediaType.APPLICATION_JSON)
  454. .get(JSONObject.class);
  455. fail("should have thrown exception on non-existent nodeid");
  456. } catch (UniformInterfaceException ue) {
  457. ClientResponse response = ue.getResponse();
  458. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  459. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  460. JSONObject msg = response.getEntity(JSONObject.class);
  461. JSONObject exception = msg.getJSONObject("RemoteException");
  462. assertEquals("incorrect number of elements", 3, exception.length());
  463. String message = exception.getString("message");
  464. String type = exception.getString("exception");
  465. String classname = exception.getString("javaClassName");
  466. WebServicesTestUtils.checkStringMatch("exception message",
  467. "Invalid NodeId \\[node_invalid_foo\\]. Expected host:port", message);
  468. WebServicesTestUtils.checkStringMatch("exception type",
  469. "IllegalArgumentException", type);
  470. WebServicesTestUtils.checkStringMatch("exception classname",
  471. "java.lang.IllegalArgumentException", classname);
  472. } finally {
  473. rm.stop();
  474. }
  475. }
  476. @Test
  477. public void testNodesXML() throws JSONException, Exception {
  478. rm.start();
  479. WebResource r = resource();
  480. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  481. // MockNM nm2 = rm.registerNode("h2:1235", 5121);
  482. ClientResponse response = r.path("ws").path("v1").path("cluster")
  483. .path("nodes").accept(MediaType.APPLICATION_XML)
  484. .get(ClientResponse.class);
  485. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  486. String xml = response.getEntity(String.class);
  487. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  488. DocumentBuilder db = dbf.newDocumentBuilder();
  489. InputSource is = new InputSource();
  490. is.setCharacterStream(new StringReader(xml));
  491. Document dom = db.parse(is);
  492. NodeList nodesApps = dom.getElementsByTagName("nodes");
  493. assertEquals("incorrect number of elements", 1, nodesApps.getLength());
  494. NodeList nodes = dom.getElementsByTagName("node");
  495. assertEquals("incorrect number of elements", 1, nodes.getLength());
  496. verifyNodesXML(nodes, nm1);
  497. rm.stop();
  498. }
  499. @Test
  500. public void testSingleNodesXML() throws JSONException, Exception {
  501. rm.start();
  502. WebResource r = resource();
  503. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  504. // MockNM nm2 = rm.registerNode("h2:1235", 5121);
  505. ClientResponse response = r.path("ws").path("v1").path("cluster")
  506. .path("nodes").path("h1:1234").accept(MediaType.APPLICATION_XML)
  507. .get(ClientResponse.class);
  508. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  509. String xml = response.getEntity(String.class);
  510. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  511. DocumentBuilder db = dbf.newDocumentBuilder();
  512. InputSource is = new InputSource();
  513. is.setCharacterStream(new StringReader(xml));
  514. Document dom = db.parse(is);
  515. NodeList nodes = dom.getElementsByTagName("node");
  516. assertEquals("incorrect number of elements", 1, nodes.getLength());
  517. verifyNodesXML(nodes, nm1);
  518. rm.stop();
  519. }
  520. @Test
  521. public void testNodes2XML() throws JSONException, Exception {
  522. rm.start();
  523. WebResource r = resource();
  524. rm.registerNode("h1:1234", 5120);
  525. rm.registerNode("h2:1235", 5121);
  526. ClientResponse response = r.path("ws").path("v1").path("cluster")
  527. .path("nodes").accept(MediaType.APPLICATION_XML)
  528. .get(ClientResponse.class);
  529. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  530. String xml = response.getEntity(String.class);
  531. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  532. DocumentBuilder db = dbf.newDocumentBuilder();
  533. InputSource is = new InputSource();
  534. is.setCharacterStream(new StringReader(xml));
  535. Document dom = db.parse(is);
  536. NodeList nodesApps = dom.getElementsByTagName("nodes");
  537. assertEquals("incorrect number of elements", 1, nodesApps.getLength());
  538. NodeList nodes = dom.getElementsByTagName("node");
  539. assertEquals("incorrect number of elements", 2, nodes.getLength());
  540. rm.stop();
  541. }
  542. @Test
  543. public void testQueryAll() throws Exception {
  544. WebResource r = resource();
  545. MockNM nm1 = rm.registerNode("h1:1234", 5120);
  546. MockNM nm2 = rm.registerNode("h2:1235", 5121);
  547. MockNM nm3 = rm.registerNode("h3:1236", 5122);
  548. rm.sendNodeStarted(nm1);
  549. rm.sendNodeStarted(nm3);
  550. rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  551. rm.NMwaitForState(nm2.getNodeId(), NodeState.NEW);
  552. rm.sendNodeLost(nm3);
  553. ClientResponse response = r.path("ws").path("v1").path("cluster")
  554. .path("nodes")
  555. .queryParam("states", Joiner.on(',').join(EnumSet.allOf(NodeState.class)))
  556. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  557. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  558. JSONObject json = response.getEntity(JSONObject.class);
  559. JSONObject nodes = json.getJSONObject("nodes");
  560. assertEquals("incorrect number of elements", 1, nodes.length());
  561. JSONArray nodeArray = nodes.getJSONArray("node");
  562. assertEquals("incorrect number of elements", 3, nodeArray.length());
  563. }
  564. public void verifyNodesXML(NodeList nodes, MockNM nm) throws JSONException,
  565. Exception {
  566. for (int i = 0; i < nodes.getLength(); i++) {
  567. Element element = (Element) nodes.item(i);
  568. verifyNodeInfoGeneric(nm,
  569. WebServicesTestUtils.getXmlString(element, "state"),
  570. WebServicesTestUtils.getXmlString(element, "rack"),
  571. WebServicesTestUtils.getXmlString(element, "id"),
  572. WebServicesTestUtils.getXmlString(element, "nodeHostName"),
  573. WebServicesTestUtils.getXmlString(element, "nodeHTTPAddress"),
  574. WebServicesTestUtils.getXmlLong(element, "lastHealthUpdate"),
  575. WebServicesTestUtils.getXmlString(element, "healthReport"),
  576. WebServicesTestUtils.getXmlInt(element, "numContainers"),
  577. WebServicesTestUtils.getXmlLong(element, "usedMemoryMB"),
  578. WebServicesTestUtils.getXmlLong(element, "availMemoryMB"),
  579. WebServicesTestUtils.getXmlLong(element, "usedVirtualCores"),
  580. WebServicesTestUtils.getXmlLong(element, "availableVirtualCores"),
  581. WebServicesTestUtils.getXmlString(element, "version"));
  582. }
  583. }
  584. public void verifyNodeInfo(JSONObject nodeInfo, MockNM nm)
  585. throws JSONException, Exception {
  586. assertEquals("incorrect number of elements", 13, nodeInfo.length());
  587. verifyNodeInfoGeneric(nm, nodeInfo.getString("state"),
  588. nodeInfo.getString("rack"),
  589. nodeInfo.getString("id"), nodeInfo.getString("nodeHostName"),
  590. nodeInfo.getString("nodeHTTPAddress"),
  591. nodeInfo.getLong("lastHealthUpdate"),
  592. nodeInfo.getString("healthReport"), nodeInfo.getInt("numContainers"),
  593. nodeInfo.getLong("usedMemoryMB"), nodeInfo.getLong("availMemoryMB"),
  594. nodeInfo.getLong("usedVirtualCores"), nodeInfo.getLong("availableVirtualCores"),
  595. nodeInfo.getString("version"));
  596. }
  597. public void verifyNodeInfoGeneric(MockNM nm, String state, String rack,
  598. String id, String nodeHostName,
  599. String nodeHTTPAddress, long lastHealthUpdate, String healthReport,
  600. int numContainers, long usedMemoryMB, long availMemoryMB, long usedVirtualCores,
  601. long availVirtualCores, String version)
  602. throws JSONException, Exception {
  603. RMNode node = rm.getRMContext().getRMNodes().get(nm.getNodeId());
  604. ResourceScheduler sched = rm.getResourceScheduler();
  605. SchedulerNodeReport report = sched.getNodeReport(nm.getNodeId());
  606. WebServicesTestUtils.checkStringMatch("state", node.getState().toString(),
  607. state);
  608. WebServicesTestUtils.checkStringMatch("rack", node.getRackName(), rack);
  609. WebServicesTestUtils.checkStringMatch("id", nm.getNodeId().toString(), id);
  610. WebServicesTestUtils.checkStringMatch("nodeHostName", nm.getNodeId()
  611. .getHost(), nodeHostName);
  612. WebServicesTestUtils.checkStringMatch("healthReport",
  613. String.valueOf(node.getHealthReport()), healthReport);
  614. String expectedHttpAddress = nm.getNodeId().getHost() + ":"
  615. + nm.getHttpPort();
  616. WebServicesTestUtils.checkStringMatch("nodeHTTPAddress",
  617. expectedHttpAddress, nodeHTTPAddress);
  618. WebServicesTestUtils.checkStringMatch("version",
  619. node.getNodeManagerVersion(), version);
  620. long expectedHealthUpdate = node.getLastHealthReportTime();
  621. assertEquals("lastHealthUpdate doesn't match, got: " + lastHealthUpdate
  622. + " expected: " + expectedHealthUpdate, expectedHealthUpdate,
  623. lastHealthUpdate);
  624. if (report != null) {
  625. assertEquals("numContainers doesn't match: " + numContainers,
  626. report.getNumContainers(), numContainers);
  627. assertEquals("usedMemoryMB doesn't match: " + usedMemoryMB, report
  628. .getUsedResource().getMemory(), usedMemoryMB);
  629. assertEquals("availMemoryMB doesn't match: " + availMemoryMB, report
  630. .getAvailableResource().getMemory(), availMemoryMB);
  631. assertEquals("usedVirtualCores doesn't match: " + usedVirtualCores, report
  632. .getUsedResource().getVirtualCores(), usedVirtualCores);
  633. assertEquals("availVirtualCores doesn't match: " + availVirtualCores, report
  634. .getAvailableResource().getVirtualCores(), availVirtualCores);
  635. }
  636. }
  637. }