Search in sources :

Example 1 with WmContext

use of org.apache.hadoop.hive.ql.wm.WmContext in project hive by apache.

the class PostExecWMEventsSummaryPrinter method run.

@Override
public void run(HookContext hookContext) throws Exception {
    assert (hookContext.getHookType() == HookContext.HookType.POST_EXEC_HOOK || hookContext.getHookType() == HookContext.HookType.ON_FAILURE_HOOK);
    HiveConf conf = hookContext.getConf();
    if (!"tez".equals(HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_EXECUTION_ENGINE))) {
        return;
    }
    LOG.info("Executing post execution hook to print workload manager events summary..");
    SessionState.LogHelper console = SessionState.getConsole();
    QueryPlan plan = hookContext.getQueryPlan();
    if (plan == null) {
        return;
    }
    List<TezTask> rootTasks = Utilities.getTezTasks(plan.getRootTasks());
    for (TezTask tezTask : rootTasks) {
        WmContext wmContext = tezTask.getDriverContext().getCtx().getWmContext();
        if (wmContext != null) {
            wmContext.shortPrint(console);
        }
    }
}
Also used : SessionState(org.apache.hadoop.hive.ql.session.SessionState) WmContext(org.apache.hadoop.hive.ql.wm.WmContext) HiveConf(org.apache.hadoop.hive.conf.HiveConf) QueryPlan(org.apache.hadoop.hive.ql.QueryPlan) TezTask(org.apache.hadoop.hive.ql.exec.tez.TezTask)

Example 2 with WmContext

use of org.apache.hadoop.hive.ql.wm.WmContext in project hive by apache.

the class TriggerValidatorRunnable method run.

@Override
public void run() {
    try {
        Map<TezSessionState, Trigger> violatedSessions = new HashMap<>();
        final List<TezSessionState> sessions = sessionTriggerProvider.getSessions();
        final List<Trigger> triggers = sessionTriggerProvider.getTriggers();
        for (TezSessionState sessionState : sessions) {
            WmContext wmContext = sessionState.getWmContext();
            if (wmContext != null && !wmContext.isQueryCompleted() && !wmContext.getSubscribedCounters().isEmpty()) {
                Map<String, Long> currentCounters = wmContext.getCurrentCounters();
                wmContext.updateElapsedTimeCounter();
                for (Trigger currentTrigger : triggers) {
                    String desiredCounter = currentTrigger.getExpression().getCounterLimit().getName();
                    // there could be interval where desired counter value is not populated by the time we make this check
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Validating trigger: {} against currentCounters: {}", currentTrigger, currentCounters);
                    }
                    if (currentCounters.containsKey(desiredCounter)) {
                        long currentCounterValue = currentCounters.get(desiredCounter);
                        if (currentTrigger.apply(currentCounterValue)) {
                            String queryId = sessionState.getWmContext().getQueryId();
                            if (violatedSessions.containsKey(sessionState)) {
                                // session already has a violation
                                Trigger existingTrigger = violatedSessions.get(sessionState);
                                // KILL always takes priority over MOVE
                                if (existingTrigger.getAction().getType().equals(Action.Type.MOVE_TO_POOL) && currentTrigger.getAction().getType().equals(Action.Type.KILL_QUERY)) {
                                    currentTrigger.setViolationMsg("Trigger " + currentTrigger + " violated. Current value: " + currentCounterValue);
                                    violatedSessions.put(sessionState, currentTrigger);
                                    LOG.info("KILL trigger replacing MOVE for query {}", queryId);
                                } else {
                                    // if multiple MOVE happens, only first move will be chosen
                                    LOG.warn("Conflicting MOVE triggers ({} and {}). Choosing the first MOVE trigger: {}", existingTrigger, currentTrigger, existingTrigger.getName());
                                }
                            } else {
                                // first violation for the session
                                currentTrigger.setViolationMsg("Trigger " + currentTrigger + " violated. Current value: " + currentCounterValue);
                                violatedSessions.put(sessionState, currentTrigger);
                            }
                        }
                    }
                }
                Trigger chosenTrigger = violatedSessions.get(sessionState);
                if (chosenTrigger != null) {
                    LOG.info("Query: {}. {}. Applying action.", sessionState.getWmContext().getQueryId(), chosenTrigger.getViolationMsg());
                }
            }
        }
        if (!violatedSessions.isEmpty()) {
            triggerActionHandler.applyAction(violatedSessions);
        }
    } catch (Throwable t) {
        // if exception is thrown in scheduled tasks, no further tasks will be scheduled, hence this ugly catch
        LOG.warn(TriggerValidatorRunnable.class.getSimpleName() + " caught exception.", t);
    }
}
Also used : HashMap(java.util.HashMap) WmContext(org.apache.hadoop.hive.ql.wm.WmContext) Trigger(org.apache.hadoop.hive.ql.wm.Trigger)

Example 3 with WmContext

use of org.apache.hadoop.hive.ql.wm.WmContext in project hive by apache.

the class WorkloadManager method updateTriggers.

