Search in sources :

Example 51 with ActionListener

use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.

the class ContextPreservingActionListenerTests method testOriginalContextIsPreservedAfterOnFailure.

public void testOriginalContextIsPreservedAfterOnFailure() throws Exception {
    try (ThreadContext threadContext = new ThreadContext(Settings.EMPTY)) {
        final boolean nonEmptyContext = randomBoolean();
        if (nonEmptyContext) {
            threadContext.putHeader("not empty", "value");
        }
        ContextPreservingActionListener<Void> actionListener;
        try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
            threadContext.putHeader("foo", "bar");
            actionListener = new ContextPreservingActionListener<>(threadContext.newRestorableContext(true), new ActionListener<Void>() {

                @Override
                public void onResponse(Void aVoid) {
                    throw new RuntimeException("onResponse shouldn't be called");
                }

                @Override
                public void onFailure(Exception e) {
                    assertEquals("bar", threadContext.getHeader("foo"));
                    assertNull(threadContext.getHeader("not empty"));
                }
            });
        }
        assertNull(threadContext.getHeader("foo"));
        assertEquals(nonEmptyContext ? "value" : null, threadContext.getHeader("not empty"));
        actionListener.onFailure(null);
        assertNull(threadContext.getHeader("foo"));
        assertEquals(nonEmptyContext ? "value" : null, threadContext.getHeader("not empty"));
    }
}
Also used : ActionListener(org.elasticsearch.action.ActionListener) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) IOException(java.io.IOException)

Example 52 with ActionListener

use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.

the class ContextPreservingActionListenerTests method testOriginalContextIsPreservedAfterOnResponse.

public void testOriginalContextIsPreservedAfterOnResponse() throws IOException {
    try (ThreadContext threadContext = new ThreadContext(Settings.EMPTY)) {
        final boolean nonEmptyContext = randomBoolean();
        if (nonEmptyContext) {
            threadContext.putHeader("not empty", "value");
        }
        ContextPreservingActionListener<Void> actionListener;
        try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
            threadContext.putHeader("foo", "bar");
            actionListener = new ContextPreservingActionListener<>(threadContext.newRestorableContext(true), new ActionListener<Void>() {

                @Override
                public void onResponse(Void aVoid) {
                    assertEquals("bar", threadContext.getHeader("foo"));
                    assertNull(threadContext.getHeader("not empty"));
                }

                @Override
                public void onFailure(Exception e) {
                    throw new RuntimeException("onFailure shouldn't be called", e);
                }
            });
        }
        assertNull(threadContext.getHeader("foo"));
        assertEquals(nonEmptyContext ? "value" : null, threadContext.getHeader("not empty"));
        actionListener.onResponse(null);
        assertNull(threadContext.getHeader("foo"));
        assertEquals(nonEmptyContext ? "value" : null, threadContext.getHeader("not empty"));
    }
}
Also used : ActionListener(org.elasticsearch.action.ActionListener) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) IOException(java.io.IOException)

Example 53 with ActionListener

use of org.elasticsearch.action.ActionListener in project elasticsearch by elastic.

the class TransportActionFilterChainTests method testActionFiltersRequest.

