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.metrics.MetricRegistry;
030import org.apache.hadoop.hbase.regionserver.OnlineRegions;
031import org.apache.hadoop.hbase.regionserver.Region;
032import org.apache.hadoop.hbase.security.User;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.apache.yetus.audience.InterfaceStability;
035
036@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
037@InterfaceStability.Evolving
038public interface RegionCoprocessorEnvironment extends CoprocessorEnvironment<RegionCoprocessor> {
039  /** Returns the region associated with this coprocessor */
040  Region getRegion();
041
042  /** Returns region information for the region this coprocessor is running on */
043  RegionInfo getRegionInfo();
044
045  /** Returns Interface to Map of regions online on this RegionServer {@link #getServerName()}}. */
046  OnlineRegions getOnlineRegions();
047
048  /** Returns shared data between all instances of this coprocessor */
049  ConcurrentMap<String, Object> getSharedData();
050
051  /** Returns Hosting Server's ServerName */
052  ServerName getServerName();
053
054  /**
055   * Returns the hosts' Connection to the Cluster. <b>Do not close! This is a shared connection with
056   * the hosting server. Throws {@link UnsupportedOperationException} if you try to close or abort
057   * it</b>. For light-weight usage only. Heavy-duty usage will pull down the hosting RegionServer
058   * responsiveness as well as that of other Coprocessors making use of this Connection. Use to
059   * create table on start or to do administrative operations. Coprocessors should create their own
060   * Connections if heavy usage to avoid impinging on hosting Server operation. To create a
061   * Connection or if a Coprocessor requires a region with a particular Configuration, use
062   * {@link org.apache.hadoop.hbase.client.ConnectionFactory} or
063   * {@link #createConnection(Configuration)}}.
064   * <p>
065   * Be aware that operations that make use of this Connection are executed as the RegionServer
066   * User, the hbase super user that started this server process. Exercise caution running
067   * operations as this User (See {@link #createConnection(Configuration)}} to run as other than the
068   * RegionServer User).
069   * <p>
070   * Be careful RPC'ing from a Coprocessor context. RPC's will fail, stall, retry, and/or crawl
071   * because the remote side is not online, is struggling or it is on the other side of a network
072   * partition. Any use of Connection from inside a Coprocessor must be able to handle all such
073   * hiccups.
074   * <p>
075   * NOTE: the returned Connection is created using {@link User#getCurrent},so the Connection is as
076   * the System User who running the RegionServer, not the client User who initiate the RPC.
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   * <p>
100   * NOTE: the returned Connection is created using {@link User#getCurrent},so the Connection is as
101   * the System User who running the RegionServer, not the client User who initiate the RPC.
102   * @return Connection created using the passed conf.
103   */
104  Connection createConnection(Configuration conf) throws IOException;
105
106  /**
107   * Returns a MetricRegistry that can be used to track metrics at the region server level. All
108   * metrics tracked at this level will be shared by all the coprocessor instances of the same class
109   * in the same region server process. Note that there will be one region coprocessor environment
110   * per region in the server, but all of these instances will share the same MetricRegistry. The
111   * metric instances (like Counter, Timer, etc) will also be shared among all of the region
112   * coprocessor instances.
113   * <p>
114   * See ExampleRegionObserverWithMetrics class in the hbase-examples modules to see examples of how
115   * metrics can be instantiated and used.
116   * </p>
117   * @return A MetricRegistry for the coprocessor class to track and export metrics.
118   */
119  // Note: we are not exposing getMetricRegistryForRegion(). per-region metrics are already costly
120  // so we do not want to allow coprocessors to export metrics at the region level. We can allow
121  // getMetricRegistryForTable() to allow coprocessors to track metrics per-table, per-regionserver.
122  MetricRegistry getMetricRegistryForRegionServer();
123
124  /**
125   * Returns a CellBuilder so that coprocessors can build cells. These cells can also include tags.
126   * Note that this builder does not support updating seqId of the cells
127   * @return the RawCellBuilder
128   */
129  RawCellBuilder getCellBuilder();
130}