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.Iterator;
022import java.util.List;
023import java.util.Map;
024import java.util.concurrent.ExecutorService;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.coprocessor.Batch;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.yetus.audience.InterfaceStability;
029
030/**
031 * Contains the attributes of a task which will be executed by
032 * {@link org.apache.hadoop.hbase.client.AsyncProcess}. The attributes will be validated by
033 * AsyncProcess. It's intended for advanced client applications.
034 * @param <T> The type of response from server-side
035 */
036@InterfaceAudience.Private
037@InterfaceStability.Evolving
038public class AsyncProcessTask<T> {
039  /**
040   * The number of processed rows. The AsyncProcess has traffic control which may reject some rows.
041   */
042  public enum SubmittedRows {
043    ALL,
044    AT_LEAST_ONE,
045    NORMAL
046  }
047
048  public static <T> Builder<T> newBuilder(final Batch.Callback<T> callback) {
049    return new Builder<>(callback);
050  }
051
052  public static Builder newBuilder() {
053    return new Builder();
054  }
055
056  public static class Builder<T> {
057
058    private ExecutorService pool;
059    private TableName tableName;
060    private RowAccess<? extends Row> rows;
061    private SubmittedRows submittedRows = SubmittedRows.ALL;
062    private Batch.Callback<T> callback;
063    private boolean needResults;
064    private int rpcTimeout;
065    private int operationTimeout;
066    private CancellableRegionServerCallable callable;
067    private Object[] results;
068    private Map<String, byte[]> requestAttributes = Collections.emptyMap();
069
070    private Builder() {
071    }
072
073    private Builder(Batch.Callback<T> callback) {
074      this.callback = callback;
075    }
076
077    Builder<T> setResults(Object[] results) {
078      this.results = results;
079      if (results != null && results.length != 0) {
080        setNeedResults(true);
081      }
082      return this;
083    }
084
085    public Builder<T> setPool(ExecutorService pool) {
086      this.pool = pool;
087      return this;
088    }
089
090    public Builder<T> setRpcTimeout(int rpcTimeout) {
091      this.rpcTimeout = rpcTimeout;
092      return this;
093    }
094
095    public Builder<T> setOperationTimeout(int operationTimeout) {
096      this.operationTimeout = operationTimeout;
097      return this;
098    }
099
100    public Builder<T> setTableName(TableName tableName) {
101      this.tableName = tableName;
102      return this;
103    }
104
105    public Builder<T> setRowAccess(List<? extends Row> rows) {
106      this.rows = new ListRowAccess<>(rows);
107      return this;
108    }
109
110    public Builder<T> setRowAccess(RowAccess<? extends Row> rows) {
111      this.rows = rows;
112      return this;
113    }
114
115    public Builder<T> setSubmittedRows(SubmittedRows submittedRows) {
116      this.submittedRows = submittedRows;
117      return this;
118    }
119
120    public Builder<T> setNeedResults(boolean needResults) {
121      this.needResults = needResults;
122      return this;
123    }
124
125    Builder<T> setCallable(CancellableRegionServerCallable callable) {
126      this.callable = callable;
127      return this;
128    }
129
130    Builder<T> setRequestAttributes(Map<String, byte[]> requestAttributes) {
131      this.requestAttributes = requestAttributes;
132      return this;
133    }
134
135    public AsyncProcessTask<T> build() {
136      return new AsyncProcessTask<>(pool, tableName, rows, submittedRows, callback, callable,
137        needResults, rpcTimeout, operationTimeout, results, requestAttributes);
138    }
139  }
140
141  private final ExecutorService pool;
142  private final TableName tableName;
143  private final RowAccess<? extends Row> rows;
144  private final SubmittedRows submittedRows;
145  private final Batch.Callback<T> callback;
146  private final CancellableRegionServerCallable callable;
147  private final boolean needResults;
148  private final int rpcTimeout;
149  private final int operationTimeout;
150  private final Object[] results;
151  private final Map<String, byte[]> requestAttributes;
152
153  AsyncProcessTask(AsyncProcessTask<T> task) {
154    this(task.getPool(), task.getTableName(), task.getRowAccess(), task.getSubmittedRows(),
155      task.getCallback(), task.getCallable(), task.getNeedResults(), task.getRpcTimeout(),
156      task.getOperationTimeout(), task.getResults(), task.getRequestAttributes());
157  }
158
159  AsyncProcessTask(ExecutorService pool, TableName tableName, RowAccess<? extends Row> rows,
160    SubmittedRows size, Batch.Callback<T> callback, CancellableRegionServerCallable callable,
161    boolean needResults, int rpcTimeout, int operationTimeout, Object[] results,
162    Map<String, byte[]> requestAttributes) {
163    this.pool = pool;
164    this.tableName = tableName;
165    this.rows = rows;
166    this.submittedRows = size;
167    this.callback = callback;
168    this.callable = callable;
169    this.needResults = needResults;
170    this.rpcTimeout = rpcTimeout;
171    this.operationTimeout = operationTimeout;
172    this.results = results;
173    this.requestAttributes = requestAttributes;
174  }
175
176  public int getOperationTimeout() {
177    return operationTimeout;
178  }
179
180  public ExecutorService getPool() {
181    return pool;
182  }
183
184  public TableName getTableName() {
185    return tableName;
186  }
187
188  public RowAccess<? extends Row> getRowAccess() {
189    return rows;
190  }
191
192  public SubmittedRows getSubmittedRows() {
193    return submittedRows;
194  }
195
196  public Batch.Callback<T> getCallback() {
197    return callback;
198  }
199
200  CancellableRegionServerCallable getCallable() {
201    return callable;
202  }
203
204  public Map<String, byte[]> getRequestAttributes() {
205    return requestAttributes;
206  }
207
208  Object[] getResults() {
209    return results;
210  }
211
212  public boolean getNeedResults() {
213    return needResults;
214  }
215
216  public int getRpcTimeout() {
217    return rpcTimeout;
218  }
219
220  static class ListRowAccess<T> implements RowAccess<T> {
221
222    private final List<T> data;
223
224    ListRowAccess(final List<T> data) {
225      this.data = data;
226    }
227
228    @Override
229    public int size() {
230      return data.size();
231    }
232
233    @Override
234    public boolean isEmpty() {
235      return data.isEmpty();
236    }
237
238    @Override
239    public Iterator<T> iterator() {
240      return data.iterator();
241    }
242  }
243}