Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add native image configurations for Spanner classes #1858

Merged
merged 28 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c94f5f3
fix: branch to test all native image configuration changes on CI
mpeddada1 Apr 27, 2022
87b0cea
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 27, 2022
27924bc
include StatementResult and PostgreSqlStatementParser
mpeddada1 Apr 28, 2022
517a155
resolve conflicts
mpeddada1 Apr 28, 2022
0088e43
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 28, 2022
5af4534
Merge branch 'main' of github.com:googleapis/java-spanner into test-c…
mpeddada1 Apr 28, 2022
7a9a181
Merge branch 'test-compatibility' of github.com:googleapis/java-spann…
mpeddada1 Apr 28, 2022
cc7d3c7
remove dependency on svm
mpeddada1 May 11, 2022
0031231
Merge branch 'main' of github.com:googleapis/java-spanner into test-c…
mpeddada1 May 11, 2022
4e30216
add graal-sdk dependency
mpeddada1 May 11, 2022
4987032
Update native-image.properties
mpeddada1 May 12, 2022
839e141
modify native-image.properties
mpeddada1 May 12, 2022
a73e505
merge with main
mpeddada1 May 20, 2022
6c73a6c
upgrade to 22.1.0
mpeddada1 May 20, 2022
203f1d0
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] May 23, 2022
ee29ce1
initialize junit class at build time
mpeddada1 May 23, 2022
c62d5c6
Merge branch 'test-compatibility' of github.com:googleapis/java-spann…
mpeddada1 May 23, 2022
036e45a
Merge branch 'main' into test-compatibility
mpeddada1 May 24, 2022
1a674d5
resolve merge conflicts
mpeddada1 May 24, 2022
c739b77
add GraalVM JDK 17 test
mpeddada1 May 24, 2022
670f798
Merge branch 'test-compatibility' of github.com:googleapis/java-spann…
mpeddada1 May 24, 2022
bf78add
Merge branch 'main' of github.com:googleapis/java-spanner into test-c…
mpeddada1 May 25, 2022
a43464a
empty commit
mpeddada1 May 25, 2022
8661e49
empty commit
mpeddada1 May 25, 2022
485245d
empty commit
mpeddada1 May 25, 2022
926f421
add pg config
mpeddada1 May 25, 2022
a43d130
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] May 25, 2022
b6119aa
ci: ci to build with JDK 11 (cont.) (#1890)
suztomo May 25, 2022
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
5 changes: 5 additions & 0 deletions .kokoro/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ integration)
verify
RETURN_CODE=$?
;;
graalvm)
# Run Unit and Integration Tests with Native Image
mvn test -Pnative -Penable-integration-tests
RETURN_CODE=$?
;;
slowtests)
mvn -B ${INTEGRATION_TEST_ARGS} \
-ntp \
Expand Down
35 changes: 20 additions & 15 deletions google-cloud-spanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<properties>
<site.installationModule>google-cloud-spanner</site.installationModule>
<opencensus.version>0.31.0</opencensus.version>
<graalvm.version>22.0.0.2</graalvm.version>
<spanner.testenv.config.class>com.google.cloud.spanner.GceTestEnvConfig</spanner.testenv.config.class>
<spanner.testenv.instance>projects/gcloud-devel/instances/spanner-testing-east1</spanner.testenv.instance>
<spanner.gce.config.project_id>gcloud-devel</spanner.gce.config.project_id>
Expand Down Expand Up @@ -56,6 +57,16 @@
<id>default-test</id>
<configuration>
<excludedGroups>com.google.cloud.spanner.TracerTest,com.google.cloud.spanner.IntegrationTest</excludedGroups>

<!-- These configurations are only needed for native tests. The native profile
in shared-config uses maven-surefire to run both unit tests and integration tests -->
<systemPropertyVariables>
<spanner.testenv.config.class>${spanner.testenv.config.class}</spanner.testenv.config.class>
<spanner.testenv.instance>${spanner.testenv.instance}</spanner.testenv.instance>
<spanner.gce.config.project_id>${spanner.gce.config.project_id}</spanner.gce.config.project_id>
<spanner.testenv.kms_key.name>${spanner.testenv.kms_key.name}</spanner.testenv.kms_key.name>
</systemPropertyVariables>

</configuration>
</execution>
<execution>
Expand Down Expand Up @@ -105,20 +116,6 @@
</executions>
</plugin>

