Search in sources :

Example 1 with OperationFuture

use of com.linkedin.kafka.cruisecontrol.async.OperationFuture in project cruise-control by linkedin.

the class SessionManager method getAndCreateSessionIfNotExist.

/**
 * Create the session for the request if needed.
 *
 * @param request the HttpServletRequest to create session for.
 * @param operation the async operation that returns an {@link OperationFuture}
 * @param step the index of the step whose future needs to be added or get.
 *
 * @return the {@link OperationFuture} for the provided async operation.
 */
@SuppressWarnings("unchecked")
synchronized <T> OperationFuture<T> getAndCreateSessionIfNotExist(HttpServletRequest request, Supplier<OperationFuture<T>> operation, int step) {
    HttpSession session = request.getSession();
    SessionInfo info = _inProgressSessions.get(session);
    String requestString = toRequestString(request);
    // Session exists.
    if (info != null) {
        LOG.info("Found existing session {}", session);
        info.ensureSameRequest(requestString, request.getParameterMap());
        // If there is next future return it.
        if (step < info.numFutures()) {
            return (OperationFuture<T>) info.future(step);
        } else if (step == info.numFutures()) {
            LOG.info("Adding new future to existing session {}.", session);
            // if there is no next future, add the future to the next list.
            OperationFuture<T> future = operation.get();
            info.addFuture(future);
            return future;
        } else {
            throw new IllegalArgumentException(String.format("There are %d steps in the session. Cannot add step %d.", info.numFutures(), step));
        }
    } else {
        if (step > 0) {
            throw new IllegalArgumentException(String.format("There are no step in the session. Cannot add step %d.", step));
        }
        // The session does not exist, add it.
        if (_inProgressSessions.size() >= _capacity) {
            throw new RuntimeException("There are already " + _inProgressSessions.size() + " active sessions, which " + "has reached the servlet capacity.");
        }
        LOG.info("Created session for {}", session);
        info = new SessionInfo(requestString, request.getParameterMap());
        OperationFuture<T> future = operation.get();
        info.addFuture(future);
        _inProgressSessions.put(session, info);
        return future;
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) OperationFuture(com.linkedin.kafka.cruisecontrol.async.OperationFuture)

Example 2 with OperationFuture

use of com.linkedin.kafka.cruisecontrol.async.OperationFuture in project cruise-control by linkedin.

the class KafkaCruiseControlServletDataFromTest method test.

@Test
public void test() throws Exception {
    AsyncKafkaCruiseControl mockKCC = EasyMock.createMock(AsyncKafkaCruiseControl.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
    EasyMock.expect(request.getMethod()).andReturn("GET").anyTimes();
    EasyMock.expect(request.getRequestURI()).andReturn("/test").anyTimes();
    EasyMock.expect(request.getParameterMap()).andReturn(Collections.emptyMap()).anyTimes();
    EasyMock.expect(session.getLastAccessedTime()).andReturn(Long.MAX_VALUE);
    KafkaCruiseControlState kccState = getState(_numReadyGoals, _totalGoals, _numValidWindows);
    OperationFuture<KafkaCruiseControlState> kccStateFuture = new OperationFuture<>("test");
    kccStateFuture.complete(kccState);
    EasyMock.expect(mockKCC.state()).andReturn(kccStateFuture).anyTimes();
    EasyMock.replay(mockKCC, request, response, session);
    KafkaCruiseControlServlet servlet = new KafkaCruiseControlServlet(mockKCC, 10, 100, new MetricRegistry());
    KafkaCruiseControlServlet.GoalsAndRequirements goalsAndRequirements = servlet.getGoalsAndRequirements(request, response, Collections.emptyList(), _dataFrom, false);
    assertEquals(new HashSet<>(goalsAndRequirements.goals()), new HashSet<>(_expectedGoalsToUse));
    if (_expectedRequirements != null) {
        assertEquals(_expectedRequirements.minRequiredNumWindows(), goalsAndRequirements.requirements().minRequiredNumWindows());
        assertEquals(_expectedRequirements.minMonitoredPartitionsPercentage(), goalsAndRequirements.requirements().minMonitoredPartitionsPercentage(), 0.0);
        assertEquals(_expectedRequirements.includeAllTopics(), goalsAndRequirements.requirements().includeAllTopics());
    } else {
        assertNull("The requirement should be null", goalsAndRequirements.requirements());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncKafkaCruiseControl(com.linkedin.kafka.cruisecontrol.async.AsyncKafkaCruiseControl) KafkaCruiseControlState(com.linkedin.kafka.cruisecontrol.KafkaCruiseControlState) HttpSession(javax.servlet.http.HttpSession) MetricRegistry(com.codahale.metrics.MetricRegistry) HttpServletResponse(javax.servlet.http.HttpServletResponse) OperationFuture(com.linkedin.kafka.cruisecontrol.async.OperationFuture) Test(org.junit.Test)

Example 3 with OperationFuture

use of com.linkedin.kafka.cruisecontrol.async.OperationFuture in project cruise-control by linkedin.

the class SessionManagerTest method testSessionExpiration.

@Test
public void testSessionExpiration() {
    TestContext context = prepareRequests(true, 2);
    SessionManager sessionManager = new SessionManager(2, 1000, context.time(), new MetricRegistry());
    List<OperationFuture<Integer>> futures = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
        futures.add(sessionManager.getAndCreateSessionIfNotExist(context.request(i), () -> new OperationFuture<>("testSessionExpiration"), 0));
    }
    assertEquals(2, sessionManager.numSessions());
    // Sleep to 1 ms before expiration.
    context.time().sleep(999);
    sessionManager.expireOldSessions();
    assertEquals(2, sessionManager.numSessions());
    for (Future future : futures) {
        assertFalse(future.isDone());
        assertFalse(future.isCancelled());
    }
    // Sleep to the exact time to expire
    context.time().sleep(1);
    sessionManager.expireOldSessions();
    assertEquals("All the sessions should have been expired", 0, sessionManager.numSessions());
    for (Future future : futures) {
        assertTrue(future.isDone());
        assertTrue(future.isCancelled());
    }
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) ArrayList(java.util.ArrayList) OperationFuture(com.linkedin.kafka.cruisecontrol.async.OperationFuture) Future(java.util.concurrent.Future) OperationFuture(com.linkedin.kafka.cruisecontrol.async.OperationFuture) Test(org.junit.Test)

Example 4 with OperationFuture

use of com.linkedin.kafka.cruisecontrol.async.OperationFuture in project cruise-control by linkedin.

the class SessionManagerTest method testMultipleOperationRequest.

@Test
public void testMultipleOperationRequest() {
    TestContext context = prepareRequests(false, 1);
    SessionManager sessionManager = new SessionManager(1, 1000, context.time(), new MetricRegistry());
    HttpServletRequest request = context.request(0);
    OperationFuture<Integer> future1 = new OperationFuture<>("future1");
    OperationFuture<String> future2 = new OperationFuture<>("future2");
    for (int i = 0; i < 2; i++) {
        // Create the first future.
        OperationFuture<Integer> intFuture = sessionManager.getAndCreateSessionIfNotExist(request, () -> future1, 0);
        assertTrue(intFuture == future1);
        future1.complete(100);
        // create the second future.
        OperationFuture<String> stringFuture = sessionManager.getAndCreateSessionIfNotExist(request, () -> future2, 1);
        assertTrue(stringFuture == future2);
        future2.complete("abc");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MetricRegistry(com.codahale.metrics.MetricRegistry) OperationFuture(com.linkedin.kafka.cruisecontrol.async.OperationFuture) Test(org.junit.Test)

Aggregations

OperationFuture (com.linkedin.kafka.cruisecontrol.async.OperationFuture)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 Test (org.junit.Test)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpSession (javax.servlet.http.HttpSession)2 KafkaCruiseControlState (com.linkedin.kafka.cruisecontrol.KafkaCruiseControlState)1 AsyncKafkaCruiseControl (com.linkedin.kafka.cruisecontrol.async.AsyncKafkaCruiseControl)1 ArrayList (java.util.ArrayList)1 Future (java.util.concurrent.Future)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1