|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.action.support; |
| 10 | + |
| 11 | +import org.elasticsearch.ElasticsearchException; |
| 12 | +import org.elasticsearch.action.ActionListener; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.core.TimeValue; |
| 15 | +import org.elasticsearch.test.ESTestCase; |
| 16 | +import org.elasticsearch.threadpool.FixedExecutorBuilder; |
| 17 | +import org.elasticsearch.threadpool.ScalingExecutorBuilder; |
| 18 | +import org.elasticsearch.threadpool.TestThreadPool; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | + |
| 26 | +public class ThreadedActionListenerTests extends ESTestCase { |
| 27 | + |
| 28 | + public void testRejectionHandling() throws InterruptedException { |
| 29 | + final var listenerCount = between(1, 1000); |
| 30 | + final var countdownLatch = new CountDownLatch(listenerCount); |
| 31 | + final var threadPool = new TestThreadPool( |
| 32 | + "test", |
| 33 | + Settings.EMPTY, |
| 34 | + new FixedExecutorBuilder(Settings.EMPTY, "fixed-bounded-queue", between(1, 3), 10, "fbq", randomBoolean()), |
| 35 | + new FixedExecutorBuilder(Settings.EMPTY, "fixed-unbounded-queue", between(1, 3), -1, "fnq", randomBoolean()), |
| 36 | + new ScalingExecutorBuilder("scaling-drop-if-shutdown", between(1, 3), between(3, 5), TimeValue.timeValueSeconds(1), false), |
| 37 | + new ScalingExecutorBuilder("scaling-reject-if-shutdown", between(1, 3), between(3, 5), TimeValue.timeValueSeconds(1), true) |
| 38 | + ); |
| 39 | + final var closeFlag = new AtomicBoolean(); |
| 40 | + try { |
| 41 | + final var pools = randomNonEmptySubsetOf( |
| 42 | + List.of("fixed-bounded-queue", "fixed-unbounded-queue", "scaling-drop-if-shutdown", "scaling-reject-if-shutdown") |
| 43 | + ); |
| 44 | + final var shutdownUnsafePools = Set.of("fixed-bounded-queue", "scaling-drop-if-shutdown"); |
| 45 | + |
| 46 | + threadPool.generic().execute(() -> { |
| 47 | + for (int i = 0; i < listenerCount; i++) { |
| 48 | + final var pool = randomFrom(pools); |
| 49 | + final var listener = new ThreadedActionListener<Void>( |
| 50 | + logger, |
| 51 | + threadPool, |
| 52 | + pool, |
| 53 | + ActionListener.wrap(countdownLatch::countDown), |
| 54 | + (pool.equals("fixed-bounded-queue") || pool.startsWith("scaling")) && rarely() |
| 55 | + ); |
| 56 | + synchronized (closeFlag) { |
| 57 | + if (closeFlag.get() && shutdownUnsafePools.contains(pool)) { |
| 58 | + // closing, so tasks submitted to this pool may just be dropped |
| 59 | + countdownLatch.countDown(); |
| 60 | + } else if (randomBoolean()) { |
| 61 | + listener.onResponse(null); |
| 62 | + } else { |
| 63 | + listener.onFailure(new ElasticsearchException("simulated")); |
| 64 | + } |
| 65 | + } |
| 66 | + Thread.yield(); |
| 67 | + } |
| 68 | + }); |
| 69 | + } finally { |
| 70 | + synchronized (closeFlag) { |
| 71 | + assertTrue(closeFlag.compareAndSet(false, true)); |
| 72 | + threadPool.shutdown(); |
| 73 | + } |
| 74 | + assertTrue(threadPool.awaitTermination(10, TimeUnit.SECONDS)); |
| 75 | + } |
| 76 | + assertTrue(countdownLatch.await(10, TimeUnit.SECONDS)); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments