Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ hs_err_pid*

# macOS
*.DS_Store

# VSCode
.vscode/settings.json
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ commons-lang3 = { module = "org.apache.commons:commons-lang3", version = "3.18.0
commons-text = { module = "org.apache.commons:commons-text", version = "1.14.0" }
eclipselink = { module = "org.eclipse.persistence:eclipselink", version = "4.0.7" }
errorprone = { module = "com.google.errorprone:error_prone_core", version = "2.41.0" }
google-cloud-storage-bom = { module = "com.google.cloud:google-cloud-storage-bom", version = "2.55.0" }
google-cloud-libraries-bom = { module = "com.google.cloud:libraries-bom", version = "26.64.0" }
Copy link
Contributor

Choose a reason for hiding this comment

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

We will need to change the License file for this change, somewhere like here

Group: com.google.api.grpc Name: proto-google-cloud-storage-v2 Version: 2.53.0
. cc @jbonofre

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point about LICENSE updates, however using a BOM does not necessary require LICENSE changes... only real dependencies need to be mentioned... IMHO, that can be done later (we have to double check dependencies for every release anyway).

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm OK with a followup PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just wondering: does libraries-bom get updated as frequently as any of its upstream artifacts are published?

Copy link
Author

Choose a reason for hiding this comment

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

I believe most of the Cloud SDKs, libraries included, are on a two week cadence more or less. The advantage (coming from the Beam experience with the Cloud Java SDKs) of using the BOM is that it keeps the various support libraries synchronized across specific SDKS. What happens otherwise is you get version drift in shared components like Protobuf or gRPC core libraries which can be really hard to spot.

guava = { module = "com.google.guava:guava", version = "33.4.8-jre" }
h2 = { module = "com.h2database:h2", version = "2.3.232" }
dnsjava = { module = "dnsjava:dnsjava", version = "3.6.3" }
Expand Down
1 change: 1 addition & 0 deletions gradle/projects.main.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ polaris-runtime-common=runtime/common
polaris-runtime-test-common=runtime/test-common
polaris-eclipselink=persistence/eclipselink
polaris-relational-jdbc=persistence/relational-jdbc
polaris-google-cloud-spanner=persistence/google-cloud-spanner
polaris-tests=integration-tests
aggregated-license-report=aggregated-license-report
polaris-immutables=tools/immutables
Expand Down
42 changes: 42 additions & 0 deletions persistence/google-cloud-spanner/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

plugins {
id("polaris-server")
alias(libs.plugins.jandex)
}

dependencies {
implementation(project(":polaris-core"))
implementation(libs.slf4j.api)
implementation(libs.guava)

implementation(platform(libs.google.cloud.libraries.bom))
implementation("com.google.cloud:google-cloud-spanner")

compileOnly(libs.jakarta.annotation.api)
compileOnly(libs.jakarta.enterprise.cdi.api)
compileOnly(libs.jakarta.inject.api)

implementation(libs.smallrye.common.annotation) // @Identifier

testImplementation(libs.mockito.junit.jupiter)
testImplementation(libs.h2)
testImplementation(testFixtures(project(":polaris-core")))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.polaris.persistence.relational.spanner;

import java.util.Optional;

public interface GoogleCloudSpannerConfiguration {

public Optional<String> projectId();

public Optional<String> instanceId();

public Optional<String> databaseId();

public Optional<String> emulatorHost();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.polaris.persistence.relational.spanner;

import com.google.cloud.spanner.Database;
import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerException;
import com.google.common.collect.ImmutableList;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.persistence.bootstrap.SchemaOptions;
import org.apache.polaris.persistence.relational.spanner.model.Realm;
import org.apache.polaris.persistence.relational.spanner.util.SpannerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ApplicationScoped
public class GoogleCloudSpannerDatabaseClientLifecycleManager {

private static Logger LOGGER =
LoggerFactory.getLogger(GoogleCloudSpannerDatabaseClientLifecycleManager.class);

@Inject GoogleCloudSpannerConfiguration spannerConfiguration;

protected Spanner spanner;
protected DatabaseId databaseId;

@PostConstruct
protected void init() {
Copy link
Contributor

Choose a reason for hiding this comment

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

just wondering: why not do the init work in the constructor?

spanner = SpannerUtil.spannerFromConfiguration(spannerConfiguration);
databaseId = SpannerUtil.databaseFromConfiguration(spannerConfiguration);
}

protected List<String> getSpannerDatabaseDdl(SchemaOptions options) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could make this method static.

final InputStream schemaStream;
if (options.schemaFile() != null) {
try {
schemaStream = new FileInputStream(options.schemaFile());
} catch (IOException e) {
throw new IllegalArgumentException("Unable to load file " + options.schemaFile(), e);
}
} else {
if (options.schemaVersion() == null || options.schemaVersion() == 1) {
schemaStream =
getClass().getResourceAsStream("/org/apache/polaris/persistence/spanner/schema-v1.sql");
} else {
throw new IllegalArgumentException("Unknown schema version " + options.schemaVersion());
}
}
try (schemaStream) {
String schema = new String(schemaStream.readAllBytes(), Charset.forName("UTF-8"));
List<String> lines = new ArrayList<>();
for (String s : schema.split("\n")) {
s = s.trim();
if (s.startsWith("--") || s.length() == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we also need to check if the line ends with ;? Later we split on that

Copy link
Author

Choose a reason for hiding this comment

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

Added a check for lines only containing ';'

continue;
}
lines.add(s);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (s.startsWith("--") || s.length() == 0) {
continue;
}
lines.add(s);
if (!s.startsWith("--") && s.length() > 0) {
lines.add(s);
}

Copy link
Author

Choose a reason for hiding this comment

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

I think I'm OK? I wanted to remove lines that are comments or just blank... Added a check for only containing ';' in case some monster does that... that said it's not like we're sending arbitrary SQL through this thing so we don't need to be super careful.

}
return List.of(String.join(" ", lines).split(";"));
} catch (IOException e) {
throw new RuntimeException("Unable to retrieve DDL statements", e);
}
}

@Produces
public Consumer<SchemaOptions> getSchemaInitializer() {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I'd personally introduce a separate interface type that extends Consumer<X> (ran into issues with CDI + generics in the past - might no longer be an issue though).

Copy link
Author

Choose a reason for hiding this comment

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

Converted this to an explicit SchemaInitializer consumer.

Copy link
Author

Choose a reason for hiding this comment

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

For completeness converted the rest of the consumers and suppliers as well.

return (options) -> {
List<String> ddlStatements = getSpannerDatabaseDdl(options);
LOGGER.info(
"Attempting to initialize Spanner database DDL with {} statements,",
ddlStatements.size());
DatabaseAdminClient client = spanner.getDatabaseAdminClient();
Database dbInfo =
client.newDatabaseBuilder(databaseId).setDialect(Dialect.GOOGLE_STANDARD_SQL).build();
try {
spanner.getDatabaseAdminClient().updateDatabaseDdl(dbInfo, ddlStatements, null).get();
LOGGER.info("Successfully applied DDL update.");
} catch (InterruptedException | ExecutionException e) {
LOGGER.error("Unable to update Spanner DDL.", e);
throw new RuntimeException(
"Unable to update Spanner DDL. Please disable this option for this database configuration.",
e);
}
};
}

@Produces
public Consumer<RealmContext> getRealmInitializer() {
return (realmContext) -> {
try {
spanner
.getDatabaseClient(databaseId)
.write(ImmutableList.of(Realm.upsert(realmContext.getRealmIdentifier())));
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this should be done under the "bootstrap" call path as opposed to on observing new realm IDs in runtime. The difference would be delegating realm initialization to the "admin" user / admin tool. Cf. #2196

Copy link
Contributor

Choose a reason for hiding this comment

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

Also RealmContext CDI beans may come and go very frequently in runtime (once per request at least).

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed with @dimas-b. The realm initialization doesn't happen very often. It only happens when we bootstrap a new realm, https://polaris.apache.org/in-dev/unreleased/admin-tool/#bootstrapping-realms-and-principal-credentials. Producing a bean here isn't necessary to me, as Polaris server will never use it for realm initialization. Here is the reference code path in JDBC impl.: https://github.com/polaris-catalog/polaris/blob/main/persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetaStoreManagerFactory.java#L142-L142

Copy link
Author

Choose a reason for hiding this comment

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

Fair enough, moving this to the bootstrap code.

} catch (SpannerException e) {
LOGGER.error("Unable to initialize realm " + realmContext.getRealmIdentifier(), e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Throw a runtime exception instead of logging an error? So that the stack trace will show the complete call chain.

}
};
}

@Produces
public Supplier<DatabaseClient> getDatabaseClientSupplier() {
return () -> spanner.getDatabaseClient(databaseId);
}

@Produces
public Supplier<DatabaseAdminClient> getDatabaseAdminClientSupplier() {
return () -> spanner.getDatabaseAdminClient();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.polaris.persistence.relational.spanner.model;

import com.google.cloud.spanner.Key;
import com.google.cloud.spanner.Mutation;

public final class Realm {

public static String TABLE_NAME = "Realms";

public static Mutation upsert(String realmId) {
return Mutation.newInsertOrUpdateBuilder(TABLE_NAME).set("RealmId").to(realmId).build();
}

public static Mutation delete(String realmId) {
return Mutation.delete(TABLE_NAME, Key.of(realmId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.polaris.persistence.relational.spanner.util;

import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

public class Modifier<T> {
T wrapped;

protected Modifier(T wrapped) {
this.wrapped = wrapped;
}

public Modifier<T> apply(Function<T, T> fn) {
wrapped = fn.apply(wrapped);
return this;
}

public <V> Modifier<T> orElse(Optional<V> toApply, V other, BiFunction<T, V, T> fn) {
wrapped = fn.apply(wrapped, toApply.orElse(other));
return this;
}

public <V> Modifier<T> ifPresent(Optional<V> toApply, BiFunction<T, V, T> fn) {
if (toApply.isPresent()) {
wrapped = fn.apply(wrapped, toApply.get());
}
return this;
}

public T get() {
return wrapped;
}

public static <T> Modifier<T> of(T value) {
return new Modifier<>(value);
}
}
Loading