Skip to content

Commit 674e6dc

Browse files
committed
[CI] Fix testClientsLifeCycleForSingleProject
More robust test for closed clients holder. Also changes IllegalStateException to AlreadyClosedException for both closed manager and holder. Resolves: #128707
1 parent 16c63ff commit 674e6dc

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

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

Lines changed: 9 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.
@@ -347,6 +348,11 @@ void doClearCache() {}
347348
*/
348349
public final void close() {
349350
if (closed.compareAndSet(false, true)) {
351+
try {
352+
Thread.sleep(1000);
353+
} catch (InterruptedException e) {
354+
throw new RuntimeException(e);
355+
}
350356
clearCache();
351357
}
352358
}

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

Lines changed: 13 additions & 9 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;
@@ -203,12 +204,15 @@ public void testClientsLifeCycleForSingleProject() throws Exception {
203204
}
204205
assertClientNotFound(projectId, clientName);
205206

206-
assertBusy(() -> assertTrue(clientsHolder.isClosed()));
207-
final var e = expectThrows(
208-
IllegalStateException.class,
209-
() -> clientsHolder.client(createRepositoryMetadata(randomFrom(clientName, anotherClientName)))
210-
);
211-
assertThat(e.getMessage(), containsString("Project [" + projectId + "] clients holder is closed"));
207+
assertBusy(() -> {
208+
assertTrue(clientsHolder.isClosed());
209+
final var e = expectThrows(AlreadyClosedException.class, () -> {
210+
var client = clientsHolder.client(createRepositoryMetadata(randomFrom(clientName, anotherClientName)));
211+
client.decRef();
212+
});
213+
assertThat(e.getMessage(), containsString("Project [" + projectId + "] clients holder is closed"));
214+
});
215+
212216
}
213217

214218
public void testClientsForMultipleProjects() throws InterruptedException {
@@ -298,16 +302,16 @@ public void testClientsHolderAfterManagerClosed() {
298302
final ProjectId projectId = randomUniqueProjectId();
299303
final String clientName = randomFrom(clientNames);
300304

301-
s3Service.close();
305+
s3Service.close()
302306
assertTrue(s3ClientsManager.isManagerClosed());
303307
// New holder can be added after the manager is closed, but no actual client can be created
304308
updateProjectInClusterState(projectId, newProjectClientsSecrets(projectId, clientName));
305309
try (var clientsHolder = s3ClientsManager.getPerProjectClientsHolders().get(projectId)) {
306310
assertNotNull(clientsHolder);
307311
assertFalse(clientsHolder.isClosed());
308312

309-
final IllegalStateException e = expectThrows(
310-
IllegalStateException.class,
313+
final var e = expectThrows(
314+
AlreadyClosedException.class,
311315
() -> s3ClientsManager.client(projectId, createRepositoryMetadata(clientName))
312316
);
313317
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)