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.util.Collections;
021import java.util.HashMap;
022import java.util.Map;
023import java.util.concurrent.ExecutorService;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Parameters for instantiating a {@link BufferedMutator}.
029 */
030@InterfaceAudience.Public
031public class BufferedMutatorParams implements Cloneable {
032
033  static final int UNSET = -1;
034
035  private final TableName tableName;
036  private long writeBufferSize = UNSET;
037  private long writeBufferPeriodicFlushTimeoutMs = UNSET;
038  private long writeBufferPeriodicFlushTimerTickMs = UNSET;
039  private int maxKeyValueSize = UNSET;
040  private ExecutorService pool = null;
041  private String implementationClassName = null;
042  private int rpcTimeout = UNSET;
043  private int operationTimeout = UNSET;
044  private int maxMutations = UNSET;
045  protected Map<String, byte[]> requestAttributes = Collections.emptyMap();
046  private BufferedMutator.ExceptionListener listener = new BufferedMutator.ExceptionListener() {
047    @Override
048    public void onException(RetriesExhaustedWithDetailsException exception,
049      BufferedMutator bufferedMutator) throws RetriesExhaustedWithDetailsException {
050      throw exception;
051    }
052  };
053
054  public BufferedMutatorParams(TableName tableName) {
055    this.tableName = tableName;
056  }
057
058  public TableName getTableName() {
059    return tableName;
060  }
061
062  public long getWriteBufferSize() {
063    return writeBufferSize;
064  }
065
066  public BufferedMutatorParams rpcTimeout(final int rpcTimeout) {
067    this.rpcTimeout = rpcTimeout;
068    return this;
069  }
070
071  public int getRpcTimeout() {
072    return rpcTimeout;
073  }
074
075  public BufferedMutatorParams operationTimeout(final int operationTimeout) {
076    this.operationTimeout = operationTimeout;
077    return this;
078  }
079
080  /**
081   * @deprecated Since 2.3.0, will be removed in 4.0.0. Use {@link #operationTimeout(int)}
082   */
083  @Deprecated
084  public BufferedMutatorParams opertationTimeout(final int operationTimeout) {
085    this.operationTimeout = operationTimeout;
086    return this;
087  }
088
089  public int getOperationTimeout() {
090    return operationTimeout;
091  }
092
093  /**
094   * Set the maximum number of mutations that this buffered mutator will buffer before flushing
095   * them. If you are talking to a cluster that uses hbase.rpc.rows.size.threshold.reject to reject
096   * large Multi requests, you may need this setting to avoid rejections. Default is no limit.
097   */
098  public BufferedMutatorParams setMaxMutations(int maxMutations) {
099    this.maxMutations = maxMutations;
100    return this;
101  }
102
103  /**
104   * The maximum number of mutations that this buffered mutator will buffer before flushing them
105   */
106  public int getMaxMutations() {
107    return maxMutations;
108  }
109
110  public BufferedMutatorParams setRequestAttribute(String key, byte[] value) {
111    if (requestAttributes.isEmpty()) {
112      requestAttributes = new HashMap<>();
113    }
114    requestAttributes.put(key, value);
115    return this;
116  }
117
118  public Map<String, byte[]> getRequestAttributes() {
119    return requestAttributes;
120  }
121
122  /**
123   * Override the write buffer size specified by the provided {@link Connection}'s
124   * {@link org.apache.hadoop.conf.Configuration} instance, via the configuration key
125   * {@code hbase.client.write.buffer}.
126   */
127  public BufferedMutatorParams writeBufferSize(long writeBufferSize) {
128    this.writeBufferSize = writeBufferSize;
129    return this;
130  }
131
132  public long getWriteBufferPeriodicFlushTimeoutMs() {
133    return writeBufferPeriodicFlushTimeoutMs;
134  }
135
136  /**
137   * Set the max timeout before the buffer is automatically flushed.
138   */
139  public BufferedMutatorParams setWriteBufferPeriodicFlushTimeoutMs(long timeoutMs) {
140    this.writeBufferPeriodicFlushTimeoutMs = timeoutMs;
141    return this;
142  }
143
144  /**
145   * @deprecated Since 3.0.0, will be removed in 4.0.0. We use a common timer in the whole client
146   *             implementation so you can not set it any more.
147   */
148  @Deprecated
149  public long getWriteBufferPeriodicFlushTimerTickMs() {
150    return writeBufferPeriodicFlushTimerTickMs;
151  }
152
153  /**
154   * Set the TimerTick how often the buffer timeout if checked.
155   * @deprecated Since 3.0.0, will be removed in 4.0.0. We use a common timer in the whole client
156   *             implementation so you can not set it any more.
157   */
158  @Deprecated
159  public BufferedMutatorParams setWriteBufferPeriodicFlushTimerTickMs(long timerTickMs) {
160    this.writeBufferPeriodicFlushTimerTickMs = timerTickMs;
161    return this;
162  }
163
164  public int getMaxKeyValueSize() {
165    return maxKeyValueSize;
166  }
167
168  /**
169   * Override the maximum key-value size specified by the provided {@link Connection}'s
170   * {@link org.apache.hadoop.conf.Configuration} instance, via the configuration key
171   * {@code hbase.client.keyvalue.maxsize}.
172   */
173  public BufferedMutatorParams maxKeyValueSize(int maxKeyValueSize) {
174    this.maxKeyValueSize = maxKeyValueSize;
175    return this;
176  }
177
178  /**
179   * @deprecated Since 3.0.0-alpha-2, will be removed in 4.0.0. You can not set it anymore.
180   *             BufferedMutator will use Connection's ExecutorService.
181   */
182  @Deprecated
183  public ExecutorService getPool() {
184    return pool;
185  }
186
187  /**
188   * Override the default executor pool defined by the {@code hbase.htable.threads.*} configuration
189   * values.
190   * @deprecated Since 3.0.0-alpha-2, will be removed in 4.0.0. You can not set it anymore.
191   *             BufferedMutator will use Connection's ExecutorService.
192   */
193  @Deprecated
194  public BufferedMutatorParams pool(ExecutorService pool) {
195    this.pool = pool;
196    return this;
197  }
198
199  /**
200   * @return Name of the class we will use when we construct a {@link BufferedMutator} instance or
201   *         null if default implementation.
202   * @deprecated Since 3.0.0, will be removed in 4.0.0. You can not set it any more as the
203   *             implementation has to use too many internal stuffs in HBase.
204   */
205  @Deprecated
206  public String getImplementationClassName() {
207    return this.implementationClassName;
208  }
209
210  /**
211   * Specify a BufferedMutator implementation other than the default.
212   * @param implementationClassName Name of the BufferedMutator implementation class
213   * @deprecated Since 3.0.0, will be removed in 4.0.0. You can not set it any more as the
214   *             implementation has to use too many internal stuffs in HBase.
215   */
216  @Deprecated
217  public BufferedMutatorParams implementationClassName(String implementationClassName) {
218    this.implementationClassName = implementationClassName;
219    return this;
220  }
221
222  public BufferedMutator.ExceptionListener getListener() {
223    return listener;
224  }
225
226  /**
227   * Override the default error handler. Default handler simply rethrows the exception.
228   */
229  public BufferedMutatorParams listener(BufferedMutator.ExceptionListener listener) {
230    this.listener = listener;
231    return this;
232  }
233
234  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "CN_IDIOM_NO_SUPER_CALL",
235      justification = "The clone below is complete")
236  @Override
237  public BufferedMutatorParams clone() {
238    BufferedMutatorParams clone = new BufferedMutatorParams(this.tableName);
239    clone.writeBufferSize = this.writeBufferSize;
240    clone.writeBufferPeriodicFlushTimeoutMs = this.writeBufferPeriodicFlushTimeoutMs;
241    clone.writeBufferPeriodicFlushTimerTickMs = this.writeBufferPeriodicFlushTimerTickMs;
242    clone.maxKeyValueSize = this.maxKeyValueSize;
243    clone.maxMutations = this.maxMutations;
244    clone.pool = this.pool;
245    clone.listener = this.listener;
246    clone.implementationClassName = this.implementationClassName;
247    return clone;
248  }
249}