Skip to content

Commit ec6f79f

Browse files
authored
Make test more robust for API key auth 429 (#59077)
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 963f285 commit ec6f79f

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;
@@ -66,9 +67,9 @@
6667
import java.util.concurrent.CountDownLatch;
6768
import java.util.concurrent.ExecutionException;
6869
import java.util.concurrent.ExecutorService;
70+
import java.util.concurrent.Future;
6971
import java.util.concurrent.TimeUnit;
7072
import java.util.stream.Collectors;
71-
import java.util.stream.IntStream;
7273
import java.util.stream.Stream;
7374

7475
import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS;
@@ -84,6 +85,7 @@
8485

8586
public class ApiKeyIntegTests extends SecurityIntegTestCase {
8687
private static final long DELETE_INTERVAL_MILLIS = 100L;
88+
private static final int CRYPTO_THREAD_POOL_QUEUE_SIZE = 10;
8789

8890
@Override
8991
public Settings nodeSettings(int nodeOrdinal) {
@@ -92,6 +94,7 @@ public Settings nodeSettings(int nodeOrdinal) {
9294
.put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true)
9395
.put(ApiKeyService.DELETE_INTERVAL.getKey(), TimeValue.timeValueMillis(DELETE_INTERVAL_MILLIS))
9496
.put(ApiKeyService.DELETE_TIMEOUT.getKey(), TimeValue.timeValueSeconds(5L))
97+
.put("xpack.security.crypto.thread_pool.queue_size", CRYPTO_THREAD_POOL_QUEUE_SIZE)
9598
.build();
9699
}
97100

@@ -833,7 +836,7 @@ public void testDerivedKeys() throws ExecutionException, InterruptedException {
833836
assertApiKeyNotCreated(client, "key-5");
834837
}
835838

836-
public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOException, InterruptedException {
839+
public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOException, InterruptedException, ExecutionException {
837840
final String nodeName = randomFrom(internalCluster().getNodeNames());
838841
final Settings settings = internalCluster().getInstance(Settings.class, nodeName);
839842
final ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class, nodeName);
@@ -859,7 +862,6 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc
859862
final int numberOfThreads = (allocatedProcessors + 1) / 2;
860863
final CountDownLatch blockingLatch = new CountDownLatch(1);
861864
final CountDownLatch readyLatch = new CountDownLatch(numberOfThreads);
862-
863865
for (int i = 0; i < numberOfThreads; i++) {
864866
executorService.submit(() -> {
865867
readyLatch.countDown();
@@ -871,8 +873,13 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc
871873
});
872874
}
873875
// Fill the whole queue for the crypto thread pool
874-
final int queueSize = 1000;
875-
IntStream.range(0, queueSize).forEach(i -> executorService.submit(() -> {}));
876+
Future<?> lastTaskFuture = null;
877+
try {
878+
for (int i = 0; i < CRYPTO_THREAD_POOL_QUEUE_SIZE; i++) {
879+
lastTaskFuture = executorService.submit(() -> { });
880+
}
881+
} catch (EsRejectedExecutionException e) {
882+
}
876883
readyLatch.await();
877884

878885
try (RestClient restClient = createRestClient(nodeInfos, null, "http")) {
@@ -887,6 +894,9 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc
887894
assertThat(responseException.getResponse().getStatusLine().getStatusCode(), is(429));
888895
} finally {
889896
blockingLatch.countDown();
897+
if (lastTaskFuture != null) {
898+
lastTaskFuture.get();
899+
}
890900
}
891901
}
892902

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
@@ -107,7 +107,7 @@ public void createThreadPool() {
107107
threadPool = Mockito.spy(
108108
new TestThreadPool("api key service tests",
109109
new FixedExecutorBuilder(Settings.EMPTY, SECURITY_CRYPTO_THREAD_POOL_NAME, 1, 1000,
110-
"xpack.security.authc.api_key.thread_pool", false))
110+
"xpack.security.crypto.thread_pool", false))
111111
);
112112
}
113113

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)