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.client;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.util.concurrent.ExecutorService;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.Abortable;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * A cluster connection encapsulating lower level individual connections to actual servers and a
032 * connection to zookeeper. Connections are instantiated through the {@link ConnectionFactory}
033 * class. The lifecycle of the connection is managed by the caller, who has to {@link #close()} the
034 * connection to release the resources.
035 * <p>
036 * The connection object contains logic to find the master, locate regions out on the cluster, keeps
037 * a cache of locations and then knows how to re-calibrate after they move. The individual
038 * connections to servers, meta cache, zookeeper connection, etc are all shared by the {@link Table}
039 * and {@link Admin} instances obtained from this connection.
040 * <p>
041 * Connection creation is a heavy-weight operation. Connection implementations are thread-safe, so
042 * that the client can create a connection once, and share it with different threads. {@link Table}
043 * and {@link Admin} instances, on the other hand, are light-weight and are not thread-safe.
044 * Typically, a single connection per client application is instantiated and every thread will
045 * obtain its own Table instance. Caching or pooling of {@link Table} and {@link Admin} is not
046 * recommended.
047 * @see ConnectionFactory
048 * @since 0.99.0
049 */
050@InterfaceAudience.Public
051public interface Connection extends Abortable, Closeable {
052
053  /*
054   * Implementation notes: - Only allow new style of interfaces: -- All table names are passed as
055   * TableName. No more byte[] and string arguments -- Most of the classes with names H is
056   * deprecated in favor of non-H versions (Table, Connection, etc) -- Only real client-facing
057   * public methods are allowed - Connection should contain only getTable(), getAdmin() kind of
058   * general methods.
059   */
060
061  /** Returns Configuration instance being used by this Connection instance. */
062  Configuration getConfiguration();
063
064  /**
065   * Retrieve a Table implementation for accessing a table. The returned Table is not thread safe, a
066   * new instance should be created for each using thread. This is a lightweight operation, pooling
067   * or caching of the returned Table is neither required nor desired.
068   * <p>
069   * The caller is responsible for calling {@link Table#close()} on the returned table instance.
070   * <p>
071   * Since 0.98.1 this method no longer checks table existence. An exception will be thrown if the
072   * table does not exist only when the first operation is attempted.
073   * @param tableName the name of the table
074   * @return a Table to use for interactions with this table
075   */
076  default Table getTable(TableName tableName) throws IOException {
077    return getTable(tableName, null);
078  }
079
080  /**
081   * Retrieve a Table implementation for accessing a table. The returned Table is not thread safe, a
082   * new instance should be created for each using thread. This is a lightweight operation, pooling
083   * or caching of the returned Table is neither required nor desired.
084   * <p>
085   * The caller is responsible for calling {@link Table#close()} on the returned table instance.
086   * <p>
087   * Since 0.98.1 this method no longer checks table existence. An exception will be thrown if the
088   * table does not exist only when the first operation is attempted.
089   * @param tableName the name of the table
090   * @param pool      The thread pool to use for batch operations, null to use a default pool.
091   * @return a Table to use for interactions with this table
092   */
093  default Table getTable(TableName tableName, ExecutorService pool) throws IOException {
094    return getTableBuilder(tableName, pool).build();
095  }
096
097  /**
098   * <p>
099   * Retrieve a {@link BufferedMutator} for performing client-side buffering of writes. The
100   * {@link BufferedMutator} returned by this method is thread-safe. This accessor will create a new
101   * ThreadPoolExecutor and will be shutdown once we close the BufferedMutator. This object can be
102   * used for long lived operations.
103   * </p>
104   * <p>
105   * The caller is responsible for calling {@link BufferedMutator#close()} on the returned
106   * {@link BufferedMutator} instance.
107   * </p>
108   * <p>
109   * @param tableName the name of the table
110   * @return a {@link BufferedMutator} for the supplied tableName.
111   */
112  BufferedMutator getBufferedMutator(TableName tableName) throws IOException;
113
114  /**
115   * Retrieve a {@link BufferedMutator} for performing client-side buffering of writes. The
116   * {@link BufferedMutator} returned by this method is thread-safe. This object can be used for
117   * long lived table operations. If user passes ThreadPool in BufferedMutatorParams then we will
118   * use that otherwise we will create for the user. For user specified ThreadPool, it is the user's
119   * responsibility to shutdown. For ThreadPool created by us, we will shutdown when user calls
120   * {@link BufferedMutator#close()}. The caller is responsible for calling
121   * {@link BufferedMutator#close()} on the returned {@link BufferedMutator} instance.
122   * @param params details on how to instantiate the {@code BufferedMutator}.
123   * @return a {@link BufferedMutator} for the supplied tableName.
124   */
125  BufferedMutator getBufferedMutator(BufferedMutatorParams params) throws IOException;
126
127  /**
128   * Retrieve a RegionLocator implementation to inspect region information on a table. The returned
129   * RegionLocator is not thread-safe, so a new instance should be created for each using thread.
130   * This is a lightweight operation. Pooling or caching of the returned RegionLocator is neither
131   * required nor desired. <br>
132   * The caller is responsible for calling {@link RegionLocator#close()} on the returned
133   * RegionLocator instance. RegionLocator needs to be unmanaged
134   * @param tableName Name of the table who's region is to be examined
135   * @return A RegionLocator instance
136   */
137  RegionLocator getRegionLocator(TableName tableName) throws IOException;
138
139  /**
140   * Clear all the entries in the region location cache, for all the tables.
141   * <p/>
142   * If you only want to clear the cache for a specific table, use
143   * {@link RegionLocator#clearRegionLocationCache()}.
144   * <p/>
145   * This may cause performance issue so use it with caution.
146   */
147  void clearRegionLocationCache();
148
149  /**
150   * Retrieve an Admin implementation to administer an HBase cluster. The returned Admin is not
151   * guaranteed to be thread-safe. A new instance should be created for each using thread. This is a
152   * lightweight operation. Pooling or caching of the returned Admin is not recommended. <br>
153   * The caller is responsible for calling {@link Admin#close()} on the returned Admin instance.
154   * @return an Admin instance for cluster administration
155   */
156  Admin getAdmin() throws IOException;
157
158  @Override
159  void close() throws IOException;
160
161  /**
162   * Returns whether the connection is closed or not.
163   * @return true if this connection is closed
164   */
165  boolean isClosed();
166
167  /**
168   * Returns an {@link TableBuilder} for creating {@link Table}.
169   * @param tableName the name of the table
170   * @param pool      the thread pool to use for requests like batch and scan
171   */
172  TableBuilder getTableBuilder(TableName tableName, ExecutorService pool);
173
174  /**
175   * Returns the cluster ID unique to this HBase cluster. <br>
176   * The default implementation is added to keep client compatibility.
177   */
178  default String getClusterId() {
179    return null;
180  }
181
182  /**
183   * Retrieve an Hbck implementation to fix an HBase cluster. The returned Hbck is not guaranteed to
184   * be thread-safe. A new instance should be created by each thread. This is a lightweight
185   * operation. Pooling or caching of the returned Hbck instance is not recommended. <br>
186   * The caller is responsible for calling {@link Hbck#close()} on the returned Hbck instance. <br>
187   * This will be used mostly by hbck tool.
188   * @return an Hbck instance for active master. Active master is fetched from the zookeeper.
189   */
190  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.HBCK)
191  default Hbck getHbck() throws IOException {
192    throw new UnsupportedOperationException("Not implemented");
193  }
194
195  /**
196   * Retrieve an Hbck implementation to fix an HBase cluster. The returned Hbck is not guaranteed to
197   * be thread-safe. A new instance should be created by each thread. This is a lightweight
198   * operation. Pooling or caching of the returned Hbck instance is not recommended. <br>
199   * The caller is responsible for calling {@link Hbck#close()} on the returned Hbck instance. <br>
200   * This will be used mostly by hbck tool. This may only be used to by pass getting registered
201   * master from ZK. In situations where ZK is not available or active master is not registered with
202   * ZK and user can get master address by other means, master can be explicitly specified.
203   * @param masterServer explicit {@link ServerName} for master server
204   * @return an Hbck instance for a specified master server
205   */
206  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.HBCK)
207  default Hbck getHbck(ServerName masterServer) throws IOException {
208    throw new UnsupportedOperationException("Not implemented");
209  }
210}