diqiu50 commented on code in PR #13209: URL: https://github.com/apache/gravitino/pull/13209#discussion_r4026433936
########## spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/plugin/SparkCatalogExtensions.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.gravitino.spark.connector.plugin; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Locale; +import java.util.Map; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; +import javax.annotation.Nullable; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Resolves Spark catalog classes contributed through {@link SparkCatalogExtension}. */ +public class SparkCatalogExtensions { + + private static final Logger LOG = LoggerFactory.getLogger(SparkCatalogExtensions.class); + + private static volatile Map<String, String> catalogClassNamesByProvider; + + private SparkCatalogExtensions() {} + + /** + * Looks up the Spark catalog class an extension registered for a provider. + * + * @param provider the Gravitino catalog provider + * @return the catalog class name, or null when no extension serves the provider + */ + @Nullable + public static String catalogClassName(String provider) { + return extensions().get(provider.toLowerCase(Locale.ROOT)); + } + + private static Map<String, String> extensions() { + Map<String, String> loaded = catalogClassNamesByProvider; + if (loaded == null) { + synchronized (SparkCatalogExtensions.class) { + loaded = catalogClassNamesByProvider; + if (loaded == null) { + loaded = load(); + catalogClassNamesByProvider = loaded; + } + } + } + return loaded; + } + + private static Map<String, String> load() { + Map<String, String> byProvider = new HashMap<>(); + Iterator<SparkCatalogExtension> iterator = + ServiceLoader.load(SparkCatalogExtension.class, extensionClassLoader()).iterator(); + while (true) { + try { + if (!iterator.hasNext()) { + break; + } + SparkCatalogExtension extension = iterator.next(); + String provider = extension.provider(); + String catalogClassName = extension.catalogClassName(); + if (StringUtils.isBlank(provider) || StringUtils.isBlank(catalogClassName)) { + LOG.error( + "Skip Spark catalog extension {}: provider and catalog class name must not be blank," + + " got provider={}, catalogClassName={}.", + extension.getClass().getName(), + provider, + catalogClassName); + continue; + } + String normalized = provider.toLowerCase(Locale.ROOT); + String previous = byProvider.putIfAbsent(normalized, catalogClassName); + if (previous != null) { + LOG.warn( + "Ignore Spark catalog extension {} for provider {}: already served by {}.", + catalogClassName, + normalized, + previous); + } + } catch (ServiceConfigurationError | LinkageError e) { Review Comment: Done in 1f52b07: `load()` now also catches `RuntimeException`, so an extension that throws from `provider()` / `catalogClassName()` is skipped with an error log and the remaining extensions and built-in catalogs still register. Covered by `ThrowingSparkCatalogExtension`. ########## spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/plugin/SparkCatalogExtension.java: ########## @@ -0,0 +1,44 @@ +/* + * 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.gravitino.spark.connector.plugin; + +/** + * Service provider interface that lets a jar outside the connector supply the Spark catalog + * implementation for a Gravitino catalog provider. Implementations are discovered through {@link + * java.util.ServiceLoader} and take precedence over the catalogs the connector ships itself. When Review Comment: Kept the flag check as is: the two flags are an explicit user opt-in for Iceberg/Paimon (they also inject the Iceberg/Paimon session extensions), and an extension jar on the class path should not turn them on implicitly. Updated the Javadoc in 1f52b07 to state that an extension for `lakehouse-iceberg` / `lakehouse-paimon` is only consulted once the corresponding flag is enabled. ########## spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/plugin/SparkCatalogExtension.java: ########## @@ -0,0 +1,44 @@ +/* + * 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.gravitino.spark.connector.plugin; + +/** + * Service provider interface that lets a jar outside the connector supply the Spark catalog + * implementation for a Gravitino catalog provider. Implementations are discovered through {@link + * java.util.ServiceLoader} and take precedence over the catalogs the connector ships itself. When Review Comment: The precedence is in `CatalogNameAdaptor.getCatalogName(String)`: it calls `SparkCatalogExtensions.catalogClassName(provider)` first and returns that when non-null, and only then falls back to the built-in per-version tables. `GravitinoDriverPlugin.registerCatalog` resolves every catalog through that method. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
