This is an automated email from the ASF dual-hosted git repository.

Caideyipi pushed a commit to branch codex/pr-tree-last-cache-schema-isolation
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 3002d3f6acca7eee6b8ea9ca9f2e158d65b93874
Author: Caideyipi <[email protected]>
AuthorDate: Fri Nov 6 18:53:33 2026 +0800

    Prevent tree last-cache updates from restoring schema
---
 .../last/AbstractUpdateLastCacheOperator.java      |  2 -
 .../planner/plan/node/write/InsertRowNode.java     |  2 +-
 .../planner/plan/node/write/InsertTabletNode.java  |  7 +-
 .../fetcher/cache/TableDeviceCacheEntry.java       | 27 -------
 .../fetcher/cache/TableDeviceSchemaCache.java      | 35 +++-----
 .../cache/TreeDeviceSchemaCacheManager.java        | 18 ++---
 .../db/storageengine/dataregion/DataRegion.java    |  8 +-
 .../cache/TreeDeviceSchemaCacheManagerTest.java    | 92 ++++++++++++++++++++--
 8 files changed, 109 insertions(+), 82 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/last/AbstractUpdateLastCacheOperator.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/last/AbstractUpdateLastCacheOperator.java
index e1c3cb335b0..38c5781d107 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/last/AbstractUpdateLastCacheOperator.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/last/AbstractUpdateLastCacheOperator.java
@@ -129,7 +129,6 @@ public abstract class AbstractUpdateLastCacheOperator 
implements ProcessOperator
                   ? new TimeValuePair(time, value)
                   : needUpdateNullEntry ? 
TableDeviceLastCache.PLACEHOLDER_EMPTY_COLUMN : null
             },
-            fullPath.isUnderAlignedEntity(),
             new IMeasurementSchema[] {fullPath.getMeasurementSchema()});
         return;
       }
@@ -149,7 +148,6 @@ public abstract class AbstractUpdateLastCacheOperator 
implements ProcessOperator
             fullPath.getIDeviceID(),
             new String[] {fullPath.getMeasurement()},
             new TimeValuePair[] {seriesScanInfo.right},
-            fullPath.isUnderAlignedEntity(),
             new IMeasurementSchema[] {fullPath.getMeasurementSchema()});
       }
     } finally {
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertRowNode.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertRowNode.java
index ecf76892e4c..d053cfd1ec6 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertRowNode.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertRowNode.java
@@ -927,7 +927,7 @@ public class InsertRowNode extends InsertNode implements 
WALEntryValue, LastCach
   public void updateLastCache(String databaseName) {
     TreeDeviceSchemaCacheManager.getInstance()
         .updateLastCacheIfExists(
-            databaseName, getDeviceID(), measurements, this, isAligned, 
measurementSchemas);
+            databaseName, getDeviceID(), measurements, this, 
measurementSchemas);
   }
 
   @Override
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertTabletNode.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertTabletNode.java
index 5c91d0af4ea..61c9a695367 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertTabletNode.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertTabletNode.java
@@ -1405,12 +1405,7 @@ public class InsertTabletNode extends InsertNode 
implements WALEntryValue {
     }
     TreeDeviceSchemaCacheManager.getInstance()
         .updateLastCacheIfExists(
-            databaseName,
-            getDeviceID(),
-            measurements,
-            timeValuePairs,
-            isAligned,
-            measurementSchemas);
+            databaseName, getDeviceID(), measurements, timeValuePairs, 
measurementSchemas);
   }
 
   @Override
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceCacheEntry.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceCacheEntry.java
index c7f67307a70..31c2054d6d2 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceCacheEntry.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceCacheEntry.java
@@ -131,33 +131,6 @@ public class TableDeviceCacheEntry {
     }
   }
 