public void testActionFiltersRequest() throws ExecutionException, InterruptedException {
    int numFilters = randomInt(10);
    Set<Integer> orders = new HashSet<>(numFilters);
    while (orders.size() < numFilters) {
        orders.add(randomInt(10));
    }
    Set<ActionFilter> filters = new HashSet<>();
    for (Integer order : orders) {
        filters.add(new RequestTestFilter(order, randomFrom(RequestOperation.values())));
    }
    String actionName = randomAsciiOfLength(randomInt(30));
    ActionFilters actionFilters = new ActionFilters(filters);
    TransportAction<TestRequest, TestResponse> transportAction = new TransportAction<TestRequest, TestResponse>(Settings.EMPTY, actionName, null, actionFilters, null, new TaskManager(Settings.EMPTY)) {

        @Override
        protected void doExecute(TestRequest request, ActionListener<TestResponse> listener) {
            listener.onResponse(new TestResponse());
        }
    };
    ArrayList<ActionFilter> actionFiltersByOrder = new ArrayList<>(filters);
    Collections.sort(actionFiltersByOrder, new Comparator<ActionFilter>() {

        @Override
        public int compare(ActionFilter o1, ActionFilter o2) {
            return Integer.compare(o1.order(), o2.order());
        }
    });
    List<ActionFilter> expectedActionFilters = new ArrayList<>();
    boolean errorExpected = false;
    for (ActionFilter filter : actionFiltersByOrder) {
        RequestTestFilter testFilter = (RequestTestFilter) filter;
        expectedActionFilters.add(testFilter);
        if (testFilter.callback == RequestOperation.LISTENER_FAILURE) {
            errorExpected = true;
        }
        if (!(testFilter.callback == RequestOperation.CONTINUE_PROCESSING)) {
            break;
        }
    }
    PlainListenableActionFuture<TestResponse> future = new PlainListenableActionFuture<>(null);
    transportAction.execute(new TestRequest(), future);
    try {
        assertThat(future.get(), notNullValue());
        assertThat("shouldn't get here if an error is expected", errorExpected, equalTo(false));
    } catch (ExecutionException e) {
        assertThat("shouldn't get here if an error is not expected " + e.getMessage(), errorExpected, equalTo(true));
    }
    List<RequestTestFilter> testFiltersByLastExecution = new ArrayList<>();
    for (ActionFilter actionFilter : actionFilters.filters()) {
        testFiltersByLastExecution.add((RequestTestFilter) actionFilter);
    }
    Collections.sort(testFiltersByLastExecution, new Comparator<RequestTestFilter>() {

        @Override
        public int compare(RequestTestFilter o1, RequestTestFilter o2) {
            return Integer.compare(o1.executionToken, o2.executionToken);
        }
    });
    ArrayList<RequestTestFilter> finalTestFilters = new ArrayList<>();
    for (ActionFilter filter : testFiltersByLastExecution) {
        RequestTestFilter testFilter = (RequestTestFilter) filter;
        finalTestFilters.add(testFilter);
        if (!(testFilter.callback == RequestOperation.CONTINUE_PROCESSING)) {
            break;
        }
    }
    assertThat(finalTestFilters.size(), equalTo(expectedActionFilters.size()));
    for (int i = 0; i < finalTestFilters.size(); i++) {
        RequestTestFilter testFilter = finalTestFilters.get(i);
        assertThat(testFilter, equalTo(expectedActionFilters.get(i)));
        assertThat(testFilter.runs.get(), equalTo(1));
        assertThat(testFilter.lastActionName, equalTo(actionName));
    }
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskManager(org.elasticsearch.tasks.TaskManager) ActionListener(org.elasticsearch.action.ActionListener)

Example 54 with ActionListener

use of org.elasticsearch.action.ActionListener in project crate by crate.

the class UpsertByIdContextTest method testKill.

@Test
public void testKill() throws Exception {
    ArgumentCaptor<ActionListener> listener = ArgumentCaptor.forClass(ActionListener.class);
    context.prepare();
    context.start();
    verify(delegate).execute(any(ShardUpsertRequest.class), listener.capture());
    // context is killed
    context.kill(null);
    // listener returns
    ShardResponse response = mock(ShardResponse.class);
    listener.getValue().onResponse(response);
    expectedException.expectCause(CauseMatcher.cause(InterruptedException.class));
    context.completionFuture().get();
}
Also used : ShardResponse(io.crate.executor.transport.ShardResponse) ActionListener(org.elasticsearch.action.ActionListener) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 55 with ActionListener

use of org.elasticsearch.action.ActionListener in project crate by crate.

the class RemoteCollectorTest method testKillRequestsAreMadeIfCollectorIsKilledAfterRemoteContextCreation.

@Test
public void testKillRequestsAreMadeIfCollectorIsKilledAfterRemoteContextCreation() throws Exception {
    remoteCollector.doCollect();
    verify(transportJobAction, times(1)).execute(eq("remoteNode"), any(JobRequest.class), listenerCaptor.capture());
    remoteCollector.kill(new InterruptedException());
    ActionListener<JobResponse> listener = listenerCaptor.getValue();
    listener.onResponse(new JobResponse());
    verify(transportKillJobsNodeAction, times(1)).broadcast(any(KillJobsRequest.class), any(ActionListener.class));
}
Also used : JobRequest(io.crate.action.job.JobRequest) ActionListener(org.elasticsearch.action.ActionListener) KillJobsRequest(io.crate.executor.transport.kill.KillJobsRequest) JobResponse(io.crate.action.job.JobResponse) Test(org.junit.Test)

Aggregations

ActionListener (org.elasticsearch.action.ActionListener)58 IOException (java.io.IOException)25 ThreadPool (org.elasticsearch.threadpool.ThreadPool)19 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 Settings (org.elasticsearch.common.settings.Settings)18 ClusterState (org.elasticsearch.cluster.ClusterState)17 Index (org.elasticsearch.index.Index)13 ShardId (org.elasticsearch.index.shard.ShardId)13 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)12 TransportService (org.elasticsearch.transport.TransportService)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 ArrayList (java.util.ArrayList)10 TimeValue (org.elasticsearch.common.unit.TimeValue)10 CrateUnitTest (io.crate.test.integration.CrateUnitTest)9 List (java.util.List)9 ActionFilters (org.elasticsearch.action.support.ActionFilters)9 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)9 TransportException (org.elasticsearch.transport.TransportException)9 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)8 ShardResponse (io.crate.executor.transport.ShardResponse)7