Skip to content

CME-206: performance #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package uk.gov.hmcts.reform.managecase.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.net.ConnectException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import javax.annotation.PreDestroy;
import javax.validation.ValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
Expand All @@ -25,31 +30,46 @@ public class NotifyService {

private final NotificationClient notificationClient;
private final ApplicationParams appParams;
private final Executor executor;

@Autowired
public NotifyService(ApplicationParams appParams,
NotificationClient notificationClient) {
this.notificationClient = notificationClient;
this.appParams = appParams;

int poolSize = Math.max(4, Runtime.getRuntime().availableProcessors() * 2);
this.executor = Executors.newFixedThreadPool(poolSize);
}

@PreDestroy
public void cleanup() {
if (executor instanceof ExecutorService) {
((ExecutorService) executor).shutdown();
}
}

public List<EmailNotificationRequestStatus> sendEmail(final List<EmailNotificationRequest> notificationRequests) {
if (notificationRequests == null || notificationRequests.isEmpty()) {
throw new ValidationException("At least one email notification request is required to send notification");
}

List<EmailNotificationRequestStatus> notificationStatuses = Lists.newArrayList();
notificationRequests.forEach(emailNotificationRequest -> {
try {
notificationStatuses.add(sendNotification(emailNotificationRequest));
} catch (Exception e) {
notificationStatuses.add(new EmailNotificationRequestFailure(emailNotificationRequest, e));
}
});
return notificationStatuses;
List<CompletableFuture<EmailNotificationRequestStatus>> futures = notificationRequests.stream()
.map(request -> CompletableFuture.supplyAsync(() -> {
try {
return sendNotification(request);
} catch (Exception e) {
return new EmailNotificationRequestFailure(request, e);
}
}, executor))
.collect(Collectors.toList());

return futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
}

@Retryable(value = {ConnectException.class}, backoff = @Backoff(delay = 1000, multiplier = 3))
@Retryable(value = {ConnectException.class}, maxAttempts = 3, backoff = @Backoff(delay = 500, multiplier = 2))
private EmailNotificationRequestSuccess sendNotification(EmailNotificationRequest request)
throws NotificationClientException {
validateRequest(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package uk.gov.hmcts.reform.managecase.service;

import com.google.common.collect.Lists;
import java.net.ConnectException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executor;
import javax.validation.ValidationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.util.ReflectionTestUtils;
import uk.gov.hmcts.reform.managecase.ApplicationParams;
import uk.gov.hmcts.reform.managecase.domain.notify.EmailNotificationRequest;
import uk.gov.hmcts.reform.managecase.domain.notify.EmailNotificationRequestFailure;
Expand All @@ -20,6 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
Expand Down Expand Up @@ -50,7 +56,7 @@ class NotifyServiceTest {

@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
this.notifyService = new NotifyService(appParams, notificationClient);
}

Expand Down Expand Up @@ -164,7 +170,6 @@ void shouldInvokeNotificationClientSendNotificationForMoreThanOneCaseId() throws
anyString(),
anyString(),
anyMap(),
anyString(),
anyString()
))
.willReturn(sendEmailResponse);
Expand Down Expand Up @@ -217,4 +222,103 @@ void shouldInvokeNotificationClientSendNotificationForMoreThanOneCaseIdAndMoreTh
anyString()
);
}

@Test
@DisplayName("should shutdown executor when cleanup is called")
void shouldShutdownExecutorWhenCleanupIsCalled() {

ExecutorService mockExecutor = mock(ExecutorService.class);

ReflectionTestUtils.setField(notifyService, "executor", mockExecutor);
notifyService.cleanup();

verify(mockExecutor).shutdown();
}

@Test
@DisplayName("should process notifications in parallel")
void shouldProcessNotificationsInParallel() throws NotificationClientException {

given(appParams.getEmailTemplateId()).willReturn(EMAIL_TEMPLATE_ID);
SendEmailResponse sendEmailResponse = mock(SendEmailResponse.class);
given(notificationClient.sendEmail(anyString(), anyString(), anyMap(), anyString()))
.willReturn(sendEmailResponse);

List<EmailNotificationRequest> requests = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
requests.add(new EmailNotificationRequest(CASE_ID + i, TEST_EMAIL));
}

long startTime = System.currentTimeMillis();
List<EmailNotificationRequestStatus> responses = notifyService.sendEmail(requests);
long endTime = System.currentTimeMillis();

System.out.println("Execution time for 10 parallel requests: " + (endTime - startTime) + "ms");

assertNotNull(responses, "response object should not be null");
assertEquals(10, responses.size(), "response size should be 10");

verify(notificationClient, times(10)).sendEmail(
anyString(),
anyString(),
anyMap(),
anyString()
);
}

@Test
@DisplayName("should use dynamic pool size based on available processors")
void shouldUseDynamicPoolSizeBasedOnAvailableProcessors() {
NotifyService service = new NotifyService(appParams, notificationClient);

int expectedPoolSize = Math.max(4, Runtime.getRuntime().availableProcessors() * 2);

Executor executor = (Executor) ReflectionTestUtils.getField(service, "executor");
assertNotNull(executor, "Executor should not be null");

System.out.println("Expected thread pool size: " + expectedPoolSize);
}

@Test
@DisplayName("should handle connection exception with proper failure response")
void shouldHandleConnectionExceptionWithRetry() throws NotificationClientException {
given(appParams.getEmailTemplateId()).willReturn(EMAIL_TEMPLATE_ID);

ArgumentCaptor<String> templateIdCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> emailCaptor = ArgumentCaptor.forClass(String.class);

// Note: In a unit test environment, Spring's @Retryable won't actually work
given(notificationClient.sendEmail(
templateIdCaptor.capture(),
emailCaptor.capture(),
anyMap(),
anyString()))
.willThrow(new NotificationClientException(new ConnectException("Connection refused")));

EmailNotificationRequest request = new EmailNotificationRequest(CASE_ID, TEST_EMAIL);

List<EmailNotificationRequestStatus> responses = notifyService.sendEmail(Lists.newArrayList(request));

assertNotNull(responses, NOT_NULL_MESSAGE);
assertEquals(1, responses.size(), SIZE_SHOULD_BE_EQUAL_TO_1);

assertTrue(responses.get(0) instanceof EmailNotificationRequestFailure,
"Should be a failure response when connection fails");

verify(notificationClient).sendEmail(
anyString(),
anyString(),
anyMap(),
anyString()
);

assertEquals(EMAIL_TEMPLATE_ID, templateIdCaptor.getValue());
assertEquals(TEST_EMAIL, emailCaptor.getValue());

EmailNotificationRequestFailure failure = (EmailNotificationRequestFailure) responses.get(0);
assertTrue(failure.getException() instanceof NotificationClientException,
"Exception should be a NotificationClientException");
assertTrue(failure.getException().getCause() instanceof ConnectException,
"Root cause should be a ConnectException");
}
}