001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.coprocessor;
019
020import java.io.IOException;
021import java.util.concurrent.ConcurrentMap;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.CoprocessorEnvironment;
024import org.apache.hadoop.hbase.HBaseInterfaceAudience;
025import org.apache.hadoop.hbase.RawCellBuilder;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.client.Connection;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.client.Scan;
030import org.apache.hadoop.hbase.metrics.MetricRegistry;
031import org.apache.hadoop.hbase.quotas.OperationQuota;
032import org.apache.hadoop.hbase.quotas.RpcQuotaManager;
033import org.apache.hadoop.hbase.quotas.RpcThrottlingException;
034import org.apache.hadoop.hbase.regionserver.OnlineRegions;
035import org.apache.hadoop.hbase.regionserver.Region;
036import org.apache.yetus.audience.InterfaceAudience;
037import org.apache.yetus.audience.InterfaceStability;
038
039@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
040@InterfaceStability.Evolving
041public interface RegionCoprocessorEnvironment extends CoprocessorEnvironment<RegionCoprocessor> {
042  /** Returns the region associated with this coprocessor */
043  Region getRegion();
044
045  /** Returns region information for the region this coprocessor is running on */
046  RegionInfo getRegionInfo();
047
048  /** Returns Interface to Map of regions online on this RegionServer {@link #getServerName()}}. */
049  OnlineRegions getOnlineRegions();
050
051  /** Returns shared data between all instances of this coprocessor */
052  ConcurrentMap<String, Object> getSharedData();
053
054  /** Returns Hosting Server's ServerName */
055  ServerName getServerName();
056
057  /**
058   * Returns the hosts' Connection to the Cluster. <b>Do not close! This is a shared connection with
059   * the hosting server. Throws {@link UnsupportedOperationException} if you try to close or abort
060   * it</b>. For light-weight usage only. Heavy-duty usage will pull down the hosting RegionServer
061   * responsiveness as well as that of other Coprocessors making use of this Connection. Use to
062   * create table on start or to do administrative operations. Coprocessors should create their own
063   * Connections if heavy usage to avoid impinging on hosting Server operation. To create a
064   * Connection or if a Coprocessor requires a region with a particular Configuration, use
065   * {@link org.apache.hadoop.hbase.client.ConnectionFactory} or
066   * {@link #createConnection(Configuration)}}.
067   * <p>
068   * Be aware that operations that make use of this Connection are executed as the RegionServer
069   * User, the hbase super user that started this server process. Exercise caution running
070   * operations as this User (See {@link #createConnection(Configuration)}} to run as other than the
071   * RegionServer User).
072   * <p>
073   * Be careful RPC'ing from a Coprocessor context. RPC's will fail, stall, retry, and/or crawl
074   * because the remote side is not online, is struggling or it is on the other side of a network
075   * partition. Any use of Connection from inside a Coprocessor must be able to handle all such
076   * hiccups.
077   * @see #createConnection(Configuration)
078   * @return The host's Connection to the Cluster.
079   */
080  Connection getConnection();
081
082  /**
083   * Creates a cluster connection using the passed Configuration. Creating a Connection is a
084   * heavy-weight operation. The resultant Connection's cache of region locations will be empty.
085   * Therefore you should cache and reuse Connections rather than create a Connection on demand.
086   * Create on start of your Coprocessor. You will have to cast the CoprocessorEnvironment
087   * appropriately to get at this API at start time because Coprocessor start method is passed a
088   * subclass of this CoprocessorEnvironment or fetch Connection using a synchronized accessor
089   * initializing the Connection on first access. Close the returned Connection when done to free
090   * resources. Using this API rather than
091   * {@link org.apache.hadoop.hbase.client.ConnectionFactory#createConnection(Configuration)}
092   * returns a Connection that will short-circuit RPC if the target is a local resource. Use
093   * ConnectionFactory if you don't need this ability.
094   * <p>
095   * Be careful RPC'ing from a Coprocessor context. RPC's will fail, stall, retry, and/or crawl
096   * because the remote side is not online, is struggling or it is on the other side of a network
097   * partition. Any use of Connection from inside a Coprocessor must be able to handle all such
098   * hiccups.
099   * @return Connection created using the passed conf.
100   */
101  Connection createConnection(Configuration conf) throws IOException;
102
103  /**
104   * Returns a MetricRegistry that can be used to track metrics at the region server level. All
105   * metrics tracked at this level will be shared by all the coprocessor instances of the same class
106   * in the same region server process. Note that there will be one region coprocessor environment
107   * per region in the server, but all of these instances will share the same MetricRegistry. The
108   * metric instances (like Counter, Timer, etc) will also be shared among all of the region
109   * coprocessor instances.
110   * <p>
111   * See ExampleRegionObserverWithMetrics class in the hbase-examples modules to see examples of how
112   * metrics can be instantiated and used.
113   * </p>
114   * @return A MetricRegistry for the coprocessor class to track and export metrics.
115   */
116  // Note: we are not exposing getMetricRegistryForRegion(). per-region metrics are already costly
117  // so we do not want to allow coprocessors to export metrics at the region level. We can allow
118  // getMetricRegistryForTable() to allow coprocessors to track metrics per-table, per-regionserver.
119  MetricRegistry getMetricRegistryForRegionServer();
120
121  /**
122   * Returns a CellBuilder so that coprocessors can build cells. These cells can also include tags.
123   * Note that this builder does not support updating seqId of the cells
124   * @return the RawCellBuilder
125   */
126  RawCellBuilder getCellBuilder();
127
128  /**
129   * Returns an RpcQuotaManager that can be used to apply quota checks against the workloads
130   * generated by the coprocessor.
131   * @return the RpcQuotaManager
132   */
133  RpcQuotaManager getRpcQuotaManager();
134
135  /**
136   * Check the quota for the current (rpc-context) user. Returns the OperationQuota used to get the
137   * available quota and to report the data/usage of the operation.
138   * @param scan                            the scan to be estimated against the quota
139   * @param maxBlockBytesScanned            the maximum bytes scanned in a single RPC call by the
140   *                                        scanner
141   * @param prevBlockBytesScannedDifference the difference between BBS of the previous two next
142   *                                        calls
143   * @return the OperationQuota
144   * @throws RpcThrottlingException if the operation cannot be executed due to quota exceeded.
145   */
146  OperationQuota checkScanQuota(Scan scan, long maxBlockBytesScanned,
147    long prevBlockBytesScannedDifference) throws IOException, RpcThrottlingException;
148
149  /**
150   * Check the quota for the current (rpc-context) user. Returns the OperationQuota used to get the
151   * available quota and to report the data/usage of the operation. This method does not support
152   * scans because estimating a scan's workload is more complicated than estimating the workload of
153   * a get/put.
154   * @param region the region where the operation will be performed
155   * @param type   the operation type
156   * @return the OperationQuota
157   * @throws RpcThrottlingException if the operation cannot be executed due to quota exceeded.
158   */
159  OperationQuota checkBatchQuota(final Region region, final OperationQuota.OperationType type)
160    throws IOException, RpcThrottlingException;
161
162  /**
163   * Check the quota for the current (rpc-context) user. Returns the OperationQuota used to get the
164   * available quota and to report the data/usage of the operation. This method does not support
165   * scans because estimating a scan's workload is more complicated than estimating the workload of
166   * a get/put.
167   * @param region    the region where the operation will be performed
168   * @param numWrites number of writes to count against quota
169   * @param numReads  number of reads to count against quota
170   * @return the OperationQuota
171   * @throws RpcThrottlingException if the operation cannot be executed due to quota exceeded.
172   */
173  OperationQuota checkBatchQuota(final Region region, int numWrites, int numReads)
174    throws IOException, RpcThrottlingException;
175}