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