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.client;
020
021import java.io.Closeable;
022import java.io.IOException;
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * <p>Used to communicate with a single HBase table similar to {@link Table} but meant for
030 * batched, asynchronous puts. Obtain an instance from a {@link Connection} and call
031 * {@link #close()} afterwards. Customizations can be applied to the {@code BufferedMutator} via
032 * the {@link BufferedMutatorParams}.
033 * </p>
034 *
035 * <p>Exception handling with asynchronously via the {@link BufferedMutator.ExceptionListener}.
036 * The default implementation is to throw the exception upon receipt. This behavior can be
037 * overridden with a custom implementation, provided as a parameter with
038 * {@link BufferedMutatorParams#listener(BufferedMutator.ExceptionListener)}.</p>
039 *
040 * <p>Map/Reduce jobs are good use cases for using {@code BufferedMutator}. Map/reduce jobs
041 * benefit from batching, but have no natural flush point. {@code BufferedMutator} receives the
042 * puts from the M/R job and will batch puts based on some heuristic, such as the accumulated size
043 * of the puts, and submit batches of puts asynchronously so that the M/R logic can continue
044 * without interruption.
045 * </p>
046 *
047 * <p>{@code BufferedMutator} can also be used on more exotic circumstances. Map/Reduce batch jobs
048 * will have a single {@code BufferedMutator} per thread. A single {@code BufferedMutator} can
049 * also be effectively used in high volume online systems to batch puts, with the caveat that
050 * extreme circumstances, such as JVM or machine failure, may cause some data loss.</p>
051 *
052 * <p>NOTE: This class replaces the functionality that used to be available via
053 * HTable#setAutoFlush(boolean) set to {@code false}.
054 * </p>
055 *
056 * <p>See also the {@code BufferedMutatorExample} in the hbase-examples module.</p>
057 * @see ConnectionFactory
058 * @see Connection
059 * @since 1.0.0
060 */
061@InterfaceAudience.Public
062public interface BufferedMutator extends Closeable {
063  /**
064   * Key to use setting non-default BufferedMutator implementation in Configuration.
065   */
066  String CLASSNAME_KEY = "hbase.client.bufferedmutator.classname";
067
068  /**
069   * Having the timer tick run more often that once every 100ms is needless and will
070   * probably cause too many timer events firing having a negative impact on performance.
071   */
072  long MIN_WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS = 100;
073
074  /**
075   * Gets the fully qualified table name instance of the table that this BufferedMutator writes to.
076   */
077  TableName getName();
078
079  /**
080   * Returns the {@link org.apache.hadoop.conf.Configuration} object used by this instance.
081   * <p>
082   * The reference returned is not a copy, so any change made to it will
083   * affect this instance.
084   */
085  Configuration getConfiguration();
086
087  /**
088   * Sends a {@link Mutation} to the table. The mutations will be buffered and sent over the
089   * wire as part of a batch. Currently only supports {@link Put} and {@link Delete} mutations.
090   *
091   * @param mutation The data to send.
092   * @throws IOException if a remote or network exception occurs.
093   */
094  void mutate(Mutation mutation) throws IOException;
095
096  /**
097   * Send some {@link Mutation}s to the table. The mutations will be buffered and sent over the
098   * wire as part of a batch. There is no guarantee of sending entire content of {@code mutations}
099   * in a single batch; it will be broken up according to the write buffer capacity.
100   *
101   * @param mutations The data to send.
102   * @throws IOException if a remote or network exception occurs.
103   */
104  void mutate(List<? extends Mutation> mutations) throws IOException;
105
106  /**
107   * Performs a {@link #flush()} and releases any resources held.
108   *
109   * @throws IOException if a remote or network exception occurs.
110   */
111  @Override
112  void close() throws IOException;
113
114  /**
115   * Executes all the buffered, asynchronous {@link Mutation} operations and waits until they
116   * are done.
117   *
118   * @throws IOException if a remote or network exception occurs.
119   */
120  void flush() throws IOException;
121
122  /**
123   * Sets the maximum time before the buffer is automatically flushed checking once per second.
124   * @param timeoutMs    The maximum number of milliseconds how long records may be buffered
125   *                     before they are flushed. Set to 0 to disable.
126   */
127  default void setWriteBufferPeriodicFlush(long timeoutMs) {
128    setWriteBufferPeriodicFlush(timeoutMs, 1000L);
129  }
130
131  /**
132   * Sets the maximum time before the buffer is automatically flushed.
133   * @param timeoutMs    The maximum number of milliseconds how long records may be buffered
134   *                     before they are flushed. Set to 0 to disable.
135   * @param timerTickMs  The number of milliseconds between each check if the
136   *                     timeout has been exceeded. Must be 100ms (as defined in
137   *                     {@link #MIN_WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS})
138   *                     or larger to avoid performance problems.
139   */
140  default void setWriteBufferPeriodicFlush(long timeoutMs, long timerTickMs) {
141    throw new UnsupportedOperationException(
142            "The BufferedMutator::setWriteBufferPeriodicFlush has not been implemented");
143  }
144
145  /**
146   * Disable periodic flushing of the write buffer.
147   */
148  default void disableWriteBufferPeriodicFlush() {
149    setWriteBufferPeriodicFlush(0, MIN_WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS);
150  }
151
152  /**
153   * Returns the current periodic flush timeout value in milliseconds.
154   * @return The maximum number of milliseconds how long records may be buffered before they
155   *   are flushed. The value 0 means this is disabled.
156   */
157  default long getWriteBufferPeriodicFlushTimeoutMs() {
158    throw new UnsupportedOperationException(
159            "The BufferedMutator::getWriteBufferPeriodicFlushTimeoutMs has not been implemented");
160  }
161
162  /**
163   * Returns the current periodic flush timertick interval in milliseconds.
164   * @return The number of milliseconds between each check if the timeout has been exceeded.
165   *   This value only has a real meaning if the timeout has been set to > 0
166   */
167  default long getWriteBufferPeriodicFlushTimerTickMs() {
168    throw new UnsupportedOperationException(
169            "The BufferedMutator::getWriteBufferPeriodicFlushTimerTickMs has not been implemented");
170  }
171
172  /**
173   * Returns the maximum size in bytes of the write buffer for this HTable.
174   * <p>
175   * The default value comes from the configuration parameter {@code hbase.client.write.buffer}.
176   * @return The size of the write buffer in bytes.
177   */
178  long getWriteBufferSize();
179
180  /**
181   * Set rpc timeout for this mutator instance
182   */
183  void setRpcTimeout(int timeout);
184
185  /**
186   * Set operation timeout for this mutator instance
187   */
188  void setOperationTimeout(int timeout);
189
190  /**
191   * Listens for asynchronous exceptions on a {@link BufferedMutator}.
192   */
193  @InterfaceAudience.Public
194  interface ExceptionListener {
195    public void onException(RetriesExhaustedWithDetailsException exception,
196        BufferedMutator mutator) throws RetriesExhaustedWithDetailsException;
197  }
198}