Search in sources :

Example 1 with FakeTcpChannel

use of org.opensearch.transport.FakeTcpChannel in project OpenSearch by opensearch-project.

the class TaskManagerTests method testTrackingChannelTask.

public void testTrackingChannelTask() throws Exception {
    final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
    Set<Task> cancelledTasks = ConcurrentCollections.newConcurrentSet();
    taskManager.setTaskCancellationService(new TaskCancellationService(mock(TransportService.class)) {

        @Override
        void cancelTaskAndDescendants(CancellableTask task, String reason, boolean waitForCompletion, ActionListener<Void> listener) {
            assertThat(reason, equalTo("channel was closed"));
            assertFalse(waitForCompletion);
            assertTrue("task [" + task + "] was cancelled already", cancelledTasks.add(task));
        }
    });
    Map<TcpChannel, Set<Task>> pendingTasks = new HashMap<>();
    Set<Task> expectedCancelledTasks = new HashSet<>();
    FakeTcpChannel[] channels = new FakeTcpChannel[randomIntBetween(1, 10)];
    List<Releasable> stopTrackingTasks = new ArrayList<>();
    for (int i = 0; i < channels.length; i++) {
        channels[i] = new SingleThreadedTcpChannel();
    }
    int iterations = randomIntBetween(1, 200);
    for (int i = 0; i < iterations; i++) {
        final List<Releasable> subset = randomSubsetOf(stopTrackingTasks);
        stopTrackingTasks.removeAll(subset);
        Releasables.close(subset);
        final FakeTcpChannel channel = randomFrom(channels);
        final Task task = taskManager.register("transport", "test", new CancellableRequest(Integer.toString(i)));
        if (channel.isOpen() && randomBoolean()) {
            channel.close();
            expectedCancelledTasks.addAll(pendingTasks.getOrDefault(channel, Collections.emptySet()));
        }
        final Releasable stopTracking = taskManager.startTrackingCancellableChannelTask(channel, (CancellableTask) task);
        if (channel.isOpen()) {
            pendingTasks.computeIfAbsent(channel, k -> new HashSet<>()).add(task);
            stopTrackingTasks.add(() -> {
                stopTracking.close();
                assertTrue(pendingTasks.get(channel).remove(task));
                expectedCancelledTasks.remove(task);
            });
        } else {
            expectedCancelledTasks.add(task);
        }
    }
    assertBusy(() -> assertThat(expectedCancelledTasks, everyItem(in(cancelledTasks))), 30, TimeUnit.SECONDS);
    for (FakeTcpChannel channel : channels) {
        channel.close();
    }
    assertThat(taskManager.numberOfChannelPendingTaskTrackers(), equalTo(0));
}
Also used : ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) HashMap(java.util.HashMap) Releasable(org.opensearch.common.lease.Releasable) Releasables(org.opensearch.common.lease.Releasables) ArrayList(java.util.ArrayList) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) HashSet(java.util.HashSet) Matchers.everyItem(org.hamcrest.Matchers.everyItem) After(org.junit.After) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) TransportTasksActionTests(org.opensearch.action.admin.cluster.node.tasks.TransportTasksActionTests) Before(org.junit.Before) TimeValue(org.opensearch.common.unit.TimeValue) TransportRequest(org.opensearch.transport.TransportRequest) Iterator(java.util.Iterator) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TcpChannel(org.opensearch.transport.TcpChannel) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) TransportService(org.opensearch.transport.TransportService) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Phaser(java.util.concurrent.Phaser) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) Matchers.in(org.hamcrest.Matchers.in) Mockito.mock(org.mockito.Mockito.mock) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) TcpChannel(org.opensearch.transport.TcpChannel) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) Releasable(org.opensearch.common.lease.Releasable) HashSet(java.util.HashSet)

Example 2 with FakeTcpChannel

use of org.opensearch.transport.FakeTcpChannel in project OpenSearch by opensearch-project.

the class TaskManagerTests method testTrackingTaskAndCloseChannelConcurrently.

public void testTrackingTaskAndCloseChannelConcurrently() throws Exception {
    final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
    Set<CancellableTask> cancelledTasks = ConcurrentCollections.newConcurrentSet();
    taskManager.setTaskCancellationService(new TaskCancellationService(mock(TransportService.class)) {

        @Override
        void cancelTaskAndDescendants(CancellableTask task, String reason, boolean waitForCompletion, ActionListener<Void> listener) {
            assertTrue("task [" + task + "] was cancelled already", cancelledTasks.add(task));
        }
    });
    Set<Task> expectedCancelledTasks = ConcurrentCollections.newConcurrentSet();
    FakeTcpChannel[] channels = new FakeTcpChannel[randomIntBetween(1, 10)];
    for (int i = 0; i < channels.length; i++) {
        channels[i] = new FakeTcpChannel();
    }
    Thread[] threads = new Thread[randomIntBetween(2, 8)];
    Phaser phaser = new Phaser(threads.length);
    for (int t = 0; t < threads.length; t++) {
        String threadName = "thread-" + t;
        threads[t] = new Thread(() -> {
            phaser.arriveAndAwaitAdvance();
            int iterations = randomIntBetween(50, 500);
            for (int i = 0; i < iterations; i++) {
                final FakeTcpChannel channel = randomFrom(channels);
                final Task task = taskManager.register("transport", "test", new CancellableRequest(threadName + ":" + i));
                expectedCancelledTasks.add(task);
                taskManager.startTrackingCancellableChannelTask(channel, (CancellableTask) task);
                if (randomInt(100) < 5) {
                    randomFrom(channels).close();
                }
            }
        });
        threads[t].start();
    }
    for (FakeTcpChannel channel : channels) {
        channel.close();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    assertBusy(() -> assertThat(cancelledTasks, equalTo(expectedCancelledTasks)), 1, TimeUnit.MINUTES);
    assertThat(taskManager.numberOfChannelPendingTaskTrackers(), equalTo(0));
}
Also used : FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) Phaser(java.util.concurrent.Phaser)

Aggregations

Phaser (java.util.concurrent.Phaser)2 FakeTcpChannel (org.opensearch.transport.FakeTcpChannel)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.everyItem (org.hamcrest.Matchers.everyItem)1 Matchers.in (org.hamcrest.Matchers.in)1 After (org.junit.After)1 Before (org.junit.Before)1 Mockito.mock (org.mockito.Mockito.mock)1 ActionListener (org.opensearch.action.ActionListener)1 TransportTasksActionTests (org.opensearch.action.admin.cluster.node.tasks.TransportTasksActionTests)1 Releasable (org.opensearch.common.lease.Releasable)1