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

# · Java · 214 lines · 134 code · 30 blank · 50 comment · 26 complexity · 2ddaa61061232c2bd629e7b6441504af 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.hive.ql.exec;
  19. import java.io.Serializable;
  20. import java.util.List;
  21. import org.apache.hadoop.hive.conf.HiveConf;
  22. import org.apache.hadoop.hive.ql.Context;
  23. import org.apache.hadoop.hive.ql.DriverContext;
  24. import org.apache.hadoop.hive.ql.QueryPlan;
  25. import org.apache.hadoop.hive.ql.plan.ConditionalResolver;
  26. import org.apache.hadoop.hive.ql.plan.ConditionalWork;
  27. import org.apache.hadoop.hive.ql.plan.api.StageType;
  28. /**
  29. * Conditional Task implementation.
  30. */
  31. public class ConditionalTask extends Task<ConditionalWork> implements Serializable {
  32. private static final long serialVersionUID = 1L;
  33. private List<Task<? extends Serializable>> listTasks;
  34. private boolean resolved = false;
  35. private List<Task<? extends Serializable>> resTasks;
  36. private ConditionalResolver resolver;
  37. private Object resolverCtx;
  38. public ConditionalTask() {
  39. super();
  40. }
  41. @Override
  42. public boolean isMapRedTask() {
  43. for (Task<? extends Serializable> task : listTasks) {
  44. if (task.isMapRedTask()) {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. @Override
  51. public boolean hasReduce() {
  52. for (Task<? extends Serializable> task : listTasks) {
  53. if (task.hasReduce()) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. @Override
  60. public void initialize(HiveConf conf, QueryPlan queryPlan, DriverContext driverContext) {
  61. super.initialize(conf, queryPlan, driverContext);
  62. }
  63. @Override
  64. public int execute(DriverContext driverContext) {
  65. resTasks = resolver.getTasks(conf, resolverCtx);
  66. resolved = true;
  67. for (Task<? extends Serializable> tsk : getListTasks()) {
  68. if (!resTasks.contains(tsk)) {
  69. driverContext.getRunnable().remove(tsk);
  70. console.printInfo(ExecDriver.getJobEndMsg("" + Utilities.randGen.nextInt())
  71. + ", job is filtered out (removed at runtime).");
  72. if (tsk.isMapRedTask()) {
  73. driverContext.incCurJobNo(1);
  74. }
  75. //recursively remove this task from its children's parent task
  76. tsk.removeFromChildrenTasks();
  77. } else {
  78. // resolved task
  79. if (!driverContext.getRunnable().contains(tsk)) {
  80. driverContext.addToRunnable(tsk);
  81. }
  82. }
  83. }
  84. return 0;
  85. }
  86. /**
  87. * @return the resolver
  88. */
  89. public ConditionalResolver getResolver() {
  90. return resolver;
  91. }
  92. /**
  93. * @param resolver
  94. * the resolver to set
  95. */
  96. public void setResolver(ConditionalResolver resolver) {
  97. this.resolver = resolver;
  98. }
  99. /**
  100. * @return the resolverCtx
  101. */
  102. public Object getResolverCtx() {
  103. return resolverCtx;
  104. }
  105. // used to determine whether child tasks can be run.
  106. @Override
  107. public boolean done() {
  108. boolean ret = true;
  109. List<Task<? extends Serializable>> parentTasks = getParentTasks();
  110. if (parentTasks != null) {
  111. for (Task<? extends Serializable> par : parentTasks) {
  112. ret = ret && par.done();
  113. }
  114. }
  115. List<Task<? extends Serializable>> retTasks;
  116. if (resolved) {
  117. retTasks = resTasks;
  118. } else {
  119. retTasks = getListTasks();
  120. }
  121. if (ret && retTasks != null) {
  122. for (Task<? extends Serializable> tsk : retTasks) {
  123. ret = ret && tsk.done();
  124. }
  125. }
  126. return ret;
  127. }
  128. /**
  129. * @param resolverCtx
  130. * the resolverCtx to set
  131. */
  132. public void setResolverCtx(Object resolverCtx) {
  133. this.resolverCtx = resolverCtx;
  134. }
  135. /**
  136. * @return the listTasks
  137. */
  138. public List<Task<? extends Serializable>> getListTasks() {
  139. return listTasks;
  140. }
  141. /**
  142. * @param listTasks
  143. * the listTasks to set
  144. */
  145. public void setListTasks(List<Task<? extends Serializable>> listTasks) {
  146. this.listTasks = listTasks;
  147. }
  148. @Override
  149. public StageType getType() {
  150. return StageType.CONDITIONAL;
  151. }
  152. @Override
  153. public String getName() {
  154. return "CONDITION";
  155. }
  156. /**
  157. * Add a dependent task on the current conditional task. The task will not be a direct child of
  158. * conditional task. Actually it will be added as child task of associated tasks.
  159. *
  160. * @return true if the task got added false if it already existed
  161. */
  162. @Override
  163. public boolean addDependentTask(Task<? extends Serializable> dependent) {
  164. boolean ret = false;
  165. if (getListTasks() != null) {
  166. for (Task<? extends Serializable> tsk : getListTasks()) {
  167. ret = ret & tsk.addDependentTask(dependent);
  168. }
  169. }
  170. return ret;
  171. }
  172. @Override
  173. protected void localizeMRTmpFilesImpl(Context ctx) {
  174. if (getListTasks() != null) {
  175. for (Task<? extends Serializable> t : getListTasks()) {
  176. t.localizeMRTmpFiles(ctx);
  177. }
  178. }
  179. }
  180. @Override
  181. public List<Task<? extends Serializable>> getDependentTasks() {
  182. return listTasks;
  183. }
  184. }