Support concurrent requests to create schema sets 80/121980/10
authorBruno Sakoto <bruno.sakoto@bell.ca>
Wed, 16 Jun 2021 15:47:54 +0000 (11:47 -0400)
committerBruno Sakoto <bruno.sakoto@bell.ca>
Fri, 9 Jul 2021 15:38:55 +0000 (11:38 -0400)
Issue-ID: CPS-466
Signed-off-by: Bruno Sakoto <bruno.sakoto@bell.ca>
Change-Id: I2ecf98b9aa5a6097518e616c08f8bb2a2182a613

cps-rest/pom.xml
cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java
cps-ri/pom.xml
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy [moved from cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy with 98% similarity]
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceUnitSpec.groovy [new file with mode: 0644]
cps-service/src/main/java/org/onap/cps/spi/exceptions/DuplicatedYangResourceException.java [new file with mode: 0644]

index 22c1b79..4b42656 100755 (executable)
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jetty</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.retry</groupId>
+            <artifactId>spring-retry</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-aspects</artifactId>
+        </dependency>
         <dependency>
             <groupId>io.swagger.core.v3</groupId>
             <artifactId>swagger-annotations</artifactId>
index 419a218..35a1bed 100755 (executable)
@@ -1,6 +1,7 @@
 /*-\r
  * ============LICENSE_START=======================================================\r
- *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * Modifications Copyright (C) 2021 Bell Canada.\r
  * ================================================================================\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -23,12 +24,14 @@ package org.onap.cps.config;
 import org.modelmapper.ModelMapper;\r
 import org.springframework.context.annotation.Bean;\r
 import org.springframework.context.annotation.Configuration;\r
+import org.springframework.retry.annotation.EnableRetry;\r
 import springfox.documentation.builders.PathSelectors;\r
 import springfox.documentation.builders.RequestHandlerSelectors;\r
 import springfox.documentation.spi.DocumentationType;\r
 import springfox.documentation.spring.web.plugins.Docket;\r
 \r
 @Configuration\r
+@EnableRetry\r
 public class CpsConfig {\r
 \r
     /**\r
index b2cf691..c89882f 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <!--\r
   ============LICENSE_START=======================================================\r
-  Modification Copyright (C) 2021 Nordix Foundation\r
+  Modifications Copyright (C) 2021 Nordix Foundation\r
   Modifications Copyright (C) 2021 Bell Canada.\r
   ================================================================================\r
   Licensed under the Apache License, Version 2.0 (the "License");\r
             <groupId>org.springframework.boot</groupId>\r
             <artifactId>spring-boot-starter-validation</artifactId>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>org.springframework.retry</groupId>\r
+            <artifactId>spring-retry</artifactId>\r
+        </dependency>\r
+        <dependency>\r
+            <groupId>org.springframework</groupId>\r
+            <artifactId>spring-aspects</artifactId>\r
+        </dependency>\r
         <dependency>\r
             <groupId>org.postgresql</groupId>\r
             <artifactId>postgresql</artifactId>\r
             <groupId>commons-codec</groupId>\r
             <artifactId>commons-codec</artifactId>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>org.apache.commons</groupId>\r
+            <artifactId>commons-lang3</artifactId>\r
+        </dependency>\r
         <!-- T E S T   D E P E N D E N C I E S -->\r
         <dependency>\r
             <groupId>org.codehaus.groovy</groupId>\r
index ae6543d..eae1ef1 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation
- *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2020-2021 Bell Canada.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -25,10 +25,16 @@ import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 import javax.transaction.Transactional;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.hibernate.exception.ConstraintViolationException;
 import org.onap.cps.spi.CascadeDeleteAllowed;
 import org.onap.cps.spi.CpsAdminPersistenceService;
 import org.onap.cps.spi.CpsModulePersistenceService;
@@ -36,6 +42,7 @@ import org.onap.cps.spi.entities.AnchorEntity;
 import org.onap.cps.spi.entities.SchemaSetEntity;
 import org.onap.cps.spi.entities.YangResourceEntity;
 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
+import org.onap.cps.spi.exceptions.DuplicatedYangResourceException;
 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
 import org.onap.cps.spi.repository.AnchorRepository;
 import org.onap.cps.spi.repository.DataspaceRepository;
@@ -44,12 +51,18 @@ import org.onap.cps.spi.repository.SchemaSetRepository;
 import org.onap.cps.spi.repository.YangResourceRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DataIntegrityViolationException;
+import org.springframework.retry.annotation.Backoff;
+import org.springframework.retry.annotation.Retryable;
 import org.springframework.stereotype.Component;
 
 
 @Component
+@Slf4j
 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
 
+    private static final String YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME = "yang_resource_checksum_key";
+    private static final Pattern CHECKSUM_EXCEPTION_PATTERN = Pattern.compile(".*\\(checksum\\)=\\((\\w+)\\).*");
+
     @Autowired
     private YangResourceRepository yangResourceRepository;
 
@@ -70,6 +83,9 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
 
     @Override
     @Transactional
+    // A retry is made to store the schema set if it fails because of duplicated yang resource exception that
+    // can occur in case of specific concurrent requests.
+    @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 2, backoff = @Backoff(delay = 500))
     public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
         final Map<String, String> yangResourcesNameToContentMap) {
 
@@ -107,7 +123,21 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
 
         final Collection<YangResourceEntity> newYangResourceEntities = checksumToEntityMap.values();
         if (!newYangResourceEntities.isEmpty()) {
-            yangResourceRepository.saveAll(newYangResourceEntities);
+            try {
+                yangResourceRepository.saveAll(newYangResourceEntities);
+            } catch (final DataIntegrityViolationException dataIntegrityViolationException) {
+                // Throw a CPS duplicated Yang resource exception if the cause of the error is a yang checksum
+                // database constraint violation.
+                // If it is not, then throw the original exception
+                final Optional<DuplicatedYangResourceException> convertedException =
+                        convertToDuplicatedYangResourceException(
+                                dataIntegrityViolationException, newYangResourceEntities);
+                convertedException.ifPresent(
+                    e ->  log.warn(
+                                "Cannot persist duplicated yang resource. "
+                                        + "A total of 2 attempts to store the schema set are planned.", e));
+                throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException;
+            }
         }
 
         return ImmutableSet.<YangResourceEntity>builder()
@@ -116,6 +146,67 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
             .build();
     }
 
+    /**
+     * Convert the specified data integrity violation exception into a CPS duplicated Yang resource exception
+     * if the cause of the error is a yang checksum database constraint violation.
+     * @param originalException the original db exception.
+     * @param yangResourceEntities the collection of Yang resources involved in the db failure.
+     * @return an optional converted CPS duplicated Yang resource exception. The optional is empty if the original
+     *      cause of the error is not a yang checksum database constraint violation.
+     */
+    private Optional<DuplicatedYangResourceException> convertToDuplicatedYangResourceException(
+            final DataIntegrityViolationException originalException,
+            final Collection<YangResourceEntity> yangResourceEntities) {
+
+        // The exception result
+        DuplicatedYangResourceException duplicatedYangResourceException = null;
+
+        final Throwable cause = originalException.getCause();
+        if (cause instanceof ConstraintViolationException) {
+            final ConstraintViolationException constraintException = (ConstraintViolationException) cause;
+            if (YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME.equals(constraintException.getConstraintName())) {
+                // Db constraint related to yang resource checksum uniqueness is not respected
+                final String checksumInError = getDuplicatedChecksumFromException(constraintException);
+                final String nameInError = getNameForChecksum(checksumInError, yangResourceEntities);
+                duplicatedYangResourceException =
+                        new DuplicatedYangResourceException(nameInError, checksumInError, constraintException);
+            }
+        }
+
+        return Optional.ofNullable(duplicatedYangResourceException);
+
+    }
+
+    /**
+     * Get the checksum that caused the constraint violation exception.
+     * @param exception the exception having the checksum in error.
+     * @return the checksum in error or null if not found.
+     */
+    private String getDuplicatedChecksumFromException(final ConstraintViolationException exception) {
+        String checksum = null;
+        final Matcher matcher = CHECKSUM_EXCEPTION_PATTERN.matcher(exception.getSQLException().getMessage());
+        if (matcher.find() && matcher.groupCount() == 1) {
+            checksum = matcher.group(1);
+        }
+        return checksum;
+    }
+
+    /**
+     * Get the name of the yang resource having the specified checksum.
+     * @param checksum the checksum. Null is supported.
+     * @param yangResourceEntities the list of yang resources to search among.
+     * @return the name found or null if none.
+     */
+    private String getNameForChecksum(
+            final String checksum, final Collection<YangResourceEntity> yangResourceEntities) {
+        return
+                yangResourceEntities.stream()
+                        .filter(entity -> StringUtils.equals(checksum, (entity.getChecksum())))
+                        .findFirst()
+                        .map(YangResourceEntity::getName)
+                        .orElse(null);
+    }
+
     @Override
     public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
