java.lang.Object
org.apache.hadoop.yarn.server.resourcemanager.placement.VariableContext

public class VariableContext extends Object
This class is a key-value store for the variables and their respective values during an application placement. The class gives support for immutable variables, which can be set only once, and has helper methods for replacing the variables with their respective values in provided strings. We don't extend the map interface, because we don't need all the features a map provides, this class tries to be as simple as possible.
  • Constructor Details

    • VariableContext

      public VariableContext()
  • Method Details

    • isImmutable

      public boolean isImmutable(String name)
      Checks if the provided variable is immutable.
      Parameters:
      name - Name of the variable to check
      Returns:
      true if the variable is immutable
    • setImmutables

      public VariableContext setImmutables(Set<String> variableNames)
      Can be used to provide a set which contains the name of the variables which should be immutable.
      Parameters:
      variableNames - Set containing the names of the immutable variables
      Returns:
      same instance of VariableContext for daisy chaining.
      Throws:
      IllegalStateException - if the immutable set is already provided.
    • setImmutables

      public VariableContext setImmutables(String... variableNames)
      Can be used to provide an array of strings which contains the names of the variables which should be immutable. An immutable set will be created from the array.
      Parameters:
      variableNames - Set containing the names of the immutable variables
      Returns:
      same instance of VariableContext for daisy chaining.
      Throws:
      IllegalStateException - if the immutable set is already provided.
    • put

      public VariableContext put(String name, String value)
      Adds a variable with value to the context or overrides an already existing one. If the variable is already set and immutable an IllegalStateException is thrown.
      Parameters:
      name - Name of the variable to be added to the context
      value - Value of the variable
      Returns:
      same instance of VariableContext for daisy chaining.
      Throws:
      IllegalStateException - if the variable is immutable and already set
    • putOriginal

      public void putOriginal(String name, String value)
    • putConditional

      public VariableContext putConditional(String name, MappingRuleConditionalVariable variable)
      This method is used to add a conditional variable to the variable context.
      Parameters:
      name - Name of the variable
      variable - The conditional variable evaluator
      Returns:
      VariableContext for daisy chaining
    • get

      public String get(String name)
      Returns the value of a variable, null values are replaced with "".
      Parameters:
      name - Name of the variable
      Returns:
      The value of the variable
    • getOriginal

      public String getOriginal(String name)
    • putExtraDataset

      public void putExtraDataset(String name, Set<String> set)
      Adds a set to the context, each name can only be added once. The extra dataset is different from the regular variables because it cannot be referenced via tokens in the paths or any other input. However matchers and actions can explicitly access these datasets and can make decisions based on them.
      Parameters:
      name - Name which can be used to reference the collection
      set - The dataset to be stored
    • getExtraDataset

      public Set<String> getExtraDataset(String name)
      Returns the dataset referenced by the name.
      Parameters:
      name - Name of the set to be returned.
      Returns:
      the dataset referenced by the name.
    • containsKey

      public boolean containsKey(String name)
      Check if a variable is part of the context.
      Parameters:
      name - Name of the variable to be checked
      Returns:
      True if the variable is added to the context, false otherwise
    • replaceVariables

      public String replaceVariables(String input)
      This method replaces all variables in the provided string. The variables are reverse ordered by the length of their names in order to avoid partial replaces when a shorter named variable is a substring of a longer named variable. All variables will be replaced in the string. Null values will be considered as empty strings during the replace. If the input is null, null will be returned.
      Parameters:
      input - The string with variables
      Returns:
      A string with all the variables substituted with their respective values.
    • replacePathVariables

      public String replacePathVariables(String input)
      This method will consider the input as a queue path, which is a String separated by dot ('.') characters. The input will be split along the dots and all parts will be replaced individually. Replace only occur if a part exactly matches a variable name, no composite names or additional characters are supported. e.g. With variables %user and %default "%user.%default" will be substituted while "%user%default.something" won't. Null values will be considered as empty strings during the replace. If the input is null, null will be returned.
      Parameters:
      input - The string with variables
      Returns:
      A string with all the variable only path parts substituted with their respective values.