use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class TopicConfigurationRequest method handle.
@Override
protected OperationFuture handle(String uuid) {
OperationFuture future = new OperationFuture("Update Topic Configuration");
pending(future.operationProgress());
_asyncKafkaCruiseControl.sessionExecutor().submit(new UpdateTopicConfigurationRunnable(_asyncKafkaCruiseControl, future, uuid, _parameters));
return future;
}
use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class ProgressResult method getJsonString.
protected String getJsonString() {
Map<String, Object> jsonResponse = new HashMap<>();
jsonResponse.put(VERSION, JSON_VERSION);
List<Object> progress = new ArrayList<>(_futures.size());
for (OperationFuture future : _futures) {
progress.add(future.getJsonStructure());
}
jsonResponse.put(PROGRESS, progress);
Gson gson = new GsonBuilder().serializeNulls().serializeSpecialFloatingPointValues().create();
return gson.toJson(jsonResponse);
}
use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class UserTaskManagerTest method testMaximumActiveTasks.
@Test
public void testMaximumActiveTasks() {
HttpSession mockHttpSession1 = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession1.getLastAccessedTime()).andReturn(100L).anyTimes();
HttpServletRequest mockHttpServletRequest1 = prepareRequest(mockHttpSession1, null);
OperationFuture future = new OperationFuture("future");
UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6), 100, new MockTime());
HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
mockHttpServletResponse.setHeader(EasyMock.anyString(), EasyMock.anyString());
EasyMock.replay(mockHttpSession1, mockHttpServletResponse);
// test-case: test max limitation active tasks
OperationFuture future1 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest1, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
Assert.assertEquals(future, future1);
EasyMock.verify(mockHttpSession1, mockHttpServletRequest1, mockHttpServletResponse);
HttpSession mockHttpSession2 = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession2.getLastAccessedTime()).andReturn(100L).anyTimes();
EasyMock.replay(mockHttpSession2);
EasyMock.reset(mockHttpServletResponse);
HttpServletRequest mockHttpServletRequest2 = prepareRequest(mockHttpSession2, null, "/test2", Collections.emptyMap());
try {
OperationFuture future2 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest2, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
Assert.assertEquals(future, future2);
EasyMock.verify(mockHttpSession2, mockHttpServletRequest2, mockHttpServletResponse);
} catch (RuntimeException e) {
userTaskManager.close();
return;
}
Assert.fail("Don't expect to be here!");
}
use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class UserTaskManagerTest method testSessionsShareUserTask.
@Test
public void testSessionsShareUserTask() {
UUID testUserTaskId = UUID.randomUUID();
UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
Map<String, String[]> requestParams1 = new HashMap<>();
requestParams1.put("param", new String[] { "true" });
HttpServletRequest mockHttpServletRequest1 = prepareRequest(mockHttpSession, null, "test", requestParams1);
HttpServletResponse mockHttpServletResponse1 = EasyMock.mock(HttpServletResponse.class);
Capture<String> userTaskHeader = Capture.newInstance();
Capture<String> userTaskHeaderValue = Capture.newInstance();
mockHttpServletResponse1.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
Map<String, String[]> requestParams2 = new HashMap<>();
requestParams2.put("param", new String[] { "true" });
HttpServletRequest mockHttpServletRequest2 = prepareRequest(mockHttpSession, null, "test", requestParams2);
HttpServletResponse mockHttpServletResponse2 = EasyMock.mock(HttpServletResponse.class);
mockHttpServletResponse2.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
Map<String, String[]> requestParams3 = new HashMap<>();
requestParams3.put("param", new String[] { "true" });
HttpServletRequest mockHttpServletRequest3 = prepareRequest(mockHttpSession, testUserTaskId.toString(), "test", requestParams3);
HttpServletResponse mockHttpServletResponse3 = EasyMock.mock(HttpServletResponse.class);
mockHttpServletResponse3.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
EasyMock.replay(mockUuidGenerator, mockHttpSession, mockHttpServletResponse1, mockHttpServletResponse2, mockHttpServletResponse3);
OperationFuture future = new OperationFuture("future");
UserTaskManager userTaskManager = new UserTaskManager(1000, 5, TimeUnit.HOURS.toMillis(6), 100, new MockTime(), mockUuidGenerator);
userTaskManager.getOrCreateUserTask(mockHttpServletRequest1, mockHttpServletResponse1, uuid -> future, 0, true, null);
userTaskManager.getOrCreateUserTask(mockHttpServletRequest2, mockHttpServletResponse2, uuid -> future, 0, true, null);
// Test UserTaskManger can recognize the previous created task by taskId.
userTaskManager.getOrCreateUserTask(mockHttpServletRequest3, mockHttpServletResponse3, uuid -> future, 0, true, null);
// The 2nd request should reuse the UserTask created for the 1st request since they use the same session and send the same request.
Assert.assertEquals(1, userTaskManager.numActiveSessionKeys());
EasyMock.verify(mockUuidGenerator, mockHttpServletResponse1, mockHttpServletResponse2, mockHttpServletResponse3, mockHttpServletRequest1, mockHttpServletRequest2, mockHttpServletRequest3);
}
use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class AbstractAsyncRequest method getResponse.
@Override
public CruiseControlResponse getResponse(HttpServletRequest request, HttpServletResponse response) throws ExecutionException, InterruptedException {
LOG.info("Processing async request {}.", name());
int step = _asyncOperationStep.get();
List<OperationFuture> futures = _userTaskManager.getOrCreateUserTask(request, response, this::handle, step, true, parameters());
_asyncOperationStep.set(step + 1);
CruiseControlResponse ccResponse;
try {
ccResponse = futures.get(step).get(_maxBlockMs, TimeUnit.MILLISECONDS);
LOG.info("Computation is completed for async request: {}.", request.getPathInfo());
} catch (TimeoutException te) {
ccResponse = new ProgressResult(futures, _asyncKafkaCruiseControl.config());
LOG.info("Computation is in progress for async request: {}.", request.getPathInfo());
}
return ccResponse;
}
Aggregations