jngli commented on code in PR #14862:
URL: https://github.com/apache/gravitino/pull/14862#discussion_r4560662160


##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/generic/GenericCatalogOperations.java:
##########
@@ -464,11 +548,29 @@ private boolean dropOrPurgeTable(NameIdentifier ident, 
boolean purge, Schema sch
           ident,
           context.tableProperties().get(Table.PROPERTY_LOCATION),
           tableLocationProvider.name());
+      if (tablesWithRetainedData != null) {
+        tablesWithRetainedData.add(ident);
+      }
     }
 
     return dropped;
   }
 
+  private void unprovisionSchemaLocation(
+      TableLocationProvider provider, SchemaLocationContext context) {
+    try {
+      ((SupportsSchemaLocations) provider).unprovisionSchemaLocation(context);
+    } catch (Exception e) {

Review Comment:
   Fail the request instead



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/generic/GenericCatalogOperations.java:
##########
@@ -218,19 +275,40 @@ public boolean dropSchema(NameIdentifier ident, boolean 
cascade) throws NonEmpty
           "Schema %s is not empty, cannot drop it without cascade", ident);
     }
 
+    TableLocationProvider provider = tableLocationProvider;
+    boolean schemaHooks = provider instanceof SupportsSchemaLocations;
+    Schema schema = null;
+    if (schemaHooks || tableIdents.length > 0) {
+      try {
+        schema = loadSchema(ident);
+      } catch (NoSuchSchemaException e) {
+        return false;

Review Comment:
   Before this change, it throws when schema does not exist.



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/generic/GenericCatalogOperations.java:
##########
@@ -169,10 +172,58 @@ public NameIdentifier[] listSchemas(Namespace namespace) 
throws NoSuchCatalogExc
     return schemaOps.listSchemas(namespace);
   }
 
+  /**
+   * Creates a schema, asking the {@link TableLocationProvider} for its 
location first when the
+   * provider implements {@link SupportsSchemaLocations}.
+   *
+   * <p>A schema that already exists is rejected before the provider is 
consulted, so that a
+   * provider is never asked to allocate for a name that is taken. A provider 
that throws fails the
+   * creation, and a non-null location it returns is stored as the schema's 
{@code location}.
+   *
+   * @param ident the identifier of the schema to create
+   * @param comment the comment of the schema
+   * @param properties the properties of the schema
+   * @return the created schema
+   * @throws NoSuchCatalogException if the catalog does not exist
+   * @throws SchemaAlreadyExistsException if the schema already exists
+   */
   @Override
   public Schema createSchema(NameIdentifier ident, String comment, Map<String, 
String> properties)
       throws NoSuchCatalogException, SchemaAlreadyExistsException {
-    return schemaOps.createSchema(ident, comment, properties);
+    TableLocationProvider provider = tableLocationProvider;
+    if (!(provider instanceof SupportsSchemaLocations)) {
+      return schemaOps.createSchema(ident, comment, properties);
+    }
+
+    try {
+      if (store.exists(ident, SCHEMA)) {
+        throw new SchemaAlreadyExistsException("Schema %s already exists", 
ident);
+      }
+    } catch (IOException e) {
+      throw new RuntimeException("Failed to check if schema " + ident + " 
exists", e);
+    }
+
+    String location =
+        ((SupportsSchemaLocations) provider)
+            .provisionSchemaLocation(
+                SchemaLocationContext.builder()
+                    .withSchemaIdentifier(ident)
+                    .withSchemaProperties(properties)
+                    .withCatalogProperties(catalogProperties)
+                    .build());
+    if (location == null) {

Review Comment:
   What's the usage of schema location? Shall we call 
schemaOps.createSchema(ident, comment, properties) right after 
provisionSchemaLocation is invoked?



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/generic/SchemaLocationContext.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.catalog.lakehouse.generic;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.NameIdentifier;
+
+/**
+ * The context passed to a {@link SupportsSchemaLocations} provider on both of 
its callbacks: when a
+ * schema is being created, and when a schema has been dropped.
+ *
+ * <p>As with {@link TableLocationContext}, one type serves both callbacks so 
that a new input
+ * becomes an additional accessor rather than a signature change. Build 
instances through {@link
+ * #builder()}.
+ */
+public class SchemaLocationContext {
+
+  private final NameIdentifier schemaIdentifier;
+
+  private final Map<String, String> schemaProperties;
+
+  private final Map<String, String> catalogProperties;
+
+  private final boolean cascade;
+
+  private final List<NameIdentifier> tablesWithRetainedData;

Review Comment:
   What's the usage of this field?



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/generic/GenericCatalogOperations.java:
##########
@@ -370,9 +448,15 @@ private boolean dropOrPurgeTable(NameIdentifier ident, 
boolean purge) {
    * @param ident the identifier of the table to drop
    * @param purge whether to purge the table instead of dropping it
    * @param schema the table's parent schema
+   * @param tablesWithRetainedData if not null, receives the identifier of the 
table when it is
+   *     dropped without its data being deleted
    * @return true if the table was dropped, false if it did not exist
    */
-  private boolean dropOrPurgeTable(NameIdentifier ident, boolean purge, Schema 
schema) {
+  private boolean dropOrPurgeTable(
+      NameIdentifier ident,
+      boolean purge,
+      Schema schema,
+      @Nullable List<NameIdentifier> tablesWithRetainedData) {

Review Comment:
   What's this new parameter used for?



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/generic/SchemaLocationContext.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.catalog.lakehouse.generic;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.NameIdentifier;
+
+/**
+ * The context passed to a {@link SupportsSchemaLocations} provider on both of 
its callbacks: when a
+ * schema is being created, and when a schema has been dropped.
+ *
+ * <p>As with {@link TableLocationContext}, one type serves both callbacks so 
that a new input
+ * becomes an additional accessor rather than a signature change. Build 
instances through {@link
+ * #builder()}.
+ */
+public class SchemaLocationContext {
+
+  private final NameIdentifier schemaIdentifier;
+
+  private final Map<String, String> schemaProperties;
+
+  private final Map<String, String> catalogProperties;
+
+  private final boolean cascade;

Review Comment:
   What's this flag?



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/generic/GenericCatalogOperations.java:
##########
@@ -218,19 +275,40 @@ public boolean dropSchema(NameIdentifier ident, boolean 
cascade) throws NonEmpty
           "Schema %s is not empty, cannot drop it without cascade", ident);
     }
 
+    TableLocationProvider provider = tableLocationProvider;
+    boolean schemaHooks = provider instanceof SupportsSchemaLocations;
+    Schema schema = null;
+    if (schemaHooks || tableIdents.length > 0) {
+      try {
+        schema = loadSchema(ident);
+      } catch (NoSuchSchemaException e) {
+        return false;
+      }
+    }
+
     // Drop all tables under the schema first if cascade is true. This goes 
through the same path
     // as the catalog level dropTable, so that the location of each table is 
unprovisioned and its
     // cached format invalidated. The schema is resolved once for the whole 
cascade rather than
     // once per table: every table here has the same parent, and the provider 
may not ask for it at
     // all.
-    if (tableIdents.length > 0) {
-      Schema cascadedSchema = loadSchema(ident);
-      for (NameIdentifier tableIdent : tableIdents) {
-        dropOrPurgeTable(tableIdent, false /* purge */, cascadedSchema);
-      }
+    List<NameIdentifier> tablesWithRetainedData = Lists.newArrayList();
+    for (NameIdentifier tableIdent : tableIdents) {
+      dropOrPurgeTable(tableIdent, false /* purge */, schema, 
tablesWithRetainedData);
     }
 
-    return schemaOps.dropSchema(ident, cascade);
+    boolean dropped = schemaOps.dropSchema(ident, cascade);
+    if (dropped && schemaHooks) {
+      unprovisionSchemaLocation(

Review Comment:
   Unprovision schema should be called before schemaOps.dropSchema



##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/generic/SupportsSchemaLocations.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.catalog.lakehouse.generic;
+
+import javax.annotation.Nullable;
+
+/**
+ * An optional capability of a {@link TableLocationProvider}: allocating the 
location of a schema
+ * when it is created, and releasing it when the schema is dropped.
+ *
+ * <p>A provider opts in by implementing this interface alongside {@link 
TableLocationProvider}.
+ * Services that allocate storage usually do so hierarchically -- a schema is 
registered first, with
+ * the owner and the storage domain it belongs to, and the paths of its tables 
are derived from it
+ * -- and the table callbacks alone offer no point at which to register the 
schema.
+ *
+ * <p>A provider that does not implement it is never consulted for schemas, 
and the catalog does no
+ * extra work on its behalf: dropping an empty schema still does not read the 
schema. That is why
+ * this is a separate interface rather than default methods on {@link 
TableLocationProvider}.
+ *
+ * <p>The division of responsibilities is the one {@link 
TableLocationProvider} describes. The
+ * provider owns what it brought into being so that the schema location can be 
used -- its
+ * registration, its quota, any container it created -- and the table formats 
own the data written
+ * under it. Like the table callbacks, both methods here are called 
concurrently and must be
+ * thread-safe, and the catalog applies no timeout to them.
+ *
+ * <p><b>Known limitations.</b>
+ *
+ * <ul>
+ *   <li>{@link #provisionSchemaLocation(SchemaLocationContext)} is called 
before the schema is
+ *       stored. A creation that fails afterwards -- a schema of the same name 
created concurrently,
+ *       or a store failure -- leaves a location provisioned for a schema that 
does not exist, and
+ *       there is no compensating unprovision. A provider that answers 
idempotently may have
+ *       returned the allocation of the schema that won the race, and 
releasing it would take it
+ *       from a schema that exists. A schema that already exists is detected 
before the provider is
+ *       consulted, so the cases that remain are the races.
+ *   <li>{@code location} is a schema property, and altering it does not tell 
the provider.
+ *   <li>{@link #unprovisionSchemaLocation(SchemaLocationContext)} is called 
after the schema is
+ *       removed, and not at all if the server stops in between, so a provider 
reclaiming real
+ *       storage needs its own reconciliation against the catalog.
+ * </ul>
+ */
+public interface SupportsSchemaLocations {
+
+  /**
+   * Provisions the location of the schema that is being created.
+   *
+   * <p>The context carries the properties of the creation request, including 
a caller-supplied
+   * {@code location}, if any. As with tables, what happens to a supplied 
location is the provider's
+   * decision: return null to keep it, return something else to place the 
schema elsewhere, or throw
+   * to refuse the creation.
+   *
+   * <p>A non-null result is stored verbatim as the schema's {@code location} 
property. It must not
+   * be blank; the catalog rejects the creation with an {@link 
IllegalArgumentException} otherwise.
+   * Because the built-in provider derives the location of a table from its 
schema's {@code
+   * location}, recording it also keeps the schema consistent with the tables 
placed under it.
+   *
+   * <p>If this method throws, the schema is not created.
+   *
+   * @param context the schema being created, with the properties of the 
creation request
+   * @return the location to store on the schema, or null to keep the 
properties of the request
+   *     unchanged
+   */
+  @Nullable
+  String provisionSchemaLocation(SchemaLocationContext context);

Review Comment:
   How about adding these two new functions into TableLocationProvider and 
rename it to LocationProvider?



-- 
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]

Reply via email to