Skip to content

Commit

Permalink
perf: various minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Jan 14, 2024
1 parent 999ea14 commit 86ca79e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2530,8 +2530,8 @@ public PooledSessionFuture getSession() throws SpannerException {
} else {
span.addAnnotation("Acquired session");
}
return checkoutSession(span, sess, waiter);
}
return checkoutSession(span, sess, waiter);
}

Session getRandomSession() {
Expand All @@ -2544,7 +2544,7 @@ private PooledSessionFuture checkoutSession(
if (waiter != null) {
logger.log(
Level.FINE,
"No session available in the pool. Blocking for one to become available/created");
() -> "No session available in the pool. Blocking for one to become available/created");
span.addAnnotation("Waiting for a session to come available");
pooledSessionFuture = createPooledSessionFuture(waiter, span);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static class Builder extends SessionPoolOptions.Builder {
private void setDefaults() {
disableInactiveTransactionActions();
setAutoDetectDialect(true);
setTrackStackTraceOfSessionCheckout(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.spanner.spi.v1;

import static com.google.cloud.spanner.ThreadFactoryUtil.createVirtualOrDaemonThreadFactory;
import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.DATABASE_ID;
import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.INSTANCE_ID;
import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.METHOD;
Expand All @@ -37,6 +38,8 @@
import io.opencensus.tags.TagValue;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.Tags;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -64,6 +67,10 @@ class HeaderInterceptor implements ClientInterceptor {
private static final Logger LOGGER = Logger.getLogger(HeaderInterceptor.class.getName());
private static final Level LEVEL = Level.INFO;

private static final ExecutorService EXECUTOR_SERVICE =
Executors.newSingleThreadExecutor(
createVirtualOrDaemonThreadFactory("server-timing-interceptor-handler", false));

HeaderInterceptor() {}

@Override
Expand All @@ -72,13 +79,15 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
TagContext tagContext = getTagContext(headers, method.getFullMethodName());
super.start(
new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onHeaders(Metadata metadata) {

processHeader(metadata, tagContext);
EXECUTOR_SERVICE.submit(
() -> {
TagContext tagContext = getTagContext(headers, method.getFullMethodName());
processHeader(metadata, tagContext);
});
super.onHeaders(metadata);
}
},
Expand All @@ -89,8 +98,8 @@ public void onHeaders(Metadata metadata) {

private void processHeader(Metadata metadata, TagContext tagContext) {
MeasureMap measureMap = STATS_RECORDER.newMeasureMap();
if (metadata.get(SERVER_TIMING_HEADER_KEY) != null) {
String serverTiming = metadata.get(SERVER_TIMING_HEADER_KEY);
String serverTiming = metadata.get(SERVER_TIMING_HEADER_KEY);
if (serverTiming != null) {
Matcher matcher = SERVER_TIMING_HEADER_PATTERN.matcher(serverTiming);
if (matcher.find()) {
try {
Expand Down Expand Up @@ -122,8 +131,8 @@ private TagContext getTagContext(Metadata headers, String method) {
String projectId = "undefined-project";
String instanceId = "undefined-database";
String databaseId = "undefined-database";
if (headers.get(GOOGLE_CLOUD_RESOURCE_PREFIX_KEY) != null) {
String googleResourcePrefix = headers.get(GOOGLE_CLOUD_RESOURCE_PREFIX_KEY);
String googleResourcePrefix = headers.get(GOOGLE_CLOUD_RESOURCE_PREFIX_KEY);
if (googleResourcePrefix != null) {
Matcher matcher = GOOGLE_CLOUD_RESOURCE_PREFIX_PATTERN.matcher(googleResourcePrefix);
if (matcher.find()) {
projectId = matcher.group("project");
Expand Down

0 comments on commit 86ca79e

Please sign in to comment.