Search in sources :

Example 1 with CruiseControlResponse

use of com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse in project cruise-control by linkedin.

the class OperationFutureTest method testGetCompleted.

@Test
public void testGetCompleted() throws InterruptedException {
    OperationFuture future = new OperationFuture("testGetCompleted");
    TestThread t = new TestThread(future);
    t.start();
    CruiseControlResponse expectedResponse = new ResumeSamplingResult(null);
    future.complete(expectedResponse);
    t.join();
    assertTrue(future.isDone());
    assertEquals(expectedResponse, t.result());
}
Also used : CruiseControlResponse(com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse) ResumeSamplingResult(com.linkedin.kafka.cruisecontrol.servlet.response.ResumeSamplingResult) Test(org.junit.Test)

Example 2 with CruiseControlResponse

use of com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse 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)

Example 3 with CruiseControlResponse

use of com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse in project cruise-control by linkedin.

the class AbstractRequest method handle.

/**
 * Handle the request and populate the response.
 *
 * @param request Http servlet request.
 * @param response Http servlet response.
 */
@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ExecutionException, InterruptedException {
    if (parameters().parseParameters(response)) {
        LOG.warn("Failed to parse parameters: {} for request: {}.", request.getParameterMap(), request.getPathInfo());
        return;
    }
    CruiseControlResponse ccResponse = getResponse(request, response);
    ccResponse.writeSuccessResponse(parameters(), response);
}
Also used : CruiseControlResponse(com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse)

Example 4 with CruiseControlResponse

use of com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse in project cruise-control by linkedin.

the class AbstractSyncRequest method getResponse.

@Override
public CruiseControlResponse getResponse(HttpServletRequest request, HttpServletResponse response) throws ExecutionException, InterruptedException {
    LOG.info("Processing sync request {}.", name());
    long requestExecutionStartTime = System.nanoTime();
    int step = 0;
    OperationFuture resultFuture = _userTaskManager.getOrCreateUserTask(request, response, uuid -> {
        OperationFuture future = new OperationFuture(String.format("%s request", parameters().endPoint().toString()));
        future.complete(handle());
        return future;
    }, step, false, parameters()).get(step);
    CruiseControlResponse ccResponse = resultFuture.get();
    _successfulRequestExecutionTimer.get(parameters().endPoint()).update(System.nanoTime() - requestExecutionStartTime, TimeUnit.NANOSECONDS);
    return ccResponse;
}
Also used : CruiseControlParameters(com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters) Logger(org.slf4j.Logger) EndPoint(com.linkedin.cruisecontrol.servlet.EndPoint) HttpServletResponse(javax.servlet.http.HttpServletResponse) LoggerFactory(org.slf4j.LoggerFactory) AbstractRequest(com.linkedin.kafka.cruisecontrol.servlet.handler.AbstractRequest) CruiseControlResponse(com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse) AbstractAsyncRequest(com.linkedin.kafka.cruisecontrol.servlet.handler.async.AbstractAsyncRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) UserTaskManager(com.linkedin.kafka.cruisecontrol.servlet.UserTaskManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) Map(java.util.Map) Timer(com.codahale.metrics.Timer) CruiseControlResponse(com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) EndPoint(com.linkedin.cruisecontrol.servlet.EndPoint)

Example 5 with CruiseControlResponse

use of com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse in project cruise-control by linkedin.

the class OperationFutureTest method testCancelInProgressFuture.

@Test
public void testCancelInProgressFuture() throws InterruptedException {
    OperationFuture future = new OperationFuture("testCancelInProgressFuture");
    AtomicBoolean interrupted = new AtomicBoolean(false);
    Semaphore resultIsCalledSemaphore = new Semaphore(1);
    resultIsCalledSemaphore.acquire();
    // An execution thread that should be interrupted before completing the future.
    Thread executionThread = new Thread(new OperationRunnable(null, future) {

        @Override
        protected CruiseControlResponse getResult() throws Exception {
            resultIsCalledSemaphore.release();
            try {
                synchronized (this) {
                    while (!interrupted.get()) {
                        this.wait();
                    }
                }
                return new ResumeSamplingResult(null);
            } catch (InterruptedException ie) {
                interrupted.set(true);
                throw ie;
            }
        }
    });
    executionThread.start();
    TestThread t = new TestThread(future);
    t.start();
    resultIsCalledSemaphore.acquire();
    future.cancel(true);
    t.join();
    executionThread.join();
    assertThat(t.result(), instanceOf(PauseSamplingResult.class));
    assertThat(t.exception(), instanceOf(CancellationException.class));
    assertTrue(future.isDone());
    assertTrue(future.isCancelled());
    assertTrue(interrupted.get());
}
Also used : CruiseControlResponse(com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CancellationException(java.util.concurrent.CancellationException) ResumeSamplingResult(com.linkedin.kafka.cruisecontrol.servlet.response.ResumeSamplingResult) PauseSamplingResult(com.linkedin.kafka.cruisecontrol.servlet.response.PauseSamplingResult) Test(org.junit.Test)

Aggregations

CruiseControlResponse (com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse)5 OperationFuture (com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture)2 ResumeSamplingResult (com.linkedin.kafka.cruisecontrol.servlet.response.ResumeSamplingResult)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.Test)2 Timer (com.codahale.metrics.Timer)1 EndPoint (com.linkedin.cruisecontrol.servlet.EndPoint)1 CruiseControlParameters (com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters)1 UserTaskManager (com.linkedin.kafka.cruisecontrol.servlet.UserTaskManager)1 AbstractRequest (com.linkedin.kafka.cruisecontrol.servlet.handler.AbstractRequest)1 AbstractAsyncRequest (com.linkedin.kafka.cruisecontrol.servlet.handler.async.AbstractAsyncRequest)1 PauseSamplingResult (com.linkedin.kafka.cruisecontrol.servlet.response.PauseSamplingResult)1 ProgressResult (com.linkedin.kafka.cruisecontrol.servlet.response.ProgressResult)1 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 Semaphore (java.util.concurrent.Semaphore)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1