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