code: private <T> CompletableFuture<T> retryWithCleanup(Supplier<CompletableFuture<T>> toTry) {
          CompletableFuture<T> result = new CompletableFuture<>();
          toTry.get()
               .thenAccept(result::complete)
               .exceptionally(ex -> {
                   // Check if the exception indicates the Metadata has reached capacity. In that case, force a cleanup
                   // and try again, exactly once.
                   try {
                       if (Exceptions.unwrap(ex) instanceof TooManyActiveSegmentsException) {
                           log.debug("{}: Forcing metadata cleanup due to capacity exceeded ({}).", this.traceObjectId,
                                   Exceptions.unwrap(ex).getMessage());
  
                           CompletableFuture<T> f = this.connector.getMetadataCleanup().get().thenComposeAsync(v -> toTry.get(), this.executor);
                           f.thenAccept(result::complete);
                           Futures.exceptionListener(f, result::completeExceptionally);
                       } else {
                           result.completeExceptionally(ex);
                       }
                   } catch (Throwable t) {
                       result.completeExceptionally(t);
                       throw t;
                   }
  
                   return null;
               });
  
          return result;
      }
docs: Retries Future from the given supplier exactly once if encountering TooManyActiveSegmentsException.
  
  @param toTry A Supplier that returns a Future to execute. This will be invoked either once or twice.
  @param <T>   Return type of Future.
  @return A CompletableFuture with the result, or failure cause.