<!--Configurations for native image tests-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<spanner.testenv.config.class>${spanner.testenv.config.class}</spanner.testenv.config.class>
<spanner.testenv.instance>${spanner.testenv.instance}</spanner.testenv.instance>
<spanner.gce.config.project_id>${spanner.gce.config.project_id}</spanner.gce.config.project_id>
<spanner.testenv.kms_key.name>${spanner.testenv.kms_key.name}</spanner.testenv.kms_key.name>
</systemPropertyVariables>
<forkedProcessTimeoutInSeconds>3000</forkedProcessTimeoutInSeconds>
</configuration>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
Expand All @@ -142,7 +139,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<ignoredDependencies>io.grpc:grpc-protobuf-lite,org.hamcrest:hamcrest,org.hamcrest:hamcrest-core,com.google.errorprone:error_prone_annotations,org.openjdk.jmh:jmh-generator-annprocess,com.google.api.grpc:grpc-google-cloud-spanner-v1,com.google.api.grpc:grpc-google-cloud-spanner-admin-instance-v1,com.google.api.grpc:grpc-google-cloud-spanner-admin-database-v1,javax.annotation:javax.annotation-api,io.opencensus:opencensus-impl</ignoredDependencies>
<ignoredDependencies>io.grpc:grpc-protobuf-lite,org.hamcrest:hamcrest,org.hamcrest:hamcrest-core,com.google.errorprone:error_prone_annotations,org.openjdk.jmh:jmh-generator-annprocess,com.google.api.grpc:grpc-google-cloud-spanner-v1,com.google.api.grpc:grpc-google-cloud-spanner-admin-instance-v1,com.google.api.grpc:grpc-google-cloud-spanner-admin-database-v1,javax.annotation:javax.annotation-api,io.opencensus:opencensus-impl,org.graalvm.sdk:graal-sdk</ignoredDependencies>
</configuration>
</plugin>
</plugins>
Expand Down Expand Up @@ -296,6 +293,13 @@
<artifactId>grpc-alts</artifactId>
</dependency>

<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>${graalvm.version}</version>
<scope>provided</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -352,6 +356,7 @@
<version>1.34</version>
<scope>test</scope>
</dependency>

</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2022 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
*
* https://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.google.cloud.spanner.nativeimage;

import com.google.api.gax.nativeimage.NativeImageUtils;
import org.graalvm.nativeimage.hosted.Feature;

/** Registers Spanner library classes for reflection. */
final class SpannerFeature implements Feature {

private static final String SPANNER_CLASS = "com.google.spanner.v1.SpannerGrpc";
private static final String SPANNER_TEST_CLASS = "com.google.cloud.spanner.GceTestEnvConfig";
private static final String MOCK_CLASS = "com.google.cloud.spanner.MockDatabaseAdminServiceImpl";
private static final String CLIENT_SIDE_IMPL_CLASS =
"com.google.cloud.spanner.connection.ClientSideStatementImpl";
private static final String CLIENT_SIDE_VALUE_CONVERTER =
"com.google.cloud.spanner.connection.ClientSideStatementValueConverters";
private static final String CONNECTION_IMPL =
"com.google.cloud.spanner.connection.ConnectionImpl";
private static final String CLIENT_SIDE_STATEMENTS =
"com.google.cloud.spanner.connection.ClientSideStatements";
private static final String CONNECTION_STATEMENT_EXECUTOR =
"com.google.cloud.spanner.connection.ConnectionStatementExecutor";
private static final String CLIENT_SIDE_STATEMENT_NO_PARAM_EXECUTOR =
"com.google.cloud.spanner.connection.ClientSideStatementNoParamExecutor";
private static final String CLIENT_SIDE_STATEMENT_SET_EXECUTOR =
"com.google.cloud.spanner.connection.ClientSideStatementSetExecutor";
private static final String ABSTRACT_STATEMENT_PARSER =
"com.google.cloud.spanner.connection.AbstractStatementParser";
private static final String STATEMENT_PARSER =
"com.google.cloud.spanner.connection.SpannerStatementParser";
private static final String POSTGRESQL_STATEMENT_PARSER =
"com.google.cloud.spanner.connection.PostgreSQLStatementParser";
private static final String STATEMENT_RESULT =
"com.google.cloud.spanner.connection.StatementResult$ResultType";

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
registerSpannerTestClasses(access);
if (access.findClassByName(CLIENT_SIDE_IMPL_CLASS) != null) {
NativeImageUtils.registerClassHierarchyForReflection(access, CLIENT_SIDE_IMPL_CLASS);
}
if (access.findClassByName(CLIENT_SIDE_STATEMENT_NO_PARAM_EXECUTOR) != null) {
NativeImageUtils.registerClassForReflection(access, CLIENT_SIDE_STATEMENT_NO_PARAM_EXECUTOR);
}
if (access.findClassByName(CLIENT_SIDE_STATEMENT_SET_EXECUTOR) != null) {
NativeImageUtils.registerClassForReflection(access, CLIENT_SIDE_STATEMENT_SET_EXECUTOR);
}
if (access.findClassByName(CLIENT_SIDE_VALUE_CONVERTER) != null) {
NativeImageUtils.registerClassHierarchyForReflection(access, CLIENT_SIDE_VALUE_CONVERTER);
}
if (access.findClassByName(CLIENT_SIDE_STATEMENTS) != null) {
NativeImageUtils.registerClassForReflection(access, CLIENT_SIDE_STATEMENTS);
}
if (access.findClassByName(CONNECTION_STATEMENT_EXECUTOR) != null) {
NativeImageUtils.registerClassForReflection(access, CONNECTION_STATEMENT_EXECUTOR);
}
if (access.findClassByName(CONNECTION_IMPL) != null) {
NativeImageUtils.registerClassForReflection(access, CONNECTION_IMPL);
}
if (access.findClassByName(ABSTRACT_STATEMENT_PARSER) != null) {
NativeImageUtils.registerClassHierarchyForReflection(access, ABSTRACT_STATEMENT_PARSER);
NativeImageUtils.registerClassForReflection(access, "com.google.cloud.spanner.Dialect");
}
if (access.findClassByName(STATEMENT_PARSER) != null) {
NativeImageUtils.registerConstructorsForReflection(access, STATEMENT_PARSER);
}
if (access.findClassByName(POSTGRESQL_STATEMENT_PARSER) != null) {
NativeImageUtils.registerConstructorsForReflection(access, POSTGRESQL_STATEMENT_PARSER);
}
if (access.findClassByName(STATEMENT_RESULT) != null) {
NativeImageUtils.registerClassForReflection(access, STATEMENT_RESULT);
}

Class<?> spannerClass = access.findClassByName(SPANNER_CLASS);
if (spannerClass != null) {
NativeImageUtils.registerClassHierarchyForReflection(
access, "com.google.spanner.admin.database.v1.Database");
NativeImageUtils.registerClassHierarchyForReflection(
access, "com.google.spanner.admin.instance.v1.Instance");
NativeImageUtils.registerClassForReflection(
access, "com.google.spanner.admin.database.v1.RestoreInfo");
}
}

