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
Modifier and TypeFieldDescriptionprivate static final Pattern
private static final String
private static final Comparator<Constraint>
private static String
private static final int
private static final String
private static final org.slf4j.Logger
private static final long
private static final String
private static final long
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
add
(HTableDescriptor desc, Class<? extends Constraint>... constraints) Add configuration-less constraints to the table.static void
add
(HTableDescriptor desc, Class<? extends Constraint> constraint, org.apache.hadoop.conf.Configuration conf) Add aConstraint
to the table with the given configurationstatic void
add
(HTableDescriptor desc, Pair<Class<? extends Constraint>, org.apache.hadoop.conf.Configuration>... constraints) Add constraints and their associated configurations to the table.private static void
addConstraint
(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 void
changeConstraintEnabled
(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.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 constraintsstatic void
disable
(HTableDescriptor desc) Turn off processing constraints for a given table, even if constraints have been turned on or added.static void
disableConstraint
(HTableDescriptor desc, Class<? extends Constraint> clazz) Disable the givenConstraint
.static void
enable
(HTableDescriptor desc) Enable constraints on a table.static void
enableConstraint
(HTableDescriptor desc, Class<? extends Constraint> clazz) Enable the givenConstraint
.static boolean
enabled
(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.Entry
in the descriptor for the specified classprivate static long
static boolean
has
(HTableDescriptor desc, Class<? extends Constraint> clazz) Check to see if the Constraint is currently set.private static org.apache.hadoop.conf.Configuration
readConfiguration
(byte[] bytes) Read theConfiguration
stored in the byte stream.private static org.apache.hadoop.conf.Configuration
readConfiguration
(String bytes) Read in the configuration from the String encoded configuration to read fromstatic void
remove
(HTableDescriptor desc) Remove allConstraints
that have been added to the table and turn off the constraint processing.static void
remove
(HTableDescriptor desc, Class<? extends Constraint> clazz) Remove the constraint (and associated information) for the table descriptor.private static String
serializeConfiguration
(org.apache.hadoop.conf.Configuration conf) Write the configuration to a String to writeprivate static String
serializeConstraintClass
(Class<? extends Constraint> clazz) Just write the class to a String representation of the class as a key for theHTableDescriptor
Constraint class to convert to aHTableDescriptor
keystatic void
setConfiguration
(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 void
updateLatestPriority
(HTableDescriptor desc, long priority) private static void
writeConstraint
(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
ConstraintProcessor
CP 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.HTableDescriptor
where to disableConstraints
. -
remove
Remove allConstraints
that have been added to the table and turn off the constraint processing.All
Configurations
and their associatedConstraint
are removed.- Parameters:
desc
-HTableDescriptor
to removeConstraints
from.
-
has
Check to see if the Constraint is currently set.HTableDescriptor
to checkConstraint
class to check for.- Returns:
- true if the
Constraint
is present, even if it is disabled. false otherwise.
-
getKeyValueForClass
private static Pair<String,String> getKeyValueForClass(HTableDescriptor desc, Class<? extends Constraint> clazz) Get the kvMap.Entry
in the descriptor for the specified class- Parameters:
desc
-HTableDescriptor
to readclazz
- To search for- Returns:
- The
Pair
of <key, value> in the table, if that class is present.NULL
otherwise.
-
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
Constraint
will be run. AConstraint
earlier 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).HTableDescriptor
to addConstraints
Constraints
to 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
Constraint
will be run. AConstraint
earlier 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).HTableDescriptor
to add aConstraint
Pair
of aConstraint
and 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 aConstraint
to the table with the given configurationEach constraint, when added to the table, will have a specific priority, dictating the order in which the
Constraint
will be run. AConstraint
added will run on the regionserver before those added to theHTableDescriptor
later. 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 theHTableDescriptor
Constraint class to convert to aHTableDescriptor
key- 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 theConfiguration
stored 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.HTableDescriptor
to updateConstraint
to update to update theConstraint
with. if the Constraint was not stored correctly if the Constraint was not present on this table.- Throws:
IOException
IllegalArgumentException
-
remove
Remove the constraint (and associated information) for the table descriptor.HTableDescriptor
to modifyConstraint
class 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.HTableDescriptor
to modifyConstraint
to 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 theConstraint
on the table.HTableDescriptor
to modifyConstraint
to 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.HTableDescriptor
to check.Constraint
to check for- Returns:
- true if the
Constraint
is 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
Constraints
if any part of reading/arguments fails - Throws:
IOException
-