From d839491c8cba3318bfc63678011b34a99b06d339 Mon Sep 17 00:00:00 2001 From: Nick Dimiduk Date: Fri, 18 Feb 2022 16:04:51 +0100 Subject: [PATCH] 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. --- .../hbase/client/AsyncConnectionImpl.java | 12 +--- .../apache/hadoop/hbase/trace/TraceUtil.java | 70 +++++++++++-------- .../hbase/regionserver/wal/AbstractFSWAL.java | 10 +-- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionImpl.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionImpl.java index 506962fd2f8a..4de9a2c4ac58 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionImpl.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionImpl.java @@ -27,7 +27,6 @@ import static org.apache.hadoop.hbase.client.NonceGenerator.CLIENT_NONCES_ENABLED_KEY; import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.SERVER_NAME_KEY; import static org.apache.hadoop.hbase.util.FutureUtils.addListener; - import io.opentelemetry.api.trace.Span; import java.io.IOException; import java.net.SocketAddress; @@ -40,7 +39,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; -import java.util.function.Supplier; import org.apache.commons.io.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.AuthUtil; @@ -61,10 +59,8 @@ import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; import org.apache.hbase.thirdparty.io.netty.util.HashedWheelTimer; - import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService; import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos; @@ -444,13 +440,7 @@ public CompletableFuture getHbck() { @Override public Hbck getHbck(ServerName masterServer) { - return TraceUtil.trace(new Supplier() { - - @Override - public Hbck get() { - return getHbckInternal(masterServer); - } - }, "AsyncConnection.getHbck"); + return TraceUtil.trace(() -> getHbckInternal(masterServer), "AsyncConnection.getHbck"); } Optional getConnectionMetrics() { diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/trace/TraceUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/trace/TraceUtil.java index 27f195df792f..7aa7988017fc 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/trace/TraceUtil.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/trace/TraceUtil.java @@ -24,8 +24,8 @@ import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; -import java.io.IOException; import java.util.List; +import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; import org.apache.hadoop.hbase.Version; @@ -84,7 +84,7 @@ public static CompletableFuture tracedFuture( Supplier spanSupplier ) { Span span = spanSupplier.get(); - try (Scope scope = span.makeCurrent()) { + try (Scope ignored = span.makeCurrent()) { CompletableFuture future = action.get(); endSpan(future, span); return future; @@ -97,7 +97,7 @@ public static CompletableFuture tracedFuture( public static CompletableFuture tracedFuture(Supplier> action, String spanName) { Span span = createSpan(spanName); - try (Scope scope = span.makeCurrent()) { + try (Scope ignored = span.makeCurrent()) { CompletableFuture future = action.get(); endSpan(future, span); return future; @@ -113,7 +113,7 @@ public static List> tracedFutures( Supplier spanSupplier ) { Span span = spanSupplier.get(); - try (Scope scope = span.makeCurrent()) { + try (Scope ignored = span.makeCurrent()) { List> futures = action.get(); endSpan(CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])), span); return futures; @@ -139,29 +139,29 @@ private static void endSpan(CompletableFuture future, Span span) { }); } - public static void trace(Runnable action, String spanName) { - trace(action, () -> createSpan(spanName)); + /** + * A {@link Runnable} that may also throw. + * @param the type of {@link Throwable} that can be produced. + */ + @FunctionalInterface + public interface ThrowingRunnable { + void run() throws T; } - public static void trace(Runnable action, Supplier creator) { - Span span = creator.get(); - try (Scope scope = span.makeCurrent()) { - action.run(); - span.setStatus(StatusCode.OK); - } catch (Throwable e) { - setError(span, e); - throw e; - } finally { - span.end(); - } + public static void trace( + final ThrowingRunnable runnable, + final String spanName) throws T { + trace(runnable, () -> createSpan(spanName)); } - public static T trace(Supplier action, String spanName) { - Span span = createSpan(spanName); - try (Scope scope = span.makeCurrent()) { - T ret = action.get(); + public static void trace( + final ThrowingRunnable runnable, + final Supplier spanSupplier + ) throws T { + Span span = spanSupplier.get(); + try (Scope ignored = span.makeCurrent()) { + runnable.run(); span.setStatus(StatusCode.OK); - return ret; } catch (Throwable e) { setError(span, e); throw e; @@ -170,20 +170,30 @@ public static T trace(Supplier action, String spanName) { } } + /** + * A {@link Callable} that may also throw. + * @param the result type of method call. + * @param the type of {@link Throwable} that can be produced. + */ @FunctionalInterface - public interface IOExceptionCallable { - V call() throws IOException; + public interface ThrowingCallable { + R call() throws T; } - public static T trace(IOExceptionCallable callable, String spanName) throws IOException { + public static R trace( + final ThrowingCallable callable, + final String spanName + ) throws T { return trace(callable, () -> createSpan(spanName)); } - public static T trace(IOExceptionCallable callable, Supplier creator) - throws IOException { - Span span = creator.get(); - try (Scope scope = span.makeCurrent()) { - T ret = callable.call(); + public static R trace( + final ThrowingCallable callable, + final Supplier spanSupplier + ) throws T { + Span span = spanSupplier.get(); + try (Scope ignored = span.makeCurrent()) { + final R ret = callable.call(); span.setStatus(StatusCode.OK); return ret; } catch (Throwable e) { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/AbstractFSWAL.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/AbstractFSWAL.java index 6d1ef9632930..de8a6af6a80d 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/AbstractFSWAL.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/AbstractFSWAL.java @@ -590,18 +590,12 @@ public final void sync(long txid) throws IOException { @Override public final void sync(boolean forceSync) throws IOException { - TraceUtil.trace(() -> { - doSync(forceSync); - return null; - }, () -> createSpan("WAL.sync")); + TraceUtil.trace(() -> doSync(forceSync), () -> createSpan("WAL.sync")); } @Override public final void sync(long txid, boolean forceSync) throws IOException { - TraceUtil.trace(() -> { - doSync(txid, forceSync); - return null; - }, () -> createSpan("WAL.sync")); + TraceUtil.trace(() -> doSync(txid, forceSync), () -> createSpan("WAL.sync")); } protected abstract void doSync(boolean forceSync) throws IOException;