Search in sources :

Example 6 with Task

use of org.batfish.common.Task in project batfish by batfish.

the class Driver method runBatfishThroughService.

public static List<String> runBatfishThroughService(final String taskId, String[] args) {
    final Settings settings;
    try {
        settings = new Settings(_mainSettings);
        settings.setRunMode(RunMode.WORKER);
        settings.parseCommandLine(args);
        // assign taskId for status updates, termination requests
        settings.setTaskId(taskId);
    } catch (Exception e) {
        return Arrays.asList("failure", "Initialization failed: " + ExceptionUtils.getStackTrace(e));
    }
    try {
        Batfish.initTestrigSettings(settings);
    } catch (Exception e) {
        return Arrays.asList("failure", "Failed while applying auto basedir. (All arguments are supplied?): " + e.getMessage());
    }
    if (settings.canExecute()) {
        if (claimIdle()) {
            try {
                final BatfishLogger jobLogger = new BatfishLogger(settings.getLogLevel(), settings.getTimestamp(), settings.getLogFile(), settings.getLogTee(), false);
                settings.setLogger(jobLogger);
                final Task task = new Task(args);
                logTask(taskId, task);
                @Nullable SpanContext runTaskSpanContext = GlobalTracer.get().activeSpan() == null ? null : GlobalTracer.get().activeSpan().context();
                // run batfish on a new thread and set idle to true when done
                Thread thread = new Thread() {

                    @Override
                    public void run() {
                        try (ActiveSpan runBatfishSpan = GlobalTracer.get().buildSpan("Initialize Batfish in a new thread").addReference(References.FOLLOWS_FROM, runTaskSpanContext).startActive()) {
                            // avoid unused warning
                            assert runBatfishSpan != null;
                            task.setStatus(TaskStatus.InProgress);
                            String errMsg = runBatfish(settings);
                            if (errMsg == null) {
                                task.setStatus(TaskStatus.TerminatedNormally);
                            } else {
                                task.setStatus(TaskStatus.TerminatedAbnormally);
                                task.setErrMessage(errMsg);
                            }
                            task.setTerminated(new Date());
                            jobLogger.close();
                            makeIdle();
                        }
                    }
                };
                thread.start();
                return Arrays.asList(BfConsts.SVC_SUCCESS_KEY, "running now");
            } catch (Exception e) {
                _mainLogger.error("Exception while running task: " + e.getMessage());
                makeIdle();
                return Arrays.asList(BfConsts.SVC_FAILURE_KEY, e.getMessage());
            }
        } else {
            return Arrays.asList(BfConsts.SVC_FAILURE_KEY, "Not idle");
        }
    } else {
        return Arrays.asList(BfConsts.SVC_FAILURE_KEY, "Non-executable command");
    }
}
Also used : Task(org.batfish.common.Task) SpanContext(io.opentracing.SpanContext) BatfishLogger(org.batfish.common.BatfishLogger) ActiveSpan(io.opentracing.ActiveSpan) EnvironmentSettings(org.batfish.config.Settings.EnvironmentSettings) Settings(org.batfish.config.Settings) TestrigSettings(org.batfish.config.Settings.TestrigSettings) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ProcessingException(javax.ws.rs.ProcessingException) QuestionException(org.batfish.common.QuestionException) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException) CleanBatfishException(org.batfish.common.CleanBatfishException) Nullable(javax.annotation.Nullable) Date(java.util.Date)

Example 7 with Task

use of org.batfish.common.Task in project batfish by batfish.

the class Driver method killTask.

