PageRenderTime 5273ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempts.java

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 665 lines | 538 code | 97 blank | 30 comment | 59 complexity | 43012671f970f576d081c995e95ab5d0 MD5 | raw file
  1. /**
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. package org.apache.hadoop.mapreduce.v2.app.webapp;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertNotNull;
  22. import static org.junit.Assert.assertTrue;
  23. import static org.junit.Assert.fail;
  24. import java.io.StringReader;
  25. import java.util.List;
  26. import java.util.Map;
  27. import javax.ws.rs.core.MediaType;
  28. import javax.xml.parsers.DocumentBuilder;
  29. import javax.xml.parsers.DocumentBuilderFactory;
  30. import org.apache.hadoop.conf.Configuration;
  31. import org.apache.hadoop.mapreduce.v2.api.records.JobId;
  32. import org.apache.hadoop.mapreduce.v2.api.records.TaskAttemptId;
  33. import org.apache.hadoop.mapreduce.v2.api.records.TaskType;
  34. import org.apache.hadoop.mapreduce.v2.app.AppContext;
  35. import org.apache.hadoop.mapreduce.v2.app.MockAppContext;
  36. import org.apache.hadoop.mapreduce.v2.app.job.Job;
  37. import org.apache.hadoop.mapreduce.v2.app.job.Task;
  38. import org.apache.hadoop.mapreduce.v2.app.job.TaskAttempt;
  39. import org.apache.hadoop.mapreduce.v2.util.MRApps;
  40. import org.apache.hadoop.yarn.util.ConverterUtils;
  41. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  42. import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
  43. import org.codehaus.jettison.json.JSONArray;
  44. import org.codehaus.jettison.json.JSONException;
  45. import org.codehaus.jettison.json.JSONObject;
  46. import org.junit.Before;
  47. import org.junit.Test;
  48. import org.w3c.dom.Document;
  49. import org.w3c.dom.Element;
  50. import org.w3c.dom.NodeList;
  51. import org.xml.sax.InputSource;
  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.UniformInterfaceException;
  58. import com.sun.jersey.api.client.WebResource;
  59. import com.sun.jersey.api.client.ClientResponse.Status;
  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. /**
  64. * Test the app master web service Rest API for getting task attempts, a
  65. * specific task attempt, and task attempt counters
  66. *
  67. * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts
  68. * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts/{attemptid}
  69. * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts/{attemptid}/counters
  70. */
  71. public class TestAMWebServicesAttempts extends JerseyTest {
  72. private static Configuration conf = new Configuration();
  73. private static AppContext appContext;
  74. private Injector injector = Guice.createInjector(new ServletModule() {
  75. @Override
  76. protected void configureServlets() {
  77. appContext = new MockAppContext(0, 1, 2, 1);
  78. bind(JAXBContextResolver.class);
  79. bind(AMWebServices.class);
  80. bind(GenericExceptionHandler.class);
  81. bind(AppContext.class).toInstance(appContext);
  82. bind(Configuration.class).toInstance(conf);
  83. serve("/*").with(GuiceContainer.class);
  84. }
  85. });
  86. public class GuiceServletConfig extends GuiceServletContextListener {
  87. @Override
  88. protected Injector getInjector() {
  89. return injector;
  90. }
  91. }
  92. @Before
  93. @Override
  94. public void setUp() throws Exception {
  95. super.setUp();
  96. }
  97. public TestAMWebServicesAttempts() {
  98. super(new WebAppDescriptor.Builder(
  99. "org.apache.hadoop.mapreduce.v2.app.webapp")
  100. .contextListenerClass(GuiceServletConfig.class)
  101. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  102. .contextPath("jersey-guice-filter").servletPath("/").build());
  103. }
  104. @Test
  105. public void testTaskAttempts() throws JSONException, Exception {
  106. WebResource r = resource();
  107. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  108. for (JobId id : jobsMap.keySet()) {
  109. String jobId = MRApps.toString(id);
  110. for (Task task : jobsMap.get(id).getTasks().values()) {
  111. String tid = MRApps.toString(task.getID());
  112. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  113. .path("jobs").path(jobId).path("tasks").path(tid).path("attempts")
  114. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  115. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  116. JSONObject json = response.getEntity(JSONObject.class);
  117. verifyAMTaskAttempts(json, task);
  118. }
  119. }
  120. }
  121. @Test
  122. public void testTaskAttemptsSlash() throws JSONException, Exception {
  123. WebResource r = resource();
  124. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  125. for (JobId id : jobsMap.keySet()) {
  126. String jobId = MRApps.toString(id);
  127. for (Task task : jobsMap.get(id).getTasks().values()) {
  128. String tid = MRApps.toString(task.getID());
  129. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  130. .path("jobs").path(jobId).path("tasks").path(tid).path("attempts/")
  131. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  132. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  133. JSONObject json = response.getEntity(JSONObject.class);
  134. verifyAMTaskAttempts(json, task);
  135. }
  136. }
  137. }
  138. @Test
  139. public void testTaskAttemptsDefault() throws JSONException, Exception {
  140. WebResource r = resource();
  141. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  142. for (JobId id : jobsMap.keySet()) {
  143. String jobId = MRApps.toString(id);
  144. for (Task task : jobsMap.get(id).getTasks().values()) {
  145. String tid = MRApps.toString(task.getID());
  146. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  147. .path("jobs").path(jobId).path("tasks").path(tid).path("attempts")
  148. .get(ClientResponse.class);
  149. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  150. JSONObject json = response.getEntity(JSONObject.class);
  151. verifyAMTaskAttempts(json, task);
  152. }
  153. }
  154. }
  155. @Test
  156. public void testTaskAttemptsXML() throws JSONException, Exception {
  157. WebResource r = resource();
  158. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  159. for (JobId id : jobsMap.keySet()) {
  160. String jobId = MRApps.toString(id);
  161. for (Task task : jobsMap.get(id).getTasks().values()) {
  162. String tid = MRApps.toString(task.getID());
  163. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  164. .path("jobs").path(jobId).path("tasks").path(tid).path("attempts")
  165. .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
  166. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  167. String xml = response.getEntity(String.class);
  168. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  169. DocumentBuilder db = dbf.newDocumentBuilder();
  170. InputSource is = new InputSource();
  171. is.setCharacterStream(new StringReader(xml));
  172. Document dom = db.parse(is);
  173. NodeList attempts = dom.getElementsByTagName("taskAttempts");
  174. assertEquals("incorrect number of elements", 1, attempts.getLength());
  175. NodeList nodes = dom.getElementsByTagName("taskAttempt");
  176. verifyAMTaskAttemptsXML(nodes, task);
  177. }
  178. }
  179. }
  180. @Test
  181. public void testTaskAttemptId() throws JSONException, Exception {
  182. WebResource r = resource();
  183. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  184. for (JobId id : jobsMap.keySet()) {
  185. String jobId = MRApps.toString(id);
  186. for (Task task : jobsMap.get(id).getTasks().values()) {
  187. String tid = MRApps.toString(task.getID());
  188. for (TaskAttempt att : task.getAttempts().values()) {
  189. TaskAttemptId attemptid = att.getID();
  190. String attid = MRApps.toString(attemptid);
  191. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  192. .path("jobs").path(jobId).path("tasks").path(tid)
  193. .path("attempts").path(attid).accept(MediaType.APPLICATION_JSON)
  194. .get(ClientResponse.class);
  195. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  196. JSONObject json = response.getEntity(JSONObject.class);
  197. assertEquals("incorrect number of elements", 1, json.length());
  198. JSONObject info = json.getJSONObject("taskAttempt");
  199. verifyAMTaskAttempt(info, att, task.getType());
  200. }
  201. }
  202. }
  203. }
  204. @Test
  205. public void testTaskAttemptIdSlash() throws JSONException, Exception {
  206. WebResource r = resource();
  207. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  208. for (JobId id : jobsMap.keySet()) {
  209. String jobId = MRApps.toString(id);
  210. for (Task task : jobsMap.get(id).getTasks().values()) {
  211. String tid = MRApps.toString(task.getID());
  212. for (TaskAttempt att : task.getAttempts().values()) {
  213. TaskAttemptId attemptid = att.getID();
  214. String attid = MRApps.toString(attemptid);
  215. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  216. .path("jobs").path(jobId).path("tasks").path(tid)
  217. .path("attempts").path(attid + "/")
  218. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  219. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  220. JSONObject json = response.getEntity(JSONObject.class);
  221. assertEquals("incorrect number of elements", 1, json.length());
  222. JSONObject info = json.getJSONObject("taskAttempt");
  223. verifyAMTaskAttempt(info, att, task.getType());
  224. }
  225. }
  226. }
  227. }
  228. @Test
  229. public void testTaskAttemptIdDefault() throws JSONException, Exception {
  230. WebResource r = resource();
  231. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  232. for (JobId id : jobsMap.keySet()) {
  233. String jobId = MRApps.toString(id);
  234. for (Task task : jobsMap.get(id).getTasks().values()) {
  235. String tid = MRApps.toString(task.getID());
  236. for (TaskAttempt att : task.getAttempts().values()) {
  237. TaskAttemptId attemptid = att.getID();
  238. String attid = MRApps.toString(attemptid);
  239. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  240. .path("jobs").path(jobId).path("tasks").path(tid)
  241. .path("attempts").path(attid).get(ClientResponse.class);
  242. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  243. JSONObject json = response.getEntity(JSONObject.class);
  244. assertEquals("incorrect number of elements", 1, json.length());
  245. JSONObject info = json.getJSONObject("taskAttempt");
  246. verifyAMTaskAttempt(info, att, task.getType());
  247. }
  248. }
  249. }
  250. }
  251. @Test
  252. public void testTaskAttemptIdXML() throws JSONException, Exception {
  253. WebResource r = resource();
  254. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  255. for (JobId id : jobsMap.keySet()) {
  256. String jobId = MRApps.toString(id);
  257. for (Task task : jobsMap.get(id).getTasks().values()) {
  258. String tid = MRApps.toString(task.getID());
  259. for (TaskAttempt att : task.getAttempts().values()) {
  260. TaskAttemptId attemptid = att.getID();
  261. String attid = MRApps.toString(attemptid);
  262. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  263. .path("jobs").path(jobId).path("tasks").path(tid)
  264. .path("attempts").path(attid).accept(MediaType.APPLICATION_XML)
  265. .get(ClientResponse.class);
  266. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  267. String xml = response.getEntity(String.class);
  268. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  269. DocumentBuilder db = dbf.newDocumentBuilder();
  270. InputSource is = new InputSource();
  271. is.setCharacterStream(new StringReader(xml));
  272. Document dom = db.parse(is);
  273. NodeList nodes = dom.getElementsByTagName("taskAttempt");
  274. for (int i = 0; i < nodes.getLength(); i++) {
  275. Element element = (Element) nodes.item(i);
  276. verifyAMTaskAttemptXML(element, att, task.getType());
  277. }
  278. }
  279. }
  280. }
  281. }
  282. @Test
  283. public void testTaskAttemptIdBogus() throws JSONException, Exception {
  284. testTaskAttemptIdErrorGeneric("bogusid",
  285. "java.lang.Exception: TaskAttemptId string : bogusid is not properly formed");
  286. }
  287. @Test
  288. public void testTaskAttemptIdNonExist() throws JSONException, Exception {
  289. testTaskAttemptIdErrorGeneric(
  290. "attempt_0_12345_m_000000_0",
  291. "java.lang.Exception: Error getting info on task attempt id attempt_0_12345_m_000000_0");
  292. }
  293. @Test
  294. public void testTaskAttemptIdInvalid() throws JSONException, Exception {
  295. testTaskAttemptIdErrorGeneric("attempt_0_12345_d_000000_0",
  296. "java.lang.Exception: Bad TaskType identifier. TaskAttemptId string : attempt_0_12345_d_000000_0 is not properly formed.");
  297. }
  298. @Test
  299. public void testTaskAttemptIdInvalid2() throws JSONException, Exception {
  300. testTaskAttemptIdErrorGeneric("attempt_12345_m_000000_0",
  301. "java.lang.Exception: TaskAttemptId string : attempt_12345_m_000000_0 is not properly formed");
  302. }
  303. @Test
  304. public void testTaskAttemptIdInvalid3() throws JSONException, Exception {
  305. testTaskAttemptIdErrorGeneric("attempt_0_12345_m_000000",
  306. "java.lang.Exception: TaskAttemptId string : attempt_0_12345_m_000000 is not properly formed");
  307. }
  308. private void testTaskAttemptIdErrorGeneric(String attid, String error)
  309. throws JSONException, Exception {
  310. WebResource r = resource();
  311. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  312. for (JobId id : jobsMap.keySet()) {
  313. String jobId = MRApps.toString(id);
  314. for (Task task : jobsMap.get(id).getTasks().values()) {
  315. String tid = MRApps.toString(task.getID());
  316. try {
  317. r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId)
  318. .path("tasks").path(tid).path("attempts").path(attid)
  319. .accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
  320. fail("should have thrown exception on invalid uri");
  321. } catch (UniformInterfaceException ue) {
  322. ClientResponse response = ue.getResponse();
  323. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  324. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  325. JSONObject msg = response.getEntity(JSONObject.class);
  326. JSONObject exception = msg.getJSONObject("RemoteException");
  327. assertEquals("incorrect number of elements", 3, exception.length());
  328. String message = exception.getString("message");
  329. String type = exception.getString("exception");
  330. String classname = exception.getString("javaClassName");
  331. WebServicesTestUtils.checkStringMatch("exception message", error,
  332. message);
  333. WebServicesTestUtils.checkStringMatch("exception type",
  334. "NotFoundException", type);
  335. WebServicesTestUtils.checkStringMatch("exception classname",
  336. "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
  337. }
  338. }
  339. }
  340. }
  341. public void verifyAMTaskAttemptXML(Element element, TaskAttempt att,
  342. TaskType ttype) {
  343. verifyTaskAttemptGeneric(att, ttype,
  344. WebServicesTestUtils.getXmlString(element, "id"),
  345. WebServicesTestUtils.getXmlString(element, "state"),
  346. WebServicesTestUtils.getXmlString(element, "type"),
  347. WebServicesTestUtils.getXmlString(element, "rack"),
  348. WebServicesTestUtils.getXmlString(element, "nodeHttpAddress"),
  349. WebServicesTestUtils.getXmlString(element, "diagnostics"),
  350. WebServicesTestUtils.getXmlString(element, "assignedContainerId"),
  351. WebServicesTestUtils.getXmlLong(element, "startTime"),
  352. WebServicesTestUtils.getXmlLong(element, "finishTime"),
  353. WebServicesTestUtils.getXmlLong(element, "elapsedTime"),
  354. WebServicesTestUtils.getXmlFloat(element, "progress"));
  355. if (ttype == TaskType.REDUCE) {
  356. verifyReduceTaskAttemptGeneric(att,
  357. WebServicesTestUtils.getXmlLong(element, "shuffleFinishTime"),
  358. WebServicesTestUtils.getXmlLong(element, "mergeFinishTime"),
  359. WebServicesTestUtils.getXmlLong(element, "elapsedShuffleTime"),
  360. WebServicesTestUtils.getXmlLong(element, "elapsedMergeTime"),
  361. WebServicesTestUtils.getXmlLong(element, "elapsedReduceTime"));
  362. }
  363. }
  364. public void verifyAMTaskAttempt(JSONObject info, TaskAttempt att,
  365. TaskType ttype) throws JSONException {
  366. if (ttype == TaskType.REDUCE) {
  367. assertEquals("incorrect number of elements", 17, info.length());
  368. } else {
  369. assertEquals("incorrect number of elements", 12, info.length());
  370. }
  371. verifyTaskAttemptGeneric(att, ttype, info.getString("id"),
  372. info.getString("state"), info.getString("type"),
  373. info.getString("rack"), info.getString("nodeHttpAddress"),
  374. info.getString("diagnostics"), info.getString("assignedContainerId"),
  375. info.getLong("startTime"), info.getLong("finishTime"),
  376. info.getLong("elapsedTime"), (float) info.getDouble("progress"));
  377. if (ttype == TaskType.REDUCE) {
  378. verifyReduceTaskAttemptGeneric(att, info.getLong("shuffleFinishTime"),
  379. info.getLong("mergeFinishTime"), info.getLong("elapsedShuffleTime"),
  380. info.getLong("elapsedMergeTime"), info.getLong("elapsedReduceTime"));
  381. }
  382. }
  383. public void verifyAMTaskAttempts(JSONObject json, Task task)
  384. throws JSONException {
  385. assertEquals("incorrect number of elements", 1, json.length());
  386. JSONObject attempts = json.getJSONObject("taskAttempts");
  387. assertEquals("incorrect number of elements", 1, json.length());
  388. JSONArray arr = attempts.getJSONArray("taskAttempt");
  389. for (TaskAttempt att : task.getAttempts().values()) {
  390. TaskAttemptId id = att.getID();
  391. String attid = MRApps.toString(id);
  392. Boolean found = false;
  393. for (int i = 0; i < arr.length(); i++) {
  394. JSONObject info = arr.getJSONObject(i);
  395. if (attid.matches(info.getString("id"))) {
  396. found = true;
  397. verifyAMTaskAttempt(info, att, task.getType());
  398. }
  399. }
  400. assertTrue("task attempt with id: " + attid
  401. + " not in web service output", found);
  402. }
  403. }
  404. public void verifyAMTaskAttemptsXML(NodeList nodes, Task task) {
  405. assertEquals("incorrect number of elements", 1, nodes.getLength());
  406. for (TaskAttempt att : task.getAttempts().values()) {
  407. TaskAttemptId id = att.getID();
  408. String attid = MRApps.toString(id);
  409. Boolean found = false;
  410. for (int i = 0; i < nodes.getLength(); i++) {
  411. Element element = (Element) nodes.item(i);
  412. if (attid.matches(WebServicesTestUtils.getXmlString(element, "id"))) {
  413. found = true;
  414. verifyAMTaskAttemptXML(element, att, task.getType());
  415. }
  416. }
  417. assertTrue("task with id: " + attid + " not in web service output", found);
  418. }
  419. }
  420. public void verifyTaskAttemptGeneric(TaskAttempt ta, TaskType ttype,
  421. String id, String state, String type, String rack,
  422. String nodeHttpAddress, String diagnostics, String assignedContainerId,
  423. long startTime, long finishTime, long elapsedTime, float progress) {
  424. TaskAttemptId attid = ta.getID();
  425. String attemptId = MRApps.toString(attid);
  426. WebServicesTestUtils.checkStringMatch("id", attemptId, id);
  427. WebServicesTestUtils.checkStringMatch("type", ttype.toString(), type);
  428. WebServicesTestUtils.checkStringMatch("state", ta.getState().toString(),
  429. state);
  430. WebServicesTestUtils.checkStringMatch("rack", ta.getNodeRackName(), rack);
  431. WebServicesTestUtils.checkStringMatch("nodeHttpAddress",
  432. ta.getNodeHttpAddress(), nodeHttpAddress);
  433. String expectDiag = "";
  434. List<String> diagnosticsList = ta.getDiagnostics();
  435. if (diagnosticsList != null && !diagnostics.isEmpty()) {
  436. StringBuffer b = new StringBuffer();
  437. for (String diag : diagnosticsList) {
  438. b.append(diag);
  439. }
  440. expectDiag = b.toString();
  441. }
  442. WebServicesTestUtils.checkStringMatch("diagnostics", expectDiag,
  443. diagnostics);
  444. WebServicesTestUtils.checkStringMatch("assignedContainerId",
  445. ConverterUtils.toString(ta.getAssignedContainerID()),
  446. assignedContainerId);
  447. assertEquals("startTime wrong", ta.getLaunchTime(), startTime);
  448. assertEquals("finishTime wrong", ta.getFinishTime(), finishTime);
  449. assertEquals("elapsedTime wrong", finishTime - startTime, elapsedTime);
  450. assertEquals("progress wrong", ta.getProgress() * 100, progress, 1e-3f);
  451. }
  452. public void verifyReduceTaskAttemptGeneric(TaskAttempt ta,
  453. long shuffleFinishTime, long mergeFinishTime, long elapsedShuffleTime,
  454. long elapsedMergeTime, long elapsedReduceTime) {
  455. assertEquals("shuffleFinishTime wrong", ta.getShuffleFinishTime(),
  456. shuffleFinishTime);
  457. assertEquals("mergeFinishTime wrong", ta.getSortFinishTime(),
  458. mergeFinishTime);
  459. assertEquals("elapsedShuffleTime wrong",
  460. ta.getShuffleFinishTime() - ta.getLaunchTime(), elapsedShuffleTime);
  461. assertEquals("elapsedMergeTime wrong",
  462. ta.getSortFinishTime() - ta.getShuffleFinishTime(), elapsedMergeTime);
  463. assertEquals("elapsedReduceTime wrong",
  464. ta.getFinishTime() - ta.getSortFinishTime(), elapsedReduceTime);
  465. }
  466. @Test
  467. public void testTaskAttemptIdCounters() throws JSONException, Exception {
  468. WebResource r = resource();
  469. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  470. for (JobId id : jobsMap.keySet()) {
  471. String jobId = MRApps.toString(id);
  472. for (Task task : jobsMap.get(id).getTasks().values()) {
  473. String tid = MRApps.toString(task.getID());
  474. for (TaskAttempt att : task.getAttempts().values()) {
  475. TaskAttemptId attemptid = att.getID();
  476. String attid = MRApps.toString(attemptid);
  477. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  478. .path("jobs").path(jobId).path("tasks").path(tid)
  479. .path("attempts").path(attid).path("counters")
  480. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  481. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  482. JSONObject json = response.getEntity(JSONObject.class);
  483. assertEquals("incorrect number of elements", 1, json.length());
  484. JSONObject info = json.getJSONObject("jobTaskAttemptCounters");
  485. verifyAMJobTaskAttemptCounters(info, att);
  486. }
  487. }
  488. }
  489. }
  490. @Test
  491. public void testTaskAttemptIdXMLCounters() throws JSONException, Exception {
  492. WebResource r = resource();
  493. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  494. for (JobId id : jobsMap.keySet()) {
  495. String jobId = MRApps.toString(id);
  496. for (Task task : jobsMap.get(id).getTasks().values()) {
  497. String tid = MRApps.toString(task.getID());
  498. for (TaskAttempt att : task.getAttempts().values()) {
  499. TaskAttemptId attemptid = att.getID();
  500. String attid = MRApps.toString(attemptid);
  501. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  502. .path("jobs").path(jobId).path("tasks").path(tid)
  503. .path("attempts").path(attid).path("counters")
  504. .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
  505. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  506. String xml = response.getEntity(String.class);
  507. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  508. DocumentBuilder db = dbf.newDocumentBuilder();
  509. InputSource is = new InputSource();
  510. is.setCharacterStream(new StringReader(xml));
  511. Document dom = db.parse(is);
  512. NodeList nodes = dom.getElementsByTagName("jobTaskAttemptCounters");
  513. verifyAMTaskCountersXML(nodes, att);
  514. }
  515. }
  516. }
  517. }
  518. public void verifyAMJobTaskAttemptCounters(JSONObject info, TaskAttempt att)
  519. throws JSONException {
  520. assertEquals("incorrect number of elements", 2, info.length());
  521. WebServicesTestUtils.checkStringMatch("id", MRApps.toString(att.getID()),
  522. info.getString("id"));
  523. // just do simple verification of fields - not data is correct
  524. // in the fields
  525. JSONArray counterGroups = info.getJSONArray("taskAttemptCounterGroup");
  526. for (int i = 0; i < counterGroups.length(); i++) {
  527. JSONObject counterGroup = counterGroups.getJSONObject(i);
  528. String name = counterGroup.getString("counterGroupName");
  529. assertTrue("name not set", (name != null && !name.isEmpty()));
  530. JSONArray counters = counterGroup.getJSONArray("counter");
  531. for (int j = 0; j < counters.length(); j++) {
  532. JSONObject counter = counters.getJSONObject(j);
  533. String counterName = counter.getString("name");
  534. assertTrue("name not set",
  535. (counterName != null && !counterName.isEmpty()));
  536. long value = counter.getLong("value");
  537. assertTrue("value >= 0", value >= 0);
  538. }
  539. }
  540. }
  541. public void verifyAMTaskCountersXML(NodeList nodes, TaskAttempt att) {
  542. for (int i = 0; i < nodes.getLength(); i++) {
  543. Element element = (Element) nodes.item(i);
  544. WebServicesTestUtils.checkStringMatch("id", MRApps.toString(att.getID()),
  545. WebServicesTestUtils.getXmlString(element, "id"));
  546. // just do simple verification of fields - not data is correct
  547. // in the fields
  548. NodeList groups = element.getElementsByTagName("taskAttemptCounterGroup");
  549. for (int j = 0; j < groups.getLength(); j++) {
  550. Element counters = (Element) groups.item(j);
  551. assertNotNull("should have counters in the web service info", counters);
  552. String name = WebServicesTestUtils.getXmlString(counters,
  553. "counterGroupName");
  554. assertTrue("name not set", (name != null && !name.isEmpty()));
  555. NodeList counterArr = counters.getElementsByTagName("counter");
  556. for (int z = 0; z < counterArr.getLength(); z++) {
  557. Element counter = (Element) counterArr.item(z);
  558. String counterName = WebServicesTestUtils.getXmlString(counter,
  559. "name");
  560. assertTrue("counter name not set",
  561. (counterName != null && !counterName.isEmpty()));
  562. long value = WebServicesTestUtils.getXmlLong(counter, "value");
  563. assertTrue("value not >= 0", value >= 0);
  564. }
  565. }
  566. }
  567. }
  568. }