Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.hadoop.hbase.constraint;
 
 import java.util.List;
 import java.util.Map;
 
Utilities for adding/removing constraints from a table.

Constraints can be added on table load time, via the org.apache.hadoop.hbase.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.

 
 public final class Constraints {
   private static final int DEFAULT_PRIORITY = -1;
 
   private Constraints() {
   }
 
   private static final Log LOG = LogFactory.getLog(Constraints.class);
   private static final String CONSTRAINT_HTD_KEY_PREFIX = "constraint $";
   private static final Pattern CONSTRAINT_HTD_ATTR_KEY_PATTERN = Pattern
 
   // configuration key for if the constraint is enabled
   private static final String ENABLED_KEY = "_ENABLED";
   // configuration key for the priority
   private static final String PRIORITY_KEY = "_PRIORITY";
 
   // smallest priority a constraiNt can have
   private static final long MIN_PRIORITY = 0L;
   // ensure a priority less than the smallest we could intentionally set
   private static final long UNSET_PRIORITY =  - 1;
 
   private static String COUNTER_KEY = "hbase.constraint.counter";

  
Enable constraints on a table.

Currently, if you attempt to add a constraint to the table, then Constraints will automatically be turned on.

Parameters:
desc table description to add the processor
Throws:
java.io.IOException If the ConstraintProcessor CP couldn't be added to the table.
 
   public static void enable(HTableDescriptor descthrows IOException {
     // if the CP has already been loaded, do nothing
     String clazz = ConstraintProcessor.class.getName();
     if (desc.hasCoprocessor(clazz)) {
       return;
     }
 
     // add the constrain processor CP to the table
     desc.addCoprocessor(clazz);
   }

  
Turn off processing constraints for a given table, even if constraints have been turned on or added.

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor where to disable Constraints.
  public static void disable(HTableDescriptor desc) {
  }

  
Remove all Constraints that have been added to the table and turn off the constraint processing.

All Configurations and their associated Constraint are removed.

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to remove Constraints from.
  public static void remove(HTableDescriptor desc) {
    // disable constraints
    disable(desc);
    // remove all the constraint settings
    // loop through all the key, values looking for constraints
        .getValues().entrySet()) {
      String key = Bytes.toString((e.getKey().get()));
      String[] className = .split(key);
      if (className.length == 2) {
        keys.add(e.getKey());
      }
    }
    // now remove all the keys we found
    for (ImmutableBytesWritable key : keys) {
      desc.remove(key);
    }
  }

  
Check to see if the Constraint is currently set.

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to check
clazz Constraint class to check for.
Returns:
true if the Constraint is present, even if it is disabled. false otherwise.
  public static boolean has(HTableDescriptor desc,
      Class<? extends Constraintclazz) {
    return getKeyValueForClass(descclazz) != null;
  }

  
Get the kv java.util.Map.Entry in the descriptor for the specified class

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to read
clazz to search for
Returns:
the org.apache.hadoop.hbase.util.Pair of <key, value> in the table, if that class is present. null otherwise.
  private static Pair<StringStringgetKeyValueForClass(
      HTableDescriptor descClass<? extends Constraintclazz) {
    // get the serialized version of the constraint
    String key = serializeConstraintClass(clazz);
    String value = desc.getValue(key);
    return value == null ? null : new Pair<StringString>(keyvalue);
  }

  
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. A Constraint 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).

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to add Constraints
constraints Constraints to add. All constraints are considered automatically enabled on add
Throws:
java.io.IOException If constraint could not be serialized/added to table
  public static void add(HTableDescriptor desc,
      Class<? extends Constraint>... constraintsthrows IOException {
    // make sure constraints are enabled
    enable(desc);
    long priority = getNextPriority(desc);
    // store each constraint
    for (Class<? extends Constraintclazz : constraints) {
      addConstraint(descclazznullpriority++);
    }
    updateLatestPriority(descpriority);
  }

  
Add 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. A Constraint 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).

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to add a Constraint
constraints org.apache.hadoop.hbase.util.Pair of a Constraint and its associated org.apache.hadoop.conf.Configuration. The Constraint will be configured on load with the specified configuration.All constraints are considered automatically enabled on add
Throws:
java.io.IOException 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.
  public static void add(HTableDescriptor desc,
      Pair<Class<? extends Constraint>, Configuration>... constraints)
      throws IOException {
    enable(desc);
    long priority = getNextPriority(desc);
    for (Pair<Class<? extends Constraint>, Configurationpair : constraints) {
      addConstraint(descpair.getFirst(), pair.getSecond(), priority++);
    }
    updateLatestPriority(descpriority);
  }

  
Add a Constraint to the table with the given configuration