private void registerSpannerTestClasses(BeforeAnalysisAccess access) {
Class<?> spannerTestClass = access.findClassByName(SPANNER_TEST_CLASS);
if (spannerTestClass != null) {
NativeImageUtils.registerConstructorsForReflection(access, SPANNER_TEST_CLASS);
}
Class<?> mockClass = access.findClassByName(MOCK_CLASS);
if (mockClass != null) {
NativeImageUtils.registerClassForReflection(
access, "com.google.cloud.spanner.MockDatabaseAdminServiceImpl$MockBackup");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Args = --initialize-at-build-time=com.google.cloud.spanner.IntegrationTestEnv \
--features=com.google.cloud.spanner.nativeimage.SpannerFeature
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"resources": [
{"pattern": "\\Qcom/google/cloud/spanner/connection/ClientSideStatements.json\\E"},
{"pattern": "\\Qcom/google/cloud/spanner/connection/PG_ClientSideStatements.json\\E"},
{"pattern": "\\Qcom/google/cloud/spanner/spi/v1/grpc-gcp-apiconfig.json\\E"},
{"pattern": "\\Qcom/google/cloud/spanner/connection/ITSqlScriptTest_TestQueryOptions.sql\\E"}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import org.mockito.MockitoAnnotations;

@RunWith(Parameterized.class)
public class SessionClientTest {
public class SessionClientTests {
private final class TestExecutorFactory implements ExecutorFactory<ScheduledExecutorService> {
@Override
public ScheduledExecutorService get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class DdlClientTest {
public class DdlClientTests {

private final String instanceId = "test-instance";
private final String databaseId = "test-database";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package com.google.cloud.spanner.connection;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.ErrorCode;
Expand Down Expand Up @@ -175,17 +173,16 @@ public SqlScriptVerifier(GenericConnectionProvider provider) {
@Override
protected void verifyExpectedException(
String statement, Exception e, String code, String messagePrefix) {
assertThat(e instanceof SpannerException, is(true));
assertTrue(e instanceof SpannerException);
SpannerException spannerException = (SpannerException) e;
assertThat(
statement + " resulted in " + spannerException.toString(),
spannerException.getErrorCode(),
is(equalTo(ErrorCode.valueOf(code))));
assertEquals(
statement + " resulted in " + spannerException,
ErrorCode.valueOf(code),
spannerException.getErrorCode());
if (messagePrefix != null) {
assertThat(
assertTrue(
statement,
e.getMessage(),
startsWith(messagePrefix.substring(1, messagePrefix.length() - 1)));
e.getMessage().startsWith(messagePrefix.substring(1, messagePrefix.length() - 1)));
}
}
}