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 BufferedMutator.ExceptionListener listener = new BufferedMutator.ExceptionListener() {
042    @Override
043    public void onException(RetriesExhaustedWithDetailsException exception,
044      BufferedMutator bufferedMutator) throws RetriesExhaustedWithDetailsException {
045      throw exception;
046    }
047  };
048
049  public BufferedMutatorParams(TableName tableName) {
050    this.tableName = tableName;
051  }
052
053  public TableName getTableName() {
054    return tableName;
055  }
056
057  public long getWriteBufferSize() {
058    return writeBufferSize;
059  }
060
061  public BufferedMutatorParams rpcTimeout(final int rpcTimeout) {
062    this.rpcTimeout = rpcTimeout;
063    return this;
064  }
065
066  public int getRpcTimeout() {
067    return rpcTimeout;
068  }
069
070  public BufferedMutatorParams operationTimeout(final int operationTimeout) {
071    this.operationTimeout = operationTimeout;
072    return this;
073  }
074
075  /**
076   * @deprecated Since 2.3.0, will be removed in 4.0.0. Use {@link #operationTimeout(int)}
077   */
078  @Deprecated
079  public BufferedMutatorParams opertationTimeout(final int operationTimeout) {
080    this.operationTimeout = operationTimeout;
081    return this;
082  }
083
084  public int getOperationTimeout() {
085    return operationTimeout;
086  }
087
088  /**
089   * Override the write buffer size specified by the provided {@link Connection}'s
090   * {@link org.apache.hadoop.conf.Configuration} instance, via the configuration key
091   * {@code hbase.client.write.buffer}.
092   */
093  public BufferedMutatorParams writeBufferSize(long writeBufferSize) {
094    this.writeBufferSize = writeBufferSize;
095    return this;
096  }
097
098  public long getWriteBufferPeriodicFlushTimeoutMs() {
099    return writeBufferPeriodicFlushTimeoutMs;
100  }
101
102  /**
103   * Set the max timeout before the buffer is automatically flushed.
104   */
105  public BufferedMutatorParams setWriteBufferPeriodicFlushTimeoutMs(long timeoutMs) {
106    this.writeBufferPeriodicFlushTimeoutMs = timeoutMs;
107    return this;
108  }
109
110  /**
111   * @deprecated Since 3.0.0, will be removed in 4.0.0. We use a common timer in the whole client
112   *             implementation so you can not set it any more.
113   */
114  @Deprecated
115  public long getWriteBufferPeriodicFlushTimerTickMs() {
116    return writeBufferPeriodicFlushTimerTickMs;
117  }
118
119  /**
120   * Set the TimerTick how often the buffer timeout if checked.
121   * @deprecated Since 3.0.0, will be removed in 4.0.0. We use a common timer in the whole client
122   *             implementation so you can not set it any more.
123   */
124  @Deprecated
125  public BufferedMutatorParams setWriteBufferPeriodicFlushTimerTickMs(long timerTickMs) {
126    this.writeBufferPeriodicFlushTimerTickMs = timerTickMs;
127    return this;
128  }
129
130  public int getMaxKeyValueSize() {
131    return maxKeyValueSize;
132  }
133
134  /**
135   * Override the maximum key-value size specified by the provided {@link Connection}'s
136   * {@link org.apache.hadoop.conf.Configuration} instance, via the configuration key
137   * {@code hbase.client.keyvalue.maxsize}.
138   */
139  public BufferedMutatorParams maxKeyValueSize(int maxKeyValueSize) {
140    this.maxKeyValueSize = maxKeyValueSize;
141    return this;
142  }
143
144  /**
145   * @deprecated Since 3.0.0-alpha-2, will be removed in 4.0.0. You can not set it anymore.
146   *             BufferedMutator will use Connection's ExecutorService.
147   */
148  @Deprecated
149  public ExecutorService getPool() {
150    return pool;
151  }
152
153  /**
154   * Override the default executor pool defined by the {@code hbase.htable.threads.*} configuration
155   * values.
156   * @deprecated Since 3.0.0-alpha-2, will be removed in 4.0.0. You can not set it anymore.
157   *             BufferedMutator will use Connection's ExecutorService.
158   */
159  @Deprecated
160  public BufferedMutatorParams pool(ExecutorService pool) {
161    this.pool = pool;
162    return this;
163  }
164
165  /**
166   * @return Name of the class we will use when we construct a {@link BufferedMutator} instance or
167   *         null if default implementation.
168   * @deprecated Since 3.0.0, will be removed in 4.0.0. You can not set it any more as the
169   *             implementation has to use too many internal stuffs in HBase.
170   */
171  @Deprecated
172  public String getImplementationClassName() {
173    return this.implementationClassName;
174  }
175
176  /**
177   * Specify a BufferedMutator implementation other than the default.
178   * @param implementationClassName Name of the BufferedMutator implementation class
179   * @deprecated Since 3.0.0, will be removed in 4.0.0. You can not set it any more as the
180   *             implementation has to use too many internal stuffs in HBase.
181   */
182  @Deprecated
183  public BufferedMutatorParams implementationClassName(String implementationClassName) {
184    this.implementationClassName = implementationClassName;
185    return this;
186  }
187
188  public BufferedMutator.ExceptionListener getListener() {
189    return listener;
190  }
191
192  /**
193   * Override the default error handler. Default handler simply rethrows the exception.
194   */
195  public BufferedMutatorParams listener(BufferedMutator.ExceptionListener listener) {
196    this.listener = listener;
197    return this;
198  }
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.pool = this.pool;
210    clone.listener = this.listener;
211    clone.implementationClassName = this.implementationClassName;
212    return clone;
213  }
214}