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.CompletableFuture;
023import java.util.concurrent.ExecutorService;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseInterfaceAudience;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.yetus.audience.InterfaceAudience;
030
031/**
032 * The asynchronous version of Connection.
033 * @since 2.0.0
034 */
035@InterfaceAudience.Public
036public interface AsyncConnection extends Closeable {
037
038  /**
039   * Returns the {@link org.apache.hadoop.conf.Configuration} object used by this instance.
040   * <p>
041   * The reference returned is not a copy, so any change made to it will affect this instance.
042   */
043  Configuration getConfiguration();
044
045  /**
046   * Retrieve a AsyncRegionLocator implementation to inspect region information on a table. The
047   * returned AsyncRegionLocator is not thread-safe, so a new instance should be created for each
048   * using thread. This is a lightweight operation. Pooling or caching of the returned
049   * AsyncRegionLocator is neither required nor desired.
050   * @param tableName Name of the table who's region is to be examined
051   * @return An AsyncRegionLocator instance
052   */
053  AsyncTableRegionLocator getRegionLocator(TableName tableName);
054
055  /**
056   * Retrieve an {@link AsyncTable} implementation for accessing a table.
057   * <p>
058   * The returned instance will use default configs. Use {@link #getTableBuilder(TableName)} if
059   * you want to customize some configs.
060   * <p>
061   * This method no longer checks table existence. An exception will be thrown if the table does not
062   * exist only when the first operation is attempted.
063   * <p>
064   * The returned {@code CompletableFuture} will be finished directly in the rpc framework's
065   * callback thread, so typically you should not do any time consuming work inside these methods.
066   * And also the observer style scan API will use {@link AdvancedScanResultConsumer} which is
067   * designed for experts only. Only use it when you know what you are doing.
068   * @param tableName the name of the table
069   * @return an AsyncTable to use for interactions with this table
070   * @see #getTableBuilder(TableName)
071   */
072  default AsyncTable<AdvancedScanResultConsumer> getTable(TableName tableName) {
073    return getTableBuilder(tableName).build();
074  }
075
076  /**
077   * Returns an {@link AsyncTableBuilder} for creating {@link AsyncTable}.
078   * <p>
079   * This method no longer checks table existence. An exception will be thrown if the table does not
080   * exist only when the first operation is attempted.
081   * @param tableName the name of the table
082   */
083  AsyncTableBuilder<AdvancedScanResultConsumer> getTableBuilder(TableName tableName);
084
085  /**
086   * Retrieve an {@link AsyncTable} implementation for accessing a table.
087   * <p>
088   * This method no longer checks table existence. An exception will be thrown if the table does not
089   * exist only when the first operation is attempted.
090   * @param tableName the name of the table
091   * @param pool the thread pool to use for executing callback
092   * @return an AsyncTable to use for interactions with this table
093   */
094  default AsyncTable<ScanResultConsumer> getTable(TableName tableName, ExecutorService pool) {
095    return getTableBuilder(tableName, pool).build();
096  }
097
098  /**
099   * Returns an {@link AsyncTableBuilder} for creating {@link AsyncTable}.
100   * <p>
101   * This method no longer checks table existence. An exception will be thrown if the table does not
102   * exist only when the first operation is attempted.
103   * @param tableName the name of the table
104   * @param pool the thread pool to use for executing callback
105   */
106  AsyncTableBuilder<ScanResultConsumer> getTableBuilder(TableName tableName, ExecutorService pool);
107
108  /**
109   * Retrieve an {@link AsyncAdmin} implementation to administer an HBase cluster.
110   * <p>
111   * The returned instance will use default configs. Use {@link #getAdminBuilder()} if you want to
112   * customize some configs.
113   * <p>
114   * The admin operation's returned {@code CompletableFuture} will be finished directly in the rpc
115   * framework's callback thread, so typically you should not do any time consuming work inside
116   * these methods.
117   * @return an {@link AsyncAdmin} instance for cluster administration
118   */
119  default AsyncAdmin getAdmin() {
120    return getAdminBuilder().build();
121  }
122
123  /**
124   * Returns an {@link AsyncAdminBuilder} for creating {@link AsyncAdmin}.
125   * <p>
126   * The admin operation's returned {@code CompletableFuture} will be finished directly in the rpc
127   * framework's callback thread, so typically you should not do any time consuming work inside
128   * these methods.
129   */
130  AsyncAdminBuilder getAdminBuilder();
131
132  /**
133   * Retrieve an {@link AsyncAdmin} implementation to administer an HBase cluster.
134   * <p>
135   * The returned instance will use default configs. Use {@link #getAdminBuilder(ExecutorService)}
136   * if you want to customize some configs.
137   * @param pool the thread pool to use for executing callback
138   * @return an {@link AsyncAdmin} instance for cluster administration
139   */
140  default AsyncAdmin getAdmin(ExecutorService pool) {
141    return getAdminBuilder(pool).build();
142  }
143
144  /**
145   * Returns an {@link AsyncAdminBuilder} for creating {@link AsyncAdmin}.
146   * @param pool the thread pool to use for executing callback
147   */
148  AsyncAdminBuilder getAdminBuilder(ExecutorService pool);
149
150  /**
151   * Retrieve an {@link AsyncBufferedMutator} for performing client-side buffering of writes.
152   * <p>
153   * The returned instance will use default configs. Use
154   * {@link #getBufferedMutatorBuilder(TableName)} if you want to customize some configs.
155   * @param tableName the name of the table
156   * @return an {@link AsyncBufferedMutator} for the supplied tableName.
157   */
158  default AsyncBufferedMutator getBufferedMutator(TableName tableName) {
159    return getBufferedMutatorBuilder(tableName).build();
160  }
161
162  /**
163   * Returns an {@link AsyncBufferedMutatorBuilder} for creating {@link AsyncBufferedMutator}.
164   * @param tableName the name of the table
165   */
166  AsyncBufferedMutatorBuilder getBufferedMutatorBuilder(TableName tableName);
167
168  /**
169   * Retrieve an {@link AsyncBufferedMutator} for performing client-side buffering of writes.
170   * <p>
171   * The returned instance will use default configs. Use
172   * {@link #getBufferedMutatorBuilder(TableName, ExecutorService)} if you want to customize some
173   * configs.
174   * @param tableName the name of the table
175   * @param pool the thread pool to use for executing callback
176   * @return an {@link AsyncBufferedMutator} for the supplied tableName.
177   */
178  default AsyncBufferedMutator getBufferedMutator(TableName tableName, ExecutorService pool) {
179    return getBufferedMutatorBuilder(tableName, pool).build();
180  }
181
182  /**
183   * Returns an {@link AsyncBufferedMutatorBuilder} for creating {@link AsyncBufferedMutator}.
184   * @param tableName the name of the table
185   * @param pool the thread pool to use for executing callback
186   */
187  AsyncBufferedMutatorBuilder getBufferedMutatorBuilder(TableName tableName, ExecutorService pool);
188
189  /**
190   * Retrieve an Hbck implementation to fix an HBase cluster. The returned Hbck is not guaranteed to
191   * be thread-safe. A new instance should be created by each thread. This is a lightweight
192   * operation. Pooling or caching of the returned Hbck instance is not recommended.
193   * <p/>
194   * The caller is responsible for calling {@link Hbck#close()} on the returned Hbck instance.
195   * <p/>
196   * This will be used mostly by hbck tool.
197   * @return an Hbck instance for active master. Active master is fetched from the zookeeper.
198   */
199  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.HBCK)
200  CompletableFuture<Hbck> getHbck();
201
202  /**
203   * Retrieve an Hbck implementation to fix an HBase cluster. The returned Hbck is not guaranteed to
204   * be thread-safe. A new instance should be created by each thread. This is a lightweight
205   * operation. Pooling or caching of the returned Hbck instance is not recommended.
206   * <p/>
207   * The caller is responsible for calling {@link Hbck#close()} on the returned Hbck instance.
208   * <p/>
209   * This will be used mostly by hbck tool. This may only be used to by pass getting registered
210   * master from ZK. In situations where ZK is not available or active master is not registered with
211   * ZK and user can get master address by other means, master can be explicitly specified.
212   * @param masterServer explicit {@link ServerName} for master server
213   * @return an Hbck instance for a specified master server
214   */
215  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.HBCK)
216  Hbck getHbck(ServerName masterServer) throws IOException;
217}