[Bug] Removing inner TaskExecutor to call DMI 91/137491/8
authormpriyank <priyank.maheshwari@est.tech>
Fri, 8 Mar 2024 12:50:53 +0000 (12:50 +0000)
committermpriyank <priyank.maheshwari@est.tech>
Thu, 14 Mar 2024 14:02:12 +0000 (14:02 +0000)
- Removing TaskExecutor from cps-ncmp-service package as anyways the
  call coming over from the cps-ncmp-rest layer is managed by a
  different thread executor(CpsNcmpTaskExecutor)
- Provided 60secs timeout value.
- Spawning new thread from a different executor might not be needed
- Removing @Async from a non spring managed method as its of no use

Issue-ID: CPS-2150
Change-Id: Ic99632983aff2c40df81421d782cf98ec600fc41
Signed-off-by: mpriyank <priyank.maheshwari@est.tech>
cps-application/src/main/resources/application.yml
cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreRequestHandler.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/executor/TaskExecutor.java [deleted file]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/data/operation/ResourceDataOperationRequestUtils.java
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy
docs/deployment.rst
docs/release-notes.rst

index 9f03fc2..d742483 100644 (file)
@@ -124,7 +124,7 @@ notification:
             queue-capacity: 500
             wait-for-tasks-to-complete-on-shutdown: true
             thread-name-prefix: Async-
-            time-out-value-in-ms: 2000
+            time-out-value-in-ms: 60000
 
 springdoc:
     swagger-ui:
