Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions java-spanner/samples/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.76.0</version>
<version>26.79.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -228,7 +228,7 @@
<archive>
<index>false</index>
<manifest>
<mainClass>com.example.spanner.admin.archived.SpannerSample</mainClass>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is intentional?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is intentional as we want to pick the newer version the mentioned class. Do you see any problems with this?

<mainClass>com.example.spanner.SpannerSample</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.spanner;

import com.google.cloud.spanner.SpannerOptions;
import com.google.common.base.Strings;

public class ExperimentalHostHelper {
private static final String EXPERIMENTAL_HOST = "spanner.experimental_host";
private static final String USE_PLAIN_TEXT = "spanner.use_plain_text";
private static final String USE_MTLS = "spanner.mtls";
private static final String CLIENT_CERT_PATH = "spanner.client_cert_path";
private static final String CLIENT_CERT_KEY_PATH = "spanner.client_cert_key_path";

/**
* Checks whether an experimental host is being used. This is done by checking if the
* {@code spanner.experimental_host} system property is set.
*
* @return true if an experimental host is being used. Returns false otherwise.
*/
public static boolean isExperimentalHost() {
return !Strings.isNullOrEmpty(System.getProperty(EXPERIMENTAL_HOST));
}

public static boolean isMtlsSetup() {
return Boolean.getBoolean(USE_MTLS);
}

public static void setExperimentalHostSpannerOptions(SpannerOptions.Builder builder) {
if (!isExperimentalHost()) {
throw new IllegalStateException(EXPERIMENTAL_HOST + " must be set to use this method");
}
String experimentalHost = System.getProperty(EXPERIMENTAL_HOST, "");
boolean usePlainText = Boolean.getBoolean(USE_PLAIN_TEXT);
builder.setExperimentalHost(experimentalHost);
builder.setBuiltInMetricsEnabled(false);
if (usePlainText) {
builder.usePlainText();
}
if (isMtlsSetup()) {
String clientCertificate = System.getProperty(CLIENT_CERT_PATH, "");
String clientKey = System.getProperty(CLIENT_CERT_KEY_PATH, "");
if (Strings.isNullOrEmpty(clientCertificate)) {
throw new IllegalArgumentException(CLIENT_CERT_PATH + " must be set when mTLS is enabled");
}
if (Strings.isNullOrEmpty(clientKey)) {
throw new IllegalArgumentException(CLIENT_CERT_KEY_PATH + " must be set when mTLS is enabled");
}
builder.useClientCert(clientCertificate, clientKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, else this cause compilation failure. essentially its missing is current version of code.


public class MutableCredentialsExample {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.example.spanner;

import static com.example.spanner.ExperimentalHostHelper.isExperimentalHost;
import static com.example.spanner.ExperimentalHostHelper.setExperimentalHostSpannerOptions;

import com.google.api.gax.paging.Page;
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
Expand Down Expand Up @@ -1192,17 +1195,19 @@ static void queryWithNumeric(DatabaseClient dbClient) {

// [START spanner_postgresql_create_client_with_query_options]
static void clientWithQueryOptions(DatabaseId db) {
SpannerOptions options =
SpannerOptions.newBuilder()
.setDefaultQueryOptions(
SpannerOptions.Builder builder = SpannerOptions.newBuilder();
builder.setDefaultQueryOptions(
db, ExecuteSqlRequest.QueryOptions
.newBuilder()
.setOptimizerVersion("1")
// The list of available statistics packages can be found by querying the
// "INFORMATION_SCHEMA.spanner_postgresql_STATISTICS" table.
.setOptimizerStatisticsPackage("latest")
.build())
.build();
.build());
if (isExperimentalHost()) {
setExperimentalHostSpannerOptions(builder);
}
SpannerOptions options = builder.build();
Spanner spanner = options.getService();
DatabaseClient dbClient = spanner.getDatabaseClient(db);
try (ResultSet resultSet =
Expand Down Expand Up @@ -1548,7 +1553,11 @@ public static void main(String[] args) {
printUsageAndExit();
}
// [START spanner_init_client]
SpannerOptions options = SpannerOptions.newBuilder().build();
SpannerOptions.Builder builder = SpannerOptions.newBuilder();
if (isExperimentalHost()) {
setExperimentalHostSpannerOptions(builder);
}
SpannerOptions options = builder.build();
Spanner spanner = options.getService();
DatabaseAdminClient dbAdminClient = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.example.spanner;

//[START spanner_query_information_schema_database_options]
// [START spanner_query_information_schema_database_options]

import static com.example.spanner.ExperimentalHostHelper.isExperimentalHost;
import static com.example.spanner.ExperimentalHostHelper.setExperimentalHostSpannerOptions;

import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
Expand All @@ -37,33 +40,34 @@ static void queryInformationSchemaDatabaseOptions() {

static void queryInformationSchemaDatabaseOptions(
String projectId, String instanceId, String databaseId) {
try (Spanner spanner = SpannerOptions
.newBuilder()
.setProjectId(projectId)
.build()
.getService()) {
SpannerOptions.Builder builder = SpannerOptions.newBuilder();
builder.setProjectId(projectId);
if (isExperimentalHost()) {
setExperimentalHostSpannerOptions(builder);
}
try (Spanner spanner = builder.build().getService()) {
final DatabaseId id = DatabaseId.of(projectId, instanceId, databaseId);
final DatabaseClient databaseClient = spanner.getDatabaseClient(id);

try (ResultSet resultSet = databaseClient
.singleUse()
.executeQuery(Statement.of(
"SELECT OPTION_NAME, OPTION_VALUE"
+ " FROM INFORMATION_SCHEMA.DATABASE_OPTIONS"
+ " WHERE OPTION_NAME = 'default_leader'")
)) {
try (ResultSet resultSet =
databaseClient
.singleUse()
.executeQuery(
Statement.of(
"SELECT OPTION_NAME, OPTION_VALUE"
+ " FROM INFORMATION_SCHEMA.DATABASE_OPTIONS"
+ " WHERE OPTION_NAME = 'default_leader'"))) {
if (resultSet.next()) {
final String optionName = resultSet.getString("OPTION_NAME");
final String optionValue = resultSet.getString("OPTION_VALUE");

System.out.println("The " + optionName + " for " + id + " is " + optionValue);
} else {
System.out.println(
"Database " + id + " does not have a value for option 'default_leader'"
);
"Database " + id + " does not have a value for option 'default_leader'");
}
}
}
}
}
//[END spanner_query_information_schema_database_options]
// [END spanner_query_information_schema_database_options]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.example.spanner;

import static com.example.spanner.ExperimentalHostHelper.isExperimentalHost;
import static com.example.spanner.ExperimentalHostHelper.setExperimentalHostSpannerOptions;

import com.google.cloud.Timestamp;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
Expand Down Expand Up @@ -554,7 +557,11 @@ public static void main(String[] args) {
if (args.length != 3 && args.length != 4) {
printUsageAndExit();
}
SpannerOptions options = SpannerOptions.newBuilder().build();
SpannerOptions.Builder builder = SpannerOptions.newBuilder();
if (isExperimentalHost()) {
setExperimentalHostSpannerOptions(builder);
}
SpannerOptions options = builder.build();
Spanner spanner = options.getService();
DatabaseAdminClient dbAdminClient = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.example.spanner;

import static com.example.spanner.ExperimentalHostHelper.isExperimentalHost;
import static com.example.spanner.ExperimentalHostHelper.setExperimentalHostSpannerOptions;

import static com.google.cloud.spanner.Type.StructField;

import com.google.api.gax.longrunning.OperationFuture;
Expand Down Expand Up @@ -1503,17 +1506,19 @@ static void queryWithNumeric(DatabaseClient dbClient) {

// [START spanner_create_client_with_query_options]
static void clientWithQueryOptions(DatabaseId db) {
SpannerOptions options =
SpannerOptions.newBuilder()
.setDefaultQueryOptions(
SpannerOptions.Builder builder = SpannerOptions.newBuilder();
builder.setDefaultQueryOptions(
db, QueryOptions
.newBuilder()
.setOptimizerVersion("1")
// The list of available statistics packages can be found by querying the
// "INFORMATION_SCHEMA.SPANNER_STATISTICS" table.
.setOptimizerStatisticsPackage("latest")
.build())
.build();
.build());
if (isExperimentalHost()) {
setExperimentalHostSpannerOptions(builder);
}
SpannerOptions options = builder.build();
Spanner spanner = options.getService();
DatabaseClient dbClient = spanner.getDatabaseClient(db);
try (ResultSet resultSet =
Expand Down Expand Up @@ -2231,7 +2236,11 @@ public static void main(String[] args) {
printUsageAndExit();
}
// [START init_client]
SpannerOptions options = SpannerOptions.newBuilder().build();
SpannerOptions.Builder builder = SpannerOptions.newBuilder();
if (isExperimentalHost()) {
setExperimentalHostSpannerOptions(builder);
}
SpannerOptions options = builder.build();
Spanner spanner = options.getService();
DatabaseAdminClient dbAdminClient = null;
try {
Expand Down
Loading