Skip to content

Commit

Permalink
Using CoundDownLatch
Browse files Browse the repository at this point in the history
Signed-off-by: Dharmesh 💤 <sdharms@amazon.com>
  • Loading branch information
psychbot authored and Sachin Kale committed Aug 31, 2023
1 parent d97cc06 commit 2956dd1
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

Expand Down Expand Up @@ -86,23 +85,28 @@ public RemoteStoreService(Supplier<RepositoriesService> repositoriesService, Thr
public void verifyRepository(List<Repository> repositories, DiscoveryNode localNode) {
for (Repository repository : repositories) {
String verificationToken = repository.startVerification();
ExecutorService executor = threadPool.executor(ThreadPool.Names.GENERIC);
executor.execute(() -> {
String repositoryName = repository.getMetadata().name();
CountDownLatch repositoryVerificationLatch = new CountDownLatch(1);
threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> {
try {
repository.verify(verificationToken, localNode);
logger.info(() -> new ParameterizedMessage("successfully verified [{}] repository", repositoryName));
repositoryVerificationLatch.countDown();
} catch (Exception e) {
logger.warn(() -> new ParameterizedMessage("[{}] failed to verify repository", repository), e);
throw new RepositoryVerificationException(repository.getMetadata().name(), e.getMessage());
throw new RepositoryVerificationException(repositoryName, e.getMessage());
}
});

// TODO: See if using listener here which is async makes sense, made this sync as
// we need the repository registration for remote store node to be completed before the bootstrap
// completes.
// we need the repository registration for remote store node to be completed before the bootstrap
// completes.
try {
if(executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) {
throw new RepositoryVerificationException(repository.getMetadata().name(), "could not complete " +
"repository verification within timeout.");
if (repositoryVerificationLatch.await(1000, TimeUnit.MILLISECONDS) == false) {
throw new RepositoryVerificationException(
repository.getMetadata().name(),
"could not complete " + "repository verification within timeout."
);
}
} catch (InterruptedException e) {
throw new RepositoryVerificationException(repository.getMetadata().name(), e.getMessage());
Expand Down

0 comments on commit 2956dd1

Please sign in to comment.