Fix potential issue while install CSAR 77/42277/4
authorxg353y <xg353y@intl.att.com>
Wed, 11 Apr 2018 13:55:45 +0000 (15:55 +0200)
committerxg353y <xg353y@intl.att.com>
Thu, 12 Apr 2018 09:07:29 +0000 (11:07 +0200)
Add random waiting timer for treat notification, so that no 2 threads
will treate the notif at the same time; Add the retry mechanism for the
DCAE Inventory api call.

Change-Id: I9bd8a58001d638c589309a9d65e4df6a2e437209
Signed-off-by: xg353y <xg353y@intl.att.com>
Issue-ID: CLAMP-151

src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java
src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java
src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java
src/main/resources/application.properties

index ffc9b8e..f1cfd18 100644 (file)
@@ -61,6 +61,8 @@ public class DcaeInventoryServices {
     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();\r
     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
     public static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
+    public static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval";\r
+    public static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit";\r
     public static final String DCAE_TYPE_NAME = "typeName";\r
     public static final String DCAE_TYPE_ID = "typeId";\r
     @Autowired\r
@@ -78,7 +80,7 @@ public class DcaeInventoryServices {
      * @throws ParseException\r
      *             In case of DCAE Json parse exception\r
      */\r
-    public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException {\r
+    public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException, InterruptedException {\r
         String artifactName = cldsModel.getControlName();\r
         DcaeEvent dcaeEvent = new DcaeEvent();\r
         DcaeInventoryResponse dcaeResponse = null;\r
@@ -159,7 +161,7 @@ public class DcaeInventoryServices {
      *             In case of issues with the Json parsing\r
      */\r
     public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)\r
-            throws IOException, ParseException {\r
+            throws IOException, ParseException, InterruptedException {\r
         Date startTime = new Date();\r
         LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");\r
         String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName="\r
@@ -167,7 +169,8 @@ public class DcaeInventoryServices {
         String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString;\r
         logger.info("Dcae Inventory Service full url - " + fullUrl);\r
         String dcaeInventoryResponse = null;\r
-        String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
+\r
+        String responseStr = queryDCAEInventory (fullUrl);\r
         JSONParser parser = new JSONParser();\r
         Object obj0 = parser.parse(responseStr);\r
         JSONObject jsonObj = (JSONObject) obj0;\r
@@ -185,6 +188,34 @@ public class DcaeInventoryServices {
         return JacksonUtils.getObjectMapperInstance().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class);\r
     }\r
 \r
+    private String queryDCAEInventory (String fullUrl) throws IOException, InterruptedException {\r
+        int retryInterval = 0;\r
+        int retryLimit = 1;\r
+        if (refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT) != null) {\r
+            retryLimit = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT));\r
+        }\r
+        if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) {\r
+               retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL));\r
+        }\r
+\r
+        int i = 0;\r
+        while (i < retryLimit) {\r
+            i++;\r
+            try {\r
+                return DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
+            } catch (BadRequestException e) {\r
+                if (i == retryLimit) {\r
+                    // reach the retry limit, but still failed to connect to DCAE\r
+                    throw e;\r
+                } else {\r
+                    // wait for a while and try to connect to DCAE again\r
+                    Thread.sleep(retryInterval);\r
+                }\r
+            }\r
+        }\r
+        // normally it should not go to this branch. It should either return the DCAE query result, or throw exception\r
+        return null;\r
+    }\r
     /**\r
      * Inserts a new DCAEServiceType or updates an existing instance. If the\r
      * typeName is same second time(already exists) then the\r
index 627bc72..a44f867 100644 (file)
@@ -27,6 +27,7 @@ import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;\r
 \r
 import java.util.Date;\r
+import java.util.concurrent.ThreadLocalRandom;\r
 \r
 import org.onap.clamp.clds.config.ClampProperties;\r
 import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration;\r
@@ -200,6 +201,10 @@ public class SdcSingleController {
     public void treatNotification(INotificationData iNotif) {\r
         CsarHandler csar = null;\r
         try {\r
+            // wait for a random time, so that 2 running Clamp will not treat the same Notification at the same time \r
+            int i = ThreadLocalRandom.current().nextInt(1, 5);\r
+            Thread.sleep(i * 1000);\r
+\r
             logger.info("Notification received for service UUID:" + iNotif.getServiceUUID());\r
             this.changeControllerStatus(SdcSingleControllerStatus.BUSY);\r
             csar = new CsarHandler(iNotif, this.sdcConfig.getSdcControllerName(),\r
@@ -241,6 +246,8 @@ public class SdcSingleController {
             this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),\r
                     sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR,\r
                     e.getMessage(), System.currentTimeMillis());\r
+        } catch (InterruptedException e) {\r
+            logger.error("Interrupt exception caught during the notification processing", e);\r
         } catch (RuntimeException e) {\r
             logger.error("Unexpected exception caught during the notification processing", e);\r
         } finally {\r
index 91c0b6a..cb0da0a 100644 (file)
@@ -97,7 +97,7 @@ public class CsarInstallerImpl implements CsarInstaller {
             createFakeCldsModel(csar, createFakeCldsTemplate(csar, this.searchForRightMapping(csar)), serviceTypeId);
         } catch (IOException e) {
             throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
-        } catch (ParseException e) {
+        } catch (ParseException | InterruptedException e) {
             throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
         }
     }
@@ -151,7 +151,7 @@ public class CsarInstallerImpl implements CsarInstaller {
         return policyNameList.get(0);
     }
 
-    private String queryDcaeToGetServiceTypeId(CsarHandler csar) throws IOException, ParseException {
+    private String queryDcaeToGetServiceTypeId(CsarHandler csar) throws IOException, ParseException, InterruptedException {
         return dcaeInventoryService.getDcaeInformation(csar.getBlueprintArtifactName(),
                 csar.getBlueprintInvariantServiceUuid(), csar.getBlueprintInvariantResourceUuid()).getTypeId();
     }
index 5f6a0d6..646c577 100644 (file)
@@ -191,6 +191,8 @@ clamp.config.clds.service.cache.invalidate.after.seconds=120
 \r
 #DCAE Inventory Url Properties\r
 clamp.config.dcae.inventory.url=http://dcae.api.simpledemo.onap.org:8080\r
+clamp.config.dcae.intentory.retry.interval=10000\r
+clamp.config.dcae.intentory.retry.limit=3\r
 \r
 #DCAE Dispatcher Url Properties\r
 clamp.config.dcae.dispatcher.url=http://dcae.api.simpledemo.onap.org:8080\r