-  int setMeasurementSchema(
-      final String database,
-      final boolean isAligned,
-      final String[] measurements,
-      final IMeasurementSchema[] schemas) {
-    if (schemas == null) {
-      return 0;
-    }
-    // Safe here because tree schema is invalidated by the whole entry
-    IDeviceSchema schema = deviceSchema.get();
-    int result = 0;
-    if (schema == null) {
-      final TreeDeviceNormalSchema newSchema = new 
TreeDeviceNormalSchema(database, isAligned);
-      if (deviceSchema.compareAndSet(null, newSchema)) {
-        schema = newSchema;
-        result = TreeDeviceNormalSchema.INSTANCE_SIZE;
-      } else {
-        schema = deviceSchema.get();
-      }
-    }
-    if (!(schema instanceof TreeDeviceNormalSchema)) {
-      return 0;
-    }
-    result += ((TreeDeviceNormalSchema) schema).update(measurements, schemas);
-    return deviceSchema.get() == schema ? result : 0;
-  }
-
   IDeviceSchema getDeviceSchema() {
     return deviceSchema.get();
   }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceSchemaCache.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceSchemaCache.java
index 17f691d8542..b9b8881dc9c 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceSchemaCache.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceSchemaCache.java
@@ -493,35 +493,30 @@ public class TableDeviceSchemaCache {
     return Objects.nonNull(entry) ? entry.getDeviceSchema() : null;
   }
 
+  /**
+   * Update tree-model last values without populating schema. Query plans and 
storage-engine writes
+   * may carry schema that has already been deleted; only schema fetches may 
populate that cache.
+   */
   void updateLastCache(
       final String database,
       final IDeviceID deviceID,
       final String[] measurements,
       final @Nullable TimeValuePair[] timeValuePairs,
-      final boolean isAligned,
       final IMeasurementSchema[] measurementSchemas,
       final boolean initOrInvalidate) {
-    final String previousDatabase = 
treeModelDatabasePool.putIfAbsent(database, database);
-    final String database2Use = Objects.nonNull(previousDatabase) ? 
previousDatabase : database;
-
     dualKeyCache.update(
         new TableId(null, deviceID.getTableName()),
         deviceID,
         Objects.isNull(timeValuePairs) ? new TableDeviceCacheEntry() : null,
         initOrInvalidate
             ? entry ->
-                entry.setMeasurementSchema(
-                        database2Use, isAligned, measurements, 
measurementSchemas)
-                    + entry.initOrInvalidateLastCache(
-                        database,
-                        deviceID.getTableName(),
-                        measurements,
-                        Objects.nonNull(timeValuePairs),
-                        false)
-            : entry ->
-                entry.setMeasurementSchema(
-                        database2Use, isAligned, measurements, 
measurementSchemas)
-                    + entry.tryUpdateLastCache(measurements, 
measurementSchemas, timeValuePairs),
+                entry.initOrInvalidateLastCache(
+                    database,
+                    deviceID.getTableName(),
+                    measurements,
+                    Objects.nonNull(timeValuePairs),
+                    false)
+            : entry -> entry.tryUpdateLastCache(measurements, 
measurementSchemas, timeValuePairs),
         Objects.isNull(timeValuePairs));
   }
 
