Class Constraints
Constraints can be added on table load time, via the HTableDescriptor.
NOTE: this class is NOT thread safe. Concurrent setting/enabling/disabling of constraints can cause constraints to be run at incorrect times or not at all.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Patternprivate static final Stringprivate static final Comparator<Constraint>private static Stringprivate static final intprivate static final Stringprivate static final org.slf4j.Loggerprivate static final longprivate static final Stringprivate static final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidadd(HTableDescriptor desc, Class<? extends Constraint>... constraints) Add configuration-less constraints to the table.static voidadd(HTableDescriptor desc, Class<? extends Constraint> constraint, org.apache.hadoop.conf.Configuration conf) Add aConstraintto the table with the given configurationstatic voidadd(HTableDescriptor desc, Pair<Class<? extends Constraint>, org.apache.hadoop.conf.Configuration>... constraints) Add constraints and their associated configurations to the table.private static voidaddConstraint(HTableDescriptor desc, Class<? extends Constraint> clazz, org.apache.hadoop.conf.Configuration conf, long priority) Write the raw constraint and configuration to the descriptor.private static voidchangeConstraintEnabled(HTableDescriptor desc, Class<? extends Constraint> clazz, boolean enabled) Change the whether the constraint (if it is already present) is enabled or disabled.private static org.apache.hadoop.conf.Configurationconfigure(org.apache.hadoop.conf.Configuration conf, boolean enabled, long priority) Setup the configuration for a constraint as to whether it is enabled and its priority on which to base the new configuration true if it should be run relative to other constraintsstatic voiddisable(HTableDescriptor desc) Turn off processing constraints for a given table, even if constraints have been turned on or added.static voiddisableConstraint(HTableDescriptor desc, Class<? extends Constraint> clazz) Disable the givenConstraint.static voidenable(HTableDescriptor desc) Enable constraints on a table.static voidenableConstraint(HTableDescriptor desc, Class<? extends Constraint> clazz) Enable the givenConstraint.static booleanenabled(HTableDescriptor desc, Class<? extends Constraint> clazz) Check to see if the given constraint is enabled.(package private) static List<? extends Constraint>getConstraints(TableDescriptor desc, ClassLoader classloader) Get the constraints stored in the table descriptor To read from To use when loading classes.getKeyValueForClass(HTableDescriptor desc, Class<? extends Constraint> clazz) Get the kvMap.Entryin the descriptor for the specified classprivate static longstatic booleanhas(HTableDescriptor desc, Class<? extends Constraint> clazz) Check to see if the Constraint is currently set.private static org.apache.hadoop.conf.ConfigurationreadConfiguration(byte[] bytes) Read theConfigurationstored in the byte stream.private static org.apache.hadoop.conf.ConfigurationreadConfiguration(String bytes) Read in the configuration from the String encoded configuration to read fromstatic voidremove(HTableDescriptor desc) Remove allConstraintsthat have been added to the table and turn off the constraint processing.static voidremove(HTableDescriptor desc, Class<? extends Constraint> clazz) Remove the constraint (and associated information) for the table descriptor.private static StringserializeConfiguration(org.apache.hadoop.conf.Configuration conf) Write the configuration to a String to writeprivate static StringserializeConstraintClass(Class<? extends Constraint> clazz) Just write the class to a String representation of the class as a key for theHTableDescriptorConstraint class to convert to aHTableDescriptorkeystatic voidsetConfiguration(HTableDescriptor desc, Class<? extends Constraint> clazz, org.apache.hadoop.conf.Configuration configuration) Update the configuration for theConstraint; does not change the order in which the constraint is run.private static voidupdateLatestPriority(HTableDescriptor desc, long priority) private static voidwriteConstraint(HTableDescriptor desc, String key, org.apache.hadoop.conf.Configuration conf) Write the given key and associated configuration to theHTableDescriptor
-
Field Details
-
DEFAULT_PRIORITY
- See Also:
-
LOG
-
CONSTRAINT_HTD_KEY_PREFIX
- See Also:
-
CONSTRAINT_HTD_ATTR_KEY_PATTERN
-
ENABLED_KEY
- See Also:
-
PRIORITY_KEY
- See Also:
-
MIN_PRIORITY
- See Also:
-
UNSET_PRIORITY
- See Also:
-
COUNTER_KEY
-
constraintComparator
-
-
Constructor Details
-
Constraints
private Constraints()
-
-
Method Details
-
enable
Enable constraints on a table.Currently, if you attempt to add a constraint to the table, then Constraints will automatically be turned on. table description to add the processor If the
ConstraintProcessorCP couldn't be added to the table.- Throws:
IOException
-
disable
Turn off processing constraints for a given table, even if constraints have been turned on or added.HTableDescriptorwhere to disableConstraints. -
remove
Remove allConstraintsthat have been added to the table and turn off the constraint processing.All
Configurationsand their associatedConstraintare removed.- Parameters:
desc-HTableDescriptorto removeConstraintsfrom.
-
has
Check to see if the Constraint is currently set.HTableDescriptorto checkConstraintclass to check for.- Returns:
- true if the
Constraintis present, even if it is disabled. false otherwise.
-
getKeyValueForClass
private static Pair<String,String> getKeyValueForClass(HTableDescriptor desc, Class<? extends Constraint> clazz) Get the kvMap.Entryin the descriptor for the specified class- Parameters:
desc-HTableDescriptorto readclazz- To search for- Returns:
- The
Pairof <key, value> in the table, if that class is present.NULLotherwise.
-
add
public static void add(HTableDescriptor desc, Class<? extends Constraint>... constraints) throws IOException Add configuration-less constraints to the table.This will overwrite any configuration associated with the previous constraint of the same class.
Each constraint, when added to the table, will have a specific priority, dictating the order in which the
Constraintwill be run. AConstraintearlier in the list will be run before those later in the list. The same logic applies between two Constraints over time (earlier added is run first on the regionserver).HTableDescriptorto addConstraintsConstraintsto add. All constraints are considered automatically enabled on add If constraint could not be serialized/added to table- Throws:
IOException
-
add
public static void add(HTableDescriptor desc, Pair<Class<? extends Constraint>, org.apache.hadoop.conf.Configuration>... constraints) throws IOExceptionAdd constraints and their associated configurations to the table.Adding the same constraint class twice will overwrite the first constraint's configuration
Each constraint, when added to the table, will have a specific priority, dictating the order in which the
Constraintwill be run. AConstraintearlier in the list will be run before those later in the list. The same logic applies between two Constraints over time (earlier added is run first on the regionserver).HTableDescriptorto add aConstraintPairof aConstraintand its associatedConfiguration. The Constraint will be configured on load with the specified configuration.All constraints are considered automatically enabled on add if any constraint could not be deserialized. Assumes if 1 constraint is not loaded properly, something has gone terribly wrong and that all constraints need to be enforced.- Throws:
IOException
-
add
public static void add(HTableDescriptor desc, Class<? extends Constraint> constraint, org.apache.hadoop.conf.Configuration conf) throws IOException Add aConstraintto the table with the given configurationEach constraint, when added to the table, will have a specific priority, dictating the order in which the
Constraintwill be run. AConstraintadded will run on the regionserver before those added to theHTableDescriptorlater. table descriptor to the constraint to to be added configuration associated with the constraint if any constraint could not be deserialized. Assumes if 1 constraint is not loaded properly, something has gone terribly wrong and that all constraints need to be enforced.- Throws:
IOException
-
addConstraint
private static void addConstraint(HTableDescriptor desc, Class<? extends Constraint> clazz, org.apache.hadoop.conf.Configuration conf, long priority) throws IOException Write the raw constraint and configuration to the descriptor.This method takes care of creating a new configuration based on the passed in configuration and then updating that with enabled and priority of the constraint.
When a constraint is added, it is automatically enabled.
- Throws:
IOException
-
configure
private static org.apache.hadoop.conf.Configuration configure(org.apache.hadoop.conf.Configuration conf, boolean enabled, long priority) Setup the configuration for a constraint as to whether it is enabled and its priority on which to base the new configuration true if it should be run relative to other constraints- Returns:
- a new configuration, storable in the
HTableDescriptor
-
serializeConstraintClass
Just write the class to a String representation of the class as a key for theHTableDescriptorConstraint class to convert to aHTableDescriptorkey- Returns:
- key to store in the
HTableDescriptor
-
writeConstraint
private static void writeConstraint(HTableDescriptor desc, String key, org.apache.hadoop.conf.Configuration conf) throws IOException Write the given key and associated configuration to theHTableDescriptor- Throws:
IOException
-
serializeConfiguration
private static String serializeConfiguration(org.apache.hadoop.conf.Configuration conf) throws IOException Write the configuration to a String to write- Returns:
- String representation of that configuration
- Throws:
IOException
-
readConfiguration
private static org.apache.hadoop.conf.Configuration readConfiguration(byte[] bytes) throws IOException Read theConfigurationstored in the byte stream. to read from- Returns:
- A valid configuration
- Throws:
IOException
-
readConfiguration
private static org.apache.hadoop.conf.Configuration readConfiguration(String bytes) throws IOException Read in the configuration from the String encoded configuration to read from- Returns:
- A valid configuration if the configuration could not be read
- Throws:
IOException
-
getNextPriority
-
updateLatestPriority
-
setConfiguration
public static void setConfiguration(HTableDescriptor desc, Class<? extends Constraint> clazz, org.apache.hadoop.conf.Configuration configuration) throws IOException, IllegalArgumentException Update the configuration for theConstraint; does not change the order in which the constraint is run.HTableDescriptorto updateConstraintto update to update theConstraintwith. if the Constraint was not stored correctly if the Constraint was not present on this table.- Throws:
IOExceptionIllegalArgumentException
-
remove
Remove the constraint (and associated information) for the table descriptor.HTableDescriptorto modifyConstraintclass to remove -
enableConstraint
public static void enableConstraint(HTableDescriptor desc, Class<? extends Constraint> clazz) throws IOException Enable the givenConstraint. Retains all the information (e.g. Configuration) for theConstraint, but makes sure that it gets loaded on the table.HTableDescriptorto modifyConstraintto enable If the constraint cannot be properly deserialized- Throws:
IOException
-
disableConstraint
public static void disableConstraint(HTableDescriptor desc, Class<? extends Constraint> clazz) throws IOException Disable the givenConstraint. Retains all the information (e.g. Configuration) for theConstraint, but it just doesn't load theConstrainton the table.HTableDescriptorto modifyConstraintto disable. if the constraint cannot be found- Throws:
IOException
-
changeConstraintEnabled
private static void changeConstraintEnabled(HTableDescriptor desc, Class<? extends Constraint> clazz, boolean enabled) throws IOException Change the whether the constraint (if it is already present) is enabled or disabled.- Throws:
IOException
-
enabled
public static boolean enabled(HTableDescriptor desc, Class<? extends Constraint> clazz) throws IOException Check to see if the given constraint is enabled.HTableDescriptorto check.Constraintto check for- Returns:
- true if the
Constraintis present and enabled. false otherwise. If the constraint has improperly stored in the table - Throws:
IOException
-
getConstraints
static List<? extends Constraint> getConstraints(TableDescriptor desc, ClassLoader classloader) throws IOException Get the constraints stored in the table descriptor To read from To use when loading classes. If a special classloader is used on a region, for instance, then that should be the classloader used to load the constraints. This could also apply to unit-testing situation, where want to ensure that class is reloaded or not.- Returns:
- List of configured
Constraintsif any part of reading/arguments fails - Throws:
IOException
-