use of org.elasticsearch.action.ActionListener in project crate by crate.
the class RepositoryServiceTest method testRepositoryIsDroppedOnFailure.
@Test
public void testRepositoryIsDroppedOnFailure() throws Throwable {
expectedException.expect(RepositoryException.class);
// add repo to cluster service so that it exists..
RepositoriesMetaData repos = new RepositoriesMetaData(new RepositoryMetaData("repo1", "fs", Settings.EMPTY));
ClusterState state = ClusterState.builder(new ClusterName("dummy")).metaData(MetaData.builder().putCustom(RepositoriesMetaData.TYPE, repos)).build();
ClusterService clusterService = new NoopClusterService(state);
final ActionFilters actionFilters = mock(ActionFilters.class, Answers.RETURNS_MOCKS.get());
IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(Settings.EMPTY);
final AtomicBoolean deleteRepoCalled = new AtomicBoolean(false);
TransportDeleteRepositoryAction deleteRepositoryAction = new TransportDeleteRepositoryAction(Settings.EMPTY, mock(TransportService.class, Answers.RETURNS_MOCKS.get()), clusterService, mock(RepositoriesService.class), threadPool, actionFilters, indexNameExpressionResolver) {
@Override
protected void doExecute(Task task, DeleteRepositoryRequest request, ActionListener<DeleteRepositoryResponse> listener) {
deleteRepoCalled.set(true);
listener.onResponse(mock(DeleteRepositoryResponse.class));
}
};
TransportPutRepositoryAction putRepo = new TransportPutRepositoryAction(Settings.EMPTY, mock(TransportService.class, Answers.RETURNS_MOCKS.get()), clusterService, mock(RepositoriesService.class), threadPool, actionFilters, indexNameExpressionResolver) {
@Override
protected void doExecute(Task task, PutRepositoryRequest request, ActionListener<PutRepositoryResponse> listener) {
listener.onFailure(new RepositoryException(request.name(), "failure"));
}
};
RepositoryService repositoryService = new RepositoryService(clusterService, deleteRepositoryAction, putRepo);
try {
repositoryService.execute(new CreateRepositoryAnalyzedStatement("repo1", "fs", Settings.EMPTY)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
assertThat(deleteRepoCalled.get(), is(true));
throw e.getCause();
}
}
use of org.elasticsearch.action.ActionListener in project crate by crate.
the class TransportsTest method testOnFailureOnListenerIsCalledIfNodeIsNotInClusterState.
@Test
public void testOnFailureOnListenerIsCalledIfNodeIsNotInClusterState() throws Exception {
Transports transports = new Transports(new NoopClusterService(), mock(TransportService.class));
ActionListener actionListener = mock(ActionListener.class);
transports.sendRequest("actionName", "invalid", mock(TransportRequest.class), actionListener, mock(TransportResponseHandler.class));
verify(actionListener, times(1)).onFailure(any(Throwable.class));
}
use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.
the class TransportGetTaskAction method runOnNodeWithTaskIfPossible.
/**
* Executed on the coordinating node to forward execution of the remaining work to the node that matches that requested
* {@link TaskId#getNodeId()}. If the node isn't in the cluster then this will just proceed to
* {@link #getFinishedTaskFromIndex(Task, GetTaskRequest, ActionListener)} on this node.
*/
private void runOnNodeWithTaskIfPossible(Task thisTask, GetTaskRequest request, ActionListener<GetTaskResponse> listener) {
TransportRequestOptions.Builder builder = TransportRequestOptions.builder();
if (request.getTimeout() != null) {
builder.withTimeout(request.getTimeout());
}
builder.withCompress(false);
DiscoveryNode node = clusterService.state().nodes().get(request.getTaskId().getNodeId());
if (node == null) {
// Node is no longer part of the cluster! Try and look the task up from the results index.
getFinishedTaskFromIndex(thisTask, request, ActionListener.wrap(listener::onResponse, e -> {
if (e instanceof ResourceNotFoundException) {
e = new ResourceNotFoundException("task [" + request.getTaskId() + "] belongs to the node [" + request.getTaskId().getNodeId() + "] which isn't part of the cluster and there is no record of the task", e);
}
listener.onFailure(e);
}));
return;
}
GetTaskRequest nodeRequest = request.nodeRequest(clusterService.localNode().getId(), thisTask.getId());
transportService.sendRequest(node, GetTaskAction.NAME, nodeRequest, builder.build(), new TransportResponseHandler<GetTaskResponse>() {
@Override
public GetTaskResponse newInstance() {
return new GetTaskResponse();
}
@Override
public void handleResponse(GetTaskResponse response) {
listener.onResponse(response);
}
@Override
public void handleException(TransportException exp) {
listener.onFailure(exp);
}
@Override
public String executor() {
return ThreadPool.Names.SAME;
}
});
}
use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.
the class TransportBulkActionTookTests method createAction.
private TransportBulkAction createAction(boolean controlled, AtomicLong expected) {
CapturingTransport capturingTransport = new CapturingTransport();
TransportService transportService = new TransportService(clusterService.getSettings(), capturingTransport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> clusterService.localNode(), null);
transportService.start();
transportService.acceptIncomingRequests();
IndexNameExpressionResolver resolver = new Resolver(Settings.EMPTY);
ActionFilters actionFilters = new ActionFilters(new HashSet<>());
TransportCreateIndexAction createIndexAction = new TransportCreateIndexAction(Settings.EMPTY, transportService, clusterService, threadPool, null, actionFilters, resolver);
if (controlled) {
return new TestTransportBulkAction(Settings.EMPTY, threadPool, transportService, clusterService, null, createIndexAction, actionFilters, resolver, null, expected::get) {
@Override
void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses) {
expected.set(1000000);
super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses);
}
};
} else {
return new TestTransportBulkAction(Settings.EMPTY, threadPool, transportService, clusterService, null, createIndexAction, actionFilters, resolver, null, System::nanoTime) {
@Override
void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses) {
long elapsed = spinForAtLeastOneMillisecond();
expected.set(elapsed);
super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses);
}
};
}
}
use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.
the class TransportRethrottleActionTests method rethrottleTestCase.
/**
* Test rethrottling.
* @param runningSlices the number of slices still running
* @param simulator simulate a response from the sub-request to rethrottle the child requests
* @param verifier verify the resulting response
*/
private void rethrottleTestCase(int runningSlices, Consumer<ActionListener<ListTasksResponse>> simulator, Consumer<ActionListener<TaskInfo>> verifier) {
Client client = mock(Client.class);
String localNodeId = randomAsciiOfLength(5);
float newRequestsPerSecond = randomValueOtherThanMany(f -> f <= 0, () -> randomFloat());
@SuppressWarnings("unchecked") ActionListener<TaskInfo> listener = mock(ActionListener.class);
TransportRethrottleAction.rethrottle(localNodeId, client, task, newRequestsPerSecond, listener);
// Capture the sub request and the listener so we can verify they are sane
ArgumentCaptor<RethrottleRequest> subRequest = ArgumentCaptor.forClass(RethrottleRequest.class);
// Magical generics incantation.....
@SuppressWarnings({ "unchecked", "rawtypes" }) ArgumentCaptor<ActionListener<ListTasksResponse>> subListener = ArgumentCaptor.forClass((Class) ActionListener.class);
if (runningSlices > 0) {
verify(client).execute(eq(RethrottleAction.INSTANCE), subRequest.capture(), subListener.capture());
assertEquals(new TaskId(localNodeId, task.getId()), subRequest.getValue().getParentTaskId());
assertEquals(newRequestsPerSecond / runningSlices, subRequest.getValue().getRequestsPerSecond(), 0.00001f);
simulator.accept(subListener.getValue());
}
verifier.accept(listener);
}
Aggregations