morningman commented on a change in pull request #7391:
URL: https://github.com/apache/incubator-doris/pull/7391#discussion_r777271525



##########
File path: fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java
##########
@@ -4615,6 +4656,9 @@ public boolean unprotectDropTable(Database db, Table 
table, boolean isForceDrop,
             // drop all temp partitions of this table, so that there is no 
temp partitions in recycle bin,
             // which make things easier.
             ((OlapTable) table).dropAllTempPartitions();
+        } else if (table.getType() == TableType.ICEBERG) {
+            // drop Iceberg database table creation record
+            icebergTableCreationRecordMgr.deRegisterTable(db, (IcebergTable) 
table);

Review comment:
       ```suggestion
               icebergTableCreationRecordMgr.deregisterTable(db, (IcebergTable) 
table);
   ```

##########
File path: fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java
##########
@@ -2642,16 +2657,24 @@ public void createDb(CreateDbStmt stmt) throws 
DdlException {
                     
ErrorReport.reportDdlException(ErrorCode.ERR_DB_CREATE_EXISTS, fullDbName);
                 }
             } else {
-                id = getNextId();
-                Database db = new Database(id, fullDbName);
-                db.setClusterName(clusterName);
                 unprotectCreateDb(db);
                 editLog.logCreateDb(db);
             }
         } finally {
             unlock();
         }
         LOG.info("createDb dbName = " + fullDbName + ", id = " + id);