@@ -530,18 +525,12 @@ public class TableDeviceSchemaCache {
       final IDeviceID deviceID,
       final String[] measurements,
       final LastCacheUpdateSource updateSource,
-      final boolean isAligned,
       final IMeasurementSchema[] measurementSchemas) {
-    final String previousDatabase = 
treeModelDatabasePool.putIfAbsent(database, database);
-    final String database2Use = Objects.nonNull(previousDatabase) ? 
previousDatabase : database;
-
     dualKeyCache.update(
         new TableId(null, deviceID.getTableName()),
         deviceID,
         null,
-        entry ->
-            entry.setMeasurementSchema(database2Use, isAligned, measurements, 
measurementSchemas)
-                + entry.tryUpdateLastCache(measurements, measurementSchemas, 
updateSource),
+        entry -> entry.tryUpdateLastCache(measurements, measurementSchemas, 
updateSource),
         false);
   }
 
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TreeDeviceSchemaCacheManager.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TreeDeviceSchemaCacheManager.java
index 0f0f92ef0ff..3746f048af9 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TreeDeviceSchemaCacheManager.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TreeDeviceSchemaCacheManager.java
@@ -350,14 +350,13 @@ public class TreeDeviceSchemaCacheManager {
       final IDeviceID deviceID,
       final String[] measurements,
       final @Nonnull TimeValuePair[] timeValuePairs,
-      final boolean isAligned,
       final IMeasurementSchema[] measurementSchemas) {
     if (!ClusterPartitionFetcher.getInstance().needLastCache(database)) {
       return;
     }
 
     tableDeviceSchemaCache.updateLastCache(
-        database, deviceID, measurements, timeValuePairs, isAligned, 
measurementSchemas, false);
+        database, deviceID, measurements, timeValuePairs, measurementSchemas, 
false);
   }
 
   public void updateLastCacheIfExists(
@@ -365,10 +364,9 @@ public class TreeDeviceSchemaCacheManager {
       final IDeviceID deviceID,
       final String[] measurements,
       final LastCacheUpdateSource updateSource,
-      final boolean isAligned,
       final IMeasurementSchema[] measurementSchemas) {
     tableDeviceSchemaCache.updateLastCache(
-        database, deviceID, measurements, updateSource, isAligned, 
measurementSchemas);
+        database, deviceID, measurements, updateSource, measurementSchemas);
   }
 
   /**
@@ -382,9 +380,9 @@ public class TreeDeviceSchemaCacheManager {
    * is written, but it won't affect the eventual consistency.
    *
    * <p>- Second time put the calculated {@link TimeValuePair}, and use {@link
-   * #updateLastCacheIfExists(String, IDeviceID, String[], TimeValuePair[], 
boolean,
-   * IMeasurementSchema[])}. The input {@link TimeValuePair} shall never be or 
contain {@code null},
-   * if the measurement is with all {@code null}s, its {@link TimeValuePair} 
shall be {@link
+   * #updateLastCacheIfExists(String, IDeviceID, String[], TimeValuePair[], 
IMeasurementSchema[])}.
+   * The input {@link TimeValuePair} shall never be or contain {@code null}, 
if the measurement is
+   * with all {@code null}s, its {@link TimeValuePair} shall be {@link
    * TableDeviceLastCache#PLACEHOLDER_EMPTY_COLUMN}. This method is not 
supposed to update time
    * column.
    *
@@ -401,8 +399,7 @@ public class TreeDeviceSchemaCacheManager {
         measurementPath.getIDeviceID(),
         new String[] {measurementPath.getMeasurement()},
         null,
-        measurementPath.isUnderAlignedEntity(),
-        new IMeasurementSchema[] {measurementPath.getMeasurementSchema()},
+        null,
         true);
   }
 
@@ -421,8 +418,7 @@ public class TreeDeviceSchemaCacheManager {
         measurementPath.getIDeviceID(),
         new String[] {measurementPath.getMeasurement()},
         new TimeValuePair[] {null},
-        measurementPath.isUnderAlignedEntity(),
-        new IMeasurementSchema[] {measurementPath.getMeasurementSchema()},
+        null,
         true);
   }
 
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java
index ee877628cd9..46c9339a507 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java
@@ -4745,10 +4745,8 @@ public class DataRegion implements IDataRegionForQuery {
           TableDeviceSchemaCache.getInstance()
               .updateLastCacheIfExists(databaseName, deviceID, measurements, 
timeValuePairs);
         } else {
-          // we do not update schema here, so aligned is not relevant
           TreeDeviceSchemaCacheManager.getInstance()
-              .updateLastCacheIfExists(
-                  databaseName, deviceID, measurements, timeValuePairs, false, 
null);
+              .updateLastCacheIfExists(databaseName, deviceID, measurements, 
timeValuePairs, null);
         }
       }
       newTsFileResource.setLastValues(null);
@@ -4766,10 +4764,8 @@ public class DataRegion implements IDataRegionForQuery {
           TableDeviceSchemaCache.getInstance()
               .updateLastCacheIfExists(databaseName, deviceID, measurements, 
timeValuePairs);
         } else {
-          // we do not update schema here, so aligned is not relevant
           TreeDeviceSchemaCacheManager.getInstance()
-              .updateLastCacheIfExists(
-                  databaseName, deviceID, measurements, timeValuePairs, false, 
null);
+              .updateLastCacheIfExists(databaseName, deviceID, measurements, 
timeValuePairs, null);
         }
       }
     } else {
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/cache/TreeDeviceSchemaCacheManagerTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/cache/TreeDeviceSchemaCacheManagerTest.java
index 59cdf620561..7d7192ddf3e 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/cache/TreeDeviceSchemaCacheManagerTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/cache/TreeDeviceSchemaCacheManagerTest.java
@@ -217,7 +217,6 @@ public class TreeDeviceSchemaCacheManagerTest {
         new TimeValuePair[] {
           new TimeValuePair(2, new TsPrimitiveType.TsInt(2)),
         },
-        false,
         new MeasurementSchema[] {s1});
     treeDeviceSchemaCacheManager.invalidateLastCache(
         database, new MeasurementPath(device.concatNode("s1"), s1));
@@ -229,7 +228,6 @@ public class TreeDeviceSchemaCacheManagerTest {
             StringArrayDeviceID.splitDeviceIdString(device.getNodes())),
         new String[] {"s2"},
         new TimeValuePair[] {tv1},
-        false,
         new MeasurementSchema[] {s2});
 
     treeDeviceSchemaCacheManager.updateLastCacheIfExists(
@@ -238,7 +236,6 @@ public class TreeDeviceSchemaCacheManagerTest {
             StringArrayDeviceID.splitDeviceIdString(device.getNodes())),
         new String[] {"s3"},
         new TimeValuePair[] {tv1},
-        false,
         new MeasurementSchema[] {s3});
 
     Assert.assertNull(
@@ -260,7 +257,6 @@ public class TreeDeviceSchemaCacheManagerTest {
           new TimeValuePair(2, new TsPrimitiveType.TsInt(2)),
           new TimeValuePair(2, new TsPrimitiveType.TsInt(2))
         },
-        false,
         measurementSchemas);
 
     Assert.assertNull(
@@ -277,6 +273,91 @@ public class TreeDeviceSchemaCacheManagerTest {
     Assert.assertEquals(0, 
TableDeviceSchemaCache.getInstance().getMemoryUsage());
   }
 
+  @Test
+  public void testLastCacheDoesNotRestoreDeletedSchema() throws 
IllegalPathException {
+    final String database = "root.db";
+    for (boolean aligned : new boolean[] {false, true}) {
+      final PartialPath device = new PartialPath("root.db.d" + aligned);
+      final MeasurementSchema schema = new MeasurementSchema("s1", 
TSDataType.INT32);
+      final MeasurementPath path = new 
MeasurementPath(device.concatNode("s1"), schema, aligned);
+      final ClusterSchemaTree schemaTree = new ClusterSchemaTree();
+      schemaTree.appendSingleMeasurementPath(path);
+      schemaTree.setDatabases(Collections.singleton(database));
+      treeDeviceSchemaCacheManager.put(schemaTree);
+      
Assert.assertFalse(treeDeviceSchemaCacheManager.getMatchedNormalSchema(path).isEmpty());
+
+      treeDeviceSchemaCacheManager.invalidate(Collections.singletonList(path));
+      
Assert.assertTrue(treeDeviceSchemaCacheManager.getMatchedNormalSchema(path).isEmpty());
+      treeDeviceSchemaCacheManager.declareLastCache(database, path);
+      Assert.assertTrue(
+          "LAST initialization must not restore deleted schema",
+          treeDeviceSchemaCacheManager.getMatchedNormalSchema(path).isEmpty());
+
+      final TimeValuePair value = new TimeValuePair(1, new 
TsPrimitiveType.TsInt(1));
+      treeDeviceSchemaCacheManager.updateLastCacheIfExists(
+          database,
+          path.getIDeviceID(),
+          new String[] {"s1"},
+          new TimeValuePair[] {value},
+          new MeasurementSchema[] {schema});
+      Assert.assertEquals(value, 
treeDeviceSchemaCacheManager.getLastCache(path));
+      
Assert.assertTrue(treeDeviceSchemaCacheManager.getMatchedNormalSchema(path).isEmpty());
+      Assert.assertTrue(treeDeviceSchemaCacheManager.get(device, new String[] 
{"s1"}).isEmpty());
+
+      treeDeviceSchemaCacheManager.invalidateLastCache(database, path);
+      Assert.assertNull(treeDeviceSchemaCacheManager.getLastCache(path));
+      
Assert.assertTrue(treeDeviceSchemaCacheManager.getMatchedNormalSchema(path).isEmpty());
+    }
+  }
+
+  @Test
+  public void testLastCacheUpdateDoesNotRestoreDeletedSchema() throws 
IllegalPathException {
+    final String database = "root.db";
+    for (boolean aligned : new boolean[] {false, true}) {
+      final PartialPath device = new PartialPath("root.db.d" + aligned);
+      final MeasurementSchema deletedSchema = new MeasurementSchema("s1", 
TSDataType.INT32);
+      final MeasurementPath deletedPath =
+          new MeasurementPath(device.concatNode("s1"), deletedSchema, aligned);
+      final MeasurementPath remainingPath =
+          new MeasurementPath(
+              device.concatNode("s2"), new MeasurementSchema("s2", 
TSDataType.INT64), aligned);
+      final ClusterSchemaTree schemaTree = new ClusterSchemaTree();
+      schemaTree.appendSingleMeasurementPath(deletedPath);
+      schemaTree.appendSingleMeasurementPath(remainingPath);
+      schemaTree.setDatabases(Collections.singleton(database));
+      treeDeviceSchemaCacheManager.put(schemaTree);
+      treeDeviceSchemaCacheManager.declareLastCache(database, deletedPath);
+
+      
treeDeviceSchemaCacheManager.invalidate(Collections.singletonList(deletedPath));
+      final ClusterSchemaTree remainingSchema = new ClusterSchemaTree();
+      remainingSchema.appendSingleMeasurementPath(remainingPath);
+      remainingSchema.setDatabases(Collections.singleton(database));
+      treeDeviceSchemaCacheManager.put(remainingSchema);
+
+      treeDeviceSchemaCacheManager.updateLastCacheIfExists(
+          database,
+          deletedPath.getIDeviceID(),
+          new String[] {"s1"},
+          new TimeValuePair[] {new TimeValuePair(1, new 
TsPrimitiveType.TsInt(1))},
+          new MeasurementSchema[] {deletedSchema});
+      Assert.assertTrue(
+          "LAST completion must not restore deleted schema in a recreated 
device entry",
+          
treeDeviceSchemaCacheManager.getMatchedNormalSchema(deletedPath).isEmpty());
+      
Assert.assertNull(treeDeviceSchemaCacheManager.getLastCache(deletedPath));
+
+      treeDeviceSchemaCacheManager.invalidateLastCache(database, deletedPath);
+      
Assert.assertTrue(treeDeviceSchemaCacheManager.getMatchedNormalSchema(deletedPath).isEmpty());
+      Assert.assertEquals(
+          remainingPath.getMeasurementSchema(),
+          treeDeviceSchemaCacheManager
+              .getMatchedNormalSchema(remainingPath)
+              .searchMeasurementPaths(remainingPath)
+              .left
+              .get(0)
+              .getMeasurementSchema());
+    }
+  }
+
   @Test
   public void testLastCacheIsInvalidatedWhenDatabaseNeedLastCacheSetFalse()
       throws IllegalPathException {
@@ -293,7 +374,6 @@ public class TreeDeviceSchemaCacheManagerTest {
             StringArrayDeviceID.splitDeviceIdString(device.getNodes())),
         new String[] {"s1"},
         new TimeValuePair[] {tv1},
-        false,
         new MeasurementSchema[] {s1});
     Assert.assertEquals(tv1, 
treeDeviceSchemaCacheManager.getLastCache(measurementPath));
 
@@ -341,6 +421,7 @@ public class TreeDeviceSchemaCacheManagerTest {
     insertRowNode.updateLastCache(database);
 
     Assert.assertEquals(1, composedValueCount.get());
+    
Assert.assertTrue(treeDeviceSchemaCacheManager.getMatchedNormalSchema(s1Path).isEmpty());
     Assert.assertEquals(
         new TimeValuePair(1L, new TsPrimitiveType.TsInt(1)),
         treeDeviceSchemaCacheManager.getLastCache(
@@ -398,7 +479,6 @@ public class TreeDeviceSchemaCacheManagerTest {
             StringArrayDeviceID.splitDeviceIdString(devicePath.getNodes())),
         new String[] {path.getMeasurement()},
         new TimeValuePair[] {timeValuePair},
-        false,
         new MeasurementSchema[] {schema});
   }
 

Reply via email to