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;
}
}
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());
}
}
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());
}
}
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");
}
}
Aggregations