PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/exec/errors/TaskLogProcessor.java

#
Java | 173 lines | 99 code | 26 blank | 48 comment | 14 complexity | 65ae37543d629474d56c170e7fbff08d MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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.hive.ql.exec.errors;
  19. import java.io.BufferedReader;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Map.Entry;
  29. import org.apache.hadoop.hive.conf.HiveConf;
  30. import org.apache.hadoop.mapred.JobConf;
  31. /**
  32. * TaskLogProcessor reads the logs from failed task attempts and tries to figure
  33. * out what the cause of the error was using various heuristics.
  34. */
  35. public class TaskLogProcessor {
  36. private final Map<ErrorHeuristic, HeuristicStats> heuristics =
  37. new HashMap<ErrorHeuristic, HeuristicStats>();
  38. private final List<String> taskLogUrls = new ArrayList<String>();
  39. private JobConf conf = null;
  40. // Query is the hive query string i.e. "SELECT * FROM src;" associated with
  41. // this set of tasks logs
  42. private String query = null;
  43. public TaskLogProcessor(JobConf conf) {
  44. this.conf = conf;
  45. query = HiveConf.getVar(conf, HiveConf.ConfVars.HIVEQUERYSTRING);
  46. heuristics.put(new ScriptErrorHeuristic(), new HeuristicStats());
  47. heuristics.put(new MapAggrMemErrorHeuristic(), new HeuristicStats());
  48. heuristics.put(new DataCorruptErrorHeuristic(), new HeuristicStats());
  49. for(ErrorHeuristic e : heuristics.keySet()) {
  50. e.init(query, conf);
  51. }
  52. }
  53. /**
  54. * Adds a task log URL for the heuristics to read through.
  55. * @param url
  56. */
  57. public void addTaskAttemptLogUrl(String url) {
  58. taskLogUrls.add(url);
  59. }
  60. private static class HeuristicStats {
  61. // The number of times eh has returned non-null errors
  62. private int triggerCount = 0;
  63. // All ErrorAndSolutions that ErrorHeuristic has generated. For the same error, they
  64. // should be the same though it's possible that different file paths etc
  65. // could generate different error messages
  66. private final List<ErrorAndSolution> ens = new ArrayList<ErrorAndSolution>();
  67. HeuristicStats() {
  68. }
  69. int getTriggerCount() {
  70. return triggerCount;
  71. }
  72. void incTriggerCount() {
  73. triggerCount++;
  74. }
  75. List<ErrorAndSolution> getErrorAndSolutions() {
  76. return ens;
  77. }
  78. void addErrorAndSolution(ErrorAndSolution e) {
  79. ens.add(e);
  80. }
  81. }
  82. /**
  83. * Processes the provided task logs using the known error heuristics to get
  84. * the matching errors.
  85. * @return A ErrorAndSolution from the ErrorHeuristic that most frequently
  86. * generated matches. In case of ties, multiple ErrorAndSolutions will be
  87. * returned.
  88. */
  89. public List<ErrorAndSolution> getErrors() {
  90. for(String urlString : taskLogUrls) {
  91. // Open the log file, and read in a line. Then feed the line into
  92. // each of the ErrorHeuristics. Repeat for all the lines in the log.
  93. URL taskAttemptLogUrl;
  94. try {
  95. taskAttemptLogUrl = new URL(urlString);
  96. } catch(MalformedURLException e) {
  97. throw new RuntimeException("Bad task log url", e);
  98. }
  99. BufferedReader in;
  100. try {
  101. in = new BufferedReader(
  102. new InputStreamReader(taskAttemptLogUrl.openStream()));
  103. String inputLine;
  104. while ((inputLine = in.readLine()) != null) {
  105. for(ErrorHeuristic e : heuristics.keySet()) {
  106. e.processLogLine(inputLine);
  107. }
  108. }
  109. in.close();
  110. } catch (IOException e) {
  111. throw new RuntimeException("Error while reading from task log url", e);
  112. }
  113. // Once the lines of the log file have been fed into the ErrorHeuristics,
  114. // see if they have detected anything. If any has, record
  115. // what ErrorAndSolution it gave so we can later return the most
  116. // frequently occurring error
  117. for(Entry<ErrorHeuristic, HeuristicStats> ent : heuristics.entrySet()) {
  118. ErrorHeuristic eh = ent.getKey();
  119. HeuristicStats hs = ent.getValue();
  120. ErrorAndSolution es = eh.getErrorAndSolution();
  121. if(es != null) {
  122. hs.incTriggerCount();
  123. hs.addErrorAndSolution(es);
  124. }
  125. }
  126. }
  127. // Return the errors that occur the most frequently
  128. int max = 0;
  129. for(HeuristicStats hs : heuristics.values()) {
  130. if(hs.getTriggerCount() > max) {
  131. max = hs.getTriggerCount();
  132. }
  133. }
  134. List<ErrorAndSolution> errors = new ArrayList<ErrorAndSolution>();
  135. for(HeuristicStats hs : heuristics.values()) {
  136. if(hs.getTriggerCount() == max) {
  137. if(hs.getErrorAndSolutions().size() > 0) {
  138. // An error heuristic could have generated different ErrorAndSolution
  139. // for each task attempt, but most likely they are the same. Plus,
  140. // one of those is probably good enough for debugging
  141. errors.add(hs.getErrorAndSolutions().get(0));
  142. }
  143. }
  144. }
  145. return errors;
  146. }
  147. }