View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.constraint;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.CoprocessorEnvironment;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.client.Put;
30  import org.apache.hadoop.hbase.client.Durability;
31  import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
32  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
33  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
34  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
35  
36  /***
37   * Processes multiple {@link Constraint Constraints} on a given table.
38   * <p>
39   * This is an ease of use mechanism - all the functionality here could be
40   * implemented on any given system by a coprocessor.
41   */
42  @InterfaceAudience.Private
43  public class ConstraintProcessor extends BaseRegionObserver {
44  
45    private static final Log LOG = LogFactory.getLog(ConstraintProcessor.class);
46  
47    private final ClassLoader classloader;
48  
49    private List<? extends Constraint> constraints = new ArrayList<Constraint>();
50  
51    /**
52     * Create the constraint processor.
53     * <p>
54     * Stores the current classloader.
55     */
56    public ConstraintProcessor() {
57      classloader = this.getClass().getClassLoader();
58    }
59  
60    @Override
61    public void start(CoprocessorEnvironment environment) {
62      // make sure we are on a region server
63      if (!(environment instanceof RegionCoprocessorEnvironment)) {
64        throw new IllegalArgumentException(
65            "Constraints only act on regions - started in an environment that was not a region");
66      }
67      RegionCoprocessorEnvironment env = (RegionCoprocessorEnvironment) environment;
68      HTableDescriptor desc = env.getRegion().getTableDesc();
69      // load all the constraints from the HTD
70      try {
71        this.constraints = Constraints.getConstraints(desc, classloader);
72      } catch (IOException e) {
73        throw new IllegalArgumentException(e);
74      }
75  
76      if (LOG.isInfoEnabled()) {
77        LOG.info("Finished loading " + constraints.size()
78            + " user Constraints on table: " + desc.getTableName());
79      }
80  
81    }
82  
83    @Override
84    public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put,
85        WALEdit edit, Durability durability) throws IOException {
86      // check the put against the stored constraints
87      for (Constraint c : constraints) {
88        c.check(put);
89      }
90      // if we made it here, then the Put is valid
91    }
92  }