[HBase Javadoc对于HTable.get(List)方法非常困惑。
作为返回参数文档,我们有以下语句:
如果重试后仍然有任何失败,这些Gets的结果数组中将为null,并且将引发异常。
我不理解“ AND”:我们可以在返回的数组中有一个异常或null,不能像文档中所暗示的那样同时存在。
我从未听说过能够同时引发异常和返回内容的Java方法。
当我调用此方法时,我在代码中处理异常,但是我还必须担心结果数组中的空引用吗?
此处的文档具有误导性,因为此功能不会返回结果,并且在发生故障的情况下会同时引发错误。
我也把它挖出来是因为我也很困惑。
这里是此功能的source code:
/** {@inheritDoc} */
@Override
public Result[] get(List<Get> gets) throws IOException {
LOG.trace("get(List<>)");
Preconditions.checkNotNull(gets);
if (gets.isEmpty()) {
return new Result[0];
} else if (gets.size() == 1) {
try {
return new Result[] {get(gets.get(0))};
} catch (IOException e) {
throw createRetriesExhaustedWithDetailsException(e, gets.get(0));
}
} else {
try (Scope scope = TRACER.spanBuilder("BigtableTable.get").startScopedSpan()) {
addBatchSizeAnnotation(gets);
return getBatchExecutor().batch(gets);
}
}
}
好,如果列表中包含多个项目,则调用getBatchExecutor().batch(gets)
,该函数的定义如下:
getBatchExecutor().batch(gets)
请参阅此处的评论:
至此,我们保证数组仅包含结果,如果有任何错误,批处理将引发异常
这意味着此函数仅在失败时抛出IOException,而没有关于结果的更多信息。但是,您如何查找批次中失败和正确处理的项目?原来有 public Result[] batch(List<? extends Row> actions) throws IOException {
try {
Object[] resultsOrErrors = new Object[actions.size()];
batchCallback(actions, resultsOrErrors, null);
// At this point we are guaranteed that the array only contains results,
// if it had any errors, batch would've thrown an exception
Result[] results = new Result[resultsOrErrors.length];
System.arraycopy(resultsOrErrors, 0, results, 0, results.length);
return results;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Encountered exception in batch(List<>).", e);
throw new IOException("Batch error", e);
}
}
支持这种情况:
another version of the functionbatch
defined onTable
以及batch
中对应的Table
:
/** {@inheritDoc} */
@Override
public void batch(List<? extends Row> actions, Object[] results)
throws IOException, InterruptedException {
LOG.trace("batch(List<>, Object[])");
try (Scope scope = TRACER.spanBuilder("BigtableTable.batch").startScopedSpan()) {
addBatchSizeAnnotation(actions);
getBatchExecutor().batch(actions, results);
}
}
使用此功能,您传入(空)definition数组,结果输入时该数组将被填充。最后,如果批处理中的任何项目失败,则将出现异常,并带有详细信息关于它们为什么失败的原因,但是仍然BatchExecutor
数组中充满了所有项目的结果。失败的项目将在该数组中为 public void batch(List<? extends Row> actions, @Nullable Object[] results)
throws IOException, InterruptedException {
batchCallback(actions, results, null);
}
public <R> void batchCallback(
List<? extends Row> actions, Object[] results, Batch.Callback<R> callback)
throws IOException, InterruptedException {
Preconditions.checkArgument(
results == null || results.length == actions.size(),
"Result array must have same dimensions as actions list.");
if (actions.isEmpty()) {
return;
}
if (results == null) {
results = new Object[actions.size()];
}
Timer.Context timerContext = batchTimer.time();
List<ApiFuture<?>> resultFutures = issueAsyncRowRequests(actions, results, callback);
// Don't want to throw an exception for failed futures, instead the place in results is
// set to null.
List<Throwable> problems = new ArrayList<>();
List<Row> problemActions = new ArrayList<>();
List<String> hosts = new ArrayList<>();
for (int i = 0; i < resultFutures.size(); i++) {
try {
resultFutures.get(i).get();
} catch (ExecutionException e) {
problemActions.add(actions.get(i));
problems.add(e.getCause());
hosts.add(options.getDataHost());
}
}
if (problems.size() > 0) {
throw new RetriesExhaustedWithDetailsException(problems, problemActions, hosts);
}
timerContext.close();
}
,而其余项包含实际的results
。