This is an automated email from the ASF dual-hosted git repository. zjffdu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/zeppelin.git
The following commit(s) were added to refs/heads/master by this push: new 8672d6ca23 [ZEPPELIN-5728] Absence of cancelNote method in Zeppelin Client Api (#4362) 8672d6ca23 is described below commit 8672d6ca23f174179b2525f5fb6b550328418fc7 Author: Guanhua Li <guanhua...@foxmail.com> AuthorDate: Wed Apr 27 13:42:44 2022 +0800 [ZEPPELIN-5728] Absence of cancelNote method in Zeppelin Client Api (#4362) * [ZEPPELIN-5728] Absence of cancelNote method in Zeppelin Client Api * add test --- .../client/examples/ZeppelinClientExample.java | 2 + .../org/apache/zeppelin/client/ZeppelinClient.java | 15 ++++++++ .../apache/zeppelin/rest/NotebookRestApiTest.java | 44 ++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/zeppelin-client-examples/src/main/java/org/apache/zeppelin/client/examples/ZeppelinClientExample.java b/zeppelin-client-examples/src/main/java/org/apache/zeppelin/client/examples/ZeppelinClientExample.java index 7254568912..6e6e043c52 100644 --- a/zeppelin-client-examples/src/main/java/org/apache/zeppelin/client/examples/ZeppelinClientExample.java +++ b/zeppelin-client-examples/src/main/java/org/apache/zeppelin/client/examples/ZeppelinClientExample.java @@ -56,6 +56,8 @@ public class ZeppelinClientExample { "%python\nimport time\ntime.sleep(5)\nprint('done')"); zClient.submitParagraph(noteId, paragraphId2); zClient.waitUtilParagraphRunning(noteId, paragraphId2); + // It's also ok here to call zClient.cancelNote(noteId); + // CancelNote() would cancel all paragraphs in the note. zClient.cancelParagraph(noteId, paragraphId2); paragraphResult = zClient.waitUtilParagraphFinish(noteId, paragraphId2); System.out.println("Added new paragraph, submit it then cancel it"); diff --git a/zeppelin-client/src/main/java/org/apache/zeppelin/client/ZeppelinClient.java b/zeppelin-client/src/main/java/org/apache/zeppelin/client/ZeppelinClient.java index 2f8f6bc527..bec349e0bc 100644 --- a/zeppelin-client/src/main/java/org/apache/zeppelin/client/ZeppelinClient.java +++ b/zeppelin-client/src/main/java/org/apache/zeppelin/client/ZeppelinClient.java @@ -502,6 +502,21 @@ public class ZeppelinClient { return queryNoteResult(noteId); } + /** + * Cancel a running note. + * + * @param noteId + * @throws Exception + */ + public void cancelNote(String noteId) throws Exception { + HttpResponse<JsonNode> response = Unirest + .delete("/notebook/job/{noteId}") + .routeParam("noteId", noteId) + .asJson(); + checkResponse(response); + JsonNode jsonNode = response.getBody(); + checkJsonNodeStatus(jsonNode); + } /** * Import note with given note json content to the specified notePath. diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/rest/NotebookRestApiTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/rest/NotebookRestApiTest.java index 4744832847..95c167d1b5 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/rest/NotebookRestApiTest.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/rest/NotebookRestApiTest.java @@ -430,6 +430,50 @@ public class NotebookRestApiTest extends AbstractTestRestApi { } } + @Test + public void testCancelNoteJob() throws Exception { + LOG.info("Running testCancelNoteJob"); + String note1Id = null; + Notebook notebook = TestUtils.getInstance(Notebook.class); + try { + note1Id = notebook.createNote("note1", anonymous); + // Add 3 paragraphs for the note. + List<Paragraph> paragraphs = notebook.processNote(note1Id, + note1 -> { + List<Paragraph> paragraphsList = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS); + p1.setText("%python\nimport time\ntime.sleep(10)\nprint('done')"); + note1.run(p1.getId()); + paragraphsList.add(p1); + } + return paragraphsList; + }); + //The first paragraph is running, and the other two is pending. + paragraphs.get(0).waitUntilRunning(); + + + // cancel running note + CloseableHttpResponse delete = httpDelete("/notebook/job/" + note1Id); + assertThat(delete, isAllowed()); + Map<String, Object> resp = gson.fromJson(EntityUtils.toString(delete.getEntity(), StandardCharsets.UTF_8), + new TypeToken<Map<String, Object>>() { + }.getType()); + assertEquals("OK", resp.get("status")); + delete.close(); + for (Paragraph p : paragraphs) { + p.waitUntilFinished(); + assertEquals(Job.Status.ABORT, p.getStatus()); + } + + } finally { + // cleanup + if (null != note1Id) { + notebook.removeNote(note1Id, anonymous); + } + } + } + @Test public void testRunParagraphSynchronously() throws IOException { LOG.info("Running testRunParagraphSynchronously");