You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by GitBox <gi...@apache.org> on 2020/10/11 18:13:53 UTC

[GitHub] [brooklyn-server] geomacy commented on a change in pull request #1115: Catalog bundle resolvers

geomacy commented on a change in pull request #1115:
URL: https://github.com/apache/brooklyn-server/pull/1115#discussion_r502842569



##########
File path: core/src/main/java/org/apache/brooklyn/core/typereg/BrooklynCatalogBundleResolvers.java
##########
@@ -18,26 +18,149 @@
  */
 package org.apache.brooklyn.core.typereg;
 
+import com.google.common.annotations.Beta;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Multimap;
 import java.io.InputStream;
+import java.util.*;
+import org.apache.brooklyn.api.framework.FrameworkLookup;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.api.typereg.RegisteredType;
+import org.apache.brooklyn.api.typereg.RegisteredTypeLoadingContext;
 import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult;
+import org.apache.brooklyn.core.typereg.BrooklynCatalogBundleResolver.BundleInstallationOptions;
+import org.apache.brooklyn.util.collections.MutableList;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.PropagatedRuntimeException;
 import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class BrooklynCatalogBundleResolvers {
 
-    public static ReferenceWithError<OsgiBundleInstallationResult> install(ManagementContext managementContext, InputStream input,
-                                                                           BrooklynCatalogBundleResolver.BundleInstallationOptions options) {
+    private static Logger LOG = LoggerFactory.getLogger(BrooklynCatalogBundleResolvers.class);
 
-        if (options==null) options = new BrooklynCatalogBundleResolver.BundleInstallationOptions();
+    private static Collection<BrooklynCatalogBundleResolver> getAll() {
+        return ImmutableList.copyOf(FrameworkLookup.lookupAll(BrooklynCatalogBundleResolver.class));
+    }
+
+    private static Collection<Class<? extends BrooklynCatalogBundleResolver>> OVERRIDE;
+    @SafeVarargs
+    @VisibleForTesting
+    public synchronized static void forceAvailable(Class<? extends BrooklynCatalogBundleResolver> ...classes) {
+        OVERRIDE = Arrays.asList(classes);
+    }
+    public synchronized static void clearForced() {
+        OVERRIDE = null;
+    }
+
+    public static Collection<BrooklynCatalogBundleResolver> all(ManagementContext mgmt) {
+        // TODO cache these in the TypeRegistry, looking for new ones periodically or supplying a way to register them
+        Collection<Class<? extends BrooklynCatalogBundleResolver>> override = OVERRIDE;
+        Collection<BrooklynCatalogBundleResolver> result = new ArrayList<>();
+        if (override!=null) {
+            for (Class<? extends BrooklynCatalogBundleResolver> o1: override) {
+                try {
+                    result.add(o1.newInstance());
+                } catch (Exception e) {
+                    Exceptions.propagate(e);
+                }
+            }
+        } else {
+            result.addAll(getAll());
+        }
+        for(BrooklynCatalogBundleResolver t : result) {
+            t.setManagementContext(mgmt);
+        }
+        return result;
+    }
+
+    /** returns a list of {@link BrooklynCatalogBundleResolver} instances for this {@link ManagementContext}
+     * which may be able to handle the given bundle; the list is sorted with highest-score transformer first */
+    @Beta
+    public static List<BrooklynCatalogBundleResolver> forBundle(ManagementContext mgmt, InputStream input,
+                                                                BrooklynCatalogBundleResolver.BundleInstallationOptions options) {
+        Multimap<Double,BrooklynCatalogBundleResolver> byScoreMulti = ArrayListMultimap.create();
+        Collection<BrooklynCatalogBundleResolver> transformers = all(mgmt);
+        for (BrooklynCatalogBundleResolver transformer : transformers) {
+            double score = transformer.scoreForBundle(options.format, input);
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("SCORE for '" + input + "' at " + transformer + ": " + score);
+            }
+            if (score>0) byScoreMulti.put(score, transformer);
+        }
+        Map<Double, Collection<BrooklynCatalogBundleResolver>> tree = new TreeMap<Double, Collection<BrooklynCatalogBundleResolver>>(byScoreMulti.asMap());
+        List<Collection<BrooklynCatalogBundleResolver>> highestFirst = new ArrayList<Collection<BrooklynCatalogBundleResolver>>(tree.values());
+        Collections.reverse(highestFirst);
+        return ImmutableList.copyOf(Iterables.concat(highestFirst));
+    }
+
+    public static ReferenceWithError<OsgiBundleInstallationResult> install(ManagementContext mgmt, InputStream input,
+                                                                           BundleInstallationOptions options) {
+//
+//        if (options==null) options = new BrooklynCatalogBundleResolver.BundleInstallationOptions();
+//
+//        BrooklynCatalogBundleResolver r;
+//        if (BrooklynBomYamlCatalogBundleResolver.FORMAT.equals(options.format)) {
+//            r = new BrooklynBomYamlCatalogBundleResolver();
+//        } else {
+//            r = new BrooklynBomBundleCatalogBundleResolver();
+//        }
+//        r.setManagementContext(managementContext);
+//        return r.install(input, options);
+
+        List<BrooklynCatalogBundleResolver> resolvers = forBundle(mgmt, input, options);
+        Collection<String> resolversWhoDontSupport = new ArrayList<String>();
+        Collection<Exception> failuresFromResolvers = new ArrayList<Exception>();
+        for (BrooklynCatalogBundleResolver t: resolvers) {
+            try {
+                ReferenceWithError<OsgiBundleInstallationResult> result = t.install(input, options);
+                if (result==null) {
+                    resolversWhoDontSupport.add(t.getFormatCode() + " (returned null)");
+                    continue;
+                }
+                return result;
+            } catch (@SuppressWarnings("deprecation") UnsupportedCatalogBundleException e) {
+                resolversWhoDontSupport.add(t.getFormatCode() +
+                        (Strings.isNonBlank(e.getMessage()) ? " ("+e.getMessage()+")" : ""));
+            } catch (Throwable e) {
+                Exceptions.propagateIfFatal(e);
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("Transformer for "+t.getFormatCode()+" gave an error creating this plan (may retry): "+e, e);
+                }
+                failuresFromResolvers.add(new PropagatedRuntimeException(
+                        (t.getFormatCode()+" bundle installation error") + ": "+
+                                Exceptions.collapseText(e), e));
+            }
+        }
 
-        BrooklynCatalogBundleResolver r;
-        if (BrooklynBomYamlCatalogBundleResolver.FORMAT.equals(options.format)) {
-            r = new BrooklynBomYamlCatalogBundleResolver();
+        // failed
+        Exception result;
+        if (!failuresFromResolvers.isEmpty()) {
+            // at least one thought he could do it
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Failure transforming plan; returning summary failure, but for reference "
+                        + "potentially application transformers were "+resolvers+", "
+                        + "available ones are "+ MutableList.builder().addAll(all(mgmt))
+                        .build()+"; "
+                        + "failures: "+failuresFromResolvers);
+            }
+            result = failuresFromResolvers.size()==1 ? Exceptions.create(null, failuresFromResolvers) :
+                    Exceptions.create("All plan transformers failed", failuresFromResolvers);

Review comment:
       "Multiple" rather than "all", surely? (If say you had 4 resolvers, and two threw UnsupportedCatalogBundleException and two something else. 

##########
File path: core/src/main/java/org/apache/brooklyn/core/typereg/BrooklynCatalogBundleResolvers.java
##########
@@ -18,26 +18,149 @@
  */
 package org.apache.brooklyn.core.typereg;
 
+import com.google.common.annotations.Beta;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Multimap;
 import java.io.InputStream;
+import java.util.*;
+import org.apache.brooklyn.api.framework.FrameworkLookup;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.api.typereg.RegisteredType;
+import org.apache.brooklyn.api.typereg.RegisteredTypeLoadingContext;
 import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult;
+import org.apache.brooklyn.core.typereg.BrooklynCatalogBundleResolver.BundleInstallationOptions;
+import org.apache.brooklyn.util.collections.MutableList;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.PropagatedRuntimeException;
 import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class BrooklynCatalogBundleResolvers {
 
-    public static ReferenceWithError<OsgiBundleInstallationResult> install(ManagementContext managementContext, InputStream input,
-                                                                           BrooklynCatalogBundleResolver.BundleInstallationOptions options) {
+    private static Logger LOG = LoggerFactory.getLogger(BrooklynCatalogBundleResolvers.class);
 
-        if (options==null) options = new BrooklynCatalogBundleResolver.BundleInstallationOptions();
+    private static Collection<BrooklynCatalogBundleResolver> getAll() {
+        return ImmutableList.copyOf(FrameworkLookup.lookupAll(BrooklynCatalogBundleResolver.class));
+    }
+
+    private static Collection<Class<? extends BrooklynCatalogBundleResolver>> OVERRIDE;
+    @SafeVarargs
+    @VisibleForTesting
+    public synchronized static void forceAvailable(Class<? extends BrooklynCatalogBundleResolver> ...classes) {
+        OVERRIDE = Arrays.asList(classes);
+    }
+    public synchronized static void clearForced() {
+        OVERRIDE = null;
+    }
+
+    public static Collection<BrooklynCatalogBundleResolver> all(ManagementContext mgmt) {
+        // TODO cache these in the TypeRegistry, looking for new ones periodically or supplying a way to register them
+        Collection<Class<? extends BrooklynCatalogBundleResolver>> override = OVERRIDE;
+        Collection<BrooklynCatalogBundleResolver> result = new ArrayList<>();
+        if (override!=null) {
+            for (Class<? extends BrooklynCatalogBundleResolver> o1: override) {
+                try {
+                    result.add(o1.newInstance());
+                } catch (Exception e) {
+                    Exceptions.propagate(e);
+                }
+            }
+        } else {
+            result.addAll(getAll());
+        }
+        for(BrooklynCatalogBundleResolver t : result) {
+            t.setManagementContext(mgmt);
+        }
+        return result;
+    }
+
+    /** returns a list of {@link BrooklynCatalogBundleResolver} instances for this {@link ManagementContext}
+     * which may be able to handle the given bundle; the list is sorted with highest-score transformer first */
+    @Beta
+    public static List<BrooklynCatalogBundleResolver> forBundle(ManagementContext mgmt, InputStream input,
+                                                                BrooklynCatalogBundleResolver.BundleInstallationOptions options) {
+        Multimap<Double,BrooklynCatalogBundleResolver> byScoreMulti = ArrayListMultimap.create();
+        Collection<BrooklynCatalogBundleResolver> transformers = all(mgmt);
+        for (BrooklynCatalogBundleResolver transformer : transformers) {
+            double score = transformer.scoreForBundle(options.format, input);
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("SCORE for '" + input + "' at " + transformer + ": " + score);
+            }
+            if (score>0) byScoreMulti.put(score, transformer);
+        }
+        Map<Double, Collection<BrooklynCatalogBundleResolver>> tree = new TreeMap<Double, Collection<BrooklynCatalogBundleResolver>>(byScoreMulti.asMap());
+        List<Collection<BrooklynCatalogBundleResolver>> highestFirst = new ArrayList<Collection<BrooklynCatalogBundleResolver>>(tree.values());

Review comment:
       Less verbose with `<>`:
   ```suggestion
           Map<Double, Collection<BrooklynCatalogBundleResolver>> tree = new TreeMap<>(byScoreMulti.asMap());
           List<Collection<BrooklynCatalogBundleResolver>> highestFirst = new ArrayList<>(tree.values());
   ```
   
   but could just use `TreeMultimap` in reverse order, and do without the above lines altogether:
   ```
       public static List<BrooklynCatalogBundleResolver> forBundle(ManagementContext mgmt, Supplier<InputStream> input,
                                                                   BrooklynCatalogBundleResolver.BundleInstallationOptions options) {
           Multimap<Double,BrooklynCatalogBundleResolver> byScoreMulti = TreeMultimap.create(Comparator.reverseOrder(), (r1, r2) -> 0);
           Collection<BrooklynCatalogBundleResolver> transformers = all(mgmt);
           for (BrooklynCatalogBundleResolver transformer : transformers) {
               double score = transformer.scoreForBundle(options==null ? null : options.format, input);
               if (LOG.isTraceEnabled()) {
                   LOG.trace("SCORE for '" + input + "' at " + transformer + ": " + score);
               }
               if (score>0) byScoreMulti.put(score, transformer);
           }
           return ImmutableList.copyOf(byScoreMulti.values());
       }
   ```
   

##########
File path: core/src/main/java/org/apache/brooklyn/core/typereg/BrooklynCatalogBundleResolver.java
##########
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.brooklyn.core.typereg;
+
+import com.google.common.annotations.Beta;
+import java.io.File;
+import java.io.InputStream;
+import java.util.ServiceLoader;
+import java.util.function.Supplier;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.apache.brooklyn.api.typereg.BrooklynTypeRegistry;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.mgmt.ManagementContextInjectable;
+import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+
+/**
+ * Interface for use by schemes which provide the capability to add types to the type registry.
+ * Typically this is by installing an OSGi bundle with metadata, or sometimes with a YAML file.
+ * <p>
+ * To add a new resolver for a bundle of types, simply create an implementation and declare it
+ * as a java service (cf {@link ServiceLoader}).

Review comment:
       I think this should document that to add a new resolver users should register these as services in the OSGI context, rather than do them with a ServiceLoader.  At least that should be the preferred way. It lets users add new bundles at runtime that provide new BundleResolver implementations, without even stopping Brooklyn, similar to how in the UI you can add new panels on the front page dynamically. The service loader these days is more of a fallback mechanism for when not using OSGI. 

##########
File path: core/src/main/java/org/apache/brooklyn/core/typereg/BrooklynTypePlanTransformer.java
##########
@@ -81,14 +83,15 @@
      * has returned a positive value, and the same constraints on the inputs as for that method apply.
      * <p>
      * Implementations should either return null or throw {@link UnsupportedTypePlanException} 
-     * if they cannot instantiate the given {@link RegisteredType#getPlan()}. */
+     * if upon closer inspection following a non-null score, they do not actually support the given {@link RegisteredType#getPlan()}.
+     * If they should support the plan but the plan contains an error, they should throw the relevant error for feedback to the user. */
     @Nullable Object create(@Nonnull RegisteredType type, @Nonnull RegisteredTypeLoadingContext context);
 
-    // TODO sketch methods for loading *catalog* definitions.  note some potential overlap
-    // with BrooklynTypeRegistery.createXxxFromPlan
-    @Beta
-    double scoreForTypeDefinition(String formatCode, Object catalogData);
-    @Beta
-    List<RegisteredType> createFromTypeDefinition(String formatCode, Object catalogData);
+    /** @deprecated since 1.0; use {@link org.apache.brooklyn.core.typereg.BrooklynCatalogBundleResolver} for adding to catalog */

Review comment:
       Since 1.0.1




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org