You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zeppelin.apache.org by zj...@apache.org on 2020/09/15 11:39:34 UTC

[zeppelin] branch branch-0.9 updated: [ZEPPELIN-5035]. ZeppelinClient#getSession should return null when there's no such session

This is an automated email from the ASF dual-hosted git repository.

zjffdu pushed a commit to branch branch-0.9
in repository https://gitbox.apache.org/repos/asf/zeppelin.git


The following commit(s) were added to refs/heads/branch-0.9 by this push:
     new b416f95  [ZEPPELIN-5035]. ZeppelinClient#getSession should return null when there's no such session
b416f95 is described below

commit b416f950fce782d7698b307154377caf37f5e0e3
Author: Jeff Zhang <zj...@apache.org>
AuthorDate: Sat Sep 12 12:17:33 2020 +0800

    [ZEPPELIN-5035]. ZeppelinClient#getSession should return null when there's no such session
    
    ### What is this PR for?
    
    Currently it would throw exception when there's no such session, it would make the api user confused. It is better to just return null when there's no such session.
    
    ### What type of PR is it?
    [ Improvement ]
    
    ### Todos
    * [ ] - Task
    
    ### What is the Jira issue?
    * https://issues.apache.org/jira/browse/ZEPPELIN-5035
    
    ### How should this be tested?
    * Unit test is added
    
    ### Screenshots (if appropriate)
    
    ### Questions:
    * Does the licenses files need update? No
    * Is there breaking changes for older versions? No
    * Does this needs documentation? No
    
    Author: Jeff Zhang <zj...@apache.org>
    
    Closes #3904 from zjffdu/ZEPPELIN-5035 and squashes the following commits:
    
    1244c0e81 [Jeff Zhang] [ZEPPELIN-5035]. ZeppelinClient#getSession should return null when there's no such session
    
    (cherry picked from commit 9c791edab95998ffe7edf90e814b75422880c1eb)
    Signed-off-by: Jeff Zhang <zj...@apache.org>
---
 .../org/apache/zeppelin/client/ZeppelinClient.java | 16 ++++++++++-
 .../integration/ZeppelinClientIntegrationTest.java | 12 +++++++++
 .../org/apache/zeppelin/rest/SessionRestApi.java   |  3 ++-
 .../rest/exception/SessionNoteFoundException.java  | 31 ++++++++++++++++++++++
 4 files changed, 60 insertions(+), 2 deletions(-)

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 539f119..bd26103 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
@@ -87,10 +87,14 @@ public class ZeppelinClient {
       throw new Exception("Please login first");
     }
     if (response.getStatus() != 200) {
+      String message = response.getStatusText();
+      if (response.getBody().getObject().has("message")) {
+        message = response.getBody().getObject().getString("message");
+      }
       throw new Exception(String.format("Unable to call rest api, status: %s, statusText: %s, message: %s",
               response.getStatus(),
               response.getStatusText(),
-              response.getBody().getObject().getString("message")));
+              message));
     }
   }
 
@@ -159,6 +163,7 @@ public class ZeppelinClient {
 
   /**
    * Get session info for the provided sessionId.
+   * Return null if provided sessionId doesn't exist.
    *
    * @param sessionId
    * @throws Exception
@@ -168,6 +173,15 @@ public class ZeppelinClient {
             .get("/session/{sessionId}")
             .routeParam("sessionId", sessionId)
             .asJson();
+    if (response.getStatus() == 404) {
+      String statusText = response.getStatusText();
+      if (response.getBody().getObject().has("message")) {
+        statusText = response.getBody().getObject().getString("message");
+      }
+      if (statusText.contains("No such session")) {
+        return null;
+      }
+    }
     checkResponse(response);
     JsonNode jsonNode = response.getBody();
     checkJsonNodeStatus(jsonNode);
diff --git a/zeppelin-interpreter-integration/src/test/java/org/apache/zeppelin/integration/ZeppelinClientIntegrationTest.java b/zeppelin-interpreter-integration/src/test/java/org/apache/zeppelin/integration/ZeppelinClientIntegrationTest.java
index 820ebff..53164ed 100644
--- a/zeppelin-interpreter-integration/src/test/java/org/apache/zeppelin/integration/ZeppelinClientIntegrationTest.java
+++ b/zeppelin-interpreter-integration/src/test/java/org/apache/zeppelin/integration/ZeppelinClientIntegrationTest.java
@@ -20,9 +20,12 @@ package org.apache.zeppelin.integration;
 import org.apache.zeppelin.client.ClientConfig;
 import org.apache.zeppelin.client.NoteResult;
 import org.apache.zeppelin.client.ParagraphResult;
+
 import org.apache.zeppelin.client.Status;
 import org.apache.zeppelin.client.ZeppelinClient;
+
 import org.apache.zeppelin.conf.ZeppelinConfiguration;
+import org.apache.zeppelin.common.SessionInfo;
 import org.apache.zeppelin.notebook.Notebook;
 import org.apache.zeppelin.rest.AbstractTestRestApi;
 import org.apache.zeppelin.utils.TestUtils;
@@ -35,6 +38,7 @@ import java.util.Map;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -375,4 +379,12 @@ public class ZeppelinClientIntegrationTest extends AbstractTestRestApi {
     assertEquals(Status.READY, p2.getStatus());
     assertEquals(0, p2.getResults().size());
   }
+
+  @Test
+  public void testSession() throws Exception {
+    SessionInfo sessionInfo = zeppelinClient.getSession("invalid_session");
+    assertNull(sessionInfo);
+
+    zeppelinClient.stopSession("invalid_session");
+  }
 }
diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SessionRestApi.java b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SessionRestApi.java
index fb4728b..33eb5b9 100644
--- a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SessionRestApi.java
+++ b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SessionRestApi.java
@@ -23,6 +23,7 @@ import org.apache.zeppelin.annotation.ZeppelinApi;
 import org.apache.zeppelin.interpreter.InterpreterSettingManager;
 import org.apache.zeppelin.notebook.Notebook;
 import org.apache.zeppelin.common.SessionInfo;
+import org.apache.zeppelin.rest.exception.SessionNoteFoundException;
 import org.apache.zeppelin.server.JsonResponse;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -98,7 +99,7 @@ public class SessionRestApi {
   public Response getSession(@PathParam("sessionId") String sessionId) throws Exception {
     SessionInfo session = sessionManager.getSession(sessionId);
     if (session == null) {
-      return new JsonResponse<>(Response.Status.NOT_FOUND).build();
+      throw new SessionNoteFoundException(sessionId);
     } else {
       return new JsonResponse<>(Response.Status.OK, "", session).build();
     }
diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/exception/SessionNoteFoundException.java b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/exception/SessionNoteFoundException.java
new file mode 100644
index 0000000..4f4cbd4
--- /dev/null
+++ b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/exception/SessionNoteFoundException.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.rest.exception;
+
+import org.apache.zeppelin.utils.ExceptionUtils;
+
+import javax.ws.rs.WebApplicationException;
+
+import static javax.ws.rs.core.Response.Status.NOT_FOUND;
+
+public class SessionNoteFoundException extends WebApplicationException {
+
+  public SessionNoteFoundException(String sessionId) {
+    super(ExceptionUtils.jsonResponseContent(NOT_FOUND, "No such session: " + sessionId));
+  }
+}