Search in sources :

Example 1 with SessionInfo

use of org.apache.zeppelin.common.SessionInfo in project zeppelin by apache.

the class SessionManagerService method getSessionInfo.

/**
 * Get the sessionInfo.
 * It method will also update its state if there's an associated interpreter process.
 *
 * @param sessionId
 * @return
 * @throws Exception
 */
public SessionInfo getSessionInfo(String sessionId) throws Exception {
    SessionInfo sessionInfo = sessions.get(sessionId);
    if (sessionInfo == null) {
        LOGGER.warn("No such session: " + sessionId);
        return null;
    }
    ManagedInterpreterGroup interpreterGroup = this.interpreterSettingManager.getInterpreterGroupById(sessionId);
    if (interpreterGroup != null) {
        RemoteInterpreterProcess remoteInterpreterProcess = interpreterGroup.getRemoteInterpreterProcess();
        if (remoteInterpreterProcess == null) {
            sessionInfo.setState(SessionState.READY.name());
        } else {
            sessionInfo.setStartTime(remoteInterpreterProcess.getStartTime());
            sessionInfo.setWeburl(interpreterGroup.getWebUrl());
            if (remoteInterpreterProcess.isRunning()) {
                sessionInfo.setState(SessionState.RUNNING.name());
            } else {
                // e.g. InterpreterProcess is terminated for whatever unexpected reason.
                if (SessionState.RUNNING.name().equalsIgnoreCase(sessionInfo.getState())) {
                    sessionInfo.setState(SessionState.STOPPED.name());
                }
            }
        }
    } else {
        if (SessionState.RUNNING.name().equalsIgnoreCase(sessionInfo.getState())) {
            // if it is running before, but interpreterGroup is null now, that means the session is stopped.
            // e.g. InterpreterProcess is killed if it exceed idle timeout threshold.
            sessionInfo.setState(SessionState.STOPPED.name());
        }
    }
    return sessionInfo;
}
Also used : SessionInfo(org.apache.zeppelin.common.SessionInfo) RemoteInterpreterProcess(org.apache.zeppelin.interpreter.remote.RemoteInterpreterProcess) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup)

Example 2 with SessionInfo

use of org.apache.zeppelin.common.SessionInfo in project zeppelin by apache.

the class SessionManagerService method createSession.

/**
 * Create a new session, including allocate new session Id and create dedicated note for this session.
 *
 * @param interpreter
 * @return
 * @throws Exception
 */
public synchronized SessionInfo createSession(String interpreter) throws Exception {
    String sessionId = null;
    int i = 0;
    while (i < RETRY) {
        sessionId = interpreter + "_" + System.currentTimeMillis();
        if (sessions.containsKey(sessionId)) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                LOGGER.error("Interrupted", e);
            }
        } else {
            break;
        }
    }
    if (sessionId == null) {
        throw new Exception("Unable to generate session id");
    }
    String sessionNoteId = notebook.createNote(buildNotePath(interpreter, sessionId), AuthenticationInfo.ANONYMOUS);
    SessionInfo sessionInfo = new SessionInfo(sessionId, sessionNoteId, interpreter);
    sessions.put(sessionId, sessionInfo);
    return sessionInfo;
}
Also used : SessionInfo(org.apache.zeppelin.common.SessionInfo) IOException(java.io.IOException)

Example 3 with SessionInfo

use of org.apache.zeppelin.common.SessionInfo in project zeppelin by apache.

the class SessionRestApi method createSession.

/**
 * Create a session for this provided interpreter.
 *
 * @param interpreter
 * @return
 * @throws Exception
 */
@POST
public Response createSession(@QueryParam("interpreter") String interpreter) throws Exception {
    LOGGER.info("Create new session for interpreter: {}", interpreter);
    SessionInfo sessionInfo = sessionManagerService.createSession(interpreter);
    return new JsonResponse<>(Response.Status.OK, sessionInfo).build();
}
Also used : SessionInfo(org.apache.zeppelin.common.SessionInfo) POST(javax.ws.rs.POST)

Example 4 with SessionInfo

use of org.apache.zeppelin.common.SessionInfo in project zeppelin by apache.

the class SessionManagerService method stopSession.

/**
 * Remove and stop this session.
 * 1. Stop associated interpreter process (InterpreterGroup)
 * 2. Remove associated session note
 *
 * @param sessionId
 */
public void stopSession(String sessionId) throws Exception {
    SessionInfo sessionInfo = this.sessions.remove(sessionId);
    if (sessionInfo == null) {
        throw new Exception("No such session: " + sessionId);
    }
    // stop the associated interpreter process
    ManagedInterpreterGroup interpreterGroup = this.interpreterSettingManager.getInterpreterGroupById(sessionId);
    if (interpreterGroup == null) {
        LOGGER.info("No interpreterGroup for session: {}", sessionId);
        return;
    }
    interpreterGroup.getInterpreterSetting().closeInterpreters(sessionId);
    // remove associated session note
    notebook.removeNote(sessionInfo.getNoteId(), AuthenticationInfo.ANONYMOUS);
}
Also used : SessionInfo(org.apache.zeppelin.common.SessionInfo) IOException(java.io.IOException) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup)

Example 5 with SessionInfo

use of org.apache.zeppelin.common.SessionInfo in project zeppelin by apache.

the class ZeppelinClient method listSessions.

/**
 * List all the sessions for the provided interpreter.
 *
 * @param interpreter
 * @return
 * @throws Exception
 */
public List<SessionInfo> listSessions(String interpreter) throws Exception {
    GetRequest getRequest = Unirest.get("/session");
    if (interpreter != null) {
        getRequest.queryString("interpreter", interpreter);
    }
    HttpResponse<JsonNode> response = getRequest.asJson();
    checkResponse(response);
    JsonNode jsonNode = response.getBody();
    checkJsonNodeStatus(jsonNode);
    JSONArray sessionJsonArray = jsonNode.getObject().getJSONArray("body");
    List<SessionInfo> sessionInfos = new ArrayList<>();
    for (int i = 0; i < sessionJsonArray.length(); ++i) {
        sessionInfos.add(createSessionInfoFromJson(sessionJsonArray.getJSONObject(i)));
    }
    return sessionInfos;
}
Also used : GetRequest(kong.unirest.GetRequest) JSONArray(kong.unirest.json.JSONArray) ArrayList(java.util.ArrayList) SessionInfo(org.apache.zeppelin.common.SessionInfo) JsonNode(kong.unirest.JsonNode)

Aggregations

SessionInfo (org.apache.zeppelin.common.SessionInfo)6 IOException (java.io.IOException)2 ManagedInterpreterGroup (org.apache.zeppelin.interpreter.ManagedInterpreterGroup)2 ArrayList (java.util.ArrayList)1 POST (javax.ws.rs.POST)1 GetRequest (kong.unirest.GetRequest)1 JsonNode (kong.unirest.JsonNode)1 JSONArray (kong.unirest.json.JSONArray)1 RemoteInterpreterProcess (org.apache.zeppelin.interpreter.remote.RemoteInterpreterProcess)1 Test (org.junit.Test)1