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  public long getWriteBufferPeriodicFlushTimerTickMs() {
147    return writeBufferPeriodicFlushTimerTickMs;
148  }
149
150  /**
151   * Set the TimerTick how often the buffer timeout if checked.
152   */
153  public BufferedMutatorParams setWriteBufferPeriodicFlushTimerTickMs(long timerTickMs) {
154    this.writeBufferPeriodicFlushTimerTickMs = timerTickMs;
155    return this;
156  }
157
158  public int getMaxKeyValueSize() {
159    return maxKeyValueSize;
160  }
161
162  /**
163   * Override the maximum key-value size specified by the provided {@link Connection}'s
164   * {@link org.apache.hadoop.conf.Configuration} instance, via the configuration key
165   * {@code hbase.client.keyvalue.maxsize}.
166   */
167  public BufferedMutatorParams maxKeyValueSize(int maxKeyValueSize) {
168    this.maxKeyValueSize = maxKeyValueSize;
169    return this;
170  }
171
172  public ExecutorService getPool() {
173    return pool;
174  }
175
176  /**
177   * Override the default executor pool defined by the {@code hbase.htable.threads.*} configuration
178   * values.
179   */
180  public BufferedMutatorParams pool(ExecutorService pool) {
181    this.pool = pool;
182    return this;
183  }
184
185  /**
186   * Returns Name of the class we will use when we construct a {@link BufferedMutator} instance or
187   * null if default implementation.
188   */
189  public String getImplementationClassName() {
190    return this.implementationClassName;
191  }
192
193  /**
194   * Specify a BufferedMutator implementation other than the default.
195   * @param implementationClassName Name of the BufferedMutator implementation class
196   */
197  public BufferedMutatorParams implementationClassName(String implementationClassName) {
198    this.implementationClassName = implementationClassName;
199    return this;
200  }
201
202  public BufferedMutator.ExceptionListener getListener() {
203    return listener;
204  }
205
206  /**
207   * Override the default error handler. Default handler simply rethrows the exception.
208   */
209  public BufferedMutatorParams listener(BufferedMutator.ExceptionListener listener) {
210    this.listener = listener;
211    return this;
212  }
213
214  /*
215   * (non-Javadoc)
216   * @see java.lang.Object#clone()
217   */
218  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "CN_IDIOM_NO_SUPER_CALL",
219      justification = "The clone below is complete")
220  @Override
221  public BufferedMutatorParams clone() {
222    BufferedMutatorParams clone = new BufferedMutatorParams(this.tableName);
223    clone.writeBufferSize = this.writeBufferSize;
224    clone.writeBufferPeriodicFlushTimeoutMs = this.writeBufferPeriodicFlushTimeoutMs;
225    clone.writeBufferPeriodicFlushTimerTickMs = this.writeBufferPeriodicFlushTimerTickMs;
226    clone.maxKeyValueSize = this.maxKeyValueSize;
227    clone.maxMutations = this.maxMutations;
228    clone.requestAttributes = Maps.newHashMap(this.requestAttributes);
229    clone.pool = this.pool;
230    clone.listener = this.listener;
231    clone.implementationClassName = this.implementationClassName;
232    return clone;
233  }
234}