HBASE-26688 Threads shared EMPTY_RESULT may lead to unexpected client job down. ...
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / client / Connection.java
blobb638e72a46dbdb19013158614dc7c43859b86d26
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import java.io.Closeable;
21 import java.io.IOException;
22 import java.util.concurrent.ExecutorService;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.Abortable;
25 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26 import org.apache.hadoop.hbase.ServerName;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.util.FutureUtils;
29 import org.apache.yetus.audience.InterfaceAudience;
31 /**
32 * A cluster connection encapsulating lower level individual connections to actual servers and
33 * a connection to zookeeper. Connections are instantiated through the {@link ConnectionFactory}
34 * class. The lifecycle of the connection is managed by the caller, who has to {@link #close()}
35 * the connection to release the resources.
37 * <p> The connection object contains logic to find the master, locate regions out on the cluster,
38 * keeps a cache of locations and then knows how to re-calibrate after they move. The individual
39 * connections to servers, meta cache, zookeeper connection, etc are all shared by the
40 * {@link Table} and {@link Admin} instances obtained from this connection.
42 * <p> Connection creation is a heavy-weight operation. Connection implementations are thread-safe,
43 * so that the client can create a connection once, and share it with different threads.
44 * {@link Table} and {@link Admin} instances, on the other hand, are light-weight and are not
45 * thread-safe. Typically, a single connection per client application is instantiated and every
46 * thread will obtain its own Table instance. Caching or pooling of {@link Table} and {@link Admin}
47 * is not recommended.
49 * @see ConnectionFactory
50 * @since 0.99.0
52 @InterfaceAudience.Public
53 public interface Connection extends Abortable, Closeable {
56 * Implementation notes:
57 * - Only allow new style of interfaces:
58 * -- All table names are passed as TableName. No more byte[] and string arguments
59 * -- Most of the classes with names H is deprecated in favor of non-H versions
60 * (Table, Connection, etc)
61 * -- Only real client-facing public methods are allowed
62 * - Connection should contain only getTable(), getAdmin() kind of general methods.
65 /**
66 * @return Configuration instance being used by this Connection instance.
68 Configuration getConfiguration();
70 /**
71 * Retrieve a Table implementation for accessing a table.
72 * The returned Table is not thread safe, a new instance should be created for each using thread.
73 * This is a lightweight operation, pooling or caching of the returned Table
74 * is neither required nor desired.
75 * <p>
76 * The caller is responsible for calling {@link Table#close()} on the returned
77 * table instance.
78 * <p>
79 * Since 0.98.1 this method no longer checks table existence. An exception
80 * will be thrown if the table does not exist only when the first operation is
81 * attempted.
82 * @param tableName the name of the table
83 * @return a Table to use for interactions with this table
85 default Table getTable(TableName tableName) throws IOException {
86 return getTable(tableName, null);
89 /**
90 * Retrieve a Table implementation for accessing a table.
91 * The returned Table is not thread safe, a new instance should be created for each using thread.
92 * This is a lightweight operation, pooling or caching of the returned Table
93 * is neither required nor desired.
94 * <p>
95 * The caller is responsible for calling {@link Table#close()} on the returned
96 * table instance.
97 * <p>
98 * Since 0.98.1 this method no longer checks table existence. An exception
99 * will be thrown if the table does not exist only when the first operation is
100 * attempted.
102 * @param tableName the name of the table
103 * @param pool The thread pool to use for batch operations, null to use a default pool.
104 * @return a Table to use for interactions with this table
106 default Table getTable(TableName tableName, ExecutorService pool) throws IOException {
107 return getTableBuilder(tableName, pool).build();
111 * <p>
112 * Retrieve a {@link BufferedMutator} for performing client-side buffering of writes. The
113 * {@link BufferedMutator} returned by this method is thread-safe. This BufferedMutator will
114 * use the Connection's ExecutorService. This object can be used for long lived operations.
115 * </p>
116 * <p>
117 * The caller is responsible for calling {@link BufferedMutator#close()} on
118 * the returned {@link BufferedMutator} instance.
119 * </p>
120 * <p>
121 * This accessor will use the connection's ExecutorService and will throw an
122 * exception in the main thread when an asynchronous exception occurs.
124 * @param tableName the name of the table
126 * @return a {@link BufferedMutator} for the supplied tableName.
128 default BufferedMutator getBufferedMutator(TableName tableName) throws IOException {
129 return getBufferedMutator(new BufferedMutatorParams(tableName));
133 * Retrieve a {@link BufferedMutator} for performing client-side buffering of writes. The
134 * {@link BufferedMutator} returned by this method is thread-safe. This object can be used for
135 * long lived table operations. The caller is responsible for calling
136 * {@link BufferedMutator#close()} on the returned {@link BufferedMutator} instance.
138 * @param params details on how to instantiate the {@code BufferedMutator}.
139 * @return a {@link BufferedMutator} for the supplied tableName.
141 BufferedMutator getBufferedMutator(BufferedMutatorParams params) throws IOException;
144 * Retrieve a RegionLocator implementation to inspect region information on a table. The returned
145 * RegionLocator is not thread-safe, so a new instance should be created for each using thread.
147 * This is a lightweight operation. Pooling or caching of the returned RegionLocator is neither
148 * required nor desired.
149 * <br>
150 * The caller is responsible for calling {@link RegionLocator#close()} on the returned
151 * RegionLocator instance.
153 * RegionLocator needs to be unmanaged
155 * @param tableName Name of the table who's region is to be examined
156 * @return A RegionLocator instance
158 RegionLocator getRegionLocator(TableName tableName) throws IOException;
161 * Clear all the entries in the region location cache, for all the tables.
162 * <p/>
163 * If you only want to clear the cache for a specific table, use
164 * {@link RegionLocator#clearRegionLocationCache()}.
165 * <p/>
166 * This may cause performance issue so use it with caution.
168 void clearRegionLocationCache();
171 * Retrieve an Admin implementation to administer an HBase cluster.
172 * The returned Admin is not guaranteed to be thread-safe. A new instance should be created for
173 * each using thread. This is a lightweight operation. Pooling or caching of the returned
174 * Admin is not recommended.
175 * <br>
176 * The caller is responsible for calling {@link Admin#close()} on the returned
177 * Admin instance.
179 * @return an Admin instance for cluster administration
181 Admin getAdmin() throws IOException;
183 @Override
184 void close() throws IOException;
187 * Returns whether the connection is closed or not.
188 * @return true if this connection is closed
190 boolean isClosed();
193 * Returns an {@link TableBuilder} for creating {@link Table}.
194 * @param tableName the name of the table
195 * @param pool the thread pool to use for requests like batch and scan
197 TableBuilder getTableBuilder(TableName tableName, ExecutorService pool);
200 * Convert this connection to an {@link AsyncConnection}.
201 * <p/>
202 * Usually we will return the same instance if you call this method multiple times so you can
203 * consider this as a light-weighted operation.
205 AsyncConnection toAsyncConnection();
208 * @return the cluster ID unique to this HBase cluster.
210 String getClusterId();
213 * Retrieve an Hbck implementation to fix an HBase cluster.
214 * The returned Hbck is not guaranteed to be thread-safe. A new instance should be created by
215 * each thread. This is a lightweight operation. Pooling or caching of the returned Hbck instance
216 * is not recommended.
217 * <br>
218 * The caller is responsible for calling {@link Hbck#close()} on the returned Hbck instance.
219 *<br>
220 * This will be used mostly by hbck tool.
222 * @return an Hbck instance for active master. Active master is fetched from the zookeeper.
224 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.HBCK)
225 default Hbck getHbck() throws IOException {
226 return FutureUtils.get(toAsyncConnection().getHbck());
230 * Retrieve an Hbck implementation to fix an HBase cluster.
231 * The returned Hbck is not guaranteed to be thread-safe. A new instance should be created by
232 * each thread. This is a lightweight operation. Pooling or caching of the returned Hbck instance
233 * is not recommended.
234 * <br>
235 * The caller is responsible for calling {@link Hbck#close()} on the returned Hbck instance.
236 *<br>
237 * This will be used mostly by hbck tool. This may only be used to by pass getting
238 * registered master from ZK. In situations where ZK is not available or active master is not
239 * registered with ZK and user can get master address by other means, master can be explicitly
240 * specified.
242 * @param masterServer explicit {@link ServerName} for master server
243 * @return an Hbck instance for a specified master server
245 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.HBCK)
246 default Hbck getHbck(ServerName masterServer) throws IOException {
247 return toAsyncConnection().getHbck(masterServer);