void updateTriggers(final WmTezSession session) {
    WmContext wmContext = session.getWmContext();
    String poolName = session.getPoolName();
    PoolState poolState = pools.get(poolName);
    if (wmContext != null && poolState != null) {
        wmContext.addTriggers(poolState.getTriggers());
        LOG.info("Subscribed to counters: {}", wmContext.getSubscribedCounters());
    }
}
Also used : WmContext(org.apache.hadoop.hive.ql.wm.WmContext)

Example 4 with WmContext

use of org.apache.hadoop.hive.ql.wm.WmContext in project hive by apache.

the class TezJobMonitor method monitorExecution.

public int monitorExecution() {
    boolean done = false;
    boolean success = false;
    int failedCounter = 0;
    final StopWatch failureTimer = new StopWatch();
    int rc = 0;
    DAGStatus status = null;
    Map<String, Progress> vertexProgressMap = null;
    long monitorStartTime = System.currentTimeMillis();
    synchronized (shutdownList) {
        shutdownList.add(dagClient);
    }
    perfLogger.PerfLogBegin(CLASS_NAME, PerfLogger.TEZ_RUN_DAG);
    perfLogger.PerfLogBegin(CLASS_NAME, PerfLogger.TEZ_SUBMIT_TO_RUNNING);
    DAGStatus.State lastState = null;
    boolean running = false;
    long checkInterval = HiveConf.getTimeVar(hiveConf, HiveConf.ConfVars.TEZ_DAG_STATUS_CHECK_INTERVAL, TimeUnit.MILLISECONDS);
    WmContext wmContext = null;
    while (true) {
        try {
            if (context != null) {
                context.checkHeartbeaterLockException();
            }
            status = dagClient.getDAGStatus(EnumSet.of(StatusGetOpts.GET_COUNTERS), checkInterval);
            TezCounters dagCounters = status.getDAGCounters();
            vertexProgressMap = status.getVertexProgress();
            wmContext = context.getWmContext();
            List<String> vertexNames = vertexProgressMap.keySet().stream().map(k -> k.replaceAll(" ", "_")).collect(Collectors.toList());
            if (dagCounters != null && wmContext != null) {
                Set<String> desiredCounters = wmContext.getSubscribedCounters();
                if (desiredCounters != null && !desiredCounters.isEmpty()) {
                    Map<String, Long> currentCounters = getCounterValues(dagCounters, vertexNames, vertexProgressMap, desiredCounters, done);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Requested DAG status. checkInterval: {}. currentCounters: {}", checkInterval, currentCounters);
                    }
                    wmContext.setCurrentCounters(currentCounters);
                }
            }
            DAGStatus.State state = status.getState();
            // AM is responsive again (recovery?)
            failedCounter = 0;
            failureTimer.reset();
            if (state != lastState || state == RUNNING) {
                lastState = state;
                switch(state) {
                    case SUBMITTED:
                        console.printInfo("Status: Submitted");
                        break;
                    case INITING:
                        console.printInfo("Status: Initializing");
                        this.executionStartTime = System.currentTimeMillis();
                        break;
                    case RUNNING:
                        if (!running) {
                            perfLogger.PerfLogEnd(CLASS_NAME, PerfLogger.TEZ_SUBMIT_TO_RUNNING);
                            console.printInfo("Status: Running (" + dagClient.getExecutionContext() + ")\n");
                            this.executionStartTime = System.currentTimeMillis();
                            running = true;
                        }
                        updateFunction.update(status, vertexProgressMap);
                        break;
                    case SUCCEEDED:
                        if (!running) {
                            this.executionStartTime = monitorStartTime;
                        }
                        updateFunction.update(status, vertexProgressMap);
                        success = true;
                        running = false;
                        done = true;
                        break;
                    case KILLED:
                        if (!running) {
                            this.executionStartTime = monitorStartTime;
                        }
                        updateFunction.update(status, vertexProgressMap);
                        console.printInfo("Status: Killed");
                        running = false;
                        done = true;
                        rc = 1;
                        break;
                    case FAILED:
                    case ERROR:
                        if (!running) {
                            this.executionStartTime = monitorStartTime;
                        }
                        updateFunction.update(status, vertexProgressMap);
                        console.printError("Status: Failed");
                        running = false;
                        done = true;
                        rc = 2;
                        break;
                }
            }
            if (wmContext != null && done) {
                wmContext.setQueryCompleted(true);
            }
        } catch (Exception e) {
            console.printInfo("Exception: " + e.getMessage());
            boolean isInterrupted = hasInterruptedException(e);
            if (failedCounter == 0) {
                failureTimer.reset();
                failureTimer.start();
            }
            if (isInterrupted || (++failedCounter >= MAX_RETRY_FAILURES && failureTimer.now(TimeUnit.MILLISECONDS) > MAX_RETRY_INTERVAL)) {
                try {
                    if (isInterrupted) {
                        console.printInfo("Killing DAG...");
                    } else {
                        console.printInfo(String.format("Killing DAG... after %d seconds", failureTimer.now(TimeUnit.SECONDS)));
                    }
                    dagClient.tryKillDAG();
                } catch (IOException | TezException tezException) {
                // best effort
                }
                console.printError("Execution has failed. stack trace: " + ExceptionUtils.getStackTrace(e));
                rc = 1;
                done = true;
            } else {
                console.printInfo("Retrying...");
            }
            if (wmContext != null && done) {
                wmContext.setQueryCompleted(true);
            }
        } finally {
            if (done) {
                if (wmContext != null && done) {
                    wmContext.setQueryCompleted(true);
                }
                if (rc != 0 && status != null) {
                    for (String diag : status.getDiagnostics()) {
                        console.printError(diag);
                        diagnostics.append(diag);
                    }
                }
                synchronized (shutdownList) {
                    shutdownList.remove(dagClient);
                }
                break;
            }
        }
    }
    perfLogger.PerfLogEnd(CLASS_NAME, PerfLogger.TEZ_RUN_DAG);
    printSummary(success, vertexProgressMap);
    return rc;
}
Also used : DAGClient(org.apache.tez.dag.api.client.DAGClient) StatusGetOpts(org.apache.tez.dag.api.client.StatusGetOpts) TezCounter(org.apache.tez.common.counters.TezCounter) ConfVars(org.apache.hadoop.hive.conf.HiveConf.ConfVars) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) InterruptedIOException(java.io.InterruptedIOException) TimeCounterLimit(org.apache.hadoop.hive.ql.wm.TimeCounterLimit) VertexCounterLimit(org.apache.hadoop.hive.ql.wm.VertexCounterLimit) Utilities(org.apache.hadoop.hive.ql.exec.Utilities) DAGStatus(org.apache.tez.dag.api.client.DAGStatus) ProgressMonitor(org.apache.hadoop.hive.common.log.ProgressMonitor) Map(java.util.Map) InPlaceUpdate(org.apache.hadoop.hive.common.log.InPlaceUpdate) Context(org.apache.hadoop.hive.ql.Context) BaseWork(org.apache.hadoop.hive.ql.plan.BaseWork) CounterGroup(org.apache.tez.common.counters.CounterGroup) LinkedList(java.util.LinkedList) EnumSet(java.util.EnumSet) PerfLogger(org.apache.hadoop.hive.ql.log.PerfLogger) Progress(org.apache.tez.dag.api.client.Progress) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Set(java.util.Set) StopWatch(org.apache.tez.util.StopWatch) IOException(java.io.IOException) DAG(org.apache.tez.dag.api.DAG) Collectors(java.util.stream.Collectors) SessionState(org.apache.hadoop.hive.ql.session.SessionState) RUNNING(org.apache.tez.dag.api.client.DAGStatus.State.RUNNING) ShutdownHookManager(org.apache.hive.common.util.ShutdownHookManager) LogHelper(org.apache.hadoop.hive.ql.session.SessionState.LogHelper) TezException(org.apache.tez.dag.api.TezException) TimeUnit(java.util.concurrent.TimeUnit) TezCounters(org.apache.tez.common.counters.TezCounters) List(java.util.List) WmContext(org.apache.hadoop.hive.ql.wm.WmContext) Preconditions(com.google.common.base.Preconditions) TezSessionPoolManager(org.apache.hadoop.hive.ql.exec.tez.TezSessionPoolManager) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) Progress(org.apache.tez.dag.api.client.Progress) WmContext(org.apache.hadoop.hive.ql.wm.WmContext) TezCounters(org.apache.tez.common.counters.TezCounters) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) TezException(org.apache.tez.dag.api.TezException) StopWatch(org.apache.tez.util.StopWatch) DAGStatus(org.apache.tez.dag.api.client.DAGStatus)

