Skip to content

Commit

Permalink
Merge branch 'main' into gfe_latency
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 authored Jan 10, 2025
2 parents 63d88d7 + 7893f24 commit 588c12b
Show file tree
Hide file tree
Showing 25 changed files with 294 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/hermetic_library_generation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
with:
fetch-depth: 0
token: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
- uses: googleapis/sdk-platform-java/.github/[email protected].0
- uses: googleapis/sdk-platform-java/.github/[email protected].1
if: env.SHOULD_RUN == 'true'
with:
base_ref: ${{ github.base_ref }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unmanaged_dependency_check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
# repository
.kokoro/build.sh
- name: Unmanaged dependency check
uses: googleapis/sdk-platform-java/java-shared-dependencies/unmanaged-dependency-check@google-cloud-shared-dependencies/v3.41.0
uses: googleapis/sdk-platform-java/java-shared-dependencies/unmanaged-dependency-check@google-cloud-shared-dependencies/v3.41.1
with:
bom-path: google-cloud-spanner-bom/pom.xml
2 changes: 1 addition & 1 deletion .kokoro/presubmit/graalvm-native-17.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Configure the docker image for kokoro-trampoline.
env_vars: {
key: "TRAMPOLINE_IMAGE"
value: "gcr.io/cloud-devrel-public-resources/graalvm_sdk_platform_b:3.41.0"
value: "gcr.io/cloud-devrel-public-resources/graalvm_sdk_platform_b:3.41.1"
}

env_vars: {
Expand Down
2 changes: 1 addition & 1 deletion .kokoro/presubmit/graalvm-native.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Configure the docker image for kokoro-trampoline.
env_vars: {
key: "TRAMPOLINE_IMAGE"
value: "gcr.io/cloud-devrel-public-resources/graalvm_sdk_platform_a:3.41.0"
value: "gcr.io/cloud-devrel-public-resources/graalvm_sdk_platform_a:3.41.1"
}

env_vars: {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ If you are using Maven without the BOM, add this to your dependencies:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>6.81.1</version>
<version>6.84.0</version>
</dependency>

```
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>6.81.1</version>
<version>6.84.0</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand Down
2 changes: 1 addition & 1 deletion generation_config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gapic_generator_version: 2.51.0
googleapis_commitish: 52e410823122cf44d265c3beecb86c773db248a2
googleapis_commitish: 00196e2a68b16a864c57db2e870822875a7f1198
libraries_bom_version: 26.52.0
libraries:
- api_shortname: spanner
Expand Down
2 changes: 1 addition & 1 deletion google-cloud-spanner-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.google.cloud</groupId>
<artifactId>sdk-platform-java-config</artifactId>
<version>3.41.0</version>
<version>3.41.1</version>
</parent>

<name>Google Cloud Spanner BOM</name>
Expand Down
2 changes: 1 addition & 1 deletion google-cloud-spanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-monitoring-v3</artifactId>
<version>3.55.0</version>
<version>3.57.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;

Expand All @@ -67,6 +68,7 @@
*/
abstract class AbstractReadContext
implements ReadContext, AbstractResultSet.Listener, SessionTransaction {
private static final Logger logger = Logger.getLogger(AbstractReadContext.class.getName());

abstract static class Builder<B extends Builder<?, T>, T extends AbstractReadContext> {
private SessionImpl session;
Expand Down Expand Up @@ -797,7 +799,6 @@ CloseableIterator<PartialResultSet> startStream(
isRouteToLeader());
session.markUsed(clock.instant());
stream.setCall(call, request.getTransaction().hasBegin());
call.request(prefetchChunks);
return stream;
}

Expand Down Expand Up @@ -952,6 +953,15 @@ ResultSet readInternalWithOptions(
} else if (defaultDirectedReadOptions != null) {
builder.setDirectedReadOptions(defaultDirectedReadOptions);
}
if (readOptions.hasLockHint()) {
if (isReadOnly()) {
logger.warning(
"Lock hint is only supported for ReadWrite transactions. "
+ "Overriding lock hint to default unspecified.");
} else {
builder.setLockHint(readOptions.lockHint());
}
}
final int prefetchChunks =
readOptions.hasPrefetchChunks() ? readOptions.prefetchChunks() : defaultPrefetchChunks;
ResumableStreamIterator stream =
Expand Down Expand Up @@ -992,7 +1002,6 @@ CloseableIterator<PartialResultSet> startStream(
isRouteToLeader());
session.markUsed(clock.instant());
stream.setCall(call, /* withBeginTransaction = */ builder.getTransaction().hasBegin());
call.request(prefetchChunks);
return stream;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ interface CloseableIterator<T> extends Iterator<T> {
default boolean initiateStreaming(AsyncResultSet.StreamMessageListener streamMessageListener) {
return false;
}

/** it requests the initial prefetch chunks from gRPC stream */
default void requestPrefetchChunks() {};
}

static double valueProtoToFloat64(com.google.protobuf.Value proto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner;

import com.google.api.core.InternalApi;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.cloud.spanner.AbstractResultSet.CloseableIterator;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
Expand All @@ -39,6 +40,7 @@ class GrpcStreamIterator extends AbstractIterator<PartialResultSet>
implements CloseableIterator<PartialResultSet> {
private static final Logger logger = Logger.getLogger(GrpcStreamIterator.class.getName());
static final PartialResultSet END_OF_STREAM = PartialResultSet.newBuilder().build();
private final int prefetchChunks;
private AsyncResultSet.StreamMessageListener streamMessageListener;

private final ConsumerImpl consumer;
Expand All @@ -60,6 +62,7 @@ class GrpcStreamIterator extends AbstractIterator<PartialResultSet>
GrpcStreamIterator(
Statement statement, int prefetchChunks, boolean cancelQueryWhenClientIsClosed) {
this.statement = statement;
this.prefetchChunks = prefetchChunks;
this.consumer = new ConsumerImpl(cancelQueryWhenClientIsClosed);
// One extra to allow for END_OF_STREAM message.
this.stream = new LinkedBlockingQueue<>(prefetchChunks + 1);
Expand Down Expand Up @@ -102,6 +105,13 @@ public void close(@Nullable String message) {
}
}

@Override
@InternalApi
public void requestPrefetchChunks() {
Preconditions.checkState(call != null, "The StreamingCall object is not initialized");
call.request(prefetchChunks);
}

@Override
public boolean isWithBeginTransaction() {
return withBeginTransaction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.base.Preconditions;
import com.google.spanner.v1.DirectedReadOptions;
import com.google.spanner.v1.ReadRequest.LockHint;
import com.google.spanner.v1.ReadRequest.OrderBy;
import com.google.spanner.v1.RequestOptions.Priority;
import java.io.Serializable;
Expand Down Expand Up @@ -75,6 +76,25 @@ public static RpcOrderBy fromProto(OrderBy proto) {
}
}

public enum RpcLockHint {
UNSPECIFIED(LockHint.LOCK_HINT_UNSPECIFIED),
SHARED(LockHint.LOCK_HINT_SHARED),
EXCLUSIVE(LockHint.LOCK_HINT_EXCLUSIVE);

private final LockHint proto;

RpcLockHint(LockHint proto) {
this.proto = Preconditions.checkNotNull(proto);
}

public static RpcLockHint fromProto(LockHint proto) {
for (RpcLockHint e : RpcLockHint.values()) {
if (e.proto.equals(proto)) return e;
}
return RpcLockHint.UNSPECIFIED;
}
}

/** Marker interface to mark options applicable to both Read and Query operations */
public interface ReadAndQueryOption extends ReadOption, QueryOption {}

Expand Down Expand Up @@ -160,6 +180,10 @@ public static ReadOption orderBy(RpcOrderBy orderBy) {
return new OrderByOption(orderBy);
}

public static ReadOption lockHint(RpcLockHint orderBy) {
return new LockHintOption(orderBy);
}

/**
* Specifying this will allow the client to prefetch up to {@code prefetchChunks} {@code
* PartialResultSet} chunks for read and query. The data size of each chunk depends on the server
Expand Down Expand Up @@ -469,6 +493,7 @@ void appendToOptions(Options options) {
private DirectedReadOptions directedReadOptions;
private DecodeMode decodeMode;
private RpcOrderBy orderBy;
private RpcLockHint lockHint;

// Construction is via factory methods below.
private Options() {}
Expand Down Expand Up @@ -605,6 +630,14 @@ OrderBy orderBy() {
return orderBy == null ? null : orderBy.proto;
}

boolean hasLockHint() {
return lockHint != null;
}

LockHint lockHint() {
return lockHint == null ? null : lockHint.proto;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();
Expand Down Expand Up @@ -661,6 +694,9 @@ public String toString() {
if (orderBy != null) {
b.append("orderBy: ").append(orderBy).append(' ');
}
if (lockHint != null) {
b.append("lockHint: ").append(lockHint).append(' ');
}
return b.toString();
}

Expand Down Expand Up @@ -700,7 +736,8 @@ public boolean equals(Object o) {
&& Objects.equals(withExcludeTxnFromChangeStreams(), that.withExcludeTxnFromChangeStreams())
&& Objects.equals(dataBoostEnabled(), that.dataBoostEnabled())
&& Objects.equals(directedReadOptions(), that.directedReadOptions())
&& Objects.equals(orderBy(), that.orderBy());
&& Objects.equals(orderBy(), that.orderBy())
&& Objects.equals(lockHint(), that.lockHint());
}

@Override
Expand Down Expand Up @@ -760,6 +797,9 @@ public int hashCode() {
if (orderBy != null) {
result = 31 * result + orderBy.hashCode();
}
if (lockHint != null) {
result = 31 * result + lockHint.hashCode();
}
return result;
}

Expand Down Expand Up @@ -853,6 +893,19 @@ void appendToOptions(Options options) {
}
}

static class LockHintOption extends InternalOption implements ReadOption {
private final RpcLockHint lockHint;

LockHintOption(RpcLockHint lockHint) {
this.lockHint = lockHint;
}

@Override
void appendToOptions(Options options) {
options.lockHint = lockHint;
}
}

static final class DataBoostQueryOption extends InternalOption implements ReadAndQueryOption {

private final Boolean dataBoostEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ private void startGrpcStreaming() {
// When start a new stream set the Span as current to make the gRPC Span a child of
// this Span.
stream = checkNotNull(startStream(resumeToken, streamMessageListener));
stream.requestPrefetchChunks();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@
import io.grpc.ExperimentalApi;
import io.grpc.ManagedChannelBuilder;
import io.grpc.MethodDescriptor;
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -952,6 +956,7 @@ public static class Builder
private boolean enableEndToEndTracing = SpannerOptions.environment.isEnableEndToEndTracing();
private boolean enableBuiltInMetrics = SpannerOptions.environment.isEnableBuiltInMetrics();
private String monitoringHost = SpannerOptions.environment.getMonitoringHost();
private SslContext mTLSContext = null;

private static String createCustomClientLibToken(String token) {
return token + " " + ServiceOptions.getGoogApiClientLibName();
Expand Down Expand Up @@ -1485,6 +1490,27 @@ public Builder setEmulatorHost(String emulatorHost) {
return this;
}

/**
* Configures mTLS authentication using the provided client certificate and key files. mTLS is
* only supported for external spanner hosts.
*
* @param clientCertificate Path to the client certificate file.
* @param clientCertificateKey Path to the client private key file.
* @throws SpannerException If an error occurs while configuring the mTLS context
*/
@ExperimentalApi("https://github.com/googleapis/java-spanner/pull/3574")
public Builder useClientCert(String clientCertificate, String clientCertificateKey) {
try {
this.mTLSContext =
GrpcSslContexts.forClient()
.keyManager(new File(clientCertificate), new File(clientCertificateKey))
.build();
} catch (Exception e) {
throw SpannerExceptionFactory.asSpannerException(e);
}
return this;
}

/**
* Sets OpenTelemetry object to be used for Spanner Metrics and Traces. GlobalOpenTelemetry will
* be used as fallback if this options is not set.
Expand Down Expand Up @@ -1594,6 +1620,15 @@ public SpannerOptions build() {
// As we are using plain text, we should never send any credentials.
this.setCredentials(NoCredentials.getInstance());
}
if (mTLSContext != null) {
this.setChannelConfigurator(
builder -> {
if (builder instanceof NettyChannelBuilder) {
((NettyChannelBuilder) builder).sslContext(mTLSContext);
}
return builder;
});
}
if (this.numChannels == null) {
this.numChannels =
this.grpcGcpExtensionEnabled ? GRPC_GCP_ENABLED_DEFAULT_CHANNELS : DEFAULT_CHANNELS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner;

import com.google.api.gax.core.GaxProperties;
import com.google.cloud.spanner.Options.TagOption;
import com.google.cloud.spanner.Options.TransactionOption;
import com.google.cloud.spanner.SpannerOptions.TracingFramework;
Expand Down Expand Up @@ -46,6 +47,12 @@ class TraceWrapper {
private static final AttributeKey<List<String>> DB_STATEMENT_ARRAY_KEY =
AttributeKey.stringArrayKey("db.statement");
private static final AttributeKey<String> DB_TABLE_NAME_KEY = AttributeKey.stringKey("db.table");
private static final AttributeKey<String> GCP_CLIENT_SERVICE_KEY =
AttributeKey.stringKey("gcp.client.service");
private static final AttributeKey<String> GCP_CLIENT_VERSION_KEY =
AttributeKey.stringKey("gcp.client.version");
private static final AttributeKey<String> GCP_CLIENT_REPO_KEY =
AttributeKey.stringKey("gcp.client.repo");
private static final AttributeKey<String> THREAD_NAME_KEY = AttributeKey.stringKey("thread.name");

private final Tracer openCensusTracer;
Expand Down Expand Up @@ -204,6 +211,9 @@ Attributes createCommonAttributes(DatabaseId db) {
AttributesBuilder builder = Attributes.builder();
builder.put(DB_NAME_KEY, db.getDatabase());
builder.put(INSTANCE_NAME_KEY, db.getInstanceId().getInstance());
builder.put(GCP_CLIENT_SERVICE_KEY, "spanner");
builder.put(GCP_CLIENT_REPO_KEY, "googleapis/java-spanner");
builder.put(GCP_CLIENT_VERSION_KEY, GaxProperties.getLibraryVersion(TraceWrapper.class));
return builder.build();
}

Expand Down
Loading

0 comments on commit 588c12b

Please sign in to comment.