Skip to content

Commit f84b766

Browse files
authored
Make test more robust for API key auth 429 (#59077) (#59136)
Adds error handling when filling up the queue of the crypto thread pool. Also reduce queue size of the crypto thread pool to 10 so that the queue can be cleared out in time. Test testAuthenticationReturns429WhenThreadPoolIsSaturated has seen failure on CI when it tries to push 1000 tasks into the queue (setup phase). Since multiple tests share the same internal test cluster, it may be possible that there are lingering requests not fully cleared out from the queue. When it happens, we will not be able to push all 1000 tasks into the queue. But since what we need is just queue saturation, so as long as we can be sure that the queue is fully filled, it is safe to ignore rejection error and just move on. A number of 1000 tasks also take some to clear out, which could cause the test suite to time out. This PR change the queue to 10 so the tests would have better chance to complete in time.
1 parent e8181fc commit f84b766

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.common.settings.Settings;
2828
import org.elasticsearch.common.unit.TimeValue;
2929
import org.elasticsearch.common.util.concurrent.EsExecutors;
30+
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
3031
import org.elasticsearch.common.util.set.Sets;
3132
import org.elasticsearch.rest.RestStatus;
3233
import org.elasticsearch.test.SecurityIntegTestCase;
@@ -63,9 +64,9 @@
6364
import java.util.concurrent.CountDownLatch;
6465
import java.util.concurrent.ExecutionException;
6566
import java.util.concurrent.ExecutorService;
67+
import java.util.concurrent.Future;
6668
import java.util.concurrent.TimeUnit;
6769
import java.util.stream.Collectors;
68-
import java.util.stream.IntStream;
6970
import java.util.stream.Stream;
7071

7172
import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME;
@@ -82,6 +83,7 @@
8283

8384
public class ApiKeyIntegTests extends SecurityIntegTestCase {
8485
private static final long DELETE_INTERVAL_MILLIS = 100L;
86+
private static final int CRYPTO_THREAD_POOL_QUEUE_SIZE = 10;
8587

8688
@Override
8789
public Settings nodeSettings(int nodeOrdinal) {
@@ -90,6 +92,7 @@ public Settings nodeSettings(int nodeOrdinal) {
9092
.put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true)
9193
.put(ApiKeyService.DELETE_INTERVAL.getKey(), TimeValue.timeValueMillis(DELETE_INTERVAL_MILLIS))
9294
.put(ApiKeyService.DELETE_TIMEOUT.getKey(), TimeValue.timeValueSeconds(5L))
95+
.put("xpack.security.crypto.thread_pool.queue_size", CRYPTO_THREAD_POOL_QUEUE_SIZE)
9396
.build();
9497
}
9598

@@ -855,7 +858,7 @@ public void testDerivedKeys() throws ExecutionException, InterruptedException {
855858
assertApiKeyNotCreated(client,"key-5");
856859
}
857860

858-
public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOException, InterruptedException {
861+
public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOException, InterruptedException, ExecutionException {
859862
final String nodeName = randomFrom(internalCluster().getNodeNames());
860863
final Settings settings = internalCluster().getInstance(Settings.class, nodeName);
861864
final ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class, nodeName);
@@ -882,7 +885,6 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc
882885
final int numberOfThreads = (allocatedProcessors + 1) / 2;
883886
final CountDownLatch blockingLatch = new CountDownLatch(1);
884887
final CountDownLatch readyLatch = new CountDownLatch(numberOfThreads);
885-
886888
for (int i = 0; i < numberOfThreads; i++) {
887889
executorService.submit(() -> {
888890
readyLatch.countDown();
@@ -894,8 +896,13 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc
894896
});
895897
}
896898
// Fill the whole queue for the crypto thread pool
897-
final int queueSize = 1000;
898-
IntStream.range(0, queueSize).forEach(i -> executorService.submit(() -> {}));
899+
Future<?> lastTaskFuture = null;
900+
try {
901+
for (int i = 0; i < CRYPTO_THREAD_POOL_QUEUE_SIZE; i++) {
902+
lastTaskFuture = executorService.submit(() -> { });
903+
}
904+
} catch (EsRejectedExecutionException e) {
905+
}
899906
readyLatch.await();
900907

901908
try (RestClient restClient = createRestClient(nodeInfos, null, "http")) {
@@ -910,6 +917,9 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc
910917
assertThat(responseException.getResponse().getStatusLine().getStatusCode(), is(429));
911918
} finally {
912919
blockingLatch.countDown();
920+
if (lastTaskFuture != null) {
921+
lastTaskFuture.get();
922+
}
913923
}
914924
}
915925

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void createThreadPool() {
105105
threadPool = Mockito.spy(
106106
new TestThreadPool("api key service tests",
107107
new FixedExecutorBuilder(Settings.EMPTY, SECURITY_CRYPTO_THREAD_POOL_NAME, 1, 1000,
108-
"xpack.security.authc.api_key.thread_pool", false))
108+
"xpack.security.crypto.thread_pool", false))
109109
);
110110
}
111111

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public void init() throws Exception {
211211
new FixedExecutorBuilder(settings, THREAD_POOL_NAME, 1, 1000,
212212
"xpack.security.authc.token.thread_pool", false),
213213
new FixedExecutorBuilder(Settings.EMPTY, SECURITY_CRYPTO_THREAD_POOL_NAME, 1, 1000,
214-
"xpack.security.authc.api_key.thread_pool", false)
214+
"xpack.security.crypto.thread_pool", false)
215215
);
216216
threadContext = threadPool.getThreadContext();
217217
when(client.threadPool()).thenReturn(threadPool);

0 commit comments

Comments
 (0)