Skip to content

Commit ee31ae0

Browse files
committed
HBASE-26764 Implement generic exception support for TraceUtil methods over Callables and Runnables
For the `TraceUtil` methods that accept `Callable` and `Runnable` types, make them generic over a child of `Throwable`. This allows us to consolidate the two method signatures into a single more flexible definition.
1 parent 55ea2ef commit ee31ae0

File tree

3 files changed

+43
-49
lines changed

3 files changed

+43
-49
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionImpl.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static org.apache.hadoop.hbase.client.NonceGenerator.CLIENT_NONCES_ENABLED_KEY;
2828
import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.SERVER_NAME_KEY;
2929
import static org.apache.hadoop.hbase.util.FutureUtils.addListener;
30-
3130
import io.opentelemetry.api.trace.Span;
3231
import java.io.IOException;
3332
import java.net.SocketAddress;
@@ -40,7 +39,6 @@
4039
import java.util.concurrent.TimeUnit;
4140
import java.util.concurrent.atomic.AtomicBoolean;
4241
import java.util.concurrent.atomic.AtomicReference;
43-
import java.util.function.Supplier;
4442
import org.apache.commons.io.IOUtils;
4543
import org.apache.hadoop.conf.Configuration;
4644
import org.apache.hadoop.hbase.AuthUtil;
@@ -61,10 +59,8 @@
6159
import org.apache.yetus.audience.InterfaceAudience;
6260
import org.slf4j.Logger;
6361
import org.slf4j.LoggerFactory;
64-
6562
import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
6663
import org.apache.hbase.thirdparty.io.netty.util.HashedWheelTimer;
67-
6864
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService;
6965
import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService;
7066
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos;
@@ -444,13 +440,7 @@ public CompletableFuture<Hbck> getHbck() {
444440

445441
@Override
446442
public Hbck getHbck(ServerName masterServer) {
447-
return TraceUtil.trace(new Supplier<Hbck>() {
448-
449-
@Override
450-
public Hbck get() {
451-
return getHbckInternal(masterServer);
452-
}
453-
}, "AsyncConnection.getHbck");
443+
return TraceUtil.trace(() -> getHbckInternal(masterServer), "AsyncConnection.getHbck");
454444
}
455445

456446
Optional<MetricsConnection> getConnectionMetrics() {

hbase-common/src/main/java/org/apache/hadoop/hbase/trace/TraceUtil.java

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import io.opentelemetry.api.trace.Tracer;
2525
import io.opentelemetry.context.Context;
2626
import io.opentelemetry.context.Scope;
27-
import java.io.IOException;
2827
import java.util.List;
28+
import java.util.concurrent.Callable;
2929
import java.util.concurrent.CompletableFuture;
3030
import java.util.function.Supplier;
3131
import org.apache.hadoop.hbase.Version;
@@ -84,7 +84,7 @@ public static <T> CompletableFuture<T> tracedFuture(
8484
Supplier<Span> spanSupplier
8585
) {
8686
Span span = spanSupplier.get();
87-
try (Scope scope = span.makeCurrent()) {
87+
try (Scope ignored = span.makeCurrent()) {
8888
CompletableFuture<T> future = action.get();
8989
endSpan(future, span);
9090
return future;
@@ -97,7 +97,7 @@ public static <T> CompletableFuture<T> tracedFuture(
9797
public static <T> CompletableFuture<T> tracedFuture(Supplier<CompletableFuture<T>> action,
9898
String spanName) {
9999
Span span = createSpan(spanName);
100-
try (Scope scope = span.makeCurrent()) {
100+
try (Scope ignored = span.makeCurrent()) {
101101
CompletableFuture<T> future = action.get();
102102
endSpan(future, span);
103103
return future;
@@ -113,7 +113,7 @@ public static <T> List<CompletableFuture<T>> tracedFutures(
113113
Supplier<Span> spanSupplier
114114
) {
115115
Span span = spanSupplier.get();
116-
try (Scope scope = span.makeCurrent()) {
116+
try (Scope ignored = span.makeCurrent()) {
117117
List<CompletableFuture<T>> futures = action.get();
118118
endSpan(CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])), span);
119119
return futures;
@@ -139,29 +139,29 @@ private static void endSpan(CompletableFuture<?> future, Span span) {
139139
});
140140
}
141141

142-
public static void trace(Runnable action, String spanName) {
143-
trace(action, () -> createSpan(spanName));
142+
/**
143+
* A {@link Runnable} that may also throw.
144+
* @param <T> the type of {@link Throwable} that can be produced.
145+
*/
146+
@FunctionalInterface
147+
public interface ThrowingRunnable<T extends Throwable> {
148+
void run() throws T;
144149
}
145150

146-
public static void trace(Runnable action, Supplier<Span> creator) {
147-
Span span = creator.get();
148-
try (Scope scope = span.makeCurrent()) {
149-
action.run();
150-
span.setStatus(StatusCode.OK);
151-
} catch (Throwable e) {
152-
setError(span, e);
153-
throw e;
154-
} finally {
155-
span.end();
156-
}
151+
public static <T extends Throwable> void trace(
152+
final ThrowingRunnable<T> runnable,
153+
final String spanName) throws T {
154+
trace(runnable, () -> createSpan(spanName));
157155
}
158156

159-
public static <T> T trace(Supplier<T> action, String spanName) {
160-
Span span = createSpan(spanName);
161-
try (Scope scope = span.makeCurrent()) {
162-
T ret = action.get();
157+
public static <T extends Throwable> void trace(
158+
final ThrowingRunnable<T> runnable,
159+
final Supplier<Span> spanSupplier
160+
) throws T {
161+
Span span = spanSupplier.get();
162+
try (Scope ignored = span.makeCurrent()) {
163+
runnable.run();
163164
span.setStatus(StatusCode.OK);
164-
return ret;
165165
} catch (Throwable e) {
166166
setError(span, e);
167167
throw e;
@@ -170,20 +170,30 @@ public static <T> T trace(Supplier<T> action, String spanName) {
170170
}
171171
}
172172

173+
/**
174+
* A {@link Callable} that may also throw.
175+
* @param <R> the result type of method call.
176+
* @param <T> the type of {@link Throwable} that can be produced.
177+
*/
173178
@FunctionalInterface
174-
public interface IOExceptionCallable<V> {
175-
V call() throws IOException;
179+
public interface ThrowingCallable<R, T extends Throwable> {
180+
R call() throws T;
176181
}
177182

178-
public static <T> T trace(IOExceptionCallable<T> callable, String spanName) throws IOException {
183+
public static <R, T extends Throwable> R trace(
184+
final ThrowingCallable<R, T> callable,
185+
final String spanName
186+
) throws T {
179187
return trace(callable, () -> createSpan(spanName));
180188
}
181189

182-
public static <T> T trace(IOExceptionCallable<T> callable, Supplier<Span> creator)
183-
throws IOException {
184-
Span span = creator.get();
185-
try (Scope scope = span.makeCurrent()) {
186-
T ret = callable.call();
190+
public static <R, T extends Throwable> R trace(
191+
final ThrowingCallable<R, T> callable,
192+
final Supplier<Span> spanSupplier
193+
) throws T {
194+
Span span = spanSupplier.get();
195+
try (Scope ignored = span.makeCurrent()) {
196+
final R ret = callable.call();
187197
span.setStatus(StatusCode.OK);
188198
return ret;
189199
} catch (Throwable e) {

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/AbstractFSWAL.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -590,18 +590,12 @@ public final void sync(long txid) throws IOException {
590590

591591
@Override
592592
public final void sync(boolean forceSync) throws IOException {
593-
TraceUtil.trace(() -> {
594-
doSync(forceSync);
595-
return null;
596-
}, () -> createSpan("WAL.sync"));
593+
TraceUtil.trace(() -> doSync(forceSync), () -> createSpan("WAL.sync"));
597594
}
598595

599596
@Override
600597
public final void sync(long txid, boolean forceSync) throws IOException {
601-
TraceUtil.trace(() -> {
602-
doSync(txid, forceSync);
603-
return null;
604-
}, () -> createSpan("WAL.sync"));
598+
TraceUtil.trace(() -> doSync(txid, forceSync), () -> createSpan("WAL.sync"));
605599
}
606600

607601
protected abstract void doSync(boolean forceSync) throws IOException;

0 commit comments

Comments
 (0)