use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class RemoveBrokerRequest method handle.
@Override
protected OperationFuture handle(String uuid) {
OperationFuture future = new OperationFuture("Remove brokers");
pending(future.operationProgress());
_asyncKafkaCruiseControl.sessionExecutor().submit(new RemoveBrokersRunnable(_asyncKafkaCruiseControl, future, _parameters, uuid));
return future;
}
use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class KafkaCruiseControlServletEndpointTest method populateUserTaskManager.
// Create 5 User Tasks. Note the 5th and 6th one have same user task id, thus count as 1.
private void populateUserTaskManager(HttpServletResponse mockHttpServletResponse, UserTaskManager userTaskManager) {
List<Object[]> allParams = new ArrayList<>();
for (Object[] initInfo : INITIALIZE_SERVLET_REQUESTS_OUTPUT) {
allParams.add(inputCreateTaskParams((HttpServletRequest) initInfo[0], 0, 0));
}
// for the 6th getOrCreateUserTask() call, we set step to 1 and get the 2nd future
allParams.get(5)[1] = 1;
allParams.get(5)[2] = 1;
for (Object[] params : allParams) {
OperationFuture future = userTaskManager.getOrCreateUserTask((HttpServletRequest) params[0], mockHttpServletResponse, FUTURE_CREATOR, (int) params[1], true, null).get((int) params[2]);
POPULATE_USER_TASK_MANAGER_OUTPUT.add(outputCreateTaskInfo(future));
}
}
use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class UserTaskManagerTest method testCompletedTasks.
@Test
public void testCompletedTasks() throws Exception {
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
mockHttpSession.invalidate();
HttpServletRequest mockHttpServletRequest = prepareRequest(mockHttpSession, null);
UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(UUID.randomUUID()).anyTimes();
OperationFuture future = new OperationFuture("future");
UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6), 100, new MockTime(), mockUuidGenerator);
HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
Capture<String> userTaskHeader = Capture.newInstance();
Capture<String> userTaskHeaderValue = Capture.newInstance();
mockHttpServletResponse.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
EasyMock.replay(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
// test-case: verify if the background cleaner task removes tasks that are completed
OperationFuture future1 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
Assert.assertEquals(future, future1);
future1.cancel(true);
Thread.sleep(TimeUnit.SECONDS.toMillis(UserTaskManager.USER_TASK_SCANNER_PERIOD_SECONDS * 4));
Assert.assertTrue(future.isDone());
Assert.assertTrue(future.isCancelled());
EasyMock.verify(mockHttpSession, mockUuidGenerator, mockHttpServletResponse);
userTaskManager.close();
}
use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class UserTaskManagerTest method testAddStepsFutures.
@Test
public void testAddStepsFutures() {
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);
// Change mock session's last access time to always return current time to avoid unintended recycling of session.
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(System.currentTimeMillis()).anyTimes();
HttpServletRequest mockHttpServletRequest = prepareRequest(mockHttpSession, null);
HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
mockHttpServletResponse.setHeader(EasyMock.anyString(), EasyMock.anyString());
EasyMock.replay(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6), 100, new MockTime(), mockUuidGenerator);
OperationFuture testFuture1 = new OperationFuture("testFuture1");
OperationFuture testFuture2 = new OperationFuture("testFuture2");
OperationFuture insertedFuture1 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> testFuture1, 0, true, null).get(0);
Assert.assertEquals(testFuture1, insertedFuture1);
EasyMock.verify(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
EasyMock.reset(mockHttpServletResponse);
OperationFuture insertedFuture2 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> testFuture2, 1, true, null).get(1);
Assert.assertEquals(testFuture2, insertedFuture2);
Assert.assertEquals(userTaskManager.getUserTaskByUserTaskId(testUserTaskId, mockHttpServletRequest).futures().size(), 2);
userTaskManager.close();
}
use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.
the class UserTaskManagerTest method testCreateUserTask.
@Test
public void testCreateUserTask() {
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();
HttpServletRequest mockHttpServletRequest1 = prepareRequest(mockHttpSession, null);
HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
Capture<String> userTaskHeader = Capture.newInstance();
Capture<String> userTaskHeaderValue = Capture.newInstance();
mockHttpServletResponse.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
EasyMock.replay(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
OperationFuture future = new OperationFuture("future");
UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6), 100, new MockTime(), mockUuidGenerator);
// test-case: create user-task based on request and get future
OperationFuture future1 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest1, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
Assert.assertEquals(userTaskHeader.getValue(), UserTaskManager.USER_TASK_HEADER_NAME);
Assert.assertEquals(userTaskHeaderValue.getValue(), testUserTaskId.toString());
Assert.assertEquals(future, future1);
EasyMock.verify(mockUuidGenerator, mockHttpServletResponse, mockHttpSession);
EasyMock.reset(mockHttpServletResponse);
// test-case: get same future back using sessions
OperationFuture future2 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest1, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
Assert.assertEquals(userTaskHeader.getValue(), UserTaskManager.USER_TASK_HEADER_NAME);
Assert.assertEquals(userTaskHeaderValue.getValue(), testUserTaskId.toString());
Assert.assertEquals(future, future2);
HttpServletRequest mockHttpServletRequest2 = prepareRequest(mockHttpSession, testUserTaskId.toString());
EasyMock.reset(mockHttpServletResponse);
// test-case: get future back using user-task-id
OperationFuture future3 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest2, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
Assert.assertEquals(userTaskHeader.getValue(), UserTaskManager.USER_TASK_HEADER_NAME);
Assert.assertEquals(userTaskHeaderValue.getValue(), testUserTaskId.toString());
Assert.assertEquals(future, future3);
EasyMock.reset(mockHttpServletResponse);
// test-case: for sync task, UserTaskManager does not create mapping between request URL and UUID.
HttpServletRequest mockHttpServletRequest3 = prepareRequest(null, null, "test_sync_request", Collections.emptyMap());
OperationFuture future4 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest3, mockHttpServletResponse, uuid -> future, 0, false, null).get(0);
// New async request have session mapping where a session key is mapped to the UUID associated with the request. Such a mapping is
// not created for sync request. So in the 2 asserts below , we expect that given a http request, we are able to find request associated UUID
// for async request but not for sync request.
UUID savedUUID = userTaskManager.getUserTaskId(mockHttpServletRequest3);
Assert.assertNull(savedUUID);
Assert.assertEquals(future4, future);
EasyMock.reset(mockHttpServletResponse);
OperationFuture future5 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest3, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
savedUUID = userTaskManager.getUserTaskId(mockHttpServletRequest3);
Assert.assertNotEquals(savedUUID, null);
Assert.assertEquals(future5, future);
userTaskManager.close();
}
Aggregations