-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add CorruptedTsFileException for TsFile corruption errors with file path #18304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
3c25788
762a160
0e5b97c
a41de95
23b822c
5a3f91d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| Files.write(tsFile.toPath(), fileBytes); | ||
|
|
||
| tableAssertTestFail( | ||
| "SELECT * FROM read_tsfile(PATHS => '" + toSqlPath(tsFile) + "')", | ||
| "timeseries metadata", | ||
| DATABASE_NAME); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadTsFileWithCorruptedPageData() throws Exception { | ||
| File tsFile = new File(tmpDir, "corrupt-page.tsfile"); | ||
| try (TsFileWriter writer = new TsFileWriter(tsFile)) { | ||
| generateTable( | ||
| writer, "table1", Arrays.asList("tag1"), Arrays.asList("s1"), TSDataType.INT64, 1, 100); | ||
| } | ||
|
|
||
| corruptDataSection(tsFile); | ||
|
|
||
| tableAssertTestFail( | ||
| "SELECT * FROM read_tsfile(PATHS => '" + toSqlPath(tsFile) + "')", "TsFile", DATABASE_NAME); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNormalQueryWithCorruptedPageData() throws Exception { | ||
| String tableName = "corrupt_table"; | ||
| // 1. Create table and insert data via session — generates TsFiles in the data directory | ||
| try (Connection connection = EnvFactory.getEnv().getTableConnection(); | ||
| Statement statement = connection.createStatement()) { | ||
| statement.execute("USE " + DATABASE_NAME); | ||
| statement.execute( | ||
| "CREATE TABLE " + tableName + "(device_id STRING TAG, s1 INT64 FIELD, s2 INT64 FIELD)"); | ||
| for (int i = 1; i <= 200; i++) { | ||
| statement.execute( | ||
| "INSERT INTO " | ||
| + tableName | ||
| + "(time, device_id, s1, s2) VALUES(" | ||
| + i | ||
| + ", 'd" | ||
| + (i % 10) | ||
| + "', " | ||
| + i | ||
| + ", " | ||
| + (i * 10) | ||
| + ")"); | ||
| } | ||
| statement.execute("FLUSH"); | ||
| } | ||
|
|
||
| // 2. Find the generated TsFile in the data directory | ||
| File sequenceDir = | ||
| new File( | ||
| EnvFactory.getEnv().getDataNodeWrapper(0).getDataNodeDir() | ||
| + File.separator | ||
| + "data" | ||
| + File.separator | ||
| + "sequence"); | ||
| File tsFile = findTsFileRecursively(sequenceDir); | ||
| if (tsFile == null) { | ||
| fail("Could not find TsFile in data directory: " + sequenceDir.getAbsolutePath()); | ||
| } | ||
|
|
||
| // 3. Corrupt the data section of the TsFile | ||
| corruptDataSection(tsFile); | ||
|
|
||
| // 4. Query — should fail with a corruption message that does NOT include the file path | ||
| try (Connection connection = EnvFactory.getEnv().getTableConnection(); | ||
| Statement statement = connection.createStatement()) { | ||
| statement.execute("USE " + DATABASE_NAME); | ||
| try { | ||
| statement.execute("SELECT * FROM " + tableName + " ORDER BY time"); | ||
| fail("Expected query on corrupted TsFile to fail"); | ||
| } catch (SQLException e) { | ||
| assertTrue( | ||
| "Error message should mention corruption without file path: " + e.getMessage(), | ||
| e.getMessage().contains("may be corrupted") | ||
| || e.getMessage().contains("check the logs")); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Corrupts a block of bytes in the data section of a TsFile (between the header and the | ||
| * MetadataIndex tree). Uses {@link TsFileSequenceReader} to read {@code metaOffset} from | ||
| * TsFileMetadata, so corruption reliably hits compressed page data rather than metadata. | ||
| * | ||
| * <p>TsFile layout: [Header] [Data chunks] [MetadataIndex Tree] [TsFileMetadata] [Magic][Size] ↑ | ||
| * metaOffset | ||
| */ | ||
| private static void corruptDataSection(File tsFile) throws IOException { | ||
| long metaOffset; | ||
| try (TsFileSequenceReader reader = new TsFileSequenceReader(tsFile.getAbsolutePath())) { | ||
| metaOffset = reader.readFileMetadata().getMetaOffset(); | ||
| } | ||
|
|
||
| byte[] fileBytes = Files.readAllBytes(tsFile.toPath()); | ||
| int magicLen = TSFileConfig.MAGIC_STRING.getBytes().length; | ||
| int dataStart = magicLen + Byte.BYTES; | ||
| int dataEnd = (int) metaOffset; | ||
| // Corrupt bytes in the middle of the data section — XOR 512 bytes to ensure decompression fails | ||
| int middle = dataStart + (dataEnd - dataStart) / 2; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] The midpoint is not a page-data boundary. In the current run both this and the normal-query case corrupt a chunk header and report |
||
| int corruptLen = Math.min(512, dataEnd - middle); | ||
| for (int i = 0; i < corruptLen; i++) { | ||
| fileBytes[middle + i] ^= 0xFF; | ||
| } | ||
| Files.write(tsFile.toPath(), fileBytes); | ||
| } | ||
|
|
||
| private static File findTsFileRecursively(File dir) { | ||
| if (dir == null || !dir.exists()) { | ||
| return null; | ||
| } | ||
| File[] files = dir.listFiles(); | ||
| if (files == null) { | ||
| return null; | ||
| } | ||
| for (File file : files) { | ||
| if (file.isDirectory()) { | ||
| File found = findTsFileRecursively(file); | ||
| if (found != null) { | ||
| return found; | ||
| } | ||
| } else if (file.getName().endsWith(".tsfile") && file.length() > 0) { | ||
| return file; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static void generateTable( | ||
| TsFileWriter writer, | ||
| String tableName, | ||
| List<String> tagColumns, | ||
| List<String> fieldColumns, | ||
| TSDataType fieldType, | ||
| long startTime, | ||
| long endTime) | ||
| throws IOException, WriteProcessException { | ||
| List<String> columnNames = new ArrayList<>(tagColumns.size() + fieldColumns.size()); | ||
| List<TSDataType> columnTypes = new ArrayList<>(tagColumns.size() + fieldColumns.size()); | ||
| List<ColumnCategory> columnCategories = | ||
| new ArrayList<>(tagColumns.size() + fieldColumns.size()); | ||
| for (String tagColumn : tagColumns) { | ||
| columnNames.add(tagColumn); | ||
| columnTypes.add(TSDataType.STRING); | ||
| columnCategories.add(ColumnCategory.TAG); | ||
| } | ||
| for (String fieldColumn : fieldColumns) { | ||
| columnNames.add(fieldColumn); | ||
| columnTypes.add(fieldType); | ||
| columnCategories.add(ColumnCategory.FIELD); | ||
| } | ||
|
|
||
| writer.registerTableSchema( | ||
| new TableSchema(tableName, columnNames, columnTypes, columnCategories)); | ||
| Tablet tablet = new Tablet(tableName, columnNames, columnTypes, columnCategories); | ||
| for (int deviceIndex = 1; deviceIndex <= 2; deviceIndex++) { | ||
| for (long time = startTime; time <= endTime; time++) { | ||
| int row = tablet.getRowSize(); | ||
| tablet.addTimestamp(row, time); | ||
| for (int i = 0; i < tagColumns.size(); i++) { | ||
| tablet.addValue(row, i, tagColumns.get(i) + "_" + deviceIndex); | ||
| } | ||
| for (int i = 0; i < fieldColumns.size(); i++) { | ||
| tablet.addValue(row, tagColumns.size() + i, time); | ||
| } | ||
| if (tablet.getRowSize() == tablet.getMaxRowNumber()) { | ||
| writer.writeTable(tablet); | ||
| tablet.reset(); | ||
| } | ||
| } | ||
| } | ||
| if (tablet.getRowSize() != 0) { | ||
| writer.writeTable(tablet); | ||
| } | ||
| } | ||
|
|
||
| private static String toSqlPath(File file) { | ||
| return file.getAbsolutePath().replace("\\", "\\\\").replace("'", "''"); | ||
| } | ||
|
|
||
| private static void deleteTmpDir() { | ||
| if (tmpDir == null || !tmpDir.exists()) { | ||
| return; | ||
| } | ||
| File[] files = tmpDir.listFiles(); | ||
| if (files != null) { | ||
| for (File file : files) { | ||
| deleteRecursively(file); | ||
| } | ||
| } | ||
| try { | ||
| Files.delete(tmpDir.toPath()); | ||
| } catch (IOException ignored) { | ||
| // ignore | ||
| } | ||
| } | ||
|
|
||
| private static void deleteRecursively(File file) { | ||
| if (file.isDirectory()) { | ||
| File[] children = file.listFiles(); | ||
| if (children != null) { | ||
| for (File child : children) { | ||
| deleteRecursively(child); | ||
| } | ||
| } | ||
| } | ||
| try { | ||
| Files.delete(file.toPath()); | ||
| } catch (IOException ignored) { | ||
| // ignore | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2]
fileMetadataPos - 1does not locate a device-index node in this fixture. With two devices, the device root is stored inTsFileMetadata; this byte belongs to the last measurement-index node. The passing IT consequently reportsREAD_TIMESERIES_METADATA, matching the broad assertion below, and never exercisesREAD_METADATA_INDEX_NODE/DeviceCollector. Force an on-disk device-index node or locate its offset explicitly, then assert the exact stage/message.