Skip to content

Commit 87ee585

Browse files
committed
HBASE-25455 Add trace support for HRegion read/write operation
1 parent 587481e commit 87ee585

File tree

8 files changed

+393
-143
lines changed

8 files changed

+393
-143
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ private <T> CompletableFuture<T> tracedLocationFuture(Supplier<CompletableFuture
103103
CompletableFuture<T> future = action.get();
104104
FutureUtils.addListener(future, (resp, error) -> {
105105
if (error != null) {
106-
span.recordException(error);
107-
span.setStatus(StatusCode.ERROR);
106+
TraceUtil.setError(span, error);
108107
} else {
109108
List<String> regionNames = getRegionNames.apply(resp);
110109
if (!regionNames.isEmpty()) {

hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/AbstractRpcClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,7 @@ public void run(Call call) {
424424
onCallFinished(call, hrc, addr, callback);
425425
} finally {
426426
if (hrc.failed()) {
427-
span.setStatus(StatusCode.ERROR);
428-
span.recordException(hrc.getFailed());
427+
TraceUtil.setError(span, hrc.getFailed());
429428
} else {
430429
span.setStatus(StatusCode.OK);
431430
}

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.opentelemetry.api.trace.attributes.SemanticAttributes;
2727
import io.opentelemetry.context.Context;
2828
import io.opentelemetry.context.Scope;
29+
import java.io.IOException;
2930
import java.util.List;
3031
import java.util.concurrent.CompletableFuture;
3132
import java.util.function.Supplier;
@@ -139,14 +140,18 @@ public static <T> List<CompletableFuture<T>> tracedFutures(
139140
}
140141
}
141142

143+
public static void setError(Span span, Throwable error) {
144+
span.recordException(error);
145+
span.setStatus(StatusCode.ERROR);
146+
}
147+
142148
/**
143149
* Finish the {@code span} when the given {@code future} is completed.
144150
*/
145151
private static void endSpan(CompletableFuture<?> future, Span span) {
146152
FutureUtils.addListener(future, (resp, error) -> {
147153
if (error != null) {
148-
span.recordException(error);
149-
span.setStatus(StatusCode.ERROR);
154+
setError(span, error);
150155
} else {
151156
span.setStatus(StatusCode.OK);
152157
}
@@ -164,8 +169,32 @@ public static void trace(Runnable action, Supplier<Span> creator) {
164169
action.run();
165170
span.setStatus(StatusCode.OK);
166171
} catch (Throwable e) {
167-
span.recordException(e);
168-
span.setStatus(StatusCode.ERROR);
172+
setError(span, e);
173+
throw e;
174+
} finally {
175+
span.end();
176+
}
177+
}
178+
179+
@FunctionalInterface
180+
public interface IOExceptionCallable<V> {
181+
V call() throws IOException;
182+
}
183+
184+
public static <T> T trace(IOExceptionCallable<T> callable, String spanName) throws IOException {
185+
return trace(callable, () -> createSpan(spanName));
186+
}
187+
188+
public static <T> T trace(IOExceptionCallable<T> callable, Supplier<Span> creator)
189+
throws IOException {
190+
Span span = creator.get();
191+
try (Scope scope = span.makeCurrent()) {
192+
T ret = callable.call();
193+
span.setStatus(StatusCode.OK);
194+
return ret;
195+
} catch (Throwable e) {
196+
setError(span, e);
197+
throw e;
169198
} finally {
170199
span.end();
171200
}

hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/CallRunner.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,10 @@ public void run() {
135135
resultPair = this.rpcServer.call(call, this.status);
136136
} catch (TimeoutIOException e){
137137
RpcServer.LOG.warn("Can not complete this request in time, drop it: " + call);
138-
span.recordException(e);
139-
span.setStatus(StatusCode.ERROR);
138+
TraceUtil.setError(span, e);
140139
return;
141140
} catch (Throwable e) {
142-
span.recordException(e);
143-
span.setStatus(StatusCode.ERROR);
141+
TraceUtil.setError(span, e);
144142
if (e instanceof ServerNotRunningYetException) {
145143
// If ServerNotRunningYetException, don't spew stack trace.
146144
if (RpcServer.LOG.isTraceEnabled()) {

hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/ServerCall.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.hadoop.hbase.io.ByteBufferListOutputStream;
3434
import org.apache.hadoop.hbase.ipc.RpcServer.CallCleanup;
3535
import org.apache.hadoop.hbase.security.User;
36+
import org.apache.hadoop.hbase.trace.TraceUtil;
3637
import org.apache.hadoop.hbase.util.ByteBufferUtils;
3738
import org.apache.hadoop.hbase.util.Bytes;
3839
import org.apache.hadoop.util.StringUtils;
@@ -232,8 +233,7 @@ public synchronized void setResponse(Message m, final CellScanner cells, Throwab
232233
}
233234
if (t != null) {
234235
this.isError = true;
235-
span.recordException(t);
236-
span.setStatus(StatusCode.ERROR);
236+
TraceUtil.setError(span, t);
237237
} else {
238238
span.setStatus(StatusCode.OK);
239239
}

0 commit comments

Comments
 (0)