pvary commented on code in PR #13032:
URL: https://github.com/apache/iceberg/pull/13032#discussion_r2095596907


##########
flink/v2.0/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/TableUpdater.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.iceberg.flink.sink.dynamic;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.UpdatePartitionSpec;
+import org.apache.iceberg.UpdateSchema;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Internal
+class TableUpdater {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(TableUpdater.class);
+  private final TableDataCache cache;
+  private final Catalog catalog;
+
+  TableUpdater(TableDataCache cache, Catalog catalog) {
+    this.cache = cache;
+    this.catalog = catalog;
+  }
+
+  /**
+   * Creates or updates a table to make sure that the given branch, schema, 
spec exists.
+   *
+   * @return a {@link Tuple3} of the new {@link Schema}, the status of the 
schema compared to the
+   *     requested one, and the new {@link PartitionSpec#specId()}.
+   */
+  Tuple3<Schema, CompareSchemasVisitor.Result, PartitionSpec> update(
+      TableIdentifier tableIdentifier, String branch, Schema schema, 
PartitionSpec spec) {
+    findOrCreateTable(tableIdentifier, schema, spec);
+    findOrCreateBranch(tableIdentifier, branch);
+    Tuple2<Schema, CompareSchemasVisitor.Result> newSchema =
+        findOrCreateSchema(tableIdentifier, schema);
+    PartitionSpec newSpec = findOrCreateSpec(tableIdentifier, spec);
+    return Tuple3.of(newSchema.f0, newSchema.f1, newSpec);
+  }
+
+  private void findOrCreateTable(TableIdentifier identifier, Schema schema, 
PartitionSpec spec) {
+    Tuple2<Boolean, Exception> exists = cache.exists(identifier);
+    if (Boolean.FALSE.equals(exists.f0)) {
+      if (exists.f1 instanceof NoSuchNamespaceException) {
+        SupportsNamespaces catalogWithNameSpace = (SupportsNamespaces) catalog;
+        LOG.info("Namespace {} not found during table search. Creating 
namespace", identifier);
+        try {
+          catalogWithNameSpace.createNamespace(identifier.namespace());
+        } catch (AlreadyExistsException e) {
+          LOG.debug("Namespace {} created concurrently", 
identifier.namespace(), e);
+        }
+
+        createTable(identifier, schema, spec);
+      } else {
+        LOG.info("Table {} not found during table search. Creating table.", 
identifier);
+        createTable(identifier, schema, spec);
+      }
+    }
+  }
+
+  private void createTable(TableIdentifier identifier, Schema schema, 
PartitionSpec spec) {
+    try {
+      Table table = catalog.createTable(identifier, schema, spec);
+      cache.update(identifier, table);
+    } catch (AlreadyExistsException e) {
+      LOG.info("Table {} created concurrently. Skipping creation.", 
identifier, e);

Review Comment:
   Do we need these logs on info level? If we have many records, then we might 
end up flooding the logs?
   Could you please check where we can use debug level without losing 
traceability?



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to