PageRenderTime 185ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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/TestRMWebServicesApps.java

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 1109 lines | 990 code | 92 blank | 27 comment | 18 complexity | 128145d98b9c23171721830e2a92084b 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.Collection;
  24. import javax.ws.rs.core.MediaType;
  25. import javax.xml.parsers.DocumentBuilder;
  26. import javax.xml.parsers.DocumentBuilderFactory;
  27. import org.apache.hadoop.conf.Configuration;
  28. import org.apache.hadoop.security.UserGroupInformation;
  29. import org.apache.hadoop.yarn.api.records.ContainerState;
  30. import org.apache.hadoop.yarn.api.records.FinalApplicationStatus;
  31. import org.apache.hadoop.yarn.api.records.YarnApplicationState;
  32. import org.apache.hadoop.yarn.conf.YarnConfiguration;
  33. import org.apache.hadoop.yarn.server.resourcemanager.MockAM;
  34. import org.apache.hadoop.yarn.server.resourcemanager.MockNM;
  35. import org.apache.hadoop.yarn.server.resourcemanager.MockRM;
  36. import org.apache.hadoop.yarn.server.resourcemanager.ResourceManager;
  37. import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp;
  38. import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppState;
  39. import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttempt;
  40. import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptState;
  41. import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler;
  42. import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fifo.FifoScheduler;
  43. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  44. import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
  45. import org.codehaus.jettison.json.JSONArray;
  46. import org.codehaus.jettison.json.JSONException;
  47. import org.codehaus.jettison.json.JSONObject;
  48. import org.junit.Before;
  49. import org.junit.Test;
  50. import org.w3c.dom.Document;
  51. import org.w3c.dom.Element;
  52. import org.w3c.dom.NodeList;
  53. import org.xml.sax.InputSource;
  54. import com.google.inject.Guice;
  55. import com.google.inject.Injector;
  56. import com.google.inject.servlet.GuiceServletContextListener;
  57. import com.google.inject.servlet.ServletModule;
  58. import com.sun.jersey.api.client.ClientResponse;
  59. import com.sun.jersey.api.client.ClientResponse.Status;
  60. import com.sun.jersey.api.client.UniformInterfaceException;
  61. import com.sun.jersey.api.client.WebResource;
  62. import com.sun.jersey.core.util.MultivaluedMapImpl;
  63. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  64. import com.sun.jersey.test.framework.JerseyTest;
  65. import com.sun.jersey.test.framework.WebAppDescriptor;
  66. public class TestRMWebServicesApps extends JerseyTest {
  67. private static MockRM rm;
  68. private static final int CONTAINER_MB = 1024;
  69. private Injector injector = Guice.createInjector(new ServletModule() {
  70. @Override
  71. protected void configureServlets() {
  72. bind(JAXBContextResolver.class);
  73. bind(RMWebServices.class);
  74. bind(GenericExceptionHandler.class);
  75. Configuration conf = new Configuration();
  76. conf.setInt(YarnConfiguration.RM_AM_MAX_ATTEMPTS,
  77. YarnConfiguration.DEFAULT_RM_AM_MAX_ATTEMPTS);
  78. conf.setClass(YarnConfiguration.RM_SCHEDULER, FifoScheduler.class,
  79. ResourceScheduler.class);
  80. rm = new MockRM(conf);
  81. bind(ResourceManager.class).toInstance(rm);
  82. serve("/*").with(GuiceContainer.class);
  83. }
  84. });
  85. public class GuiceServletConfig extends GuiceServletContextListener {
  86. @Override
  87. protected Injector getInjector() {
  88. return injector;
  89. }
  90. }
  91. @Before
  92. @Override
  93. public void setUp() throws Exception {
  94. super.setUp();
  95. }
  96. public TestRMWebServicesApps() {
  97. super(new WebAppDescriptor.Builder(
  98. "org.apache.hadoop.yarn.server.resourcemanager.webapp")
  99. .contextListenerClass(GuiceServletConfig.class)
  100. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  101. .contextPath("jersey-guice-filter").servletPath("/").build());
  102. }
  103. @Test
  104. public void testApps() throws JSONException, Exception {
  105. rm.start();
  106. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  107. RMApp app1 = rm.submitApp(CONTAINER_MB);
  108. amNodeManager.nodeHeartbeat(true);
  109. testAppsHelper("apps", app1, MediaType.APPLICATION_JSON);
  110. rm.stop();
  111. }
  112. @Test
  113. public void testAppsSlash() throws JSONException, Exception {
  114. rm.start();
  115. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  116. RMApp app1 = rm.submitApp(CONTAINER_MB);
  117. amNodeManager.nodeHeartbeat(true);
  118. testAppsHelper("apps/", app1, MediaType.APPLICATION_JSON);
  119. rm.stop();
  120. }
  121. @Test
  122. public void testAppsDefault() throws JSONException, Exception {
  123. rm.start();
  124. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  125. RMApp app1 = rm.submitApp(CONTAINER_MB);
  126. amNodeManager.nodeHeartbeat(true);
  127. testAppsHelper("apps/", app1, "");
  128. rm.stop();
  129. }
  130. @Test
  131. public void testAppsXML() throws JSONException, Exception {
  132. rm.start();
  133. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  134. RMApp app1 = rm.submitApp(CONTAINER_MB, "testwordcount", "user1");
  135. amNodeManager.nodeHeartbeat(true);
  136. WebResource r = resource();
  137. ClientResponse response = r.path("ws").path("v1").path("cluster")
  138. .path("apps").accept(MediaType.APPLICATION_XML)
  139. .get(ClientResponse.class);
  140. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  141. String xml = response.getEntity(String.class);
  142. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  143. DocumentBuilder db = dbf.newDocumentBuilder();
  144. InputSource is = new InputSource();
  145. is.setCharacterStream(new StringReader(xml));
  146. Document dom = db.parse(is);
  147. NodeList nodesApps = dom.getElementsByTagName("apps");
  148. assertEquals("incorrect number of elements", 1, nodesApps.getLength());
  149. NodeList nodes = dom.getElementsByTagName("app");
  150. assertEquals("incorrect number of elements", 1, nodes.getLength());
  151. verifyAppsXML(nodes, app1);
  152. rm.stop();
  153. }
  154. @Test
  155. public void testAppsXMLMulti() throws JSONException, Exception {
  156. rm.start();
  157. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  158. rm.submitApp(CONTAINER_MB, "testwordcount", "user1");
  159. rm.submitApp(2048, "testwordcount2", "user1");
  160. amNodeManager.nodeHeartbeat(true);
  161. WebResource r = resource();
  162. ClientResponse response = r.path("ws").path("v1").path("cluster")
  163. .path("apps").accept(MediaType.APPLICATION_XML)
  164. .get(ClientResponse.class);
  165. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  166. String xml = response.getEntity(String.class);
  167. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  168. DocumentBuilder db = dbf.newDocumentBuilder();
  169. InputSource is = new InputSource();
  170. is.setCharacterStream(new StringReader(xml));
  171. Document dom = db.parse(is);
  172. NodeList nodesApps = dom.getElementsByTagName("apps");
  173. assertEquals("incorrect number of elements", 1, nodesApps.getLength());
  174. NodeList nodes = dom.getElementsByTagName("app");
  175. assertEquals("incorrect number of elements", 2, nodes.getLength());
  176. rm.stop();
  177. }
  178. public void testAppsHelper(String path, RMApp app, String media)
  179. throws JSONException, Exception {
  180. WebResource r = resource();
  181. ClientResponse response = r.path("ws").path("v1").path("cluster")
  182. .path(path).accept(media).get(ClientResponse.class);
  183. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  184. JSONObject json = response.getEntity(JSONObject.class);
  185. assertEquals("incorrect number of elements", 1, json.length());
  186. JSONObject apps = json.getJSONObject("apps");
  187. assertEquals("incorrect number of elements", 1, apps.length());
  188. JSONArray array = apps.getJSONArray("app");
  189. assertEquals("incorrect number of elements", 1, array.length());
  190. verifyAppInfo(array.getJSONObject(0), app);
  191. }
  192. @Test
  193. public void testAppsQueryState() throws JSONException, Exception {
  194. rm.start();
  195. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  196. RMApp app1 = rm.submitApp(CONTAINER_MB);
  197. amNodeManager.nodeHeartbeat(true);
  198. WebResource r = resource();
  199. ClientResponse response = r.path("ws").path("v1").path("cluster")
  200. .path("apps")
  201. .queryParam("state", YarnApplicationState.ACCEPTED.toString())
  202. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  203. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  204. JSONObject json = response.getEntity(JSONObject.class);
  205. assertEquals("incorrect number of elements", 1, json.length());
  206. JSONObject apps = json.getJSONObject("apps");
  207. assertEquals("incorrect number of elements", 1, apps.length());
  208. JSONArray array = apps.getJSONArray("app");
  209. assertEquals("incorrect number of elements", 1, array.length());
  210. verifyAppInfo(array.getJSONObject(0), app1);
  211. rm.stop();
  212. }
  213. @Test
  214. public void testAppsQueryStates() throws JSONException, Exception {
  215. rm.start();
  216. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  217. rm.submitApp(CONTAINER_MB);
  218. RMApp killedApp = rm.submitApp(CONTAINER_MB);
  219. rm.killApp(killedApp.getApplicationId());
  220. amNodeManager.nodeHeartbeat(true);
  221. WebResource r = resource();
  222. MultivaluedMapImpl params = new MultivaluedMapImpl();
  223. params.add("states", YarnApplicationState.ACCEPTED.toString());
  224. ClientResponse response = r.path("ws").path("v1").path("cluster")
  225. .path("apps").queryParams(params)
  226. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  227. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  228. JSONObject json = response.getEntity(JSONObject.class);
  229. assertEquals("incorrect number of elements", 1, json.length());
  230. JSONObject apps = json.getJSONObject("apps");
  231. assertEquals("incorrect number of elements", 1, apps.length());
  232. JSONArray array = apps.getJSONArray("app");
  233. assertEquals("incorrect number of elements", 1, array.length());
  234. assertEquals("state not equal to ACCEPTED", "ACCEPTED", array
  235. .getJSONObject(0).getString("state"));
  236. r = resource();
  237. params = new MultivaluedMapImpl();
  238. params.add("states", YarnApplicationState.ACCEPTED.toString());
  239. params.add("states", YarnApplicationState.KILLED.toString());
  240. response = r.path("ws").path("v1").path("cluster")
  241. .path("apps").queryParams(params)
  242. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  243. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  244. json = response.getEntity(JSONObject.class);
  245. assertEquals("incorrect number of elements", 1, json.length());
  246. apps = json.getJSONObject("apps");
  247. assertEquals("incorrect number of elements", 1, apps.length());
  248. array = apps.getJSONArray("app");
  249. assertEquals("incorrect number of elements", 2, array.length());
  250. assertTrue("both app states of ACCEPTED and KILLED are not present",
  251. (array.getJSONObject(0).getString("state").equals("ACCEPTED") &&
  252. array.getJSONObject(1).getString("state").equals("KILLED")) ||
  253. (array.getJSONObject(0).getString("state").equals("KILLED") &&
  254. array.getJSONObject(1).getString("state").equals("ACCEPTED")));
  255. rm.stop();
  256. }
  257. @Test
  258. public void testAppsQueryStatesComma() throws JSONException, Exception {
  259. rm.start();
  260. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  261. rm.submitApp(CONTAINER_MB);
  262. RMApp killedApp = rm.submitApp(CONTAINER_MB);
  263. rm.killApp(killedApp.getApplicationId());
  264. amNodeManager.nodeHeartbeat(true);
  265. WebResource r = resource();
  266. MultivaluedMapImpl params = new MultivaluedMapImpl();
  267. params.add("states", YarnApplicationState.ACCEPTED.toString());
  268. ClientResponse response = r.path("ws").path("v1").path("cluster")
  269. .path("apps").queryParams(params)
  270. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  271. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  272. JSONObject json = response.getEntity(JSONObject.class);
  273. assertEquals("incorrect number of elements", 1, json.length());
  274. JSONObject apps = json.getJSONObject("apps");
  275. assertEquals("incorrect number of elements", 1, apps.length());
  276. JSONArray array = apps.getJSONArray("app");
  277. assertEquals("incorrect number of elements", 1, array.length());
  278. assertEquals("state not equal to ACCEPTED", "ACCEPTED", array
  279. .getJSONObject(0).getString("state"));
  280. r = resource();
  281. params = new MultivaluedMapImpl();
  282. params.add("states", YarnApplicationState.ACCEPTED.toString() + ","
  283. + YarnApplicationState.KILLED.toString());
  284. response = r.path("ws").path("v1").path("cluster")
  285. .path("apps").queryParams(params)
  286. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  287. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  288. json = response.getEntity(JSONObject.class);
  289. assertEquals("incorrect number of elements", 1, json.length());
  290. apps = json.getJSONObject("apps");
  291. assertEquals("incorrect number of elements", 1, apps.length());
  292. array = apps.getJSONArray("app");
  293. assertEquals("incorrect number of elements", 2, array.length());
  294. assertTrue("both app states of ACCEPTED and KILLED are not present",
  295. (array.getJSONObject(0).getString("state").equals("ACCEPTED") &&
  296. array.getJSONObject(1).getString("state").equals("KILLED")) ||
  297. (array.getJSONObject(0).getString("state").equals("KILLED") &&
  298. array.getJSONObject(1).getString("state").equals("ACCEPTED")));
  299. rm.stop();
  300. }
  301. @Test
  302. public void testAppsQueryStatesNone() throws JSONException, Exception {
  303. rm.start();
  304. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  305. rm.submitApp(CONTAINER_MB);
  306. amNodeManager.nodeHeartbeat(true);
  307. WebResource r = resource();
  308. ClientResponse response = r.path("ws").path("v1").path("cluster")
  309. .path("apps")
  310. .queryParam("states", YarnApplicationState.RUNNING.toString())
  311. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  312. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  313. JSONObject json = response.getEntity(JSONObject.class);
  314. assertEquals("incorrect number of elements", 1, json.length());
  315. assertEquals("apps is not null", JSONObject.NULL, json.get("apps"));
  316. rm.stop();
  317. }
  318. @Test
  319. public void testAppsQueryStateNone() throws JSONException, Exception {
  320. rm.start();
  321. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  322. rm.submitApp(CONTAINER_MB);
  323. amNodeManager.nodeHeartbeat(true);
  324. WebResource r = resource();
  325. ClientResponse response = r.path("ws").path("v1").path("cluster")
  326. .path("apps")
  327. .queryParam("state", YarnApplicationState.RUNNING.toString())
  328. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  329. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  330. JSONObject json = response.getEntity(JSONObject.class);
  331. assertEquals("incorrect number of elements", 1, json.length());
  332. assertEquals("apps is not null", JSONObject.NULL, json.get("apps"));
  333. rm.stop();
  334. }
  335. @Test
  336. public void testAppsQueryStatesInvalid() throws JSONException, Exception {
  337. rm.start();
  338. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  339. rm.submitApp(CONTAINER_MB);
  340. amNodeManager.nodeHeartbeat(true);
  341. WebResource r = resource();
  342. try {
  343. r.path("ws").path("v1").path("cluster").path("apps")
  344. .queryParam("states", "INVALID_test")
  345. .accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
  346. fail("should have thrown exception on invalid state query");
  347. } catch (UniformInterfaceException ue) {
  348. ClientResponse response = ue.getResponse();
  349. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  350. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  351. JSONObject msg = response.getEntity(JSONObject.class);
  352. JSONObject exception = msg.getJSONObject("RemoteException");
  353. assertEquals("incorrect number of elements", 3, exception.length());
  354. String message = exception.getString("message");
  355. String type = exception.getString("exception");
  356. String classname = exception.getString("javaClassName");
  357. WebServicesTestUtils.checkStringContains(
  358. "exception message",
  359. "Invalid application-state INVALID_test",
  360. message);
  361. WebServicesTestUtils.checkStringMatch("exception type",
  362. "BadRequestException", type);
  363. WebServicesTestUtils.checkStringMatch("exception classname",
  364. "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
  365. } finally {
  366. rm.stop();
  367. }
  368. }
  369. @Test
  370. public void testAppsQueryStateInvalid() throws JSONException, Exception {
  371. rm.start();
  372. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  373. rm.submitApp(CONTAINER_MB);
  374. amNodeManager.nodeHeartbeat(true);
  375. WebResource r = resource();
  376. try {
  377. r.path("ws").path("v1").path("cluster").path("apps")
  378. .queryParam("state", "INVALID_test")
  379. .accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
  380. fail("should have thrown exception on invalid state query");
  381. } catch (UniformInterfaceException ue) {
  382. ClientResponse response = ue.getResponse();
  383. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  384. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  385. JSONObject msg = response.getEntity(JSONObject.class);
  386. JSONObject exception = msg.getJSONObject("RemoteException");
  387. assertEquals("incorrect number of elements", 3, exception.length());
  388. String message = exception.getString("message");
  389. String type = exception.getString("exception");
  390. String classname = exception.getString("javaClassName");
  391. WebServicesTestUtils.checkStringContains(
  392. "exception message",
  393. "Invalid application-state INVALID_test",
  394. message);
  395. WebServicesTestUtils.checkStringMatch("exception type",
  396. "BadRequestException", type);
  397. WebServicesTestUtils.checkStringMatch("exception classname",
  398. "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
  399. } finally {
  400. rm.stop();
  401. }
  402. }
  403. @Test
  404. public void testAppsQueryFinalStatus() throws JSONException, Exception {
  405. rm.start();
  406. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  407. RMApp app1 = rm.submitApp(CONTAINER_MB);
  408. amNodeManager.nodeHeartbeat(true);
  409. WebResource r = resource();
  410. ClientResponse response = r.path("ws").path("v1").path("cluster")
  411. .path("apps").queryParam("finalStatus", FinalApplicationStatus.UNDEFINED.toString())
  412. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  413. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  414. JSONObject json = response.getEntity(JSONObject.class);
  415. assertEquals("incorrect number of elements", 1, json.length());
  416. System.out.println(json.toString());
  417. JSONObject apps = json.getJSONObject("apps");
  418. assertEquals("incorrect number of elements", 1, apps.length());
  419. JSONArray array = apps.getJSONArray("app");
  420. assertEquals("incorrect number of elements", 1, array.length());
  421. verifyAppInfo(array.getJSONObject(0), app1);
  422. rm.stop();
  423. }
  424. @Test
  425. public void testAppsQueryFinalStatusNone() throws JSONException, Exception {
  426. rm.start();
  427. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  428. rm.submitApp(CONTAINER_MB);
  429. amNodeManager.nodeHeartbeat(true);
  430. WebResource r = resource();
  431. ClientResponse response = r.path("ws").path("v1").path("cluster")
  432. .path("apps").queryParam("finalStatus", FinalApplicationStatus.KILLED.toString())
  433. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  434. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  435. JSONObject json = response.getEntity(JSONObject.class);
  436. assertEquals("incorrect number of elements", 1, json.length());
  437. assertEquals("apps is not null", JSONObject.NULL, json.get("apps"));
  438. rm.stop();
  439. }
  440. @Test
  441. public void testAppsQueryFinalStatusInvalid() throws JSONException, Exception {
  442. rm.start();
  443. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  444. rm.submitApp(CONTAINER_MB);
  445. amNodeManager.nodeHeartbeat(true);
  446. WebResource r = resource();
  447. try {
  448. r.path("ws").path("v1").path("cluster").path("apps")
  449. .queryParam("finalStatus", "INVALID_test")
  450. .accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
  451. fail("should have thrown exception on invalid state query");
  452. } catch (UniformInterfaceException ue) {
  453. ClientResponse response = ue.getResponse();
  454. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  455. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  456. JSONObject msg = response.getEntity(JSONObject.class);
  457. JSONObject exception = msg.getJSONObject("RemoteException");
  458. assertEquals("incorrect number of elements", 3, exception.length());
  459. String message = exception.getString("message");
  460. String type = exception.getString("exception");
  461. String classname = exception.getString("javaClassName");
  462. WebServicesTestUtils
  463. .checkStringContains(
  464. "exception message",
  465. "org.apache.hadoop.yarn.api.records.FinalApplicationStatus.INVALID_test",
  466. message);
  467. WebServicesTestUtils.checkStringMatch("exception type",
  468. "IllegalArgumentException", type);
  469. WebServicesTestUtils.checkStringMatch("exception classname",
  470. "java.lang.IllegalArgumentException", classname);
  471. } finally {
  472. rm.stop();
  473. }
  474. }
  475. @Test
  476. public void testAppsQueryUser() throws JSONException, Exception {
  477. rm.start();
  478. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  479. rm.submitApp(CONTAINER_MB);
  480. rm.submitApp(CONTAINER_MB);
  481. amNodeManager.nodeHeartbeat(true);
  482. WebResource r = resource();
  483. ClientResponse response = r
  484. .path("ws")
  485. .path("v1")
  486. .path("cluster")
  487. .path("apps")
  488. .queryParam("user",
  489. UserGroupInformation.getCurrentUser().getShortUserName())
  490. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  491. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  492. JSONObject json = response.getEntity(JSONObject.class);
  493. assertEquals("incorrect number of elements", 1, json.length());
  494. JSONObject apps = json.getJSONObject("apps");
  495. assertEquals("incorrect number of elements", 1, apps.length());
  496. JSONArray array = apps.getJSONArray("app");
  497. assertEquals("incorrect number of elements", 2, array.length());
  498. rm.stop();
  499. }
  500. @Test
  501. public void testAppsQueryQueue() throws JSONException, Exception {
  502. rm.start();
  503. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  504. rm.submitApp(CONTAINER_MB);
  505. rm.submitApp(CONTAINER_MB);
  506. amNodeManager.nodeHeartbeat(true);
  507. WebResource r = resource();
  508. ClientResponse response = r.path("ws").path("v1").path("cluster")
  509. .path("apps").queryParam("queue", "default")
  510. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  511. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  512. JSONObject json = response.getEntity(JSONObject.class);
  513. assertEquals("incorrect number of elements", 1, json.length());
  514. JSONObject apps = json.getJSONObject("apps");
  515. assertEquals("incorrect number of elements", 1, apps.length());
  516. JSONArray array = apps.getJSONArray("app");
  517. assertEquals("incorrect number of elements", 2, array.length());
  518. rm.stop();
  519. }
  520. @Test
  521. public void testAppsQueryLimit() throws JSONException, Exception {
  522. rm.start();
  523. rm.registerNode("127.0.0.1:1234", 2048);
  524. rm.submitApp(CONTAINER_MB);
  525. rm.submitApp(CONTAINER_MB);
  526. rm.submitApp(CONTAINER_MB);
  527. WebResource r = resource();
  528. ClientResponse response = r.path("ws").path("v1").path("cluster")
  529. .path("apps").queryParam("limit", "2")
  530. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  531. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  532. JSONObject json = response.getEntity(JSONObject.class);
  533. assertEquals("incorrect number of elements", 1, json.length());
  534. JSONObject apps = json.getJSONObject("apps");
  535. assertEquals("incorrect number of elements", 1, apps.length());
  536. JSONArray array = apps.getJSONArray("app");
  537. assertEquals("incorrect number of elements", 2, array.length());
  538. rm.stop();
  539. }
  540. @Test
  541. public void testAppsQueryStartBegin() throws JSONException, Exception {
  542. rm.start();
  543. long start = System.currentTimeMillis();
  544. Thread.sleep(1);
  545. rm.registerNode("127.0.0.1:1234", 2048);
  546. rm.submitApp(CONTAINER_MB);
  547. rm.submitApp(CONTAINER_MB);
  548. rm.submitApp(CONTAINER_MB);
  549. WebResource r = resource();
  550. ClientResponse response = r.path("ws").path("v1").path("cluster")
  551. .path("apps").queryParam("startedTimeBegin", String.valueOf(start))
  552. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  553. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  554. JSONObject json = response.getEntity(JSONObject.class);
  555. assertEquals("incorrect number of elements", 1, json.length());
  556. JSONObject apps = json.getJSONObject("apps");
  557. assertEquals("incorrect number of elements", 1, apps.length());
  558. JSONArray array = apps.getJSONArray("app");
  559. assertEquals("incorrect number of elements", 3, array.length());
  560. rm.stop();
  561. }
  562. @Test
  563. public void testAppsQueryStartBeginSome() throws JSONException, Exception {
  564. rm.start();
  565. rm.registerNode("127.0.0.1:1234", 2048);
  566. rm.submitApp(CONTAINER_MB);
  567. rm.submitApp(CONTAINER_MB);
  568. long start = System.currentTimeMillis();
  569. Thread.sleep(1);
  570. rm.submitApp(CONTAINER_MB);
  571. WebResource r = resource();
  572. ClientResponse response = r.path("ws").path("v1").path("cluster")
  573. .path("apps").queryParam("startedTimeBegin", String.valueOf(start))
  574. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  575. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  576. JSONObject json = response.getEntity(JSONObject.class);
  577. assertEquals("incorrect number of elements", 1, json.length());
  578. JSONObject apps = json.getJSONObject("apps");
  579. assertEquals("incorrect number of elements", 1, apps.length());
  580. JSONArray array = apps.getJSONArray("app");
  581. assertEquals("incorrect number of elements", 1, array.length());
  582. rm.stop();
  583. }
  584. @Test
  585. public void testAppsQueryStartEnd() throws JSONException, Exception {
  586. rm.start();
  587. rm.registerNode("127.0.0.1:1234", 2048);
  588. long end = System.currentTimeMillis();
  589. Thread.sleep(1);
  590. rm.submitApp(CONTAINER_MB);
  591. rm.submitApp(CONTAINER_MB);
  592. rm.submitApp(CONTAINER_MB);
  593. WebResource r = resource();
  594. ClientResponse response = r.path("ws").path("v1").path("cluster")
  595. .path("apps").queryParam("startedTimeEnd", String.valueOf(end))
  596. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  597. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  598. JSONObject json = response.getEntity(JSONObject.class);
  599. assertEquals("incorrect number of elements", 1, json.length());
  600. assertEquals("apps is not null", JSONObject.NULL, json.get("apps"));
  601. rm.stop();
  602. }
  603. @Test
  604. public void testAppsQueryStartBeginEnd() throws JSONException, Exception {
  605. rm.start();
  606. rm.registerNode("127.0.0.1:1234", 2048);
  607. long start = System.currentTimeMillis();
  608. Thread.sleep(1);
  609. rm.submitApp(CONTAINER_MB);
  610. rm.submitApp(CONTAINER_MB);
  611. long end = System.currentTimeMillis();
  612. Thread.sleep(1);
  613. rm.submitApp(CONTAINER_MB);
  614. WebResource r = resource();
  615. ClientResponse response = r.path("ws").path("v1").path("cluster")
  616. .path("apps").queryParam("startedTimeBegin", String.valueOf(start))
  617. .queryParam("startedTimeEnd", String.valueOf(end))
  618. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  619. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  620. JSONObject json = response.getEntity(JSONObject.class);
  621. assertEquals("incorrect number of elements", 1, json.length());
  622. JSONObject apps = json.getJSONObject("apps");
  623. assertEquals("incorrect number of elements", 1, apps.length());
  624. JSONArray array = apps.getJSONArray("app");
  625. assertEquals("incorrect number of elements", 2, array.length());
  626. rm.stop();
  627. }
  628. @Test
  629. public void testAppsQueryFinishBegin() throws JSONException, Exception {
  630. rm.start();
  631. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  632. long start = System.currentTimeMillis();
  633. Thread.sleep(1);
  634. RMApp app1 = rm.submitApp(CONTAINER_MB);
  635. amNodeManager.nodeHeartbeat(true);
  636. // finish App
  637. MockAM am = rm
  638. .sendAMLaunched(app1.getCurrentAppAttempt().getAppAttemptId());
  639. am.registerAppAttempt();
  640. am.unregisterAppAttempt();
  641. amNodeManager.nodeHeartbeat(app1.getCurrentAppAttempt().getAppAttemptId(),
  642. 1, ContainerState.COMPLETE);
  643. rm.submitApp(CONTAINER_MB);
  644. rm.submitApp(CONTAINER_MB);
  645. WebResource r = resource();
  646. ClientResponse response = r.path("ws").path("v1").path("cluster")
  647. .path("apps").queryParam("finishedTimeBegin", String.valueOf(start))
  648. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  649. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  650. JSONObject json = response.getEntity(JSONObject.class);
  651. assertEquals("incorrect number of elements", 1, json.length());
  652. JSONObject apps = json.getJSONObject("apps");
  653. assertEquals("incorrect number of elements", 1, apps.length());
  654. JSONArray array = apps.getJSONArray("app");
  655. assertEquals("incorrect number of elements", 1, array.length());
  656. rm.stop();
  657. }
  658. @Test
  659. public void testAppsQueryFinishEnd() throws JSONException, Exception {
  660. rm.start();
  661. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  662. RMApp app1 = rm.submitApp(CONTAINER_MB);
  663. amNodeManager.nodeHeartbeat(true);
  664. // finish App
  665. MockAM am = rm
  666. .sendAMLaunched(app1.getCurrentAppAttempt().getAppAttemptId());
  667. am.registerAppAttempt();
  668. am.unregisterAppAttempt();
  669. amNodeManager.nodeHeartbeat(app1.getCurrentAppAttempt().getAppAttemptId(),
  670. 1, ContainerState.COMPLETE);
  671. rm.submitApp(CONTAINER_MB);
  672. rm.submitApp(CONTAINER_MB);
  673. long end = System.currentTimeMillis();
  674. WebResource r = resource();
  675. ClientResponse response = r.path("ws").path("v1").path("cluster")
  676. .path("apps").queryParam("finishedTimeEnd", String.valueOf(end))
  677. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  678. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  679. JSONObject json = response.getEntity(JSONObject.class);
  680. assertEquals("incorrect number of elements", 1, json.length());
  681. JSONObject apps = json.getJSONObject("apps");
  682. assertEquals("incorrect number of elements", 1, apps.length());
  683. JSONArray array = apps.getJSONArray("app");
  684. assertEquals("incorrect number of elements", 3, array.length());
  685. rm.stop();
  686. }
  687. @Test
  688. public void testAppsQueryFinishBeginEnd() throws JSONException, Exception {
  689. rm.start();
  690. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  691. long start = System.currentTimeMillis();
  692. Thread.sleep(1);
  693. RMApp app1 = rm.submitApp(CONTAINER_MB);
  694. amNodeManager.nodeHeartbeat(true);
  695. // finish App
  696. MockAM am = rm
  697. .sendAMLaunched(app1.getCurrentAppAttempt().getAppAttemptId());
  698. am.registerAppAttempt();
  699. am.unregisterAppAttempt();
  700. amNodeManager.nodeHeartbeat(app1.getCurrentAppAttempt().getAppAttemptId(),
  701. 1, ContainerState.COMPLETE);
  702. rm.submitApp(CONTAINER_MB);
  703. rm.submitApp(CONTAINER_MB);
  704. long end = System.currentTimeMillis();
  705. WebResource r = resource();
  706. ClientResponse response = r.path("ws").path("v1").path("cluster")
  707. .path("apps").queryParam("finishedTimeBegin", String.valueOf(start))
  708. .queryParam("finishedTimeEnd", String.valueOf(end))
  709. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  710. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  711. JSONObject json = response.getEntity(JSONObject.class);
  712. assertEquals("incorrect number of elements", 1, json.length());
  713. JSONObject apps = json.getJSONObject("apps");
  714. assertEquals("incorrect number of elements", 1, apps.length());
  715. JSONArray array = apps.getJSONArray("app");
  716. assertEquals("incorrect number of elements", 1, array.length());
  717. rm.stop();
  718. }
  719. @Test
  720. public void testAppsQueryAppTypes() throws JSONException, Exception {
  721. rm.start();
  722. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  723. Thread.sleep(1);
  724. RMApp app1 = rm.submitApp(CONTAINER_MB);
  725. amNodeManager.nodeHeartbeat(true);
  726. // finish App
  727. MockAM am = rm
  728. .sendAMLaunched(app1.getCurrentAppAttempt().getAppAttemptId());
  729. am.registerAppAttempt();
  730. am.unregisterAppAttempt();
  731. amNodeManager.nodeHeartbeat(app1.getCurrentAppAttempt().getAppAttemptId(),
  732. 1, ContainerState.COMPLETE);
  733. rm.submitApp(CONTAINER_MB, "", UserGroupInformation.getCurrentUser()
  734. .getShortUserName(), null, false, null, 2, null, "MAPREDUCE");
  735. rm.submitApp(CONTAINER_MB, "", UserGroupInformation.getCurrentUser()
  736. .getShortUserName(), null, false, null, 2, null, "NON-YARN");
  737. WebResource r = resource();
  738. ClientResponse response = r.path("ws").path("v1").path("cluster")
  739. .path("apps").queryParam("applicationTypes", "MAPREDUCE")
  740. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  741. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  742. JSONObject json = response.getEntity(JSONObject.class);
  743. assertEquals("incorrect number of elements", 1, json.length());
  744. JSONObject apps = json.getJSONObject("apps");
  745. assertEquals("incorrect number of elements", 1, apps.length());
  746. JSONArray array = apps.getJSONArray("app");
  747. assertEquals("incorrect number of elements", 1, array.length());
  748. assertEquals("MAPREDUCE",
  749. array.getJSONObject(0).getString("applicationType"));
  750. r = resource();
  751. response =
  752. r.path("ws").path("v1").path("cluster").path("apps")
  753. .queryParam("applicationTypes", "YARN")
  754. .queryParam("applicationTypes", "MAPREDUCE")
  755. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  756. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  757. json = response.getEntity(JSONObject.class);
  758. assertEquals("incorrect number of elements", 1, json.length());
  759. apps = json.getJSONObject("apps");
  760. assertEquals("incorrect number of elements", 1, apps.length());
  761. array = apps.getJSONArray("app");
  762. assertEquals("incorrect number of elements", 2, array.length());
  763. assertTrue((array.getJSONObject(0).getString("applicationType")
  764. .equals("YARN") && array.getJSONObject(1).getString("applicationType")
  765. .equals("MAPREDUCE")) ||
  766. (array.getJSONObject(1).getString("applicationType").equals("YARN")
  767. && array.getJSONObject(0).getString("applicationType")
  768. .equals("MAPREDUCE")));
  769. r = resource();
  770. response =
  771. r.path("ws").path("v1").path("cluster").path("apps")
  772. .queryParam("applicationTypes", "YARN,NON-YARN")
  773. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  774. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  775. json = response.getEntity(JSONObject.class);
  776. assertEquals("incorrect number of elements", 1, json.length());
  777. apps = json.getJSONObject("apps");
  778. assertEquals("incorrect number of elements", 1, apps.length());
  779. array = apps.getJSONArray("app");
  780. assertEquals("incorrect number of elements", 2, array.length());
  781. assertTrue((array.getJSONObject(0).getString("applicationType")
  782. .equals("YARN") && array.getJSONObject(1).getString("applicationType")
  783. .equals("NON-YARN")) ||
  784. (array.getJSONObject(1).getString("applicationType").equals("YARN")
  785. && array.getJSONObject(0).getString("applicationType")
  786. .equals("NON-YARN")));
  787. r = resource();
  788. response = r.path("ws").path("v1").path("cluster")
  789. .path("apps").queryParam("applicationTypes", "")
  790. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  791. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  792. json = response.getEntity(JSONObject.class);
  793. assertEquals("incorrect number of elements", 1, json.length());
  794. apps = json.getJSONObject("apps");
  795. assertEquals("incorrect number of elements", 1, apps.length());
  796. array = apps.getJSONArray("app");
  797. assertEquals("incorrect number of elements", 3, array.length());
  798. r = resource();
  799. response =
  800. r.path("ws").path("v1").path("cluster").path("apps")
  801. .queryParam("applicationTypes", "YARN,NON-YARN")
  802. .queryParam("applicationTypes", "MAPREDUCE")
  803. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  804. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  805. json = response.getEntity(JSONObject.class);
  806. assertEquals("incorrect number of elements", 1, json.length());
  807. apps = json.getJSONObject("apps");
  808. assertEquals("incorrect number of elements", 1, apps.length());
  809. array = apps.getJSONArray("app");
  810. assertEquals("incorrect number of elements", 3, array.length());
  811. r = resource();
  812. response =
  813. r.path("ws").path("v1").path("cluster").path("apps")
  814. .queryParam("applicationTypes", "YARN")
  815. .queryParam("applicationTypes", "")
  816. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  817. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  818. json = response.getEntity(JSONObject.class);
  819. assertEquals("incorrect number of elements", 1, json.length());
  820. apps = json.getJSONObject("apps");
  821. assertEquals("incorrect number of elements", 1, apps.length());
  822. array = apps.getJSONArray("app");
  823. assertEquals("incorrect number of elements", 1, array.length());
  824. assertEquals("YARN",
  825. array.getJSONObject(0).getString("applicationType"));
  826. r = resource();
  827. response =
  828. r.path("ws").path("v1").path("cluster").path("apps")
  829. .queryParam("applicationTypes", ",,, ,, YARN ,, ,")
  830. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  831. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  832. json = response.getEntity(JSONObject.class);
  833. assertEquals("incorrect number of elements", 1, json.length());
  834. apps = json.getJSONObject("apps");
  835. assertEquals("incorrect number of elements", 1, apps.length());
  836. array = apps.getJSONArray("app");
  837. assertEquals("incorrect number of elements", 1, array.length());
  838. assertEquals("YARN",
  839. array.getJSONObject(0).getString("applicationType"));
  840. r = resource();
  841. response =
  842. r.path("ws").path("v1").path("cluster").path("apps")
  843. .queryParam("applicationTypes", ",,, ,, ,, ,")
  844. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  845. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  846. json = response.getEntity(JSONObject.class);
  847. assertEquals("incorrect number of elements", 1, json.length());
  848. apps = json.getJSONObject("apps");
  849. assertEquals("incorrect number of elements", 1, apps.length());
  850. array = apps.getJSONArray("app");
  851. assertEquals("incorrect number of elements", 3, array.length());
  852. r = resource();
  853. response =
  854. r.path("ws").path("v1").path("cluster").path("apps")
  855. .queryParam("applicationTypes", "YARN, ,NON-YARN, ,,")
  856. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  857. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  858. json = response.getEntity(JSONObject.class);
  859. assertEquals("incorrect number of elements", 1, json.length());
  860. apps = json.getJSONObject("apps");
  861. assertEquals("incorrect number of elements", 1, apps.length());
  862. array = apps.getJSONArray("app");
  863. assertEquals("incorrect number of elements", 2, array.length());
  864. assertTrue((array.getJSONObject(0).getString("applicationType")
  865. .equals("YARN") && array.getJSONObject(1).getString("applicationType")
  866. .equals("NON-YARN")) ||
  867. (array.getJSONObject(1).getString("applicationType").equals("YARN")
  868. && array.getJSONObject(0).getString("applicationType")
  869. .equals("NON-YARN")));
  870. r = resource();
  871. response =
  872. r.path("ws").path("v1").path("cluster").path("apps")
  873. .queryParam("applicationTypes", " YARN, , ,,,")
  874. .queryParam("applicationTypes", "MAPREDUCE , ,, ,")
  875. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  876. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  877. json = response.getEntity(JSONObject.class);
  878. assertEquals("incorrect number of elements", 1, json.length());
  879. apps = json.getJSONObject("apps");
  880. assertEquals("incorrect number of elements", 1, apps.length());
  881. array = apps.getJSONArray("app");
  882. assertEquals("incorrect number of elements", 2, array.length());
  883. assertTrue((array.getJSONObject(0).getString("applicationType")
  884. .equals("YARN") && array.getJSONObject(1).getString("applicationType")
  885. .equals("MAPREDUCE")) ||
  886. (array.getJSONObject(1).getString("applicationType").equals("YARN")
  887. && array.getJSONObject(0).getString("applicationType")
  888. .equals("MAPREDUCE")));
  889. rm.stop();
  890. }
  891. @Test
  892. public void testAppStatistics() throws JSONException, Exception {
  893. try {
  894. rm.start();
  895. MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 4096);
  896. Thread.sleep(1);
  897. RMApp app1 = rm.submitApp(CONTAINER_MB, "", UserGroupInformation.getCurrentUser()
  898. .getShortUserName(), null, false, null, 2, null, "MAPREDUCE");
  899. amNodeManager.nodeHeartbeat(true);
  900. // finish App
  901. MockAM am = rm
  902. .sendAMLaunched(app1.getCurrentAppAttempt().getAppAttemptId());
  903. am.registerAppAttempt();
  904. am.unregisterAppAttempt();
  905. amNodeManager.nodeHeartbeat(app1.getCurrentAppAttempt().getAppAttemptId(),
  906. 1, ContainerState.COMPLETE);
  907. rm.submitApp(CONTAINER_MB, "", UserGroupInformation.getCurrentUser()
  908. .getShortUserName(), null, false, null, 2, null, "MAPREDUCE");
  909. rm.submitApp(CONTAINER_MB, "", UserGroupInformation.getCurrentUser()
  910. .getShortUserName(), null, false, null, 2, null, "OTHER");
  911. // zero type, zero state
  912. WebResource r = resource();
  913. ClientResponse response = r.path("ws").path("v1").path("cluster")
  914. .path("appstatistics")
  915. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  916. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  917. JSONObject json = response.getEntity(JSONObject.class);
  918. assertEquals("incorrect number of elements", 1, json.length());
  919. JSONObject appsStatInfo = json.getJSONObject("appStatInfo");
  920. assertEquals("incorrect number of elements", 1, appsStatInfo.length());
  921. JSONArray statItems = appsStatInfo.getJSONArray("statItem");
  922. assertEquals("incorrect number of elements",
  923. YarnApplicationState.values().length, statItems.length());
  924. for (int i = 0; i < YarnApplicationState.values().length; ++i) {
  925. assertEquals("*", statItems.getJSONObject(0).getString("type"));
  926. if (statItems.getJSONObject(0).getString("state").equals("ACCEPTED")) {
  927. assertEquals("2", statItems.getJSONObject(0).getString("count"));
  928. } else if (
  929. statItems.getJSONObject(0).getString("state").equals("FINISHED")) {
  930. assertEquals("1", statItems.getJSONObject(0).getString("count"));
  931. } else {
  932. assertEquals("0", statItems.getJSONObject(0).getString("count"));
  933. }
  934. }
  935. // zero type, one state
  936. r = resource();
  937. response = r.path("ws").path("v1").path("cluster")
  938. .path("appstatistics")
  939. .queryParam("states", YarnApplicationState.ACCEPTED.toString())
  940. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  941. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  942. json = response.getEntity(JSONObject.class);
  943. assertEquals("incorrect number of elements", 1, json.length());
  944. appsStatInfo = json.getJSONObject("appStatInfo");
  945. assertEquals("incorrect number of elements", 1, appsStatInfo.length());
  946. statItems = appsStatInfo.getJSONArray("statItem");
  947. assertEquals("incorrect number of elements", 1, statItems.length());
  948. assertEquals("ACCEPTED", statItems.getJSONObject(0).getString("state"));
  949. assertEquals("*", statItems.getJSONObject(0).getString("type"));
  950. assertEquals("2", statItems.getJSONObject(0).getString("count"));
  951. // one type, zero state
  952. r = resource();
  953. response = r.path("ws").path("v1").path("cluster")
  954. .path("appstatistics")
  955. .queryParam("applicationTypes", "MAPREDUCE")
  956. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  957. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  958. json = response.getEntity(JSONObject.class);
  959. assertEquals("incorrect number of elements", 1, json.length());
  960. appsStatInfo = json.getJSONObject("appStatInfo");
  961. assertEquals("incorrect number of elements", 1, appsStatInfo.length());
  962. statItems = appsStatInfo.getJSONArray("statItem");
  963. assertEquals("incorrect number of elements",
  964. YarnApplicationState.values().length, statItems.length());
  965. for (int i = 0; i < YarnApplicationState.values().length; ++i) {
  966. assertEquals("mapreduce", statItems.getJSONObject(0).getString("type"));
  967. if (statItems.getJSONObject(0).getString("state").equals("ACCEPTED")) {
  968. assertEquals("1", statItems.getJSONObject(0).getString("count"));
  969. } else if (
  970. statItems.getJSONObject(0).getString("state").equals("FINISHED")) {
  971. assertEquals("1", statItems.getJSONObject(0).getString("count"));
  972. } else {
  973. assertEquals("0", statItems.getJSONObject(0).getString("count"));
  974. }
  975. }
  976. // two types, zero state
  977. r = resource();
  978. response = r.path("ws").path("v1").path("cluster")
  979. .path("appstatistics")
  980. .queryParam("applicationTypes", "MAPREDUCE,OTHER")
  981. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  982. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  983. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  984. json = response.getEntity(JSONObject.class);
  985. assertEquals("incorrect number of elements", 1, json.length());
  986. JSONObject exception = json.getJSONObject("RemoteException");
  987. assertEquals("incorrect number of elements", 3, exception.length());
  988. String message = exception.getString("message");
  989. String type = exception.getString("exception");
  990. String className = exception.getString("javaClassName");
  991. WebServicesTestUtils.checkStringContains("exception message",
  992. "we temporarily support at most one applicationType", message);
  993. WebServicesTestUtils.checkStringEqual("exception type",
  994. "BadRequestException", type);
  995. WebServicesTestUtils.checkStringEqual("exception className",
  996. "org.apache.hadoop.yarn.webapp.BadRequestException", className);
  997. // one type, two states
  998. r = resource();
  999. response = r.path("ws").path("v1").path("cluster")
  1000. .path("appstatistics")
  1001. .queryParam("states", YarnApplicationState.FINISHED.toString()
  1002. + "," + YarnApplicationState.ACCEPTED.toString())
  1003. .queryParam("applicationTypes", "MAPREDUCE")
  1004. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  1005. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  1006. json = response.getEntity(JSONObject.class);
  1007. assertEquals("incorrect number of elements", 1, json.length());
  1008. appsStatInfo = json.getJSONObject("appStatInfo");
  1009. assertEquals("incorrect number of elements", 1, appsStatInfo.length());
  1010. statItems = appsStatInfo.getJSONArray("statItem");
  1011. assertEquals("incorrect number of elements", 2, statItems.length());
  1012. JSONObject statItem1 = statItems.getJSONObject(0);
  1013. JSONObject statItem2 = statItems.getJSONObject(1);
  1014. assertTrue((statItem1.getString("state").equals("ACCEPTED") &&
  1015. statItem2.getString("state").equals("FINISHED")) ||
  1016. (statItem2.getString("state").equals("ACCEPTED") &&
  1017. sta