* @param conf configuration file for this operation
* @return int 0 - success, 1 - killed, 2 - failed
*/
public int monitorExecution(final DAGClient dagClient, HiveTxnManager txnMgr, HiveConf conf,
DAG dag) throws InterruptedException {
DAGStatus status = null;
completed = new HashSet<String>();
boolean running = false;
boolean done = false;
int failedCounter = 0;
int rc = 0;
DAGStatus.State lastState = null;
String lastReport = null;
Set<StatusGetOpts> opts = new HashSet<StatusGetOpts>();
Heartbeater heartbeater = new Heartbeater(txnMgr, conf);
long startTime = 0;
boolean isProfileEnabled = conf.getBoolVar(conf, HiveConf.ConfVars.TEZ_EXEC_SUMMARY);
boolean inPlaceUpdates = conf.getBoolVar(conf, HiveConf.ConfVars.TEZ_EXEC_INPLACE_PROGRESS);
boolean wideTerminal = false;
boolean isTerminal = inPlaceUpdates == true ? isUnixTerminal() : false;
// we need at least 80 chars wide terminal to display in-place updates properly
if (isTerminal) {
if (getTerminalWidth() >= MIN_TERMINAL_WIDTH) {
wideTerminal = true;
}
}
boolean inPlaceEligible = false;
if (inPlaceUpdates && isTerminal && wideTerminal && !console.getIsSilent()) {
inPlaceEligible = true;
}
shutdownList.add(dagClient);
console.printInfo("\n");
perfLogger.PerfLogBegin(CLASS_NAME, PerfLogger.TEZ_RUN_DAG);
perfLogger.PerfLogBegin(CLASS_NAME, PerfLogger.TEZ_SUBMIT_TO_RUNNING);
while (true) {
try {
status = dagClient.getDAGStatus(opts);
Map<String, Progress> progressMap = status.getVertexProgress();
DAGStatus.State state = status.getState();
heartbeater.heartbeat();
if (state != lastState || state == RUNNING) {
lastState = state;
switch (state) {
case SUBMITTED:
console.printInfo("Status: Submitted");
break;
case INITING:
console.printInfo("Status: Initializing");
startTime = System.currentTimeMillis();
break;
case RUNNING:
if (!running) {
perfLogger.PerfLogEnd(CLASS_NAME, PerfLogger.TEZ_SUBMIT_TO_RUNNING);
console.printInfo("Status: Running (" + dagClient.getExecutionContext() + ")\n");
startTime = System.currentTimeMillis();
running = true;
}
if (inPlaceEligible) {
printStatusInPlace(progressMap, startTime, false, dagClient);
// log the progress report to log file as well
console.logInfo(getReport(progressMap));
} else {
lastReport = printStatus(progressMap, lastReport, console);
}
break;
case SUCCEEDED:
if (inPlaceEligible) {
printStatusInPlace(progressMap, startTime, false, dagClient);
// log the progress report to log file as well
console.logInfo(getReport(progressMap));
} else {
lastReport = printStatus(progressMap, lastReport, console);
}
/* Profile info is collected anyways, isProfileEnabled
* decides if it gets printed or not
*/
if (isProfileEnabled) {
double duration = (System.currentTimeMillis() - startTime) / 1000.0;
console.printInfo("Status: DAG finished successfully in "
+ String.format("%.2f seconds", duration));
console.printInfo("\n");
printMethodsSummary();
printDagSummary(progressMap, console, dagClient, conf, dag);
}
running = false;
done = true;
break;
case KILLED:
if (inPlaceEligible) {
printStatusInPlace(progressMap, startTime, true, dagClient);
// log the progress report to log file as well
console.logInfo(getReport(progressMap));
}
console.printInfo("Status: Killed");
running = false;
done = true;
rc = 1;
break;
case FAILED:
case ERROR:
if (inPlaceEligible) {
printStatusInPlace(progressMap, startTime, true, dagClient);
// log the progress report to log file as well
console.logInfo(getReport(progressMap));
}
console.printError("Status: Failed");
running = false;
done = true;
rc = 2;
break;
}
}
if (!done) {
Thread.sleep(checkInterval);
}
} catch (Exception e) {
console.printInfo("Exception: " + e.getMessage());
if (++failedCounter % maxRetryInterval / checkInterval == 0
|| e instanceof InterruptedException) {
try {
console.printInfo("Killing DAG...");
dagClient.tryKillDAG();
} catch (IOException io) {
// best effort
} catch (TezException te) {
// best effort
}
e.printStackTrace();
console.printError("Execution has failed.");
rc = 1;
done = true;
} else {
console.printInfo("Retrying...");
}
} finally {
if (done) {
if (rc != 0 && status != null) {
for (String diag : status.getDiagnostics()) {
console.printError(diag);
}
}
shutdownList.remove(dagClient);
break;