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.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.coprocessor.BaseRegionObserver;
31  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
32  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
33  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
34  
35  /***
36   * Processes multiple {@link Constraint Constraints} on a given table.
37   * <p>
38   * This is an ease of use mechanism - all the functionality here could be
39   * implemented on any given system by a coprocessor.
40   */
41  @InterfaceAudience.Private
42  public class ConstraintProcessor extends BaseRegionObserver {
43  
44    private static final Log LOG = LogFactory.getLog(ConstraintProcessor.class);
45  
46    private final ClassLoader classloader;
47  
48    private List<? extends Constraint> constraints = new ArrayList<Constraint>();
49  
50    /**
51     * Create the constraint processor.
52     * <p>
53     * Stores the current classloader.
54     */
55    public ConstraintProcessor() {
56      classloader = this.getClass().getClassLoader();
57    }
58  
59    @Override
60    public void start(CoprocessorEnvironment environment) {
61      // make sure we are on a region server
62      if (!(environment instanceof RegionCoprocessorEnvironment)) {
63        throw new IllegalArgumentException(
64            "Constraints only act on regions - started in an environment that was not a region");
65      }
66      RegionCoprocessorEnvironment env = (RegionCoprocessorEnvironment) environment;
67      HTableDescriptor desc = env.getRegion().getTableDesc();
68      // load all the constraints from the HTD
69      try {
70        this.constraints = Constraints.getConstraints(desc, classloader);
71      } catch (IOException e) {
72        throw new IllegalArgumentException(e);
73      }
74  
75      if (LOG.isInfoEnabled()) {
76        LOG.info("Finished loading " + constraints.size()
77            + " user Constraints on table: " + new String(desc.getName()));
78      }
79  
80    }
81  
82    @Override
83    public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put,
84        WALEdit edit, boolean writeToWAL) throws IOException {
85      // check the put against the stored constraints
86      for (Constraint c : constraints) {
87        c.check(put);
88      }
89      // if we made it here, then the Put is valid
90    }
91  }