index 8b08090..65410d3 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022-2023 Nordix Foundation
+ *  Copyright (C) 2022-2024 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ public abstract class NcmpDatastoreRequestHandler {
     private static final String NO_REQUEST_ID = null;
     private static final String NO_TOPIC = null;
 
-    @Value("${notification.async.executor.time-out-value-in-ms:2000}")
+    @Value("${notification.async.executor.time-out-value-in-ms:60000}")
     protected int timeOutInMilliSeconds;
 
     @Value("${notification.enabled:true}")
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/executor/TaskExecutor.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/executor/TaskExecutor.java
deleted file mode 100644 (file)
index 192062f..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- *  ============LICENSE_START=======================================================
- *  Copyright (C) 2023 Nordix Foundation
- *  ================================================================================
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  SPDX-License-Identifier: Apache-2.0
- *  ============LICENSE_END=========================================================
- */
-
-package org.onap.cps.ncmp.api.impl.executor;
-
-import static java.util.concurrent.TimeUnit.MILLISECONDS;
-
-import java.util.concurrent.CompletableFuture;
-import java.util.function.Supplier;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public class TaskExecutor {
-
-    /**
-     * Execute task asynchronously.
-     *
-     * @param taskSupplier    functional method is get() task need to executed asynchronously
-     * @param timeOutInMillis the timeout value in milliseconds
-     */
-    public static CompletableFuture<Object> executeTask(final Supplier<Object> taskSupplier,
-                                                        final long timeOutInMillis) {
-        return CompletableFuture.supplyAsync(taskSupplier::get)
-                .orTimeout(timeOutInMillis, MILLISECONDS);
-    }
-}
-
-
-
index a77e78a..2a4bcec 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2021-2023 Nordix Foundation
+ *  Copyright (C) 2021-2024 Nordix Foundation
  *  Modifications Copyright (C) 2022 Bell Canada
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -37,7 +37,6 @@ import org.onap.cps.ncmp.api.NcmpResponseStatus;
 import org.onap.cps.ncmp.api.impl.client.DmiRestClient;
 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration;
 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
-import org.onap.cps.ncmp.api.impl.executor.TaskExecutor;
 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder;
@@ -59,8 +58,6 @@ import org.springframework.web.util.UriComponentsBuilder;
 @Slf4j
 public class DmiDataOperations extends DmiOperations {
 
-    private static final long DEFAULT_ASYNC_TASK_EXECUTOR_TIMEOUT_IN_MILLISECONDS = 30000L;
-
     public DmiDataOperations(final InventoryPersistence inventoryPersistence,
                              final JsonObjectMapper jsonObjectMapper,
                              final NcmpConfiguration.DmiProperties dmiProperties,
@@ -259,11 +256,12 @@ public class DmiDataOperations extends DmiOperations {
                 .operations(dmiDataOperationRequestBodies).build();
         final String dmiDataOperationRequestAsJsonString =
                 jsonObjectMapper.asJsonString(dmiDataOperationRequest);
-        TaskExecutor.executeTask(() -> dmiRestClient.postOperationWithJsonData(dataOperationResourceUrl,
-                                dmiDataOperationRequestAsJsonString, READ, authorization),
-                        DEFAULT_ASYNC_TASK_EXECUTOR_TIMEOUT_IN_MILLISECONDS)
-                .whenCompleteAsync((response, throwable) -> handleTaskCompletionException(throwable,
-                        dataOperationResourceUrl, dmiDataOperationRequestBodies));
+        try {
+            dmiRestClient.postOperationWithJsonData(dataOperationResourceUrl, dmiDataOperationRequestAsJsonString, READ,
+                    authorization);
+        } catch (final Exception exception) {
+            handleTaskCompletionException(exception, dataOperationResourceUrl, dmiDataOperationRequestBodies);
+        }
     }
 
     private void handleTaskCompletionException(final Throwable throwable,
index f13c842..a8b4e28 100644 (file)
@@ -45,7 +45,6 @@ import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext;
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
 import org.onap.cps.ncmp.api.models.DataOperationDefinition;
 import org.onap.cps.ncmp.api.models.DataOperationRequest;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 
@@ -123,7 +122,6 @@ public class ResourceDataOperationRequestUtils {
      * @param requestId                                unique identifier per request
      * @param cmHandleIdsPerResponseCodesPerOperation list of cm handle ids per operation with response code
      */
-    @Async
     public static void publishErrorMessageToClientTopic(final String clientTopic,
                                                          final String requestId,
                                                          final MultiValueMap<DmiDataOperation,
index 7d8ac74..e154588 100644 (file)
 
 package org.onap.cps.ncmp.api.impl.operations
 
-import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
-import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL
-import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING
-import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
-import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
-import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE
-import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNABLE_TO_READ_RESOURCE_DATA
-import static org.onap.cps.ncmp.api.NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
-
 import com.fasterxml.jackson.databind.ObjectMapper
 import org.onap.cps.events.EventsPublisher
 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
@@ -43,13 +34,22 @@ import org.onap.cps.utils.JsonObjectMapper
 import org.spockframework.spring.SpringBean
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.boot.test.context.SpringBootTest
+import org.springframework.http.HttpStatus
 import org.springframework.http.ResponseEntity
 import org.springframework.test.context.ContextConfiguration
-import org.springframework.http.HttpStatus
 import spock.lang.Shared
-import spock.util.concurrent.PollingConditions
+
 import java.util.concurrent.TimeoutException
 
+import static org.onap.cps.ncmp.api.NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
+import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNABLE_TO_READ_RESOURCE_DATA
+import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
+import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL
+import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING
+import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
+import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
+import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE
+
 @SpringBootTest
 @ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, NcmpConfiguration.DmiProperties, DmiDataOperations])
 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
@@ -107,18 +107,10 @@ class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
             def expectedBatchRequestAsJson = '{"operations":[{"operation":"read","operationId":"operational-14","datastore":"ncmp-datastore:passthrough-operational","options":"some option","resourceIdentifier":"some resource identifier","cmHandles":[{"id":"some-cm-handle","cmHandleProperties":{"prop1":"val1"}}]}]}'
             mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ.operationName, NO_AUTH_HEADER) >> responseFromDmi
             dmiServiceUrlBuilder.getDataOperationRequestUrl(_, _) >> expectedDmiBatchResourceDataUrl
-        and: ' a flag to track the post operation call'
-            def postOperationWithJsonDataMethodCalled = false
-        and: 'the (mocked) dmi rest client will use the flag to indicate it is called and capture the request body'
-            mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, expectedBatchRequestAsJson, READ, null) >> {
-                postOperationWithJsonDataMethodCalled = true
-            }
         when: 'get resource data for group of cm handles are invoked'
             objectUnderTest.requestResourceDataFromDmi('my-topic-name', dataOperationRequest, 'requestId', NO_AUTH_HEADER)
-        then: 'validate the post operation was called and ncmp generated dmi request body json args'
-            new PollingConditions().within(1) {
-                assert postOperationWithJsonDataMethodCalled == true
-            }
+        then: 'the post operation was called and ncmp generated dmi request body json args'
+            1 * mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, expectedBatchRequestAsJson, READ, NO_AUTH_HEADER)
     }
 
     def 'Execute (async) data operation from DMI service for #scenario.'() {
index ae17c56..56485fb 100644 (file)
@@ -24,8 +24,6 @@ package org.onap.cps.ncmp.api.impl.operations
 import com.fasterxml.jackson.core.JsonProcessingException
 import com.fasterxml.jackson.databind.ObjectMapper
 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
-import org.onap.cps.ncmp.api.impl.executor.TaskExecutor
-import org.onap.cps.ncmp.api.impl.utils.DmiServiceNameOrganizer
 import org.onap.cps.spi.model.ModuleReference
 import org.onap.cps.utils.JsonObjectMapper
 import org.spockframework.spring.SpringBean
@@ -36,7 +34,7 @@ import org.springframework.http.ResponseEntity
 import org.springframework.test.context.ContextConfiguration
 import spock.lang.Shared
 
-import static  org.onap.cps.ncmp.api.impl.operations.OperationType.READ
+import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
 
 @SpringBootTest
 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiModelOperations])
index c1ee1eb..de276ce 100644 (file)
@@ -255,6 +255,10 @@ Any spring supported property can be configured by providing in ``config.additio
 | notification.async.executor.              |                                                                                                         |                               |
 | thread-name-prefix                        |                                                                                                         |                               |
 +-------------------------------------------+---------------------------------------------------------------------------------------------------------+-------------------------------+
+| config.additional.                        | Maximum time allowed by the thread pool executor for execution of one of the threads in milliseconds.   | ``60000``                     |
+| notification.async.executor.              |                                                                                                         |                               |
+| time-out-value-in-ms                      |                                                                                                         |                               |
++-------------------------------------------+---------------------------------------------------------------------------------------------------------+-------------------------------+
 | config.additional.                        | Specifies number of database connections between database and application.                              | ``10``                        |
 | spring.datasource.hikari.                 | This property controls the maximum size that the pool is allowed to reach,                              |                               |
 | maximumPoolSize                           | including both idle and in-use connections.                                                             |                               |
index e79a188..2202d11 100644 (file)
@@ -39,6 +39,7 @@ Release Data
 Bug Fixes
 ---------
 3.4.7
+    - `CPS-2150 <https://jira.onap.org/browse/CPS-2150>`_ Fix for Async task execution failed by TimeoutException.
 
 Features
 --------