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