morrySnow commented on code in PR #63863:
URL: https://github.com/apache/doris/pull/63863#discussion_r3427623096


##########
fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalCatalog.java:
##########
@@ -1057,7 +1057,9 @@ public boolean createTable(CreateTableInfo 
createTableInfo) throws UserException
             boolean res = metadataOps.createTable(createTableInfo);
             if (!res) {
                 // res == false means the table does not exist before, and we 
create it.
-                // we should get the table stored in Doris, and use local name 
in edit log.
+                // Register the new table into meta cache so it is immediately 
visible
+                // without requiring a manual REFRESH CATALOG.
+                registerTableFromCreate(createTableInfo.getDbName(), 
createTableInfo.getTableName());

Review Comment:
   **Ordering concern**: `registerTableFromCreate()` is called **before** 
`logCreateTable(info)` (line 1067). If `registerTableFromCreate()` were to 
throw an unchecked exception (the method internally catches most errors, but 
e.g. `Util.genIdByName` could theoretically throw), the edit log would not be 
written and followers/observers wouldn't learn about the new table.
   
   **Suggestion**: Consider moving the call to **after** the edit log write:
   ```java
   Env.getCurrentEnv().getEditLog().logCreateTable(info);
   registerTableFromCreate(createTableInfo.getDbName(), 
createTableInfo.getTableName());
   ```
   This follows "log first, cache second" and ensures the cache warming is a 
best-effort optimization that can't interfere with edit log persistence.



##########
fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalDatabase.java:
##########
@@ -593,6 +593,32 @@ public boolean registerTable(TableIf tableIf) {
         return true;
     }
 
+    /**
+     * Register a newly created table into the meta cache after DDL execution.
+     * This method builds the table object and updates both the names cache 
and the meta object cache,
+     * so that the table is immediately visible (e.g., via SHOW CREATE TABLE) 
without requiring
+     * a manual REFRESH CATALOG.
+     */
+    public void registerTableFromCreate(String tblName) {

Review Comment:
   **Naming suggestion**: The method is named `registerTableFromCreate` but is 
also called from the **rename** path (`ExternalCatalog.renameTable()` line 
1100, `IcebergMetadataOps.afterRenameTable()` line 445, 
`RefreshManager.replayRefreshTable()` line 188). The name implies it's only for 
table creation.
   
   Consider a more general name like `registerTableAfterDdl()` or simply 
mention in the Javadoc that it also applies to rename scenarios.



##########
fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalDatabase.java:
##########
@@ -593,6 +593,32 @@ public boolean registerTable(TableIf tableIf) {
         return true;
     }
 
+    /**
+     * Register a newly created table into the meta cache after DDL execution.
+     * This method builds the table object and updates both the names cache 
and the meta object cache,
+     * so that the table is immediately visible (e.g., via SHOW CREATE TABLE) 
without requiring
+     * a manual REFRESH CATALOG.
+     */
+    public void registerTableFromCreate(String tblName) {
+        makeSureInitialized();
+        if (!isInitialized()) {
+            return;
+        }
+        String localName = extCatalog.fromRemoteTableName(this.remoteName, 
tblName);
+        long tblId = Util.genIdByName(extCatalog.getName(), name, localName);
+        T table = buildTableForInit(tblName, localName, tblId, extCatalog, 
this, false);
+        if (table == null) {
+            LOG.warn("Failed to build table {}.{} after create, fall back to 
reset names cache",
+                    this.name, tblName);
+            resetMetaCacheNames();
+            return;
+        }
+        metaCache.updateCache(tblName, localName, table, tblId);

Review Comment:
   **Thread safety note**: `metaCache.updateCache()` (MetaCache.java:128) 
writes to `metaObjCache`, `namesCache`, and `idToName` without explicit 
synchronization. However, `metaObjCache` and `namesCache` are Guava Cache 
instances which are thread-safe by design, and `idToName` is likely a 
`ConcurrentHashMap`. This pattern matches the existing `registerTable()` method 
at line 588 and is consistent with the codebase's approach.



##########
fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalDatabase.java:
##########
@@ -593,6 +593,32 @@ public boolean registerTable(TableIf tableIf) {
         return true;
     }
 
+    /**
+     * Register a newly created table into the meta cache after DDL execution.
+     * This method builds the table object and updates both the names cache 
and the meta object cache,
+     * so that the table is immediately visible (e.g., via SHOW CREATE TABLE) 
without requiring
+     * a manual REFRESH CATALOG.
+     */
+    public void registerTableFromCreate(String tblName) {
+        makeSureInitialized();
+        if (!isInitialized()) {
+            return;
+        }
+        String localName = extCatalog.fromRemoteTableName(this.remoteName, 
tblName);
+        long tblId = Util.genIdByName(extCatalog.getName(), name, localName);
+        T table = buildTableForInit(tblName, localName, tblId, extCatalog, 
this, false);

Review Comment:
   **Good**: Using `checkExists=false` avoids a redundant remote existence 
check — we know the table exists because we just created/renamed it. The 
fallback to `resetMetaCacheNames()` at line 613 ensures that even if the table 
can't be built (e.g., connectivity issues), the stale names cache is cleared 
and the table will be lazily loaded on next access.



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