View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import java.util.concurrent.ExecutorService;
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  
27  /**
28   * Parameters for instantiating a {@link BufferedMutator}.
29   */
30  @InterfaceAudience.Public
31  @InterfaceStability.Evolving
32  public class BufferedMutatorParams implements Cloneable {
33  
34    static final int UNSET = -1;
35  
36    private final TableName tableName;
37    private long writeBufferSize = UNSET;
38    private long writeBufferPeriodicFlushTimeoutMs = UNSET;
39    private long writeBufferPeriodicFlushTimerTickMs = UNSET;
40    private int maxKeyValueSize = UNSET;
41    private ExecutorService pool = null;
42    private BufferedMutator.ExceptionListener listener = new BufferedMutator.ExceptionListener() {
43      @Override
44      public void onException(RetriesExhaustedWithDetailsException exception,
45          BufferedMutator bufferedMutator)
46          throws RetriesExhaustedWithDetailsException {
47        throw exception;
48      }
49    };
50  
51    public BufferedMutatorParams(TableName tableName) {
52      this.tableName = tableName;
53    }
54  
55    public TableName getTableName() {
56      return tableName;
57    }
58  
59    public long getWriteBufferSize() {
60      return writeBufferSize;
61    }
62  
63    /**
64     * Override the write buffer size specified by the provided {@link Connection}'s
65     * {@link org.apache.hadoop.conf.Configuration} instance, via the configuration key
66     * {@code hbase.client.write.buffer}.
67     */
68    public BufferedMutatorParams writeBufferSize(long writeBufferSize) {
69      this.writeBufferSize = writeBufferSize;
70      return this;
71    }
72  
73    public long getWriteBufferPeriodicFlushTimeoutMs() {
74      return writeBufferPeriodicFlushTimeoutMs;
75    }
76  
77    /**
78     * Set the max timeout before the buffer is automatically flushed.
79     */
80    public BufferedMutatorParams setWriteBufferPeriodicFlushTimeoutMs(long timeoutMs) {
81      this.writeBufferPeriodicFlushTimeoutMs = timeoutMs;
82      return this;
83    }
84  
85    public long getWriteBufferPeriodicFlushTimerTickMs() {
86      return writeBufferPeriodicFlushTimerTickMs;
87    }
88  
89    /**
90     * Set the TimerTick how often the buffer timeout if checked.
91     */
92    public BufferedMutatorParams setWriteBufferPeriodicFlushTimerTickMs(long timerTickMs) {
93      this.writeBufferPeriodicFlushTimerTickMs = timerTickMs;
94      return this;
95    }
96  
97    public int getMaxKeyValueSize() {
98      return maxKeyValueSize;
99    }
100 
101   /**
102    * Override the maximum key-value size specified by the provided {@link Connection}'s
103    * {@link org.apache.hadoop.conf.Configuration} instance, via the configuration key
104    * {@code hbase.client.keyvalue.maxsize}.
105    */
106   public BufferedMutatorParams maxKeyValueSize(int maxKeyValueSize) {
107     this.maxKeyValueSize = maxKeyValueSize;
108     return this;
109   }
110 
111   public ExecutorService getPool() {
112     return pool;
113   }
114 
115   /**
116    * Override the default executor pool defined by the {@code hbase.htable.threads.*}
117    * configuration values.
118    */
119   public BufferedMutatorParams pool(ExecutorService pool) {
120     this.pool = pool;
121     return this;
122   }
123 
124   public BufferedMutator.ExceptionListener getListener() {
125     return listener;
126   }
127 
128   /**
129    * Override the default error handler. Default handler simply rethrows the exception.
130    */
131   public BufferedMutatorParams listener(BufferedMutator.ExceptionListener listener) {
132     this.listener = listener;
133     return this;
134   }
135 
136   /*
137    * (non-Javadoc)
138    *
139    * @see java.lang.Object#clone()
140    */
141   @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="CN_IDIOM_NO_SUPER_CALL",
142           justification="The clone below is complete")
143   @Override
144   public BufferedMutatorParams clone() {
145     BufferedMutatorParams clone = new BufferedMutatorParams(this.tableName);
146     clone.writeBufferSize                     = this.writeBufferSize;
147     clone.writeBufferPeriodicFlushTimeoutMs   = this.writeBufferPeriodicFlushTimeoutMs;
148     clone.writeBufferPeriodicFlushTimerTickMs = this.writeBufferPeriodicFlushTimerTickMs;
149     clone.maxKeyValueSize                     = this.maxKeyValueSize;
150     clone.pool                                = this.pool;
151     clone.listener                            = this.listener;
152     return clone;
153   }
154 
155 }