From 4fd883129d1c0fa6da1854d9a1136d501138291f Mon Sep 17 00:00:00 2001 From: xg353y Date: Wed, 11 Apr 2018 15:55:45 +0200 Subject: Fix potential issue while install CSAR 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 Issue-ID: CLAMP-151 --- .../clamp/clds/client/DcaeInventoryServices.java | 37 ++++++++++++++++++++-- .../clds/sdc/controller/SdcSingleController.java | 7 ++++ .../controller/installer/CsarInstallerImpl.java | 4 +-- 3 files changed, 43 insertions(+), 5 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java index ffc9b8e28..f1cfd18f3 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -61,6 +61,8 @@ public class DcaeInventoryServices { protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); public static final String DCAE_INVENTORY_URL = "dcae.inventory.url"; + public static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval"; + public static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit"; public static final String DCAE_TYPE_NAME = "typeName"; public static final String DCAE_TYPE_ID = "typeId"; @Autowired @@ -78,7 +80,7 @@ public class DcaeInventoryServices { * @throws ParseException * In case of DCAE Json parse exception */ - public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException { + public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException, InterruptedException { String artifactName = cldsModel.getControlName(); DcaeEvent dcaeEvent = new DcaeEvent(); DcaeInventoryResponse dcaeResponse = null; @@ -159,7 +161,7 @@ public class DcaeInventoryServices { * In case of issues with the Json parsing */ public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid) - throws IOException, ParseException { + throws IOException, ParseException, InterruptedException { Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "getDcaeInformation"); String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName=" @@ -167,7 +169,8 @@ public class DcaeInventoryServices { String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString; logger.info("Dcae Inventory Service full url - " + fullUrl); String dcaeInventoryResponse = null; - String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null); + + String responseStr = queryDCAEInventory (fullUrl); JSONParser parser = new JSONParser(); Object obj0 = parser.parse(responseStr); JSONObject jsonObj = (JSONObject) obj0; @@ -185,6 +188,34 @@ public class DcaeInventoryServices { return JacksonUtils.getObjectMapperInstance().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class); } + private String queryDCAEInventory (String fullUrl) throws IOException, InterruptedException { + int retryInterval = 0; + int retryLimit = 1; + if (refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT) != null) { + retryLimit = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT)); + } + if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) { + retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL)); + } + + int i = 0; + while (i < retryLimit) { + i++; + try { + return DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null); + } catch (BadRequestException e) { + if (i == retryLimit) { + // reach the retry limit, but still failed to connect to DCAE + throw e; + } else { + // wait for a while and try to connect to DCAE again + Thread.sleep(retryInterval); + } + } + } + // normally it should not go to this branch. It should either return the DCAE query result, or throw exception + return null; + } /** * Inserts a new DCAEServiceType or updates an existing instance. If the * typeName is same second time(already exists) then the diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java b/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java index 627bc72ad..a44f86771 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java @@ -27,6 +27,7 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import java.util.Date; +import java.util.concurrent.ThreadLocalRandom; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration; @@ -200,6 +201,10 @@ public class SdcSingleController { public void treatNotification(INotificationData iNotif) { CsarHandler csar = null; try { + // wait for a random time, so that 2 running Clamp will not treat the same Notification at the same time + int i = ThreadLocalRandom.current().nextInt(1, 5); + Thread.sleep(i * 1000); + logger.info("Notification received for service UUID:" + iNotif.getServiceUUID()); this.changeControllerStatus(SdcSingleControllerStatus.BUSY); csar = new CsarHandler(iNotif, this.sdcConfig.getSdcControllerName(), @@ -241,6 +246,8 @@ public class SdcSingleController { this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(), sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR, e.getMessage(), System.currentTimeMillis()); + } catch (InterruptedException e) { + logger.error("Interrupt exception caught during the notification processing", e); } catch (RuntimeException e) { logger.error("Unexpected exception caught during the notification processing", e); } finally { diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java index 91c0b6a64..cb0da0aa0 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java @@ -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(); } -- cgit 1.2.3-korg