Search in sources :

Example 16 with OperationFuture

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;
}
Also used : RemoveBrokersRunnable(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.RemoveBrokersRunnable) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture)

Example 17 with OperationFuture

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));
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture)

Example 18 with OperationFuture

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Capture(org.easymock.Capture) Time(org.apache.kafka.common.utils.Time) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) EasyMock(org.easymock.EasyMock) TimeUnit(java.util.concurrent.TimeUnit) MockTime(kafka.utils.MockTime) HttpServletRequest(javax.servlet.http.HttpServletRequest) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) Map(java.util.Map) GET_METHOD(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServletUtils.GET_METHOD) Assert(org.junit.Assert) Collections(java.util.Collections) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) MockTime(kafka.utils.MockTime) Test(org.junit.Test)

Example 19 with OperationFuture

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Capture(org.easymock.Capture) Time(org.apache.kafka.common.utils.Time) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) EasyMock(org.easymock.EasyMock) TimeUnit(java.util.concurrent.TimeUnit) MockTime(kafka.utils.MockTime) HttpServletRequest(javax.servlet.http.HttpServletRequest) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) Map(java.util.Map) GET_METHOD(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServletUtils.GET_METHOD) Assert(org.junit.Assert) Collections(java.util.Collections) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) UUID(java.util.UUID) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) MockTime(kafka.utils.MockTime) Test(org.junit.Test)

Example 20 with OperationFuture

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Capture(org.easymock.Capture) Time(org.apache.kafka.common.utils.Time) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) EasyMock(org.easymock.EasyMock) TimeUnit(java.util.concurrent.TimeUnit) MockTime(kafka.utils.MockTime) HttpServletRequest(javax.servlet.http.HttpServletRequest) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) Map(java.util.Map) GET_METHOD(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServletUtils.GET_METHOD) Assert(org.junit.Assert) Collections(java.util.Collections) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) UUID(java.util.UUID) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) MockTime(kafka.utils.MockTime) Test(org.junit.Test)

Aggregations

OperationFuture (com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HashMap (java.util.HashMap)7 UUID (java.util.UUID)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 HttpSession (javax.servlet.http.HttpSession)6 MockTime (kafka.utils.MockTime)6 Test (org.junit.Test)6 GET_METHOD (com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServletUtils.GET_METHOD)5 Collections (java.util.Collections)5 Time (org.apache.kafka.common.utils.Time)5 Capture (org.easymock.Capture)5 EasyMock (org.easymock.EasyMock)5 Assert (org.junit.Assert)5 CruiseControlResponse (com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse)2 ArrayList (java.util.ArrayList)2 Timer (com.codahale.metrics.Timer)1 Gson (com.google.gson.Gson)1