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 */
019package org.apache.hadoop.hbase.coprocessor;
020
021import java.io.IOException;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.CoprocessorEnvironment;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.client.Connection;
028import org.apache.hadoop.hbase.metrics.MetricRegistry;
029import org.apache.hadoop.hbase.regionserver.OnlineRegions;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.apache.yetus.audience.InterfaceStability;
032
033@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
034@InterfaceStability.Evolving
035public interface RegionServerCoprocessorEnvironment
036    extends CoprocessorEnvironment<RegionServerCoprocessor> {
037  /**
038   * @return Hosting Server's ServerName
039   */
040  ServerName getServerName();
041
042  /**
043   * @return Interface to Map of regions online on this RegionServer {@link #getServerName()}}.
044   */
045  OnlineRegions getOnlineRegions();
046
047  /**
048   * Returns the hosts' Connection to the Cluster. <b>Do not close! This is a shared connection
049   * with the hosting server. Throws {@link UnsupportedOperationException} if you try to close
050   * or abort it</b>.
051   *
052   * For light-weight usage only. Heavy-duty usage will pull down
053   * the hosting RegionServer responsiveness as well as that of other Coprocessors making use of
054   * this Connection. Use to create table on start or to do administrative operations. Coprocessors
055   * should create their own Connections if heavy usage to avoid impinging on hosting Server
056   * operation. To create a Connection or if a Coprocessor requires a region with a particular
057   * Configuration, use {@link org.apache.hadoop.hbase.client.ConnectionFactory} or
058   * {@link #createConnection(Configuration)}}.
059   *
060   * <p>Be aware that operations that make use of this Connection are executed as the RegionServer
061   * User, the hbase super user that started this server process. Exercise caution running
062   * operations as this User (See {@link #createConnection(Configuration)}} to run as other than
063   * the RegionServer User).
064   *
065   * <p>Be careful RPC'ing from a Coprocessor context. RPC's will fail, stall, retry, and/or crawl
066   * because the remote side is not online, is struggling or it is on the other side of a network
067   * partition. Any use of Connection from inside a Coprocessor must be able to handle all such
068   * hiccups.
069   *
070   * @see #createConnection(Configuration)
071   * @return The host's Connection to the Cluster.
072   */
073  Connection getConnection();
074
075  /**
076   * Creates a cluster connection using the passed Configuration.
077   *
078   * Creating a Connection is a heavy-weight operation. The resultant Connection's cache of
079   * region locations will be empty. Therefore you should cache and reuse Connections rather than
080   * create a Connection on demand. Create on start of your Coprocessor. You will have to cast
081   * the CoprocessorEnvironment appropriately to get at this API at start time because
082   * Coprocessor start method is passed a subclass of this CoprocessorEnvironment or fetch
083   * Connection using a synchronized accessor initializing the Connection on first access. Close
084   * the returned Connection when done to free resources. Using this API rather
085   * than {@link org.apache.hadoop.hbase.client.ConnectionFactory#createConnection(Configuration)}
086   * returns a Connection that will short-circuit RPC if the target is a local resource. Use
087   * ConnectionFactory if you don't need this ability.
088   *
089   * <p>Be careful RPC'ing from a Coprocessor context. RPC's will fail, stall, retry, and/or crawl
090   * because the remote side is not online, is struggling or it is on the other side of a network
091   * partition. Any use of Connection from inside a Coprocessor must be able to handle all such
092   * hiccups.
093   * @return Connection created using the passed conf.
094   */
095  Connection createConnection(Configuration conf) throws IOException;
096
097  /**
098   * Returns a MetricRegistry that can be used to track metrics at the region server level.
099   *
100   * <p>See ExampleMasterObserverWithMetrics class in the hbase-examples modules for examples
101   * of how metrics can be instantiated and used.</p>
102   * @return A MetricRegistry for the coprocessor class to track and export metrics.
103   */
104  MetricRegistry getMetricRegistryForRegionServer();
105}