Example 5 with WmContext

use of org.apache.hadoop.hive.ql.wm.WmContext in project hive by apache.

the class Driver method setTriggerContext.

private void setTriggerContext(final String queryId) {
    final long queryStartTime;
    // the time when query display object is created)
    if (queryInfo != null) {
        queryStartTime = queryInfo.getBeginTime();
    } else {
        queryStartTime = queryDisplay.getQueryStartTime();
    }
    WmContext wmContext = new WmContext(queryStartTime, queryId);
    ctx.setWmContext(wmContext);
}
Also used : WmContext(org.apache.hadoop.hive.ql.wm.WmContext)

Aggregations

WmContext (org.apache.hadoop.hive.ql.wm.WmContext)7 SessionState (org.apache.hadoop.hive.ql.session.SessionState)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HiveConf (org.apache.hadoop.hive.conf.HiveConf)2 Context (org.apache.hadoop.hive.ql.Context)2 BaseWork (org.apache.hadoop.hive.ql.plan.BaseWork)2 CounterGroup (org.apache.tez.common.counters.CounterGroup)2 TezCounter (org.apache.tez.common.counters.TezCounter)2 DAG (org.apache.tez.dag.api.DAG)2 TezException (org.apache.tez.dag.api.TezException)2 DAGClient (org.apache.tez.dag.api.client.DAGClient)2 Preconditions (com.google.common.base.Preconditions)1 InterruptedIOException (java.io.InterruptedIOException)1 StringWriter (java.io.StringWriter)1 EnumSet (java.util.EnumSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1