Skip to content

Commit ae9c8ad

Browse files
authored
perf: only capture the call stack if the call is actually async (#2471)
Only capture the call stack of a caller of one of the Connection API methods if the top-level call was an async call. For synchronous calls, we do not need to additionally capture the call stack, as any exception will include the current call stack of the synchronous method call.
1 parent 7c23729 commit ae9c8ad

16 files changed

+562
-260
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/AbstractBaseUnitOfWork.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ public void cancel() {
177177
}
178178

179179
<T> ApiFuture<T> executeStatementAsync(
180+
CallType callType,
180181
ParsedStatement statement,
181182
Callable<T> callable,
182183
@Nullable MethodDescriptor<?, ?> applyStatementTimeoutToMethod) {
183184
return executeStatementAsync(
185+
callType,
184186
statement,
185187
callable,
186188
InterceptorsUsage.INVOKE_INTERCEPTORS,
@@ -190,11 +192,16 @@ <T> ApiFuture<T> executeStatementAsync(
190192
}
191193

192194
<T> ApiFuture<T> executeStatementAsync(
195+
CallType callType,
193196
ParsedStatement statement,
194197
Callable<T> callable,
195198
Collection<MethodDescriptor<?, ?>> applyStatementTimeoutToMethods) {
196199
return executeStatementAsync(
197-
statement, callable, InterceptorsUsage.INVOKE_INTERCEPTORS, applyStatementTimeoutToMethods);
200+
callType,
201+
statement,
202+
callable,
203+
InterceptorsUsage.INVOKE_INTERCEPTORS,
204+
applyStatementTimeoutToMethods);
198205
}
199206

200207
<ResponseT, MetadataT> ResponseT getWithStatementTimeout(
@@ -237,6 +244,7 @@ <ResponseT, MetadataT> ResponseT getWithStatementTimeout(
237244
}
238245

239246
<T> ApiFuture<T> executeStatementAsync(
247+
CallType callType,
240248
ParsedStatement statement,
241249
Callable<T> callable,
242250
InterceptorsUsage interceptorUsage,
@@ -268,13 +276,17 @@ public <ReqT, RespT> ApiCallContext configure(
268276
}
269277
ApiFuture<T> f = statementExecutor.submit(context.wrap(callable));
270278
final SpannerAsyncExecutionException caller =
271-
new SpannerAsyncExecutionException(statement.getStatement());
279+
callType == CallType.ASYNC
280+
? new SpannerAsyncExecutionException(statement.getStatement())
281+
: null;
272282
final ApiFuture<T> future =
273283
ApiFutures.catching(
274284
f,
275285
Throwable.class,
276286
input -> {
277-
input.addSuppressed(caller);
287+
if (caller != null) {
288+
input.addSuppressed(caller);
289+
}
278290
throw SpannerExceptionFactory.asSpannerException(input);
279291
},
280292
MoreExecutors.directExecutor());

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/AbstractMultiUseTransaction.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,21 @@ public boolean isActive() {
102102
* Check that the current transaction actually has a valid underlying transaction. If not, the
103103
* method will throw a {@link SpannerException}.
104104
*/
105-
abstract void checkValidTransaction();
105+
abstract void checkValidTransaction(CallType callType);
106106

107107
/** Returns the {@link ReadContext} that can be used for queries on this transaction. */
108108
abstract ReadContext getReadContext();
109109

110+
@Override
110111
public ApiFuture<ResultSet> executeQueryAsync(
112+
final CallType callType,
111113
final ParsedStatement statement,
112114
final AnalyzeMode analyzeMode,
113115
final QueryOption... options) {
114116
Preconditions.checkArgument(statement.isQuery(), "Statement is not a query");
115-
checkValidTransaction();
117+
checkValidTransaction(callType);
116118
return executeStatementAsync(
119+
callType,
117120
statement,
118121
() -> {
119122
checkAborted();
@@ -133,7 +136,7 @@ ResultSet internalExecuteQuery(
133136
}
134137

135138
@Override
136-
public ApiFuture<long[]> runBatchAsync() {
139+
public ApiFuture<long[]> runBatchAsync(CallType callType) {
137140
throw SpannerExceptionFactory.newSpannerException(
138141
ErrorCode.FAILED_PRECONDITION, "Run batch is not supported for transactions");
139142
}

0 commit comments

Comments
 (0)