Uploaded image for project: 'Configuration Persistence Service'
  1. Configuration Persistence Service
  2. CPS-2165

Improve missing SchemaSet error handling

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Done
    • Icon: Medium Medium
    • None
    • None
    • None
    • None

      Background

      In the bug CPS-2139, when a search for CM-handles with a given Yang module is run, if a CM-handle delete is happening at the same time, it can cause the search to fail with Internal Server Error.

      Cause

      This is caused by use of Hibernate Lazy fetching.

      More specifically, in NCMP, the module search is done at org.onap.cps.ncmp.api.impl.inventory.InventoryPersistenceImpl#getCmHandleIdsWithGivenModules:

      public Collection<String> getCmHandleIdsWithGivenModules(final Collection<String> moduleNamesForQuery) {
          return cpsAnchorService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
      } 

      Ultimately, this method in cps-ri gets called: org.onap.cps.spi.impl.CpsAdminPersistenceServiceImpl#queryAnchors:

      final Collection<AnchorEntity> anchorEntities = anchorRepository
          .getAnchorsByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames, inputModuleNames.size());
      return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());| 

      Note it calls toAnchor method in the stream, which calls anchorEntity.getSchemaSet:

      private static Anchor toAnchor(final AnchorEntity anchorEntity) {
          return Anchor.builder()
              .name(anchorEntity.getName())
              .dataspaceName(anchorEntity.getDataspace().getName())
              .schemaSetName(anchorEntity.getSchemaSet().getName())
              .build();
      } 

      In AnchorEntity class, schemaSet is marked as FetchType.LAZY:

      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "schema_set_id")
      private SchemaSetEntity schemaSet; 

      The net result of this is that the DB query getAnchorsByDataspaceIdAndModuleNames may return 20K anchors, and then in the loop calling toAnchor will result in 20,000 additional SQL queries to lazily fetch each schemaset. This is not only extremely inefficient, but it means the schemaset/anchor could be deleted after the anchor was read in the initial query. This would result in an exception being thrown using anchorEntity.getSchemaSet().getName()

      Possible solution

      It is proposed that NCMP's InventoryService should not query anchors for modules, but rather query schemasets. (Yang modules logically belong to schemasets, not anchors, so a module search should happen in the CpsModuleService anyway).

      public Collection<String> getCmHandleIdsWithGivenModules(final Collection<String> moduleNamesForQuery) {
          return cpsModuleService.querySchemaSetNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
      }

      Relocating the module search to CpsModuleService return schemasets will completely avoid the above lazy fetching issues, as well as massively improving search speed, reducing memory consumption, relieving DB pressure, and simplifying the source code.

            danielhanrahan Daniel Hanrahan
            leventecsanyi Levente Csanyi
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: