brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahgittin <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #462: Inherit config default values
Date Mon, 13 Feb 2017 13:46:07 GMT
Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/462#discussion_r100795410
  
    --- Diff: core/src/main/java/org/apache/brooklyn/core/config/BasicConfigInheritance.java
---
    @@ -28,51 +32,188 @@
     import org.apache.brooklyn.config.ConfigKey;
     import org.apache.brooklyn.config.ConfigValueAtContainer;
     import org.apache.brooklyn.util.collections.CollectionMerger;
    +import org.apache.brooklyn.util.exceptions.Exceptions;
     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;
     
    +@SuppressWarnings("serial")
     public class BasicConfigInheritance implements ConfigInheritance {
     
    +    private static final Logger log = LoggerFactory.getLogger(BasicConfigInheritance.class);
    +    
         private static final long serialVersionUID = -5916548049057961051L;
     
         public static final String CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE = "deep_merge";
         public static final String CONFLICT_RESOLUTION_STRATEGY_OVERWRITE = "overwrite";
         
    +    public static abstract class DelegatingConfigInheritance implements ConfigInheritance
{
    +        protected abstract ConfigInheritance getDelegate();
    +        
    +        @Override @Deprecated public InheritanceMode isInherited(ConfigKey<?> key,
Object from, Object to) {
    +            return getDelegate().isInherited(key, from, to);
    +        }
    +        @Override public <TContainer, TValue> boolean isReinheritable(ConfigValueAtContainer<TContainer,
TValue> parent, ConfigInheritanceContext context) {
    +            return getDelegate().isReinheritable(parent, context);
    +        }
    +        @Override public <TContainer, TValue> boolean considerParent(ConfigValueAtContainer<TContainer,
TValue> local, ConfigValueAtContainer<TContainer, TValue> parent, ConfigInheritanceContext
context) {
    +            return getDelegate().considerParent(local, parent, context);
    +        }
    +        @Override
    +        public <TContainer, TValue> ReferenceWithError<ConfigValueAtContainer<TContainer,
TValue>> resolveWithParent(ConfigValueAtContainer<TContainer, TValue> local, ConfigValueAtContainer<TContainer,
TValue> resolvedParent, ConfigInheritanceContext context) {
    +            return getDelegate().resolveWithParent(local, resolvedParent, context);
    +        }
    +        @Override
    +        public boolean equals(Object obj) {
    +            return super.equals(obj) || getDelegate().equals(obj);
    +        }
    +        
    +        // standard deserialization method
    +        protected ConfigInheritance readResolve() {
    +            return returnEquivalentConstant(this);
    +        }
    +    }
    +
    +    private static ConfigInheritance returnEquivalentConstant(ConfigInheritance candidate)
{
    +        for (ConfigInheritance knownMode: Arrays.asList(
    +                NOT_REINHERITED, NOT_REINHERITED_ELSE_DEEP_MERGE, NEVER_INHERITED, OVERWRITE,
BasicConfigInheritance.DEEP_MERGE)) {
    +            if (candidate.equals(knownMode)) return knownMode;
    +        }
    +        if (candidate.equals(new BasicConfigInheritance(false, CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,
true, true))) {
    +            // ignore the ancestor flag for this mode
    +            return NEVER_INHERITED;
    +        }
    +        return candidate;
    +    }
    +    
    +    /*
    +     * use of delegate is so that stateless classes can be defined to make the serialization
nice,
    +     * both the name and hiding the implementation detail (also making it easier for
that detail to change);
    +     * with aliased type names the field names here could even be aliases for the type
names.
    +     * (we could alternatively have an "alias-for-final-instance" mode in serialization,
    +     * to optimize where we know that a java instance is final.)   
    +     */
    +    
    +    private static class NotReinherited extends DelegatingConfigInheritance {
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(false,
CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, false, true); 
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
         /** Indicates that a config key value should not be passed down from a container
where it is defined.
          * Unlike {@link #NEVER_INHERITED} these values can be passed down if set as anonymous
keys at a container
          * (ie the container does not expect it) to a container which does expect it, but
it will not be passed down further. 
          * If the inheritor also defines a value the parent's value is ignored irrespective

          * (as in {@link #OVERWRITE}; see {@link #NOT_REINHERITED_ELSE_DEEP_MERGE} if merging
is desired). */
    -    public static BasicConfigInheritance NOT_REINHERITED = new BasicConfigInheritance(false,CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,false);
    +    public static ConfigInheritance NOT_REINHERITED = new NotReinherited();
    +    
    +    private static class NotReinheritedElseDeepMerge extends DelegatingConfigInheritance
{
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(false,
CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE, false, true); 
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
         /** As {@link #NOT_REINHERITED} but in cases where a value is inherited because a
parent did not recognize it,
          * if the inheritor also defines a value the two values should be merged. */
    -    public static BasicConfigInheritance NOT_REINHERITED_ELSE_DEEP_MERGE = new BasicConfigInheritance(false,CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE,false);
    -    /** Indicates that a key's value should never be inherited, even if defined on a
container that does not know the key.
    -     * Most usages will prefer {@link #NOT_REINHERITED}. */
    -    public static BasicConfigInheritance NEVER_INHERITED = new BasicConfigInheritance(false,CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,true);
    +    public static ConfigInheritance NOT_REINHERITED_ELSE_DEEP_MERGE = new NotReinheritedElseDeepMerge();
    +    
    +    private static class NeverInherited extends DelegatingConfigInheritance {
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(false,
CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, true, false);
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
    +    /** Indicates that a key's value should never be inherited, even if inherited from
a value set on a container that does not know the key.
    +     * (Most usages will prefer {@link #NOT_REINHERITED}.) */
    +    public static ConfigInheritance NEVER_INHERITED = new NeverInherited();
    +    
    +    private static class Overwrite extends DelegatingConfigInheritance {
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(true,
CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, false, true);
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
         /** Indicates that if a key has a value at both an ancestor and a descendant, the
descendant and his descendants
          * will prefer the value at the descendant. */
    -    public static BasicConfigInheritance OVERWRITE = new BasicConfigInheritance(true,CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,false);
    +    public static ConfigInheritance OVERWRITE = new Overwrite();
    +    
    +    private static class DeepMerge extends DelegatingConfigInheritance {
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(true,
CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE, false, true);
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
         /** Indicates that if a key has a value at both an ancestor and a descendant, the
descendant and his descendants
    -     * should attempt to merge the values. If the values are not mergable behaviour is
undefined
    +     * should attempt to merge the values. If the values are not mergeable behaviour
is undefined
          * (and often the descendant's value will simply overwrite). */
    -    public static BasicConfigInheritance DEEP_MERGE = new BasicConfigInheritance(true,CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE,false);
    +    public static ConfigInheritance DEEP_MERGE = new DeepMerge();
    +
    +    // support conversion from these legacy fields
    +    @SuppressWarnings("deprecation")
    +    private static void registerReplacements() {
    +        ConfigInheritance.Legacy.registerReplacement(ConfigInheritance.DEEP_MERGE, DEEP_MERGE);

    +        ConfigInheritance.Legacy.registerReplacement(ConfigInheritance.ALWAYS, OVERWRITE);

    +        ConfigInheritance.Legacy.registerReplacement(ConfigInheritance.NONE, NOT_REINHERITED);

    +    }
    +    static { registerReplacements(); }
         
    -    /** reinheritable? true/false; if false, children/descendants/inheritors will never
see it; default true */
    +    /** whether a value on a key defined locally should be inheritable by descendants.
    +     * if false at a point where a key is defined, 
    +     * children/descendants/inheritors will not be able to see its value, whether explicit
or default.
    +     * default true:  things are normally reinherited.
    +     * <p>
    +     * note that this only takes effect where a key is defined locally.
    +     * if a key is not defined at an ancestor, a descendant setting this value false
will not prevent it 
    +     * from inheriting values from ancestors.
    +     * <p> 
    +     * typical use case for setting this is false is where a key is consumed and descendants
should not
    +     * "reconsume" it.  for example setting files to install on a VM need only be applied
once,
    +     * and if it has <b>runtime management</b> hierarchy descendants which
also understand that field they
    +     * should not install the same files. 
    +     * (there is normally no reason to set this false in the context of <b>type
hierarchy</b> inheritance because
    +     * an ancestor type cannot "consume" a value.) */
         protected final boolean isReinherited;
    -    /** conflict-resolution-strategy? in {@link BasicConfigInheritance} supported values
are
    +    
    +    /** a symbol indicating a conflict-resolution-strategy understood by the implementation.
    +     * in {@link BasicConfigInheritance} supported values are
          * {@link #CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE} and {@link #CONFLICT_RESOLUTION_STRATEGY_OVERWRITE}.
    -     * subclasses may pass null if they provide a custom implementaton of {@link #resolveWithParentCustomStrategy(ConfigValueAtContainer,
ConfigValueAtContainer, org.apache.brooklyn.config.ConfigInheritance.ConfigInheritanceContext)}
*/
    +     * subclasses may pass null or a different string if they provide a custom implementaton

    +     * of {@link #resolveWithParentCustomStrategy(ConfigValueAtContainer, ConfigValueAtContainer,
org.apache.brooklyn.config.ConfigInheritance.ConfigInheritanceContext)} */
         @Nullable protected final String conflictResolutionStrategy;
    -    /** use-local-default-value? true/false; if true, overwrite above means "always ignore
(even if null)"; default false
    -     * whereas merge means "use local default (if non-null)" */
    -    protected final boolean useLocalDefaultValue;
    +    
    +    /** @deprecated since 0.10.0 when this was introduced, now renamed {@link #localDefaultResolvesWithAncestorValue}
*/
    +    @Deprecated protected final Boolean useLocalDefaultValue;
    +    /** whether a local default value should be considered for resolution in the presence
of an ancestor value.
    +     * can use true with overwrite to mean don't inherit, or true with merge to mean
local default merged on top of inherited
    +     * (but be careful here, if local default is null in a merge it will delete ancestor
values).
    +     * <p>
    +     * in most cases this is false, meaning a default value is ignored if the parent
has a value.
    +     * <p>
    +     * null should not be used. a boxed object is taken (as opposed to a primitive boolean)
only in order to support migration.    
    +     */
    +    @Nonnull
    +    protected final Boolean localDefaultResolvesWithAncestorValue;
     
    -    protected BasicConfigInheritance(boolean isReinherited, @Nullable String conflictResolutionStrategy,
boolean useLocalDefaultValue) {
    +    /** whether a default set in an ancestor container's key definition will be considered
as the
    +     * local default value at descendants who don't define any other value (nothing set
locally and local default is null);
    +     * <p>
    +     * if true (now the usual behaviour), if an ancestor defines a default and a descendant
doesn't, the ancestor's value will be taken as a default.
    +     * if it is also the case that localDefaultResolvesWithAncestorValue is true at the
<i>ancestor</i> then a descendant who
    +     * defines a local default value (with this field true) will have its conflict resolution
strategy
    +     * applied with the ancestor's default value.
    +     * <p>
    +     * if this is false, ancestor defaults are completely ignored; prior to 0.10.0 this
was the normal behaviour,
    +     * but it caused surprises where default values in parameters did not take effect.
    +     * <p>
    +     * null should not be used. a boxed object is taken (as opposed to a primitive boolean)
only in order to support migration.    
    +     */
    +    @Nonnull
    +    protected final Boolean ancestorDefaultInheritable;
    --- End diff --
    
    have thought about this:  the two conditions are independent dimensions, and it would
matter if we used `localDefaultResolvesWithAncestorValue` for any purpose other than blocking
inheritance from ancestors.  have added more comments to explain, including your words about
the "independent dimensions" which is a good point:  including the control dimensions lets
the logic be correct, even if we only expose a subset of possibilities to the end user (which
simplifies a little bit the complexity of config inheritance).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message