Search in sources :

Example 1 with OperationFuture

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

Example 2 with OperationFuture

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);
}
Also used : HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture)

Example 3 with OperationFuture

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!");
}
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 4 with OperationFuture

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) 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 5 with OperationFuture

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;
}
Also used : CruiseControlResponse(com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse) ProgressResult(com.linkedin.kafka.cruisecontrol.servlet.response.ProgressResult) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) TimeoutException(java.util.concurrent.TimeoutException)

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