diff options
author | Ittay Stern <ittay.stern@att.com> | 2019-11-06 14:27:53 +0200 |
---|---|---|
committer | Ittay Stern <ittay.stern@att.com> | 2019-11-07 14:19:47 +0000 |
commit | 97d42f7035ade2c171c349d01cfcd9b61b71dd52 (patch) | |
tree | 1a1b6d7c52545fcf78a0eb94eb12ec6d225c5d58 /vid-app-common/src/main | |
parent | ebef5249cb9d282ace976feac8517a7b44add450 (diff) |
fix - when retrieve topology we are using threadPool and the MDC values are not updated
This time for homing-by-vnf-id parallel requests.
Issue-ID: VID-253
Change-Id: Ic0be1470445c6fef0e18f9b9d8f1604df508265b
Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-app-common/src/main')
5 files changed, 62 insertions, 16 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java b/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java index 096dba39f..c50578e82 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java @@ -58,7 +58,6 @@ import org.onap.vid.properties.VidProperties; import org.onap.vid.scheduler.SchedulerService; import org.onap.vid.scheduler.SchedulerServiceImpl; import org.onap.vid.services.AAIServiceTree; -import org.onap.vid.services.AAITreeNodeBuilder; import org.onap.vid.services.AaiService; import org.onap.vid.services.AaiServiceImpl; import org.onap.vid.services.ChangeManagementService; @@ -101,8 +100,8 @@ public class WebConfig implements WebMvcConfigurer { @Bean public AaiService getAaiService(AaiClientInterface aaiClient, AaiResponseTranslator aaiResponseTranslator, - AAITreeNodeBuilder aaiTreeNode, AAIServiceTree aaiServiceTree, ExecutorService executorService) { - return new AaiServiceImpl(aaiClient, aaiResponseTranslator, aaiServiceTree, executorService); + AAIServiceTree aaiServiceTree, Logging logging, ExecutorService executorService) { + return new AaiServiceImpl(aaiClient, aaiResponseTranslator, aaiServiceTree, executorService, logging); } @Bean diff --git a/vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodeBuilder.java b/vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodeBuilder.java index c8434609e..209f37025 100644 --- a/vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodeBuilder.java +++ b/vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodeBuilder.java @@ -59,6 +59,7 @@ import org.onap.vid.model.aaiTree.FailureAAITreeNode; import org.onap.vid.model.aaiTree.NodeType; import org.onap.vid.mso.model.CloudConfiguration; import org.onap.vid.properties.VidProperties; +import org.onap.vid.utils.Logging; import org.onap.vid.utils.Streams; import org.onap.vid.utils.Tree; import org.onap.vid.utils.Unchecked; @@ -71,7 +72,8 @@ import org.springframework.stereotype.Component; public class AAITreeNodeBuilder { private static final String RESULTS = "results"; - private AaiClientInterface aaiClient; + private final AaiClientInterface aaiClient; + private final Logging logging; private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAITreeNodeBuilder.class); @@ -97,8 +99,9 @@ public class AAITreeNodeBuilder { } @Inject - public AAITreeNodeBuilder(AaiClientInterface aaiClient) { + public AAITreeNodeBuilder(AaiClientInterface aaiClient, Logging logging) { this.aaiClient = aaiClient; + this.logging = logging; } List<AAITreeNode> buildNode(NodeType nodeType, @@ -296,13 +299,7 @@ public class AAITreeNodeBuilder { } private <V> Callable<V> withCopyOfMDC(Callable<V> callable) { - //in order to be able to write the correct data while creating the node on a new thread - // save a copy of the current thread's context map, with keys and values of type String. - final Map<String, String> copyOfParentMDC = MDC.getCopyOfContextMap(); - return () -> { - MDC.setContextMap(copyOfParentMDC); - return callable.call(); - }; + return logging.withMDC(MDC.getCopyOfContextMap(), callable); } private List<AAITreeNode> getChildNode(ExecutorService threadPool, ConcurrentSkipListSet<AAITreeNode> nodesAccumulator, diff --git a/vid-app-common/src/main/java/org/onap/vid/services/AaiServiceImpl.java b/vid-app-common/src/main/java/org/onap/vid/services/AaiServiceImpl.java index b64a233c7..b3ac16884 100644 --- a/vid-app-common/src/main/java/org/onap/vid/services/AaiServiceImpl.java +++ b/vid-app-common/src/main/java/org/onap/vid/services/AaiServiceImpl.java @@ -87,7 +87,9 @@ import org.onap.vid.model.aaiTree.VpnBinding; import org.onap.vid.model.aaiTree.VpnBindingKt; import org.onap.vid.roles.RoleValidator; import org.onap.vid.utils.Intersection; +import org.onap.vid.utils.Logging; import org.onap.vid.utils.Tree; +import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; @@ -104,6 +106,7 @@ public class AaiServiceImpl implements AaiService { private AaiResponseTranslator aaiResponseTranslator; private AAIServiceTree aaiServiceTree; private ExecutorService executorService; + private final Logging logging; private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AaiServiceImpl.class); @@ -113,12 +116,13 @@ public class AaiServiceImpl implements AaiService { AaiClientInterface aaiClient, AaiResponseTranslator aaiResponseTranslator, AAIServiceTree aaiServiceTree, - ExecutorService executorService) + ExecutorService executorService, Logging logging) { this.aaiClient = aaiClient; this.aaiResponseTranslator = aaiResponseTranslator; this.aaiServiceTree = aaiServiceTree; this.executorService = executorService; + this.logging = logging; } private List<Service> convertModelToService(Model model) { @@ -523,10 +527,12 @@ public class AaiServiceImpl implements AaiService { .map(RelatedVnf::from) .collect(Collectors.toList()); + final Map<String, String> copyOfParentMDC = MDC.getCopyOfContextMap(); + try { return executorService.submit(() -> convertedVnfs.parallelStream() - .map(this::enrichRelatedVnfWithCloudRegionAndTenant) + .map(logging.withMDC(copyOfParentMDC, this::enrichRelatedVnfWithCloudRegionAndTenant)) .collect(Collectors.toList()) ).get(); } catch (Exception e) { diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java index 0d8e58878..43f059d54 100644 --- a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java @@ -33,8 +33,11 @@ import com.google.common.collect.ImmutableList; import io.joshworks.restclient.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.Map; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.function.Function; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Response; import org.apache.commons.io.IOUtils; @@ -42,6 +45,8 @@ import org.apache.commons.lang3.StringUtils; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.exceptions.GenericUncheckedException; +import org.onap.vid.utils.Unchecked.UncheckedThrowingSupplier; +import org.slf4j.MDC; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Service; import org.springframework.web.context.request.RequestContextHolder; @@ -197,5 +202,29 @@ public class Logging { } } + /** + * in order to be able to write the correct data while creating the node on a new thread save a copy of the current + * thread's context map, with keys and values of type String. + */ + public <T> Callable<T> withMDC(Map<String, String> copyOfParentMDC, Callable<T> callable) { + return () -> withMDCInternal(copyOfParentMDC, callable::call); + } + + /** + * in order to be able to write the correct data while creating the node on a new thread save a copy of the current + * thread's context map, with keys and values of type String. + */ + public <T, U> Function<T, U> withMDC(Map<String, String> copyOfParentMDC, Function<T, U> function) { + return t -> withMDCInternal(copyOfParentMDC, () -> function.apply(t)); + } + + <T> T withMDCInternal(Map<String, String> copyOfParentMDC, UncheckedThrowingSupplier<T> supplier) { + try { + MDC.setContextMap(copyOfParentMDC); + return supplier.get(); + } finally { + MDC.clear(); + } + } } diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java b/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java index 23127b61a..9fb15f690 100644 --- a/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java @@ -20,10 +20,11 @@ package org.onap.vid.utils; -import org.onap.vid.exceptions.GenericUncheckedException; - import java.net.URI; import java.net.URISyntaxException; +import java.util.function.Supplier; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.onap.vid.exceptions.GenericUncheckedException; public class Unchecked { private Unchecked() { @@ -39,5 +40,19 @@ public class Unchecked { } } + @FunctionalInterface + public interface UncheckedThrowingSupplier<T> extends Supplier<T> { + + @Override + default T get() { + try { + return getThrows(); + } catch (Exception e) { + return ExceptionUtils.rethrow(e); + } + } + + T getThrows() throws Exception; + } } |