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   * <p/>
066   * @deprecated Since 3.0.0, will be removed in 4.0.0. For internal test use only, do not use it
067   *             any more.
068   */
069  @Deprecated
070  String CLASSNAME_KEY = "hbase.client.bufferedmutator.classname";
071
072  /**
073   * Having the timer tick run more often that once every 100ms is needless and will
074   * probably cause too many timer events firing having a negative impact on performance.
075   */
076  long MIN_WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS = 100;
077
078  /**
079   * Gets the fully qualified table name instance of the table that this BufferedMutator writes to.
080   */
081  TableName getName();
082
083  /**
084   * Returns the {@link org.apache.hadoop.conf.Configuration} object used by this instance.
085   * <p>
086   * The reference returned is not a copy, so any change made to it will
087   * affect this instance.
088   */
089  Configuration getConfiguration();
090
091  /**
092   * Sends a {@link Mutation} to the table. The mutations will be buffered and sent over the
093   * wire as part of a batch. Currently only supports {@link Put} and {@link Delete} mutations.
094   *
095   * @param mutation The data to send.
096   * @throws IOException if a remote or network exception occurs.
097   */
098  void mutate(Mutation mutation) throws IOException;
099
100  /**
101   * Send some {@link Mutation}s to the table. The mutations will be buffered and sent over the
102   * wire as part of a batch. There is no guarantee of sending entire content of {@code mutations}
103   * in a single batch; it will be broken up according to the write buffer capacity.
104   *
105   * @param mutations The data to send.
106   * @throws IOException if a remote or network exception occurs.
107   */
108  void mutate(List<? extends Mutation> mutations) throws IOException;
109
110  /**
111   * Performs a {@link #flush()} and releases any resources held.
112   *
113   * @throws IOException if a remote or network exception occurs.
114   */
115  @Override
116  void close() throws IOException;
117
118  /**
119   * Executes all the buffered, asynchronous {@link Mutation} operations and waits until they
120   * are done.
121   *
122   * @throws IOException if a remote or network exception occurs.
123   */
124  void flush() throws IOException;
125
126  /**
127   * Sets the maximum time before the buffer is automatically flushed checking once per second.
128   * @param timeoutMs    The maximum number of milliseconds how long records may be buffered
129   *                     before they are flushed. Set to 0 to disable.
130   */
131  default void setWriteBufferPeriodicFlush(long timeoutMs) {
132    setWriteBufferPeriodicFlush(timeoutMs, 1000L);
133  }
134
135  /**
136   * Sets the maximum time before the buffer is automatically flushed.
137   * @param timeoutMs    The maximum number of milliseconds how long records may be buffered
138   *                     before they are flushed. Set to 0 to disable.
139   * @param timerTickMs  The number of milliseconds between each check if the
140   *                     timeout has been exceeded. Must be 100ms (as defined in
141   *                     {@link #MIN_WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS})
142   *                     or larger to avoid performance problems.
143   */
144  default void setWriteBufferPeriodicFlush(long timeoutMs, long timerTickMs) {
145    throw new UnsupportedOperationException(
146            "The BufferedMutator::setWriteBufferPeriodicFlush has not been implemented");
147  }
148
149  /**
150   * Disable periodic flushing of the write buffer.
151   */
152  default void disableWriteBufferPeriodicFlush() {
153    setWriteBufferPeriodicFlush(0, MIN_WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS);
154  }
155
156  /**
157   * Returns the current periodic flush timeout value in milliseconds.
158   * @return The maximum number of milliseconds how long records may be buffered before they
159   *   are flushed. The value 0 means this is disabled.
160   */
161  default long getWriteBufferPeriodicFlushTimeoutMs() {
162    throw new UnsupportedOperationException(
163            "The BufferedMutator::getWriteBufferPeriodicFlushTimeoutMs has not been implemented");
164  }
165
166  /**
167   * Returns the current periodic flush timertick interval in milliseconds.
168   * @return The number of milliseconds between each check if the timeout has been exceeded.
169   *   This value only has a real meaning if the timeout has been set to > 0
170   */
171  default long getWriteBufferPeriodicFlushTimerTickMs() {
172    throw new UnsupportedOperationException(
173            "The BufferedMutator::getWriteBufferPeriodicFlushTimerTickMs has not been implemented");
174  }
175
176  /**
177   * Returns the maximum size in bytes of the write buffer for this HTable.
178   * <p>
179   * The default value comes from the configuration parameter {@code hbase.client.write.buffer}.
180   * @return The size of the write buffer in bytes.
181   */
182  long getWriteBufferSize();
183
184  /**
185   * Set rpc timeout for this mutator instance
186   * @deprecated Since 3.0.0, will be removed in 4.0.0. Please set this through the
187   *             {@link BufferedMutatorParams}.
188   */
189  @Deprecated
190  void setRpcTimeout(int timeout);
191
192  /**
193   * Set operation timeout for this mutator instance
194   * @deprecated Since 3.0.0, will be removed in 4.0.0. Please set this through the
195   *             {@link BufferedMutatorParams}.
196   */
197  @Deprecated
198  void setOperationTimeout(int timeout);
199
200  /**
201   * Listens for asynchronous exceptions on a {@link BufferedMutator}.
202   */
203  @InterfaceAudience.Public
204  interface ExceptionListener {
205    public void onException(RetriesExhaustedWithDetailsException exception,
206        BufferedMutator mutator) throws RetriesExhaustedWithDetailsException;
207  }
208}