-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve handling when crossing async and sync boundaries (#2976)
* feat: improve handling when crossing async and sync boundaries Previously we would blindly uses ApiExceptions.callAndTranslateApiException and Futures.getUnchecked to await future results. This created a number of problems: * It would bubble ApiExceptions & CheckExceptions to the surface, which is quite unexpected for an hbase client * It would hide hbase specific IOExceptions This PR introduces FutureUtil#unwrap, which will try to preserve the original exception. However to aid in debugging, it will augment the stacktrace to include the caller. This is a similar approach as hbase took in: https://github.com/apache/hbase/blob/5b9940907e695e20d24968cbc725277a19ce2170/hbase-common/src/main/java/org/apache/hadoop/hbase/util/FutureUtils.java#L129-L140 Unfortunately we can't just call that utility because it only exists in hbase 2x, so I inlined relevant bits * typo * fix test case * fix deps
- Loading branch information
1 parent
68c2773
commit 0a63256
Showing
8 changed files
with
297 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...-parent/bigtable-hbase/src/main/java/com/google/cloud/bigtable/hbase/util/FutureUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2021 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.cloud.bigtable.hbase.util; | ||
|
||
import com.google.api.core.InternalApi; | ||
import java.io.IOException; | ||
import java.io.InterruptedIOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
|
||
/** Helpers to handle async operations inside the hbase adapter. */ | ||
@InternalApi | ||
public class FutureUtil { | ||
|
||
/** | ||
* Extract the underlying causes for the future's failure. | ||
* | ||
* <p>The future's and the callers stacktraces will be merged to ease debugging. | ||
*/ | ||
// This functionality was extracted from HBase 2.x | ||
public static <T> T unwrap(Future<T> future) throws IOException { | ||
try { | ||
return future.get(); | ||
} catch (InterruptedException e) { | ||
throw (IOException) new InterruptedIOException().initCause(e); | ||
} catch (ExecutionException e) { | ||
throw rethrow(e.getCause()); | ||
} | ||
} | ||
|
||
private static IOException rethrow(Throwable error) throws IOException { | ||
if (error instanceof IOException) { | ||
setStackTrace(error); | ||
throw (IOException) error; | ||
} else if (error instanceof RuntimeException) { | ||
setStackTrace(error); | ||
throw (RuntimeException) error; | ||
} else if (error instanceof Error) { | ||
setStackTrace(error); | ||
throw (Error) error; | ||
} else { | ||
throw new IOException(error); | ||
} | ||
} | ||
|
||
private static void setStackTrace(Throwable error) { | ||
StackTraceElement[] localStackTrace = Thread.currentThread().getStackTrace(); | ||
StackTraceElement[] originalStackTrace = error.getStackTrace(); | ||
StackTraceElement[] newStackTrace = | ||
new StackTraceElement[localStackTrace.length + originalStackTrace.length + 1]; | ||
System.arraycopy(localStackTrace, 0, newStackTrace, 0, localStackTrace.length); | ||
newStackTrace[localStackTrace.length] = | ||
new StackTraceElement("--------Future", "get--------", null, -1); | ||
System.arraycopy( | ||
originalStackTrace, | ||
0, | ||
newStackTrace, | ||
localStackTrace.length + 1, | ||
originalStackTrace.length); | ||
error.setStackTrace(newStackTrace); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.