Skip to content

Commit b286748

Browse files
authored
[CI] Fix testClientsLifeCycleForSingleProject (#128718)
More robust test for closed clients holder. Also changes IllegalStateException to AlreadyClosedException for both closed manager and holder. Resolves: #128707
1 parent df16eee commit b286748

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3ClientsManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.repositories.s3;
1111

12+
import org.apache.lucene.store.AlreadyClosedException;
1213
import org.elasticsearch.cluster.ClusterChangedEvent;
1314
import org.elasticsearch.cluster.ClusterStateApplier;
1415
import org.elasticsearch.cluster.metadata.ProjectId;
@@ -293,7 +294,7 @@ S3ClientSettings singleClientSettings(RepositoryMetadata repositoryMetadata) {
293294
* @param repositoryMetadata The metadata of the repository for which the Amazon S3 client is required.
294295
* @return An {@link AmazonS3Reference} instance corresponding to the repository metadata.
295296
* @throws IllegalArgumentException If no client settings exist for the given repository metadata.
296-
* @throws IllegalStateException If the client manager is closed and a new client cannot be created.
297+
* @throws AlreadyClosedException If either the clients manager or the holder is closed
297298
*/
298299
final AmazonS3Reference client(RepositoryMetadata repositoryMetadata) {
299300
final var clientKey = clientKey(repositoryMetadata);
@@ -313,12 +314,12 @@ final AmazonS3Reference client(RepositoryMetadata repositoryMetadata) {
313314
}
314315
if (closed.get()) {
315316
// Not adding a new client once the clients holder is closed since there won't be anything to close it
316-
throw new IllegalStateException("Project [" + projectId() + "] clients holder is closed");
317+
throw new AlreadyClosedException("Project [" + projectId() + "] clients holder is closed");
317318
}
318319
if (managerClosed.get()) {
319320
// This clients holder must be added after the manager is closed. It must have no cached clients.
320321
assert clientsCache.isEmpty() : "expect empty cache, but got " + clientsCache;
321-
throw new IllegalStateException("s3 clients manager is closed");
322+
throw new AlreadyClosedException("s3 clients manager is closed");
322323
}
323324
// The close() method maybe called after we checked it, it is ok since we are already inside the synchronized block.
324325
// The close method calls clearCache() which will clear the newly added client.

modules/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ClientsManagerTests.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
1515
import software.amazon.awssdk.regions.Region;
1616

17+
import org.apache.lucene.store.AlreadyClosedException;
1718
import org.elasticsearch.cluster.ClusterState;
1819
import org.elasticsearch.cluster.metadata.Metadata;
1920
import org.elasticsearch.cluster.metadata.ProjectId;
@@ -41,6 +42,7 @@
4142
import java.util.Map;
4243
import java.util.concurrent.ExecutionException;
4344
import java.util.concurrent.atomic.AtomicInteger;
45+
import java.util.concurrent.atomic.AtomicReference;
4446
import java.util.stream.Collectors;
4547
import java.util.stream.IntStream;
4648

@@ -50,6 +52,7 @@
5052
import static org.hamcrest.Matchers.containsString;
5153
import static org.hamcrest.Matchers.equalTo;
5254
import static org.hamcrest.Matchers.hasKey;
55+
import static org.hamcrest.Matchers.instanceOf;
5356
import static org.hamcrest.Matchers.not;
5457
import static org.hamcrest.Matchers.sameInstance;
5558
import static org.mockito.ArgumentMatchers.any;
@@ -203,11 +206,18 @@ public void testClientsLifeCycleForSingleProject() throws Exception {
203206
}
204207
assertClientNotFound(projectId, clientName);
205208

206-
assertBusy(() -> assertTrue(clientsHolder.isClosed()));
207-
final var e = expectThrows(
208-
IllegalStateException.class,
209-
() -> clientsHolder.client(createRepositoryMetadata(randomFrom(clientName, anotherClientName)))
210-
);
209+
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
210+
assertBusy(() -> {
211+
assertTrue(clientsHolder.isClosed());
212+
try (var client = clientsHolder.client(createRepositoryMetadata(randomFrom(clientName, anotherClientName)))) {
213+
fail("client should be closed"); // the cache is still being cleared out
214+
} catch (Exception e) {
215+
exceptionRef.compareAndSet(null, e); // the first exception must be expected and is checked below
216+
}
217+
});
218+
219+
final var e = exceptionRef.get();
220+
assertThat(e, instanceOf(AlreadyClosedException.class));
211221
assertThat(e.getMessage(), containsString("Project [" + projectId + "] clients holder is closed"));
212222
}
213223

@@ -306,8 +316,8 @@ public void testClientsHolderAfterManagerClosed() {
306316
assertNotNull(clientsHolder);
307317
assertFalse(clientsHolder.isClosed());
308318

309-
final IllegalStateException e = expectThrows(
310-
IllegalStateException.class,
319+
final var e = expectThrows(
320+
AlreadyClosedException.class,
311321
() -> s3ClientsManager.client(projectId, createRepositoryMetadata(clientName))
312322
);
313323
assertThat(e.getMessage(), containsString("s3 clients manager is closed"));

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,6 @@ tests:
504504
- class: org.elasticsearch.xpack.esql.expression.function.scalar.string.RLikeTests
505505
method: testEvaluateInManyThreads {TestCase=100 random code points matches self case insensitive with keyword}
506506
issue: https://github.com/elastic/elasticsearch/issues/128706
507-
- class: org.elasticsearch.repositories.s3.S3ClientsManagerTests
508-
method: testClientsLifeCycleForSingleProject
509-
issue: https://github.com/elastic/elasticsearch/issues/128707
510507
- class: org.elasticsearch.xpack.esql.expression.function.scalar.string.RLikeTests
511508
method: testCrankyEvaluateBlockWithNulls {TestCase=100 random code points matches self case insensitive with text}
512509
issue: https://github.com/elastic/elasticsearch/issues/128710

0 commit comments

Comments
 (0)