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