@Nullable
public static synchronized Task killTask(String taskId) {
    Task task = _taskLog.get(taskId);
    if (_mainSettings.getParentPid() <= 0) {
        throw new BatfishException("Cannot kill tasks when started in non-watchdog mode");
    } else if (task == null) {
        throw new BatfishException("Task with provided id not found: " + taskId);
    } else if (task.getStatus().isTerminated()) {
        throw new BatfishException("Task with provided id already terminated " + taskId);
    } else {
        // update task details in case a new query for status check comes in
        task.newBatch("Got kill request");
        task.setStatus(TaskStatus.TerminatedByUser);
        task.setTerminated(new Date());
        task.setErrMessage("Terminated by user");
        // we die after a little bit, to allow for the response making it back to the coordinator
        new java.util.Timer().schedule(new java.util.TimerTask() {

            @Override
            public void run() {
                System.exit(0);
            }
        }, 3000);
        return task;
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) CleanBatfishException(org.batfish.common.CleanBatfishException) Task(org.batfish.common.Task) Date(java.util.Date) Nullable(javax.annotation.Nullable)

Example 8 with Task

use of org.batfish.common.Task in project batfish by batfish.

the class Driver method newBatch.

public static synchronized AtomicInteger newBatch(Settings settings, String description, int jobs) {
    Batch batch = null;
    Task task = getTask(settings);
    if (task != null) {
        batch = task.newBatch(description);
        batch.setSize(jobs);
        return batch.getCompleted();
    } else {
        return new AtomicInteger();
    }
}
Also used : Task(org.batfish.common.Task) Batch(org.batfish.common.Task.Batch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 9 with Task

use of org.batfish.common.Task in project batfish by batfish.

the class WorkMgr method killWork.

public boolean killWork(QueuedWork work) {
    String worker = work.getAssignedWorker();
    if (worker != null) {
        return killWork(work, worker);
    }
    // (worker = null) => this work was not assigned in the first place
    boolean killed = false;
    Task fakeTask = new Task(TaskStatus.TerminatedByUser, "Killed unassigned work");
    try {
        _workQueueMgr.processTaskCheckResult(work, fakeTask);
        killed = true;
    } catch (Exception e) {
        _logger.errorf("exception: %s\n", ExceptionUtils.getStackTrace(e));
    }
    return killed;
}
Also used : Task(org.batfish.common.Task) ProcessingException(javax.ws.rs.ProcessingException) BatfishException(org.batfish.common.BatfishException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Example 10 with Task

use of org.batfish.common.Task in project batfish by batfish.

the class WorkQueueMgr method processTaskCheckResult.

public synchronized void processTaskCheckResult(QueuedWork work, Task task) throws Exception {
    switch(task.getStatus()) {
        case Unscheduled:
        case InProgress:
            work.setStatus(WorkStatusCode.ASSIGNED);
            work.recordTaskCheckResult(task);
            break;
        case TerminatedAbnormally:
        case TerminatedByUser:
        case TerminatedNormally:
        case RequeueFailure:
            {
                // move the work to completed queue
                _queueIncompleteWork.delete(work);
                _queueCompletedWork.enque(work);
                work.setStatus(WorkStatusCode.fromTerminatedTaskStatus(task.getStatus()));
                work.recordTaskCheckResult(task);
                // update testrig metadata
                WorkItem wItem = work.getWorkItem();
                WorkDetails wDetails = work.getDetails();
                if (wDetails.workType == WorkType.PARSING) {
                    ProcessingStatus status = (task.getStatus() == TaskStatus.TerminatedNormally) ? ProcessingStatus.PARSED : ProcessingStatus.PARSING_FAIL;
                    TestrigMetadataMgr.updateEnvironmentStatus(wItem.getContainerName(), wDetails.baseTestrig, wDetails.baseEnv, status, task.getErrMessage());
                } else if (wDetails.workType == WorkType.DATAPLANING) {
                    // no change in status needed if task.getStatus() is RequeueFailure
                    if (task.getStatus() == TaskStatus.TerminatedAbnormally || task.getStatus() == TaskStatus.TerminatedByUser) {
                        TestrigMetadataMgr.updateEnvironmentStatus(wItem.getContainerName(), wDetails.baseTestrig, wDetails.baseEnv, ProcessingStatus.DATAPLANING_FAIL, task.getErrMessage());
                    } else if (task.getStatus() == TaskStatus.TerminatedNormally) {
                        TestrigMetadataMgr.updateEnvironmentStatus(wItem.getContainerName(), wDetails.baseTestrig, wDetails.baseEnv, ProcessingStatus.DATAPLANED, null);
                    }
                }
                // check if we unblocked anything
                if (_blockingWork.contains(wItem.getId())) {
                    _blockingWork.remove(wItem.getId());
                    List<QueuedWork> requeueWorks = new LinkedList<>();
                    for (QueuedWork incompleteWork : _queueIncompleteWork) {
                        if (incompleteWork.getStatus() == WorkStatusCode.BLOCKED && wDetails.isOverlappingInput(incompleteWork.getDetails())) {
                            requeueWorks.add(incompleteWork);
                        }
                    }
                    for (QueuedWork requeueWork : requeueWorks) {
                        _queueIncompleteWork.delete(requeueWork);
                        requeueWork.setStatus(WorkStatusCode.UNASSIGNED);
                    }
                    for (QueuedWork requeueWork : requeueWorks) {
                        try {
                            boolean queued = queueUnassignedWork(requeueWork);
                            if (!queued) {
                                throw new BatfishException("Failed to requeue previously blocked work " + requeueWork.getId());
                            }
                        } catch (Exception e) {
                            String stackTrace = ExceptionUtils.getStackTrace(e);
                            _logger.errorf("exception: %s\n", stackTrace);
                            // put this work back on incomplete queue and process as if it terminatedabnormally
                            // people may be checking its status and this work may be blocking others
                            _queueIncompleteWork.enque(requeueWork);
                            Task fakeTask = new Task(TaskStatus.RequeueFailure, "Couldn't requeue after unblocking");
                            processTaskCheckResult(requeueWork, fakeTask);
                        }
                    }
                }
            }
            break;
        case Unknown:
            // we mark this unassigned, so we try to schedule it again
            work.setStatus(WorkStatusCode.UNASSIGNED);
            work.clearAssignment();
            break;
        case UnreachableOrBadResponse:
            {
                if (work.getLastTaskCheckResult().getStatus() == TaskStatus.UnreachableOrBadResponse) {
                    // if we saw the same thing last time around, free the task to be scheduled elsewhere
                    work.setStatus(WorkStatusCode.UNASSIGNED);
                    work.clearAssignment();
                    work.recordTaskCheckResult(task);
                    // update testrig metadata
                    WorkItem wItem = work.getWorkItem();
                    WorkDetails wDetails = work.getDetails();
                    if (wDetails.workType == WorkType.PARSING || wDetails.workType == WorkType.DATAPLANING) {
                        EnvironmentMetadata envMetadata = TestrigMetadataMgr.getEnvironmentMetadata(wItem.getContainerName(), wDetails.baseTestrig, wDetails.baseEnv);
                        if (wDetails.workType == WorkType.PARSING) {
                            if (envMetadata.getProcessingStatus() != ProcessingStatus.PARSING) {
                                _logger.errorf("Unexpected status %s when parsing failed for %s / %s", envMetadata.getProcessingStatus(), wDetails.baseTestrig, wDetails.baseEnv);
                            } else {
                                TestrigMetadataMgr.updateEnvironmentStatus(wItem.getContainerName(), wDetails.baseTestrig, wDetails.baseEnv, ProcessingStatus.UNINITIALIZED, task.getErrMessage());
                            }
                        } else {
                            // wDetails.workType == WorkType.DATAPLANING
                            if (envMetadata.getProcessingStatus() != ProcessingStatus.DATAPLANING) {
                                _logger.errorf("Unexpected status %s when dataplaning failed for %s / %s", envMetadata.getProcessingStatus(), wDetails.baseTestrig, wDetails.baseEnv);
                            } else {
                                TestrigMetadataMgr.updateEnvironmentStatus(wItem.getContainerName(), wDetails.baseTestrig, wDetails.baseEnv, ProcessingStatus.PARSED, task.getErrMessage());
                            }
                        }
                    }
                } else {
                    work.setStatus(WorkStatusCode.ASSIGNED);
                    work.recordTaskCheckResult(task);
                }
            }
            break;
        default:
            throw new BatfishException("Unhandled " + TaskStatus.class.getCanonicalName() + ": " + task.getStatus());
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) Task(org.batfish.common.Task) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) WorkItem(org.batfish.common.WorkItem) ProcessingStatus(org.batfish.datamodel.EnvironmentMetadata.ProcessingStatus) EnvironmentMetadata(org.batfish.datamodel.EnvironmentMetadata) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

Task (org.batfish.common.Task)11 IOException (java.io.IOException)6 BatfishException (org.batfish.common.BatfishException)6 ProcessingException (javax.ws.rs.ProcessingException)4 JSONArray (org.codehaus.jettison.json.JSONArray)4 JSONException (org.codehaus.jettison.json.JSONException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ActiveSpan (io.opentracing.ActiveSpan)3 SpanContext (io.opentracing.SpanContext)3 Date (java.util.Date)2 Nullable (javax.annotation.Nullable)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Client (javax.ws.rs.client.Client)2 WebTarget (javax.ws.rs.client.WebTarget)2 Response (javax.ws.rs.core.Response)2 CleanBatfishException (org.batfish.common.CleanBatfishException)2 Batch (org.batfish.common.Task.Batch)2 WorkItem (org.batfish.common.WorkItem)2