shuwenwei commented on code in PR #18304:
URL: https://github.com/apache/iotdb/pull/18304#discussion_r3662781116


##########
integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBQueryWithCorruptedTsFileIT.java:
##########
@@ -0,0 +1,325 @@
+/*
+ * 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.iotdb.relational.it.query.recent;
+
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
+
+import org.apache.tsfile.common.conf.TSFileConfig;
+import org.apache.tsfile.enums.ColumnCategory;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.exception.write.WriteProcessException;
+import org.apache.tsfile.file.metadata.TableSchema;
+import org.apache.tsfile.read.TsFileSequenceReader;
+import org.apache.tsfile.write.TsFileWriter;
+import org.apache.tsfile.write.record.Tablet;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({TableLocalStandaloneIT.class})
+public class IoTDBQueryWithCorruptedTsFileIT {
+  private static final String DATABASE_NAME = "test_corrupted_read_tsfile";
+
+  private static File tmpDir;
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    EnvFactory.getEnv().initClusterEnvironment();
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("CREATE DATABASE " + DATABASE_NAME);
+    }
+  }
+
+  @Before
+  public void setUpBeforeTest() throws IOException {
+    tmpDir = new File(Files.createTempDirectory("corrupt-tsfile").toUri());
+  }
+
+  @After
+  public void tearDownAfterTest() {
+    deleteTmpDir();
+  }
+
+  @AfterClass
+  public static void tearDown() {
+    EnvFactory.getEnv().cleanClusterEnvironment();
+  }
+
+  @Test
+  public void testReadTsFileWithCorruptedMetadataIndexNode() throws Exception {
+    File tsFile = new File(tmpDir, "corrupt-meta.tsfile");
+    try (TsFileWriter writer = new TsFileWriter(tsFile)) {
+      generateTable(
+          writer, "table1", Arrays.asList("tag1"), Arrays.asList("s1"), 
TSDataType.INT64, 1, 10);
+    }
+
+    // TsFile layout: [Header] [Data] [MetadataIndex Tree] [TsFileMetadata] 
[Magic][Size]
+    //                                    ↑ metaOffset           ↑ 
fileMetadataPos
+    //
+    // MetadataIndexNode serialization:
+    //   [entryCount (varInt)] [entry1]...[entryN] [endOffset (long, 8B)] 
[nodeType (1B)]
+    //   nodeType valid values: 0=INTERNAL_DEVICE, 1=LEAF_DEVICE, 
2=INTERNAL_MEASUREMENT,
+    // 3=LEAF_MEASUREMENT
+    //   nodeType is the LAST byte before TsFileMetadata, i.e. at 
fileMetadataPos - 1
+    long metaOffset;
+    long fileMetadataPos;
+    try (TsFileSequenceReader reader = new 
TsFileSequenceReader(tsFile.getAbsolutePath())) {
+      metaOffset = reader.readFileMetadata().getMetaOffset();
+      fileMetadataPos = reader.getFileMetadataPos();
+    }
+
+    // Corrupt the nodeType byte to 0xFF (all valid types are 0-3)
+    byte[] fileBytes = Files.readAllBytes(tsFile.toPath());
+    fileBytes[(int) fileMetadataPos - 1] = (byte) 0xFF;

Review Comment:
   Thanks for pointing this out. For `read_tsfile`, metadata-index traversal is 
handled by `ExternalTsFileQueryResource.DeviceCollector`. Exceptions raised 
while constructing or advancing `LazyTsFileDeviceIterator` are consistently 
wrapped as `CorruptedTsFileException` with the `READ_METADATA_INDEX_NODE` 
stage, without going through `FileLoaderUtils`. I have updated the test to 
assert the exact stage-specific message: `Failed to read metadata index node 
from TsFile: <path>`.



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