Search in sources :

Example 6 with Session

use of com.thinkbiganalytics.kylo.spark.model.Session in project kylo by Teradata.

the class SparkLivyProcessManager method start.

public void start(@Nonnull final SparkLivyProcess sparkLivyProcess) {
    JerseyRestClient jerseyClient = getClient(sparkLivyProcess);
    // fetch or create new server session
    Session currentSession;
    if (sparkLivyProcess.getSessionId() != null) {
        Optional<Session> optSession = getLivySession(sparkLivyProcess);
        if (optSession.isPresent()) {
            currentSession = optSession.get();
        } else {
            currentSession = startLivySession(sparkLivyProcess);
        }
    } else {
        currentSession = startLivySession(sparkLivyProcess);
    }
    Integer currentSessionId = currentSession.getId();
    if (!currentSession.getState().equals(SessionState.idle)) {
        logger.debug("Created session with id='{}', but it was returned with state != idle, state = '{}'", currentSession.getId(), currentSession.getState());
        if (!waitForSessionToBecomeIdle(jerseyClient, currentSessionId)) {
            throw new LivyUserException("livy.start_failed");
        }
        // At this point the server is ready and we can send it an initialization command, any following
        // statement sent by UI will wait for their turn to execute
        initSession(sparkLivyProcess);
    }
    // end if
    // notifies all and any waiting threads session is started, OK to call many times..
    sparkLivyProcess.sessionStarted();
}
Also used : LivyUserException(com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Session(com.thinkbiganalytics.kylo.spark.model.Session)

Example 7 with Session

use of com.thinkbiganalytics.kylo.spark.model.Session in project kylo by Teradata.

the class SparkLivyProcessManager method getLivySession.

private Optional<Session> getLivySession(SparkLivyProcess sparkLivyProcess) {
    JerseyRestClient jerseyClient = getClient(sparkLivyProcess);
    SessionsGetResponse sessions = livyClient.getSessions(jerseyClient);
    if (sessions == null) {
        logger.error("Server not reachable", new LivyServerNotReachableException("Empty result from LivyClient.getSessions"));
        throw new LivyUserException("livy.server_not_found");
    }
    Optional<Session> optSession = sessions.getSessionWithId(sparkLivyProcess.getSessionId());
    if (!optSession.isPresent()) {
        // current client not found... let's make a new one
        clearClientState(sparkLivyProcess);
    }
    return optSession;
}
Also used : SessionsGetResponse(com.thinkbiganalytics.kylo.spark.model.SessionsGetResponse) LivyUserException(com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) LivyServerNotReachableException(com.thinkbiganalytics.kylo.spark.exceptions.LivyServerNotReachableException) Session(com.thinkbiganalytics.kylo.spark.model.Session)

Example 8 with Session

use of com.thinkbiganalytics.kylo.spark.model.Session in project kylo by Teradata.

the class TestLivyHeartbeatMonitor method livyHeartbeatMonitor.

public LivyHeartbeatMonitor livyHeartbeatMonitor() {
    LivyClient mockLivyClient = Mockito.mock(LivyClient.class);
    Session sessionNotStarted = new Session.Builder().id(1).state(SessionState.not_started).build();
    Session sessionStarting = new Session.Builder().id(1).state(SessionState.starting).build();
    Session sessionIdle = new Session.Builder().id(1).state(SessionState.idle).build();
    Session sessioShuttingDown = new Session.Builder().id(1).state(SessionState.shutting_down).build();
    final List<Session> answers = Lists.newArrayList(sessionNotStarted, sessionStarting, sessionIdle, sessioShuttingDown);
    final AtomicInteger numResponse = new AtomicInteger(0);
    JerseyRestClient client = Mockito.mock(JerseyRestClient.class);
    Mockito.when(client.get(Mockito.anyString(), Mockito.eq(Session.class))).thenAnswer(new Answer<Session>() {

        @Override
        public Session answer(InvocationOnMock invocation) throws Throwable {
            try {
                if (numResponse.get() == 3 || numResponse.get() == 8) {
                    // third response and second to last response is session not_found
                    throw new WebApplicationException("Can't find session", 404);
                }
                // Now get from our list of Sessions with certain states
                Session session = answers.remove(0);
                logger.debug("Returning mock response for session with id='{}' and state='{}'", session.getId(), session.getState());
                return session;
            } catch (IndexOutOfBoundsException e) {
                // used up our list of known responses, pretend the server doesn't know the session anymore
                throw new WebApplicationException("Can't find session", 404);
            }
        }
    });
    Mockito.when(mockLivyClient.getSession(Mockito.any(), Mockito.any(SparkLivyProcess.class))).thenAnswer(invocation -> {
        int responseNum = numResponse.addAndGet(1);
        logger.debug("Number of responses return from mockLivyClient:: numResponse={}", responseNum);
        if (responseNum == 1 || responseNum >= 9) {
            // first response and last response is server not started
            throw new ProcessingException(new SocketTimeoutException("Server not started"));
        }
        if (responseNum == 2) {
            // second response is server not HTTPS
            throw new ProcessingException(new SSLHandshakeException("Server is not HTTPS"));
        }
        return livyClient.getSession(client, (SparkLivyProcess) invocation.getArguments()[1]);
    });
    return new LivyHeartbeatMonitor(mockLivyClient, client, livyServer, livyProperties);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SocketTimeoutException(java.net.SocketTimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LivyClient(com.thinkbiganalytics.kylo.spark.client.LivyClient) DefaultLivyClient(com.thinkbiganalytics.kylo.spark.client.DefaultLivyClient) SparkLivyProcess(com.thinkbiganalytics.kylo.spark.livy.SparkLivyProcess) Session(com.thinkbiganalytics.kylo.spark.model.Session) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

Session (com.thinkbiganalytics.kylo.spark.model.Session)8 LivyUserException (com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException)4 JerseyRestClient (com.thinkbiganalytics.rest.JerseyRestClient)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 LivyException (com.thinkbiganalytics.kylo.spark.exceptions.LivyException)3 LivyInvalidSessionException (com.thinkbiganalytics.kylo.spark.exceptions.LivyInvalidSessionException)2 LivyServerNotReachableException (com.thinkbiganalytics.kylo.spark.exceptions.LivyServerNotReachableException)2 SessionsGetResponse (com.thinkbiganalytics.kylo.spark.model.SessionsGetResponse)2 SocketTimeoutException (java.net.SocketTimeoutException)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 ProcessingException (javax.ws.rs.ProcessingException)2 DefaultLivyClient (com.thinkbiganalytics.kylo.spark.client.DefaultLivyClient)1 LivyClient (com.thinkbiganalytics.kylo.spark.client.LivyClient)1 LivyCodeException (com.thinkbiganalytics.kylo.spark.exceptions.LivyCodeException)1 SparkLivyProcess (com.thinkbiganalytics.kylo.spark.livy.SparkLivyProcess)1 SessionsPost (com.thinkbiganalytics.kylo.spark.model.SessionsPost)1 SessionState (com.thinkbiganalytics.kylo.spark.model.enums.SessionState)1 SocketException (java.net.SocketException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1