Each constraint, when added to the table, will have a specific priority, dictating the order in which the Constraint will be run. A Constraint added will run on the regionserver before those added to the org.apache.hadoop.hbase.HTableDescriptor later.

Parameters:
desc table descriptor to the constraint to
constraint to be added
conf configuration associated with the constraint
Throws:
java.io.IOException 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.
  public static void add(HTableDescriptor desc,
      Class<? extends ConstraintconstraintConfiguration conf)
      throws IOException {
    enable(desc);
    long priority = getNextPriority(desc);
    addConstraint(descconstraintconfpriority++);
    updateLatestPriority(descpriority);
  }

  
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.

  private static void addConstraint(HTableDescriptor desc,
      Class<? extends ConstraintclazzConfiguration conflong priority)
      throws IOException {
        configure(conftruepriority));
  }

  
Setup the configuration for a constraint as to whether it is enabled and its priority

Parameters:
conf on which to base the new configuration
enabled true if it should be run
priority relative to other constraints
Returns:
a new configuration, storable in the org.apache.hadoop.hbase.HTableDescriptor
  private static Configuration configure(Configuration confboolean enabled,
      long priority) {
    // create the configuration to actually be stored
    // clone if possible, but otherwise just create an empty configuration
    Configuration toWrite = conf == null ? new Configuration()
        : new Configuration(conf);
    // update internal properties
    toWrite.setBooleanIfUnset(enabled);
    // set if unset long
    if (toWrite.getLong() == ) {
      toWrite.setLong(priority);
    }
    return toWrite;
  }

  
Just write the class to a String representation of the class as a key for the org.apache.hadoop.hbase.HTableDescriptor

Parameters:
clazz Constraint class to convert to a org.apache.hadoop.hbase.HTableDescriptor key
Returns:
key to store in the org.apache.hadoop.hbase.HTableDescriptor
  private static String serializeConstraintClass(
      Class<? extends Constraintclazz) {
    String constraintClazz = clazz.getName();
    return  + constraintClazz;
  }

  
Write the given key and associated configuration to the org.apache.hadoop.hbase.HTableDescriptor
  private static void writeConstraint(HTableDescriptor descString key,
      Configuration confthrows IOException {
    // store the key and conf in the descriptor
    desc.setValue(keyserializeConfiguration(conf));
  }

  
Write the configuration to a String

Parameters:
conf to write
Returns:
String representation of that configuration
Throws:
java.io.IOException
  private static String serializeConfiguration(Configuration conf)
      throws IOException {
    // write the configuration out to the data stream
    DataOutputStream dos = new DataOutputStream(bos);
    conf.writeXml(dos);
    dos.flush();
    byte[] data = bos.toByteArray();
    return Bytes.toString(data);
  }

  
Read the org.apache.hadoop.conf.Configuration stored in the byte stream.

Parameters:
bytes to read from
Returns:
A valid configuration
  private static Configuration readConfiguration(byte[] bytes)
      throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    Configuration conf = new Configuration(false);
    conf.addResource(is);
    return conf;
  }

  
Read in the configuration from the String encoded configuration

Parameters:
bytes to read from
Returns:
A valid configuration
Throws:
java.io.IOException if the configuration could not be read
  private static Configuration readConfiguration(String bytes)
      throws IOException {
    return readConfiguration(Bytes.toBytes(bytes));
  }
  private static long getNextPriority(HTableDescriptor desc) {
    String value = desc.getValue();
    long priority;
    // get the current priority
    if (value == null) {
      priority = ;
    } else {
      priority = Long.parseLong(value) + 1;
    }
    return priority;
  }
  private static void updateLatestPriority(HTableDescriptor desclong priority) {
    // update the max priority
    desc.setValue(, Long.toString(priority));
  }

  
Update the configuration for the Constraint; does not change the order in which the constraint is run.

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to update
clazz Constraint to update
configuration to update the Constraint with.
Throws:
java.io.IOException if the Constraint was not stored correctly
java.lang.IllegalArgumentException if the Constraint was not present on this table.
  public static void setConfiguration(HTableDescriptor desc,
      Class<? extends ConstraintclazzConfiguration configuration)
      throws IOExceptionIllegalArgumentException {
    // get the entry for this class
    Pair<StringStringe = getKeyValueForClass(descclazz);
    if (e == null) {
      throw new IllegalArgumentException("Constraint: " + clazz.getName()
          + " is not associated with this table.");
    }
    // clone over the configuration elements
    Configuration conf = new Configuration(configuration);
    // read in the previous info about the constraint
    Configuration internal = readConfiguration(e.getSecond());
    // update the fields based on the previous settings
    conf.setIfUnset(internal.get());
    conf.setIfUnset(internal.get());
    // update the current value
    writeConstraint(desce.getFirst(), conf);
  }

  
