Search in sources :

Example 91 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 92 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 93 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 94 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)

Example 95 with ActionListener

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

the class BulkRetryCoordinatorTest method testScheduleRetryAfterRejectedExecution.

@Test
public void testScheduleRetryAfterRejectedExecution() throws Exception {
    ThreadPool threadPool = mock(ThreadPool.class);
    BulkRetryCoordinator coordinator = new BulkRetryCoordinator(threadPool);
    BulkRequestExecutor<ShardUpsertRequest> executor = (request, listener) -> {
        listener.onFailure(new EsRejectedExecutionException("Dummy execution rejected"));
    };
    coordinator.retry(shardRequest(), executor, new ActionListener<ShardResponse>() {

        @Override
        public void onResponse(ShardResponse shardResponse) {
        }

        @Override
        public void onFailure(Throwable e) {
        }
    });
    verify(threadPool).schedule(eq(TimeValue.timeValueMillis(0)), eq(ThreadPool.Names.SAME), any(Runnable.class));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Reference(io.crate.metadata.Reference) Test(org.junit.Test) TableIdent(io.crate.metadata.TableIdent) UUID(java.util.UUID) SettableFuture(com.google.common.util.concurrent.SettableFuture) CrateUnitTest(io.crate.test.integration.CrateUnitTest) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) RowGranularity(io.crate.metadata.RowGranularity) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) DataTypes(io.crate.types.DataTypes) TimeValue(org.elasticsearch.common.unit.TimeValue) ShardResponse(io.crate.executor.transport.ShardResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ReferenceIdent(io.crate.metadata.ReferenceIdent) EsExecutors.daemonThreadFactory(org.elasticsearch.common.util.concurrent.EsExecutors.daemonThreadFactory) ActionListener(org.elasticsearch.action.ActionListener) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ShardResponse(io.crate.executor.transport.ShardResponse) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) ThreadPool(org.elasticsearch.threadpool.ThreadPool) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

ActionListener (org.elasticsearch.action.ActionListener)148 IOException (java.io.IOException)75 List (java.util.List)58 ThreadPool (org.elasticsearch.threadpool.ThreadPool)53 ClusterState (org.elasticsearch.cluster.ClusterState)50 ArrayList (java.util.ArrayList)46 Settings (org.elasticsearch.common.settings.Settings)45 Map (java.util.Map)42 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)41 ShardId (org.elasticsearch.index.shard.ShardId)40 Collections (java.util.Collections)39 Set (java.util.Set)39 ClusterService (org.elasticsearch.cluster.service.ClusterService)35 Logger (org.apache.logging.log4j.Logger)34 HashMap (java.util.HashMap)32 TransportService (org.elasticsearch.transport.TransportService)32 ElasticsearchException (org.elasticsearch.ElasticsearchException)31 Collectors (java.util.stream.Collectors)30 Index (org.elasticsearch.index.Index)30 Test (org.junit.Test)30