@@ -1,6 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Nordix Foundation
+ *  Modifications Copyright (C) 2021 Bell Canada.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the 'License');
  *  you may not use this file except in compliance with the License.
@@ -33,7 +34,7 @@ import org.onap.cps.spi.repository.SchemaSetRepository
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.test.context.jdbc.Sql
 
-class CpsModulePersistenceServiceSpec extends CpsPersistenceSpecBase {
+class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
 
     @Autowired
     CpsModulePersistenceService objectUnderTest
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceUnitSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceUnitSpec.groovy
new file mode 100644 (file)
index 0000000..ffbac48
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (c) 2021 Bell Canada.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+*/
+
+package org.onap.cps.spi.impl
+
+import org.hibernate.exception.ConstraintViolationException
+import org.onap.cps.spi.CpsModulePersistenceService
+import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
+import org.onap.cps.spi.repository.DataspaceRepository
+import org.onap.cps.spi.repository.SchemaSetRepository
+import org.onap.cps.spi.repository.YangResourceRepository
+import org.springframework.dao.DataIntegrityViolationException
+import spock.lang.Shared
+import spock.lang.Specification
+
+import java.sql.SQLException
+
+/**
+ * Specification unit test class for CPS module persistence service.
+ */
+class CpsModulePersistenceServiceUnitSpec extends Specification {
+
+    // Instance to test
+    CpsModulePersistenceService objectUnderTest
+
+    // Mocks
+    def dataspaceRepositoryMock = Mock(DataspaceRepository)
+    def yangResourceRepositoryMock = Mock(YangResourceRepository)
+    def schemaSetRepositoryMock = Mock(SchemaSetRepository)
+
+    // Constants
+    def yangResourceName = 'my-yang-resource-name'
+    def yangResourceContent = 'my-yang-resource-content'
+
+    // Scenario data
+    @Shared
+    yangResourceChecksum = 'ac2352cc20c10467528b2390bbf2d72d48b0319152ebaabcda207786b4a641c2'
+    @Shared
+    yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
+    @Shared
+    sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
+    @Shared
+    checksumIntegrityException =
+            new DataIntegrityViolationException(
+                    "checksum integrity exception",
+                    new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
+    @Shared
+    anotherIntegrityException = new DataIntegrityViolationException("another integrity exception")
+
+    def setup() {
+        objectUnderTest = new CpsModulePersistenceServiceImpl()
+        objectUnderTest.dataspaceRepository = dataspaceRepositoryMock
+        objectUnderTest.yangResourceRepository = yangResourceRepositoryMock
+        objectUnderTest.schemaSetRepository = schemaSetRepositoryMock
+    }
+
+    def 'Store schema set error scenario: #scenario.'() {
+        given: 'no yang resource are currently saved'
+            yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
+        and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)'
+            yangResourceRepositoryMock.saveAll(_) >> { throw dbException }
+        when: 'attempt to store schema set '
+            def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent]
+            objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap)
+        then: 'an #expectedThrownException is thrown'
+            def e = thrown(expectedThrownException)
+            e.getMessage().contains(expectedThrownExceptionMessage)
+        where: 'the following data is used'
+            scenario                | dbException                || expectedThrownException | expectedThrownExceptionMessage
+            'checksum data failure' | checksumIntegrityException || DuplicatedYangResourceException | yangResourceChecksum
+            'other data failure'    | anotherIntegrityException  || DataIntegrityViolationException | 'another integrity exception'
+    }
+
+}
diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/DuplicatedYangResourceException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/DuplicatedYangResourceException.java
new file mode 100644 (file)
index 0000000..735e677
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (c) 2021 Bell Canada.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.exceptions;
+
+import lombok.Getter;
+
+/**
+ * Duplicated Yang resource exception. It is raised when trying to persist a duplicated yang resource.
+ */
+@Getter
+public class DuplicatedYangResourceException extends CpsException {
+
+    private static final long serialVersionUID = 9085557087319212380L;
+
+    private final String name;
+    private final String checksum;
+
+    /**
+     * Constructor.
+     * @param cause the exception cause
+     */
+    public DuplicatedYangResourceException(final String name, final String checksum, final Throwable cause) {
+        super(
+                String.format("Unexpected duplicated yang resource: '%s' with checksum '%s'", name, checksum),
+                "The error could be caused by concurrent requests. Retrying sending the request could be required.",
+                cause);
+        this.name = name;
+        this.checksum = checksum;
+    }
+
+}