PageRenderTime 27ms CodeModel.GetById 1ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

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