Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/868#discussion_r146686839
  
    --- Diff: 
core/src/main/java/org/apache/brooklyn/core/typereg/BasicBrooklynTypeRegistry.java
 ---
    @@ -498,10 +524,34 @@ public void delete(VersionedName type) {
     //        throw new NoSuchElementException("No catalog item found with id 
"+type);
         }
         
    +    /** removes the given registered type in the noted bundle; 
    +     * if not known in that bundle tries deleting from legacy catalog.
    +     * @throws NoSuchElementException if not found */
         public void delete(RegisteredType type) {
    -        delete(type.getVersionedName());
    +        Map<String, RegisteredType> m = 
localRegisteredTypesAndContainingBundles.get(type.getId());
    +        if (m!=null) {
    +            RegisteredType removedItem = 
m.remove(type.getContainingBundle());
    +            if (m.isEmpty()) {
    +                synchronized (localRegisteredTypesAndContainingBundles) {
    +                    m = 
localRegisteredTypesAndContainingBundles.get(type.getId());
    +                    if (m!=null && m.isEmpty()) {
    +                        
localRegisteredTypesAndContainingBundles.remove(type.getId());
    +                    } else {
    +                        // either removed in parallel or no longer empty
    +                    }
    +                }
    +                if (removedItem==null) {
    +                    throw new NoSuchElementException("Requested to delete 
"+type+" from "+type.getContainingBundle()+", "
    +                        + "but that type was not known in that bundle, it 
is in "+m.keySet()+" instead");
    +                }
    +            }
    +        } else {
    +            // try legacy delete
    +            delete(type.getVersionedName());
    --- End diff --
    
    Feels dangerous (but a small race): another thread might be adding a 
`RegisteredType` (with same id, from another bundle) 
`localRegisteredTypesAndContainingBundles`. The `delete(VersionedName)` will 
then delete the thing added by the other thread.
    
    Can we instead separate out the legacy deletion, so it doesn't try to also 
again delete from `localRegisteredTypesAndContainingBundles`?


---

Reply via email to