From ca19d1864d7c39982e0fcf991840bd52c8fd6ddb Mon Sep 17 00:00:00 2001 From: mpriyank Date: Thu, 18 Aug 2022 15:20:09 +0100 Subject: Performance Improvement: Temporal event - Call the admin service in the async pool thread to fetch the anchor details in case of raising event on save, update and delete a node or delete list elements - Admin service is still called in the main thread in case of DeleteNodes method as we require to have anchor as we are deleting all the anchors. - Fixed the test scenarios related to processing async event - Rearranged method formal params Issue-ID: CPS-1126 Issue-ID: CPS-1209 Change-Id: Ibd8f001c4d7c7de63914f6de24042aaf01a063b2 Signed-off-by: mpriyank --- .../org/onap/cps/api/impl/CpsDataServiceImpl.java | 37 ++++++++-------------- .../onap/cps/notification/NotificationService.java | 21 +++++++----- 2 files changed, 26 insertions(+), 32 deletions(-) (limited to 'cps-service/src/main') diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java index 0772a8c9f..7bdc2c166 100755 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java @@ -65,7 +65,7 @@ public class CpsDataServiceImpl implements CpsDataService { CpsValidator.validateNameCharacters(dataspaceName, anchorName); final DataNode dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData); cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, CREATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, ROOT_NODE_XPATH, CREATE, observedTimestamp); } @Override @@ -74,7 +74,7 @@ public class CpsDataServiceImpl implements CpsDataService { CpsValidator.validateNameCharacters(dataspaceName, anchorName); final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, CREATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, CREATE, observedTimestamp); } @Override @@ -85,7 +85,7 @@ public class CpsDataServiceImpl implements CpsDataService { buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath, listElementDataNodeCollection); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -102,7 +102,7 @@ public class CpsDataServiceImpl implements CpsDataService { final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves()); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -117,7 +117,7 @@ public class CpsDataServiceImpl implements CpsDataService { for (final DataNode dataNodeUpdate : dataNodeUpdates) { processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate); } - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -147,7 +147,7 @@ public class CpsDataServiceImpl implements CpsDataService { CpsValidator.validateNameCharacters(dataspaceName, anchorName); final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -164,7 +164,7 @@ public class CpsDataServiceImpl implements CpsDataService { final Collection dataNodes, final OffsetDateTime observedTimestamp) { CpsValidator.validateNameCharacters(dataspaceName, anchorName); cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, dataNodes); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE); + processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp); } @Override @@ -172,16 +172,15 @@ public class CpsDataServiceImpl implements CpsDataService { final OffsetDateTime observedTimestamp) { CpsValidator.validateNameCharacters(dataspaceName, anchorName); cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, DELETE); + processDataUpdatedEventAsync(dataspaceName, anchorName, dataNodeXpath, DELETE, observedTimestamp); } @Override public void deleteDataNodes(final String dataspaceName, final String anchorName, final OffsetDateTime observedTimestamp) { CpsValidator.validateNameCharacters(dataspaceName, anchorName); - final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + processDataUpdatedEventAsync(dataspaceName, anchorName, ROOT_NODE_XPATH, DELETE, observedTimestamp); cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName); - processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, DELETE, observedTimestamp); } @Override @@ -189,7 +188,7 @@ public class CpsDataServiceImpl implements CpsDataService { final OffsetDateTime observedTimestamp) { CpsValidator.validateNameCharacters(dataspaceName, anchorName); cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath); - processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, DELETE); + processDataUpdatedEventAsync(dataspaceName, anchorName, listNodeXpath, DELETE, observedTimestamp); } private DataNode buildDataNode(final String dataspaceName, final String anchorName, @@ -230,20 +229,10 @@ public class CpsDataServiceImpl implements CpsDataService { } - private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName, - final OffsetDateTime observedTimestamp, final String xpath, - final Operation operation) { - final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); - this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp); - } - - private void processDataUpdatedEventAsync(final Anchor anchor, - final String xpath, - final Operation operation, - final OffsetDateTime observedTimestamp) { + private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName, final String xpath, + final Operation operation, final OffsetDateTime observedTimestamp) { try { - notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, - operation); + notificationService.processDataUpdatedEvent(dataspaceName, anchorName, xpath, operation, observedTimestamp); } catch (final Exception exception) { //If async message can't be queued for notification service, the initial request should not failed. log.error("Failed to send message to notification service", exception); diff --git a/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java index 30bb85142..7da3a6123 100644 --- a/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java +++ b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java @@ -32,6 +32,7 @@ import java.util.stream.Collectors; import javax.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsAdminService; import org.onap.cps.spi.model.Anchor; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -45,6 +46,7 @@ public class NotificationService { private final NotificationPublisher notificationPublisher; private final CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory; private final NotificationErrorHandler notificationErrorHandler; + private final CpsAdminService cpsAdminService; private List dataspacePatterns; @PostConstruct @@ -68,21 +70,24 @@ public class NotificationService { /** * Process Data Updated Event and publishes the notification. * - * @param anchor anchor - * @param observedTimestamp observedTimestamp + * @param dataspaceName dataspaceName + * @param anchorName anchorName * @param xpath xpath of changed data node * @param operation operation + * @param observedTimestamp observedTimestamp * @return future */ @Async("notificationExecutor") - public Future processDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp, - final String xpath, final Operation operation) { + public Future processDataUpdatedEvent(final String dataspaceName, final String anchorName, + final String xpath, final Operation operation, final OffsetDateTime observedTimestamp) { + + final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); log.debug("process data updated event for anchor '{}'", anchor); try { - if (shouldSendNotification(anchor.getDataspaceName())) { + if (shouldSendNotification(dataspaceName)) { final var cpsDataUpdatedEvent = - cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, - observedTimestamp, getRootNodeOperation(xpath, operation)); + cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, + observedTimestamp, getRootNodeOperation(xpath, operation)); log.debug("data updated event to be published {}", cpsDataUpdatedEvent); notificationPublisher.sendNotification(cpsDataUpdatedEvent); } @@ -91,7 +96,7 @@ public class NotificationService { CPS operation should not fail if sending event fails for any reason. */ notificationErrorHandler.onException("Failed to process cps-data-updated-event.", - exception, anchor, xpath, operation); + exception, anchor, xpath, operation); } return CompletableFuture.completedFuture(null); } -- cgit 1.2.3-korg