Search in sources :

Example 76 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class NotebookRestApiTest method testRunAllParagraph_FirstFailed.

@Test
public void testRunAllParagraph_FirstFailed() throws IOException {
    LOG.info("Running testRunAllParagraph_FirstFailed");
    String note1Id = null;
    try {
        note1Id = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
        // 2 paragraphs
        // P1:
        // %python
        // from __future__ import print_function
        // import time
        // time.sleep(1)
        // print(user2)
        // 
        // P2:
        // %python
        // user2='abc'
        // print(user2)
        // 
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Paragraph p2 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setText("%python from __future__ import print_function\nimport time\ntime.sleep(1)\nprint(user2)");
            p2.setText("%python user2='abc'\nprint(user2)");
            return null;
        });
        CloseableHttpResponse post = httpPost("/notebook/job/" + note1Id + "?blocking=true", "");
        assertThat(post, isAllowed());
        post.close();
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            Paragraph p2 = note1.getParagraph(1);
            assertEquals(Job.Status.ERROR, p1.getStatus());
            // p2 will be skipped because p1 is failed.
            assertEquals(Job.Status.READY, p2.getStatus());
            return null;
        });
    } finally {
        // cleanup
        if (null != note1Id) {
            TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 77 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class HeliumApplicationFactoryTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    // set AppEventListener properly
    for (InterpreterSetting interpreterSetting : interpreterSettingManager.get()) {
        interpreterSetting.setAppEventListener(heliumAppFactory);
    }
    AuthorizationService authorizationService = mock(AuthorizationService.class);
    notebookRepo = mock(NotebookRepo.class);
    notebook = new Notebook(conf, authorizationService, notebookRepo, new NoteManager(notebookRepo, ZeppelinConfiguration.create()), interpreterFactory, interpreterSettingManager, new Credentials());
    heliumAppFactory = new HeliumApplicationFactory(notebook, null);
    notebook.addNotebookEventListener(heliumAppFactory);
    anonymous = new AuthenticationInfo("anonymous");
}
Also used : NotebookRepo(org.apache.zeppelin.notebook.repo.NotebookRepo) Notebook(org.apache.zeppelin.notebook.Notebook) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) NoteManager(org.apache.zeppelin.notebook.NoteManager) Credentials(org.apache.zeppelin.user.Credentials) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) Before(org.junit.Before)

Example 78 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class CronJobListener method jobWasExecuted.

@Override
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
    JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
    String noteId = jobDataMap.getString("noteId");
    Notebook notebook = (Notebook) jobDataMap.get("notebook");
    String noteName = "unknown";
    try {
        noteName = notebook.processNote(noteId, note -> {
            if (note == null) {
                LOGGER.warn("Failed to get note: {}", noteId);
                return "unknown";
            }
            return note.getName();
        });
    } catch (IOException e) {
        LOGGER.error("Failed to get note: {}", noteId, e);
    } finally {
        Timer.Sample sample = cronJobTimerSamples.remove(context);
        String result = StringUtils.defaultString(context.getResult().toString(), "unknown");
        LOGGER.info("cron job of noteId {} executed with result {}", noteId, result);
        if (sample != null) {
            Tag noteIdTag = Tag.of("nodeid", noteId);
            Tag nameTag = Tag.of("name", noteName);
            Tag statusTag = Tag.of("result", result);
            sample.stop(Metrics.timer("cronjob", Tags.of(noteIdTag, nameTag, statusTag)));
        } else {
            LOGGER.warn("No Timer.Sample for NoteId {} found", noteId);
        }
    }
}
Also used : JobExecutionContext(org.quartz.JobExecutionContext) Tag(io.micrometer.core.instrument.Tag) Tags(io.micrometer.core.instrument.Tags) Logger(org.slf4j.Logger) JobListener(org.quartz.JobListener) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Notebook(org.apache.zeppelin.notebook.Notebook) Metrics(io.micrometer.core.instrument.Metrics) JobExecutionException(org.quartz.JobExecutionException) Timer(io.micrometer.core.instrument.Timer) JobDataMap(org.quartz.JobDataMap) Map(java.util.Map) JobDataMap(org.quartz.JobDataMap) Notebook(org.apache.zeppelin.notebook.Notebook) Timer(io.micrometer.core.instrument.Timer) IOException(java.io.IOException) Tag(io.micrometer.core.instrument.Tag)

Example 79 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class CronJob method execute.

@Override
public void execute(JobExecutionContext context) {
    JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
    String noteId = jobDataMap.getString("noteId");
    Notebook notebook = (Notebook) jobDataMap.get("notebook");
    try {
        notebook.processNote(noteId, note -> {
            if (note == null) {
                LOGGER.warn("Failed to run CronJob of note: {} because there's no such note", noteId);
                context.setResult(RESULT_FAILED);
                return null;
            }
            if (note.haveRunningOrPendingParagraphs()) {
                LOGGER.warn("execution of the cron job is skipped because there is a running or pending " + "paragraph (note id: {})", note.getId());
                context.setResult(RESULT_SKIPPED);
                return null;
            }
            String cronExecutingUser = (String) note.getConfig().get("cronExecutingUser");
            String cronExecutingRoles = (String) note.getConfig().get("cronExecutingRoles");
            if (null == cronExecutingUser) {
                cronExecutingUser = "anonymous";
            }
            AuthenticationInfo authenticationInfo = new AuthenticationInfo(cronExecutingUser, StringUtils.isEmpty(cronExecutingRoles) ? null : cronExecutingRoles, null);
            try {
                note.runAll(authenticationInfo, true, true, new HashMap<>());
                context.setResult(RESULT_SUCCEEDED);
            } catch (Exception e) {
                context.setResult(RESULT_FAILED);
                LOGGER.warn("Fail to run note: {}", note.getName(), e);
            }
            return null;
        });
    } catch (IOException e) {
        LOGGER.warn("Failed to run CronJob of note: {} because fail to get it", noteId, e);
        context.setResult(RESULT_FAILED);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Notebook(org.apache.zeppelin.notebook.Notebook) IOException(java.io.IOException) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) IOException(java.io.IOException)

Aggregations

Notebook (org.apache.zeppelin.notebook.Notebook)79 Test (org.junit.Test)53 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)48 Paragraph (org.apache.zeppelin.notebook.Paragraph)43 TypeToken (com.google.gson.reflect.TypeToken)33 Map (java.util.Map)25 HashMap (java.util.HashMap)24 Note (org.apache.zeppelin.notebook.Note)20 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)12 AuthorizationService (org.apache.zeppelin.notebook.AuthorizationService)12 Before (org.junit.Before)12 ZeppelinConfiguration (org.apache.zeppelin.conf.ZeppelinConfiguration)11 List (java.util.List)10 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)10 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)9 BeforeClass (org.junit.BeforeClass)9 InterpreterSettingManager (org.apache.zeppelin.interpreter.InterpreterSettingManager)8 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)7 RemoteAngularObjectRegistry (org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)7