3838
3939import java .nio .charset .StandardCharsets ;
4040import java .time .Duration ;
41- import java .util .ArrayList ;
4241import java .util .Arrays ;
4342import java .util .HashMap ;
4443import java .util .List ;
4544import java .util .Map ;
4645import java .util .concurrent .CountDownLatch ;
4746import java .util .concurrent .ExecutionException ;
48- import java .util .concurrent .atomic .AtomicBoolean ;
4947import java .util .concurrent .atomic .AtomicInteger ;
5048import java .util .concurrent .atomic .AtomicReference ;
5149import java .util .stream .Collectors ;
@@ -75,14 +73,12 @@ public class S3ClientsManagerTests extends ESTestCase {
7573 private S3Service s3Service ;
7674 private S3ClientsManager s3ClientsManager ;
7775 private final AtomicReference <CountDownLatch > clientRefsCloseLatchRef = new AtomicReference <>();
78- private final AtomicBoolean closeInternalInvoked = new AtomicBoolean (false );
7976
8077 @ Override
8178 public void setUp () throws Exception {
8279 super .setUp ();
8380 s3SecretsIdGenerators = ConcurrentCollections .newConcurrentMap ();
8481 clientRefsCloseLatchRef .set (null );
85- closeInternalInvoked .set (false );
8682 clientNames = IntStream .range (0 , between (2 , 5 )).mapToObj (i -> randomIdentifier () + "_" + i ).toList ();
8783
8884 final Settings .Builder builder = Settings .builder ();
@@ -111,28 +107,7 @@ public void setUp() throws Exception {
111107 TestProjectResolvers .allProjects (),
112108 mock (ResourceWatcherService .class ),
113109 () -> Region .of ("es-test-region" )
114- ) {
115- @ Override
116- protected AmazonS3Reference buildClientReference (S3ClientSettings clientSettings ) {
117- final var original = super .buildClientReference (clientSettings );
118- final var closeLatch = clientRefsCloseLatchRef .get ();
119- if (closeLatch == null ) {
120- return original ;
121- }
122-
123- original .decRef ();
124- final AmazonS3Reference proxy = new AmazonS3Reference (original .client (), DummySdkHttpClient .INSTANCE ) {
125- @ Override
126- protected void closeInternal () {
127- closeInternalInvoked .set (true );
128- safeAwait (closeLatch );
129- original .close ();
130- }
131- };
132- proxy .mustIncRef ();
133- return proxy ;
134- }
135- };
110+ );
136111 s3Service .refreshAndClearCache (S3ClientSettings .load (settings ));
137112 s3ClientsManager = s3Service .getS3PerProjectClientManager ();
138113 assertNotNull (s3ClientsManager );
@@ -145,8 +120,6 @@ public void tearDown() throws Exception {
145120 s3Service .close ();
146121 clusterService .close ();
147122 threadPool .close ();
148- final var clientsCloseListener = s3ClientsManager .getClientsCloseListener ();
149- assertTrue (clientsCloseListener == null || clientsCloseListener .isDone ());
150123 s3ClientsManager .getClientsHolders ().forEach ((projectId , clientsHolder ) -> assertTrue (clientsHolder .isClosed ()));
151124 }
152125
@@ -279,42 +252,6 @@ public void testClientsForMultipleProjects() throws InterruptedException {
279252 }
280253 }
281254
282- public void testWaitForAsyncClientClose () throws Exception {
283- final CountDownLatch closeLatch = new CountDownLatch (1 );
284- clientRefsCloseLatchRef .set (closeLatch );
285-
286- final List <ProjectId > projectIds = randomList (1 , 3 , ESTestCase ::randomUniqueProjectId );
287- final int iterations = between (3 , 8 );
288-
289- final List <AmazonS3Reference > clientRefs = new ArrayList <>();
290- for (int i = 0 ; i < iterations ; i ++) {
291- for (var projectId : projectIds ) {
292- final List <String > subsetOfClientNames = randomNonEmptySubsetOf (clientNames );
293- updateProjectInClusterState (projectId , newProjectClientsSecrets (projectId , subsetOfClientNames .toArray (String []::new )));
294- subsetOfClientNames .forEach (clientName -> {
295- final var newClient = s3ClientsManager .client (projectId , createRepositoryMetadata (clientName ));
296- clientRefs .add (newClient );
297- newClient .decRef ();
298- });
299- if (randomBoolean () && randomBoolean ()) {
300- removeProjectFromClusterState (projectId );
301- }
302- }
303- }
304-
305- final Thread thread = new Thread (() -> s3Service .close ());
306- thread .start ();
307-
308- assertBusy (() -> assertTrue (closeInternalInvoked .get ()));
309- Thread .sleep (between (0 , 100 ));
310- assertFalse (s3ClientsManager .getClientsCloseListener ().isDone ());
311-
312- closeLatch .countDown ();
313- assertTrue (thread .join (Duration .ofSeconds (10 )));
314- assertTrue (s3ClientsManager .getClientsCloseListener ().isDone ());
315- clientRefs .forEach (clientRef -> assertFalse (clientRef .hasReferences ()));
316- }
317-
318255 public void testClusterAndProjectClients () {
319256 final ProjectId projectId = randomUniqueProjectId ();
320257 final String clientName = randomFrom (clientNames );
0 commit comments