From f98cb081a4a1819b9f372c6994e5611ed3b4e5cd Mon Sep 17 00:00:00 2001 From: Jacopo Carlini Date: Fri, 6 Dec 2024 12:39:38 +0100 Subject: [PATCH] retry --- .../bizpmingestion/config/LoggingAspect.java | 165 ------------------ .../entity/pm/PPTransaction.java | 2 +- .../service/impl/MapperComponent.java | 5 + .../service/impl/SliceService.java | 28 +-- 4 files changed, 23 insertions(+), 177 deletions(-) delete mode 100644 src/main/java/it/gov/pagopa/bizpmingestion/config/LoggingAspect.java diff --git a/src/main/java/it/gov/pagopa/bizpmingestion/config/LoggingAspect.java b/src/main/java/it/gov/pagopa/bizpmingestion/config/LoggingAspect.java deleted file mode 100644 index f0fa839..0000000 --- a/src/main/java/it/gov/pagopa/bizpmingestion/config/LoggingAspect.java +++ /dev/null @@ -1,165 +0,0 @@ -package it.gov.pagopa.bizpmingestion.config; - -import static it.gov.pagopa.bizpmingestion.util.CommonUtility.deNull; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import javax.annotation.PostConstruct; - -import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.AfterReturning; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.aspectj.lang.reflect.CodeSignature; -import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; - -import it.gov.pagopa.bizpmingestion.exception.AppError; -import it.gov.pagopa.bizpmingestion.model.ProblemJson; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import lombok.extern.slf4j.Slf4j; - - - -@Aspect -@Component -@Slf4j -public class LoggingAspect { - - public static final String START_TIME = "startTime"; - public static final String METHOD = "method"; - public static final String STATUS = "status"; - public static final String CODE = "httpCode"; - public static final String RESPONSE_TIME = "responseTime"; - public static final String FAULT_CODE = "faultCode"; - public static final String FAULT_DETAIL = "faultDetail"; - public static final String REQUEST_ID = "requestId"; - public static final String OPERATION_ID = "operationId"; - public static final String ARGS = "args"; - - final HttpServletRequest httRequest; - - final HttpServletResponse httpResponse; - - @Value("${info.application.name}") - private String name; - - @Value("${info.application.version}") - private String version; - - @Value("${info.properties.environment}") - private String environment; - - public LoggingAspect(HttpServletRequest httRequest, HttpServletResponse httpResponse) { - this.httRequest = httRequest; - this.httpResponse = httpResponse; - } - - private static String getDetail(ResponseEntity result) { - if (result != null && result.getBody() != null && result.getBody().getDetail() != null) { - return result.getBody().getDetail(); - } else return AppError.UNKNOWN.getDetails(); - } - - private static String getTitle(ResponseEntity result) { - if (result != null && result.getBody() != null && result.getBody().getTitle() != null) { - return result.getBody().getTitle(); - } else return AppError.UNKNOWN.getTitle(); - } - - public static String getExecutionTime() { - String startTime = MDC.get(START_TIME); - if (startTime != null) { - long endTime = System.currentTimeMillis(); - long executionTime = endTime - Long.parseLong(startTime); - return String.valueOf(executionTime); - } - return "-"; - } - - private static Map getParams(ProceedingJoinPoint joinPoint) { - CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature(); - Map params = new HashMap<>(); - int i = 0; - for (var paramName : codeSignature.getParameterNames()) { - params.put(paramName, deNull(joinPoint.getArgs()[i++])); - } - return params; - } - - @Pointcut("@within(org.springframework.web.bind.annotation.RestController)") - public void restController() { - // all rest controllers - } - - @Pointcut("@within(org.springframework.stereotype.Repository)") - public void repository() { - // all repository methods - } - - @Pointcut("@within(org.springframework.stereotype.Service)") - public void service() { - // all service methods - } - - /** Log essential info of application during the startup. */ - @PostConstruct - public void logStartup() { - log.info("-> Starting {} version {} - environment {}", name, version, environment); - } - - @Around(value = "restController()") - public Object logApiInvocation(ProceedingJoinPoint joinPoint) throws Throwable { - MDC.put(METHOD, joinPoint.getSignature().getName()); - MDC.put(START_TIME, String.valueOf(System.currentTimeMillis())); - MDC.put(OPERATION_ID, UUID.randomUUID().toString()); - if (MDC.get(REQUEST_ID) == null) { - var requestId = UUID.randomUUID().toString(); - MDC.put(REQUEST_ID, requestId); - } - Map params = getParams(joinPoint); - MDC.put(ARGS, params.toString()); - - log.info("Invoking API operation {} - args: {}", joinPoint.getSignature().getName(), params); - - Object result = joinPoint.proceed(); - - MDC.put(STATUS, "OK"); - MDC.put(CODE, String.valueOf(httpResponse.getStatus())); - MDC.put(RESPONSE_TIME, getExecutionTime()); - log.info( - "Successful API operation {} - result: {}", joinPoint.getSignature().getName(), result); - MDC.remove(STATUS); - MDC.remove(CODE); - MDC.remove(RESPONSE_TIME); - MDC.remove(START_TIME); - return result; - } - - @AfterReturning(value = "execution(* *..exception.ErrorHandler.*(..))", returning = "result") - public void trowingApiInvocation(JoinPoint joinPoint, ResponseEntity result) { - MDC.put(STATUS, "KO"); - MDC.put(CODE, String.valueOf(result.getStatusCode().value())); - MDC.put(RESPONSE_TIME, getExecutionTime()); - MDC.put(FAULT_CODE, getTitle(result)); - MDC.put(FAULT_DETAIL, getDetail(result)); - log.info("Failed API operation {} - error: {}", MDC.get(METHOD), result); - MDC.clear(); - } - - @Around(value = "repository() || service()") - public Object logTrace(ProceedingJoinPoint joinPoint) throws Throwable { - Map params = getParams(joinPoint); - log.debug("Call method {} - args: {}", joinPoint.getSignature().toShortString(), params); - Object result = joinPoint.proceed(); - log.debug("Return method {} - result: {}", joinPoint.getSignature().toShortString(), result); - return result; - } -} diff --git a/src/main/java/it/gov/pagopa/bizpmingestion/entity/pm/PPTransaction.java b/src/main/java/it/gov/pagopa/bizpmingestion/entity/pm/PPTransaction.java index 272d912..cbe446e 100644 --- a/src/main/java/it/gov/pagopa/bizpmingestion/entity/pm/PPTransaction.java +++ b/src/main/java/it/gov/pagopa/bizpmingestion/entity/pm/PPTransaction.java @@ -25,7 +25,7 @@ name = "ppWallet-subgraph", attributeNodes = { @NamedAttributeNode("ppCreditCard"), -// @NamedAttributeNode("ppPsp"), + @NamedAttributeNode("ppPsp"), // @NamedAttributeNode("ppPayPal"), } ) diff --git a/src/main/java/it/gov/pagopa/bizpmingestion/service/impl/MapperComponent.java b/src/main/java/it/gov/pagopa/bizpmingestion/service/impl/MapperComponent.java index 57fc962..0fcb46b 100644 --- a/src/main/java/it/gov/pagopa/bizpmingestion/service/impl/MapperComponent.java +++ b/src/main/java/it/gov/pagopa/bizpmingestion/service/impl/MapperComponent.java @@ -1,5 +1,6 @@ package it.gov.pagopa.bizpmingestion.service.impl; +import com.microsoft.azure.functions.annotation.ExponentialBackoffRetry; import it.gov.pagopa.bizpmingestion.entity.pm.*; import it.gov.pagopa.bizpmingestion.enumeration.PaymentMethodType; import it.gov.pagopa.bizpmingestion.model.pm.PMEvent; @@ -18,6 +19,10 @@ @Component public class MapperComponent { + @ExponentialBackoffRetry( + maxRetryCount = 3, + maximumInterval = "00:00:30", + minimumInterval = "00:00:05") public PMEvent convert(PPTransaction ppTransaction) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); diff --git a/src/main/java/it/gov/pagopa/bizpmingestion/service/impl/SliceService.java b/src/main/java/it/gov/pagopa/bizpmingestion/service/impl/SliceService.java index 30d1903..4ac7c68 100644 --- a/src/main/java/it/gov/pagopa/bizpmingestion/service/impl/SliceService.java +++ b/src/main/java/it/gov/pagopa/bizpmingestion/service/impl/SliceService.java @@ -3,7 +3,6 @@ import com.azure.spring.data.cosmos.repository.CosmosRepository; import com.microsoft.azure.functions.annotation.ExponentialBackoffRetry; import it.gov.pagopa.bizpmingestion.entity.cosmos.execution.BizEventsPMIngestionExecution; -import it.gov.pagopa.bizpmingestion.entity.cosmos.execution.SkippedTransaction; import it.gov.pagopa.bizpmingestion.entity.pm.PPTransaction; import it.gov.pagopa.bizpmingestion.model.WrapperObject; import it.gov.pagopa.bizpmingestion.model.pm.PMEvent; @@ -11,7 +10,6 @@ import it.gov.pagopa.bizpmingestion.model.pm.PMEventToViewResult; import it.gov.pagopa.bizpmingestion.repository.*; import it.gov.pagopa.bizpmingestion.service.IPMEventToViewService; -import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; @@ -64,7 +62,7 @@ public BizEventsPMIngestionExecution computeSlice( int sliceNumber) { try { pmIngestionExec.setId(UUID.randomUUID().toString()); - List ppTrList = ppTransactionRepository.findAll(spec); + List ppTrList = getPpTransactions(spec); List pmEventList = ppTrList.stream().map(elem -> mapperComponent.convert(elem)).toList(); @@ -90,11 +88,19 @@ public BizEventsPMIngestionExecution computeSlice( pmIngestionExec.getStatus(), pmIngestionExec.getNumRecordIngested(), pmIngestionExec.getNumRecordFound(), - LocalDate.parse(pmIngestionExec.getDateFrom())); + LocalDateTime.parse(pmIngestionExec.getDateFrom()).toLocalDate()); } return pmIngestionExec; } + @ExponentialBackoffRetry( + maxRetryCount = 3, + maximumInterval = "00:00:30", + minimumInterval = "00:00:05") + private List getPpTransactions(Specification spec) { + return ppTransactionRepository.findAll(spec); + } + private void handleEventList( List pmEventList, BizEventsPMIngestionExecution pmIngestionExec, int sliceNumber) { pmIngestionExec.setStatus("ALL_COMPLETED"); @@ -171,13 +177,13 @@ private void handleEventList( @ExponentialBackoffRetry( maxRetryCount = 3, maximumInterval = "00:00:30", - minimumInterval = "00:00:5") + minimumInterval = "00:00:05") private void bulkSave(List generals, CosmosRepository repository) { - int bulkSize = 500; - for (int i = 0; i < generals.size(); i += bulkSize) { - int endIndex = Math.min(i + bulkSize, generals.size()); - List bulk = generals.subList(i, endIndex); - repository.saveAll(bulk); - } + int bulkSize = 500; + for (int i = 0; i < generals.size(); i += bulkSize) { + int endIndex = Math.min(i + bulkSize, generals.size()); + List bulk = generals.subList(i, endIndex); + repository.saveAll(bulk); + } } }