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());
}
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;
}
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);
}
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;
}
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());
}
Aggregations