+
+        // create tables in iceberg database
+        if (db.getDbProperties().getIcebergProperty().isExist()) {
+            IcebergProperty icebergProperty = 
db.getDbProperties().getIcebergProperty();
+            IcebergCatalog icebergCatalog = 
IcebergCatalogMgr.getCatalog(icebergProperty);
+            List<TableIdentifier> icebergTables = 
icebergCatalog.listTables(icebergProperty.getDatabase());

Review comment:
       What if the `listTables` throw exception?

##########
File path: 
fe/fe-core/src/main/java/org/apache/doris/external/iceberg/IcebergTableCreationRecordMgr.java
##########
@@ -0,0 +1,229 @@
+// 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.doris.external.iceberg;
+
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.IcebergProperty;
+import org.apache.doris.catalog.IcebergTable;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.property.PropertySchema;
+import org.apache.doris.common.util.MasterDaemon;
+
+import com.google.common.collect.Maps;
+
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * Manager for Iceberg automatic creation table records
+ * used to create iceberg tables and show table creation records
+ */
+public class IcebergTableCreationRecordMgr extends MasterDaemon {
+    private static final Logger LOG = 
LogManager.getLogger(IcebergTableCreationRecordMgr.class);
+
+    private static final String SUCCESS = "success";
+    private static final String FAIL = "fail";
+
+    // database -> table identifier -> properties
+    // used to create table
+    private Map<Database, Map<TableIdentifier, IcebergProperty>> 
dbToTableIdentifiers = Maps.newConcurrentMap();
+    // table creation records, used for show stmt
+    // db -> table -> create msg
+    private Map<String, Map<String, IcebergTableCreationRecord>> 
dbToTableToCreationRecord = Maps.newConcurrentMap();
+
+    private Queue<IcebergTableCreationRecord> tableCreationRecordQueue = new 
PriorityQueue<>(new TableCreationComparator());
+    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+
+
+    public IcebergTableCreationRecordMgr() {
+        super("iceberg_table_creation_record_mgr", 
Config.iceberg_table_creation_interval_second * 1000);
+    }
+
+    public void registerTable(Database db, TableIdentifier identifier, 
IcebergProperty icebergProperty) {
+        if (dbToTableIdentifiers.containsKey(db)) {
+            dbToTableIdentifiers.get(db).put(identifier, icebergProperty);
+        } else {
+            Map<TableIdentifier, IcebergProperty> identifierToProperties = 
Maps.newConcurrentMap();
+            identifierToProperties.put(identifier, icebergProperty);
+            dbToTableIdentifiers.put(db, identifierToProperties);
+        }
+        LOG.info("Register a new table[{}] to database[{}]", 
identifier.name(), db.getFullName());
+    }
+
+    public void deRegisterDb(Database db) {
+        dbToTableIdentifiers.remove(db);
+        dbToTableToCreationRecord.remove(db.getFullName());
+        LOG.info("DeRegister database[{}]", db.getFullName());
+    }
+
+    public void deRegisterTable(Database db, IcebergTable table) {
+        if (dbToTableIdentifiers.containsKey(db)) {
+            TableIdentifier identifier = 
TableIdentifier.of(table.getIcebergDb(), table.getIcebergTbl());
+            Map<TableIdentifier, IcebergProperty> identifierToProperties = 
dbToTableIdentifiers.get(db);
+            identifierToProperties.remove(identifier);
+        }
+        if (dbToTableToCreationRecord.containsKey(db.getFullName())) {
+            Map<String, IcebergTableCreationRecord> recordMap = 
dbToTableToCreationRecord.get(db.getFullName());
+            recordMap.remove(table.getName());
+        }
+        LOG.info("DeRegister table[{}] from database[{}]", table.getName(), 
db.getFullName());
+    }
+
+    // remove already created tables or failed tables
+    private void removeDuplicateTables() {
+        for (Map.Entry<String, Map<String, IcebergTableCreationRecord>> entry 
: dbToTableToCreationRecord.entrySet()) {
+            String dbName = entry.getKey();
+            Catalog.getCurrentCatalog().getDb(dbName).ifPresent(db -> {
+                if (dbToTableIdentifiers.containsKey(db)) {
+                    for (Map.Entry<String, IcebergTableCreationRecord> 
innerEntry : entry.getValue().entrySet()) {
+                        String tableName = innerEntry.getKey();
+                        String icebergDbName = 
db.getDbProperties().getIcebergProperty().getDatabase();
+                        TableIdentifier identifier = 
TableIdentifier.of(icebergDbName, tableName);
+                        dbToTableIdentifiers.get(db).remove(identifier);
+                    }
+                }
+            });
+        }
+    }
+
+    @Override
+    protected void runAfterCatalogReady() {
+        PropertySchema.DateProperty prop =
+                new PropertySchema.DateProperty("key", new 
SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
+        for (Map.Entry<Database, Map<TableIdentifier, IcebergProperty>> entry 
: dbToTableIdentifiers.entrySet()) {
+            Database db = entry.getKey();
+            for (Map.Entry<TableIdentifier, IcebergProperty> innerEntry : 
entry.getValue().entrySet()) {
+                TableIdentifier identifier = innerEntry.getKey();
+                IcebergProperty icebergProperty = innerEntry.getValue();
+                try {
+                    // get doris table from iceberg
+                    IcebergTable table = 
IcebergCatalogMgr.getTableFromIceberg(identifier.name(),
+                            icebergProperty, identifier, false);
+                    // check iceberg table if exists in doris database
+                    if (!db.createTableWithLock(table, false, false).first) {
+                        
ErrorReport.reportDdlException(ErrorCode.ERR_CANT_CREATE_TABLE,
+                                table.getName(), 
ErrorCode.ERR_TABLE_EXISTS_ERROR.getCode());
+                    }
+                    addTableCreationRecord(db.getFullName(), table.getName(), 
SUCCESS,
+                            prop.writeTimeFormat(new 
Date(System.currentTimeMillis())), "");
+                    LOG.info("Successfully create table[{}-{}]", 
table.getName(), table.getId());
+                } catch (Exception e) {
+                    addTableCreationRecord(db.getFullName(), 
identifier.name(), FAIL,
+                            prop.writeTimeFormat(new 
Date(System.currentTimeMillis())), e.getMessage());
+                    LOG.warn("Failed create table[{}], error: {}", 
identifier.name(), e.getMessage());
+                }
+            }
+        }
+        removeDuplicateTables();
+    }
+
+    private void addTableCreationRecord(String db, String table, String 
status, String createTime, String errorMsg) {
+        writeLock();

Review comment:
       Use `try...finally` to wrap the lock

##########
File path: 
fe/fe-core/src/main/java/org/apache/doris/external/iceberg/IcebergTableCreationRecordMgr.java
##########
@@ -0,0 +1,229 @@
+// 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.doris.external.iceberg;
+
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.IcebergProperty;
+import org.apache.doris.catalog.IcebergTable;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.property.PropertySchema;
+import org.apache.doris.common.util.MasterDaemon;
+
+import com.google.common.collect.Maps;
+
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * Manager for Iceberg automatic creation table records
+ * used to create iceberg tables and show table creation records
+ */
+public class IcebergTableCreationRecordMgr extends MasterDaemon {
+    private static final Logger LOG = 
LogManager.getLogger(IcebergTableCreationRecordMgr.class);
+
+    private static final String SUCCESS = "success";
+    private static final String FAIL = "fail";
+
+    // database -> table identifier -> properties
+    // used to create table
+    private Map<Database, Map<TableIdentifier, IcebergProperty>> 
dbToTableIdentifiers = Maps.newConcurrentMap();
+    // table creation records, used for show stmt
+    // db -> table -> create msg
+    private Map<String, Map<String, IcebergTableCreationRecord>> 
dbToTableToCreationRecord = Maps.newConcurrentMap();
+
+    private Queue<IcebergTableCreationRecord> tableCreationRecordQueue = new 
PriorityQueue<>(new TableCreationComparator());
+    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+
+
+    public IcebergTableCreationRecordMgr() {
+        super("iceberg_table_creation_record_mgr", 
Config.iceberg_table_creation_interval_second * 1000);
+    }
+
+    public void registerTable(Database db, TableIdentifier identifier, 
IcebergProperty icebergProperty) {
+        if (dbToTableIdentifiers.containsKey(db)) {
+            dbToTableIdentifiers.get(db).put(identifier, icebergProperty);
+        } else {
+            Map<TableIdentifier, IcebergProperty> identifierToProperties = 
Maps.newConcurrentMap();
+            identifierToProperties.put(identifier, icebergProperty);
+            dbToTableIdentifiers.put(db, identifierToProperties);
+        }
+        LOG.info("Register a new table[{}] to database[{}]", 
identifier.name(), db.getFullName());
+    }
+
+    public void deRegisterDb(Database db) {

Review comment:
       ```suggestion
       public void deregisterDb(Database db) {
   ```




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to