Remove the constraint (and associated information) for the table descriptor.

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to modify
clazz Constraint class to remove
  public static void remove(HTableDescriptor desc,
      Class<? extends Constraintclazz) {
    String key = serializeConstraintClass(clazz);
    desc.remove(key);
  }

  
Enable the given Constraint. Retains all the information (e.g. Configuration) for the Constraint, but makes sure that it gets loaded on the table.

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to modify
clazz Constraint to enable
Throws:
java.io.IOException If the constraint cannot be properly deserialized
  public static void enableConstraint(HTableDescriptor desc,
      Class<? extends Constraintclazzthrows IOException {
    changeConstraintEnabled(descclazztrue);
  }

  
Disable the given Constraint. Retains all the information (e.g. Configuration) for the Constraint, but it just doesn't load the Constraint on the table.

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to modify
clazz Constraint to disable.
Throws:
java.io.IOException if the constraint cannot be found
  public static void disableConstraint(HTableDescriptor desc,
      Class<? extends Constraintclazzthrows IOException {
    changeConstraintEnabled(descclazzfalse);
  }

  
Change the whether the constraint (if it is already present) is enabled or disabled.
  private static void changeConstraintEnabled(HTableDescriptor desc,
      Class<? extends Constraintclazzboolean enabledthrows IOException {
    // get the original constraint
    Pair<StringStringentry = getKeyValueForClass(descclazz);
    if (entry == null) {
      throw new IllegalArgumentException("Constraint: " + clazz.getName()
          + " is not associated with this table. You can't enable it!");
    }
    // create a new configuration from that conf
    Configuration conf = readConfiguration(entry.getSecond());
    // set that it is enabled
    conf.setBoolean(enabled);
    // write it back out
    writeConstraint(descentry.getFirst(), conf);
  }

  
Check to see if the given constraint is enabled.

Parameters:
desc org.apache.hadoop.hbase.HTableDescriptor to check.
clazz Constraint to check for
Returns:
true if the Constraint is present and enabled. false otherwise.
Throws:
java.io.IOException If the constraint has improperly stored in the table
  public static boolean enabled(HTableDescriptor desc,
      Class<? extends Constraintclazzthrows IOException {
    // get the kv
    Pair<StringStringentry = getKeyValueForClass(descclazz);
    // its not enabled so just return false. In fact, its not even present!
    if (entry == null) {
      return false;
    }
    // get the info about the constraint
    Configuration conf = readConfiguration(entry.getSecond());
    return conf.getBoolean(false);
  }

  
Get the constraints stored in the table descriptor

Parameters:
desc To read from
classloader 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
Throws:
java.io.IOException if any part of reading/arguments fails
  static List<? extends ConstraintgetConstraints(HTableDescriptor desc,
      ClassLoader classloaderthrows IOException {
    List<Constraintconstraints = new ArrayList<Constraint>();
    // loop through all the key, values looking for constraints
        .getValues().entrySet()) {
      // read out the constraint
      String key = Bytes.toString(e.getKey().get()).trim();
      String[] className = .split(key);
      if (className.length == 2) {
        key = className[1];
        if (.isDebugEnabled()) {
          .debug("Loading constraint:" + key);
        }
        // read in the rest of the constraint
        Configuration conf;
        try {
          conf = readConfiguration(e.getValue().get());
        } catch (IOException e1) {
          // long that we don't have a valid configuration stored, and move on.
          .warn("Corrupted configuration found for key:" + key
              + ",  skipping it.");
          continue;
        }
        // if it is not enabled, skip it
        if (!conf.getBoolean(false)) {
          if (.isDebugEnabled())
            .debug("Constraint: " + key + " is DISABLED - skipping it");
          // go to the next constraint
          continue;
        }
        try {
          // add the constraint, now that we expect it to be valid.
          Class<? extends Constraintclazz = classloader.loadClass(key)
              .asSubclass(Constraint.class);
          Constraint constraint = clazz.newInstance();
          constraint.setConf(conf);
          constraints.add(constraint);
        } catch (ClassNotFoundException e1) {
          throw new IOException(e1);
        } catch (InstantiationException e1) {
          throw new IOException(e1);
        } catch (IllegalAccessException e1) {
          throw new IOException(e1);
        }
      }
    }
    // sort them, based on the priorities
    Collections.sort(constraints);
    return constraints;
  }
  private static final Comparator<ConstraintconstraintComparator = new Comparator<Constraint>() {
    @Override
    public int compare(Constraint c1Constraint c2) {
      // compare the priorities of the constraints stored in their configuration
      return Long.valueOf(c1.getConf().getLong())
    }
  };
New to GrepCode? Check out our FAQ X