-
Notifications
You must be signed in to change notification settings - Fork 344
Spanner Persistence Backend for Polaris (Initial change) #3259
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * 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") | ||
| id("org.kordamp.gradle.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) | ||
|
|
||
| compileOnly(libs.smallrye.common.annotation) // @Identifier | ||
| compileOnly(libs.smallrye.config.core) // @ConfigMapping | ||
|
|
||
| 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,25 @@ | ||
| /* | ||
| * 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.spanner; | ||
|
|
||
| import com.google.cloud.spanner.DatabaseAdminClient; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public interface DatabaseAdminClientSupplier extends Supplier<DatabaseAdminClient> {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * 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.spanner; | ||
|
|
||
| import com.google.cloud.spanner.DatabaseClient; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public interface DatabaseClientSupplier extends Supplier<DatabaseClient> {} |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||
| /* | ||||||
| * 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.spanner; | ||||||
|
|
||||||
| import io.smallrye.config.ConfigMapping; | ||||||
| import java.util.Optional; | ||||||
|
|
||||||
| @ConfigMapping(prefix = "polaris.persistence.spanner") | ||||||
| public interface GoogleCloudSpannerConfiguration { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: same naming suggestion here
Suggested change
|
||||||
|
|
||||||
| public Optional<String> quotaProjectId(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: all methods are public by default in an interface, no need to explicitly add a modifier.
Suggested change
|
||||||
|
|
||||||
| 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,112 @@ | ||||||
| /* | ||||||
| * 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.spanner; | ||||||
|
|
||||||
| import com.google.cloud.spanner.Database; | ||||||
| import com.google.cloud.spanner.DatabaseAdminClient; | ||||||
| import com.google.cloud.spanner.DatabaseId; | ||||||
| import com.google.cloud.spanner.Dialect; | ||||||
| import com.google.cloud.spanner.Spanner; | ||||||
| import jakarta.enterprise.context.ApplicationScoped; | ||||||
| import jakarta.enterprise.inject.Produces; | ||||||
| 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 org.apache.polaris.core.persistence.bootstrap.SchemaOptions; | ||||||
| import org.apache.polaris.persistence.spanner.util.SpannerUtil; | ||||||
| import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | ||||||
|
|
||||||
| @ApplicationScoped | ||||||
| public class GoogleCloudSpannerDatabaseClientLifecycleManager { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Does it make sense to shorten the name a bit? I think
Suggested change
|
||||||
|
|
||||||
| private static final Logger LOGGER = | ||||||
| LoggerFactory.getLogger(GoogleCloudSpannerDatabaseClientLifecycleManager.class); | ||||||
|
|
||||||
| protected final GoogleCloudSpannerConfiguration spannerConfiguration; | ||||||
| protected final Spanner spanner; | ||||||
| protected final DatabaseId databaseId; | ||||||
|
|
||||||
| public GoogleCloudSpannerDatabaseClientLifecycleManager( | ||||||
| GoogleCloudSpannerConfiguration spannerConfiguration) { | ||||||
| this.spannerConfiguration = spannerConfiguration; | ||||||
| spanner = SpannerUtil.spannerFromConfiguration(spannerConfiguration); | ||||||
| databaseId = SpannerUtil.databaseFromConfiguration(spannerConfiguration); | ||||||
| } | ||||||
|
|
||||||
| protected List<String> getSpannerDatabaseDdl(SchemaOptions options) { | ||||||
| final InputStream schemaStream; | ||||||
| int schemaVersion = options.schemaVersion().orElse(1); | ||||||
| if (schemaVersion != 1) { | ||||||
| throw new IllegalArgumentException("Unknown or invalid schema version " + schemaVersion); | ||||||
| } | ||||||
|
|
||||||
| schemaStream = getClass().getResourceAsStream("/spanner/schema-v1.sql"); | ||||||
| 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(); | ||||||
| // Drop comments and empty lines. | ||||||
| if (s.startsWith("--") || s.length() == 0 || s.equals(";")) { | ||||||
| continue; | ||||||
| } | ||||||
| lines.add(s); | ||||||
| } | ||||||
| return List.of(String.join(" ", lines).split(";")); | ||||||
| } catch (IOException e) { | ||||||
| throw new RuntimeException("Unable to retrieve DDL statements", e); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @Produces | ||||||
| public SchemaInitializer getSchemaInitializer() { | ||||||
| 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 { | ||||||
| client.updateDatabaseDdl(dbInfo, ddlStatements, null).get(); | ||||||
| LOGGER.info("Successfully applied DDL update."); | ||||||
| } catch (InterruptedException | ExecutionException e) { | ||||||
| throw new RuntimeException( | ||||||
| "Unable to update Spanner DDL. Please disable this option for this database configuration.", | ||||||
| e); | ||||||
| } | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| @Produces | ||||||
| public DatabaseClientSupplier getDatabaseClientSupplier() { | ||||||
| return () -> spanner.getDatabaseClient(databaseId); | ||||||
| } | ||||||
|
|
||||||
| @Produces | ||||||
| public DatabaseAdminClientSupplier getDatabaseAdminClientSupplier() { | ||||||
| return () -> spanner.getDatabaseAdminClient(); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * 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.spanner; | ||
|
|
||
| import java.util.function.Consumer; | ||
| import org.apache.polaris.core.persistence.bootstrap.SchemaOptions; | ||
|
|
||
| public interface SchemaInitializer extends Consumer<SchemaOptions> {} |
| 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.spanner.model; | ||
|
|
||
| import com.google.cloud.spanner.Key; | ||
| import com.google.cloud.spanner.Mutation; | ||
|
|
||
| public final class Realm { | ||
|
|
||
| public static final 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.spanner.util; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.function.BiFunction; | ||
| import java.util.function.Function; | ||
|
|
||
| public final 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, thanks for the change, this makes the versions more consistent across Google Cloud components.
Continuing my review of #2328: a license update is still required. I’m fine with addressing this in a follow-up (see the earlier discussion here: #2328 (comment)).
Would it make sense to file a tracking issue and mark it as a 1.4 release blocker? That would help streamline the release manager’s workflow, since any outstanding license issue will cause a release RC to fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM! I can create a follow up PR after merging this.