Search in sources :

Example 1 with QuestionException

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

the class Answer method append.

public void append(Answer answer) {
    if (answer._question != null) {
        _question = answer._question;
    }
    _answerElements.addAll(answer._answerElements);
    _status = answer._status;
    _summary.combine(answer.getSummary());
    for (AnswerElement answerElement : answer._answerElements) {
        if (answerElement instanceof BatfishStackTrace) {
            BatfishException e = ((BatfishStackTrace) answerElement).getException();
            throw new QuestionException("Exception answering question", e, this);
        }
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) QuestionException(org.batfish.common.QuestionException) BatfishStackTrace(org.batfish.common.BatfishException.BatfishStackTrace)

Example 2 with QuestionException

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

the class Driver method runBatfish.

@SuppressWarnings("deprecation")
private static String runBatfish(final Settings settings) {
    final BatfishLogger logger = settings.getLogger();
    try {
        final Batfish batfish = new Batfish(settings, CACHED_COMPRESSED_TESTRIGS, CACHED_TESTRIGS, CACHED_COMPRESSED_DATA_PLANES, CACHED_DATA_PLANES, CACHED_ENVIRONMENT_BGP_TABLES, CACHED_ENVIRONMENT_ROUTING_TABLES, CACHED_FORWARDING_ANALYSES);
        @Nullable SpanContext runBatfishSpanContext = GlobalTracer.get().activeSpan() == null ? null : GlobalTracer.get().activeSpan().context();
        Thread thread = new Thread() {

            @Override
            public void run() {
                try (ActiveSpan runBatfishSpan = GlobalTracer.get().buildSpan("Run Batfish job in a new thread and get the answer").addReference(References.FOLLOWS_FROM, runBatfishSpanContext).startActive()) {
                    assert runBatfishSpan != null;
                    Answer answer = null;
                    try {
                        answer = batfish.run();
                        if (answer.getStatus() == null) {
                            answer.setStatus(AnswerStatus.SUCCESS);
                        }
                    } catch (CleanBatfishException e) {
                        String msg = "FATAL ERROR: " + e.getMessage();
                        logger.error(msg);
                        batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
                        answer = Answer.failureAnswer(msg, null);
                    } catch (QuestionException e) {
                        String stackTrace = ExceptionUtils.getStackTrace(e);
                        logger.error(stackTrace);
                        batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
                        answer = e.getAnswer();
                        answer.setStatus(AnswerStatus.FAILURE);
                    } catch (BatfishException e) {
                        String stackTrace = ExceptionUtils.getStackTrace(e);
                        logger.error(stackTrace);
                        batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
                        answer = new Answer();
                        answer.setStatus(AnswerStatus.FAILURE);
                        answer.addAnswerElement(e.getBatfishStackTrace());
                    } catch (Throwable e) {
                        String stackTrace = ExceptionUtils.getStackTrace(e);
                        logger.error(stackTrace);
                        batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
                        answer = new Answer();
                        answer.setStatus(AnswerStatus.FAILURE);
                        answer.addAnswerElement(new BatfishException("Batfish job failed", e).getBatfishStackTrace());
                    } finally {
                        try (ActiveSpan outputAnswerSpan = GlobalTracer.get().buildSpan("Outputting answer").startActive()) {
                            assert outputAnswerSpan != null;
                            if (settings.getAnswerJsonPath() != null) {
                                batfish.outputAnswerWithLog(answer);
                            }
                        }
                    }
                }
            }
        };
        thread.start();
        thread.join(settings.getMaxRuntimeMs());
        if (thread.isAlive()) {
            // this is deprecated but we should be safe since we don't have
            // locks and such
            // AF: This doesn't do what you think it does, esp. not in Java 8.
            // It needs to be replaced. TODO
            thread.stop();
            logger.error("Batfish worker took too long. Terminated.");
            batfish.setTerminatingExceptionMessage("Batfish worker took too long. Terminated.");
        }
        return batfish.getTerminatingExceptionMessage();
    } catch (Exception e) {
        String stackTrace = ExceptionUtils.getStackTrace(e);
        logger.error(stackTrace);
        return stackTrace;
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) CleanBatfishException(org.batfish.common.CleanBatfishException) SpanContext(io.opentracing.SpanContext) BatfishLogger(org.batfish.common.BatfishLogger) CleanBatfishException(org.batfish.common.CleanBatfishException) 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) Answer(org.batfish.datamodel.answers.Answer) ActiveSpan(io.opentracing.ActiveSpan) QuestionException(org.batfish.common.QuestionException) Nullable(javax.annotation.Nullable)

Aggregations

BatfishException (org.batfish.common.BatfishException)2 QuestionException (org.batfish.common.QuestionException)2 ActiveSpan (io.opentracing.ActiveSpan)1 SpanContext (io.opentracing.SpanContext)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 ProcessingException (javax.ws.rs.ProcessingException)1 BatfishStackTrace (org.batfish.common.BatfishException.BatfishStackTrace)1 BatfishLogger (org.batfish.common.BatfishLogger)1 CleanBatfishException (org.batfish.common.CleanBatfishException)1 Answer (org.batfish.datamodel.answers.Answer)1