Skip to content

Commit 0f2483f

Browse files
authored
Load: Fixed the issue of TSFile parent directory being null and TSFile resource being updated during the Load process. (apache#16751)
* Load: Fixed the issue of TSFile parent directory being null and TSFile resource being updated during the Load process. * add IT * fix * FIX * update * update
1 parent f708b96 commit 0f2483f

File tree

6 files changed

+105
-11
lines changed

6 files changed

+105
-11
lines changed

integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBLoadTsFileIT.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.iotdb.commons.schema.column.ColumnHeaderConstant;
2424
import org.apache.iotdb.db.it.utils.TestUtils;
2525
import org.apache.iotdb.it.env.EnvFactory;
26+
import org.apache.iotdb.it.env.cluster.node.DataNodeWrapper;
2627
import org.apache.iotdb.it.framework.IoTDBTestRunner;
2728
import org.apache.iotdb.it.utils.TsFileGenerator;
2829
import org.apache.iotdb.itbase.category.ClusterIT;
@@ -67,6 +68,7 @@
6768
import static org.apache.iotdb.db.it.utils.TestUtils.executeNonQuery;
6869
import static org.apache.iotdb.db.it.utils.TestUtils.grantUserSeriesPrivilege;
6970
import static org.apache.iotdb.db.it.utils.TestUtils.grantUserSystemPrivileges;
71+
import static org.apache.iotdb.it.env.cluster.ClusterConstant.USER_DIR;
7072

7173
@RunWith(IoTDBTestRunner.class)
7274
@Category({LocalStandaloneIT.class, ClusterIT.class})
@@ -742,6 +744,47 @@ public void testLoadWithOnNonStandardTsFileName() throws Exception {
742744
}
743745
}
744746

747+
@Test
748+
public void testLoadWithRelativePathName() throws Exception {
749+
DataNodeWrapper dataNodeWrapper = EnvFactory.getEnv().getDataNodeWrapper(0);
750+
751+
registerSchema();
752+
753+
final long writtenPoint1;
754+
// device 0, device 1, sg 0
755+
File relativePathFile = new File(System.getProperty(USER_DIR), "1-0-0-0.tsfile");
756+
try {
757+
try (final TsFileGenerator generator = new TsFileGenerator(relativePathFile)) {
758+
generator.registerTimeseries(
759+
SchemaConfig.DEVICE_0, Collections.singletonList(SchemaConfig.MEASUREMENT_00));
760+
generator.generateData(SchemaConfig.DEVICE_0, 1, PARTITION_INTERVAL / 10_000, false);
761+
writtenPoint1 = generator.getTotalNumber();
762+
}
763+
764+
try (final Connection connection =
765+
EnvFactory.getEnv().getConnectionWithSpecifiedDataNode(dataNodeWrapper);
766+
final Statement statement = connection.createStatement()) {
767+
768+
statement.execute(String.format("load \"%s\" sglevel=2", "1-0-0-0.tsfile"));
769+
770+
try (final ResultSet resultSet =
771+
statement.executeQuery("select count(*) from root.sg.** group by level=1,2")) {
772+
if (resultSet.next()) {
773+
final long sg1Count = resultSet.getLong("count(root.sg.test_0.*.*)");
774+
Assert.assertEquals(writtenPoint1, sg1Count);
775+
} else {
776+
Assert.fail("This ResultSet is empty.");
777+
}
778+
}
779+
}
780+
781+
} finally {
782+
if (relativePathFile.exists()) {
783+
relativePathFile.delete();
784+
}
785+
}
786+
}
787+
745788
@Test
746789
public void testLoadWithMods() throws Exception {
747790
final long writtenPoint1;

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/scheduler/load/LoadTsFileDispatcherImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,22 @@ public void dispatchLocally(FragmentInstance instance) throws FragmentInstanceDi
170170
}
171171
} else if (planNode instanceof LoadSingleTsFileNode) { // do not need to split
172172
final TsFileResource tsFileResource = ((LoadSingleTsFileNode) planNode).getTsFileResource();
173+
final String filePath = tsFileResource.getTsFile().getAbsolutePath();
173174
try {
174175
PipeDataNodeAgent.runtime().assignProgressIndexForTsFileLoad(tsFileResource);
175176
tsFileResource.setGeneratedByPipe(isGeneratedByPipe);
176177
tsFileResource.serialize();
178+
TsFileResource cloneTsFileResource = null;
179+
try {
180+
cloneTsFileResource = tsFileResource.shallowCloneForNative();
181+
} catch (CloneNotSupportedException e) {
182+
cloneTsFileResource = tsFileResource.shallowClone();
183+
}
177184

178185
StorageEngine.getInstance()
179186
.getDataRegion((DataRegionId) groupId)
180187
.loadNewTsFile(
181-
tsFileResource,
188+
cloneTsFileResource,
182189
((LoadSingleTsFileNode) planNode).isDeleteAfterLoad(),
183190
isGeneratedByPipe,
184191
false);
@@ -189,8 +196,7 @@ public void dispatchLocally(FragmentInstance instance) throws FragmentInstanceDi
189196
resultStatus.setMessage(e.getMessage());
190197
throw new FragmentInstanceDispatchException(resultStatus);
191198
} catch (IOException e) {
192-
LOGGER.warn(
193-
"Serialize TsFileResource {} error.", tsFileResource.getTsFile().getAbsolutePath(), e);
199+
LOGGER.warn("Serialize TsFileResource {} error.", filePath, e);
194200
TSStatus resultStatus = new TSStatus();
195201
resultStatus.setCode(TSStatusCode.LOAD_FILE_ERROR.getStatusCode());
196202
resultStatus.setMessage(e.getMessage());

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/crud/LoadTsFileStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class LoadTsFileStatement extends Statement {
7070
private List<Long> writePointCountList;
7171

7272
public LoadTsFileStatement(String filePath) throws FileNotFoundException {
73-
this.file = new File(filePath);
73+
this.file = new File(filePath).getAbsoluteFile();
7474
this.databaseLevel = IoTDBDescriptor.getInstance().getConfig().getDefaultDatabaseLevel();
7575
this.verifySchema = true;
7676
this.deleteAfterLoad = false;

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3467,10 +3467,12 @@ public void loadNewTsFile(
34673467
final boolean isGeneratedByPipe,
34683468
final boolean isFromConsensus)
34693469
throws LoadFileException {
3470-
final File tsfileToBeInserted = newTsFileResource.getTsFile();
3470+
final File tsfileToBeInserted = newTsFileResource.getTsFile().getAbsoluteFile();
34713471
final long newFilePartitionId = newTsFileResource.getTimePartitionWithCheck();
34723472

3473-
if (!TsFileValidator.getInstance().validateTsFile(newTsFileResource)) {
3473+
if (!TsFileValidator.getInstance().validateTsFile(newTsFileResource)
3474+
|| !tsfileToBeInserted.exists()
3475+
|| tsfileToBeInserted.getParentFile() == null) {
34743476
throw new LoadFileException(
34753477
"tsfile validate failed, " + newTsFileResource.getTsFile().getName());
34763478
}

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
import static org.apache.tsfile.common.constant.TsFileConstant.TSFILE_SUFFIX;
9797

9898
@SuppressWarnings("java:S1135") // ignore todos
99-
public class TsFileResource implements PersistentResource {
99+
public class TsFileResource implements PersistentResource, Cloneable {
100100

101101
private static final long INSTANCE_SIZE =
102102
RamUsageEstimator.shallowSizeOfInstance(TsFileResource.class)
@@ -1585,4 +1585,45 @@ public Map<IDeviceID, List<Pair<String, TimeValuePair>>> getLastValues() {
15851585
public void setLastValues(Map<IDeviceID, List<Pair<String, TimeValuePair>>> lastValues) {
15861586
this.lastValues = lastValues;
15871587
}
1588+
1589+
public TsFileResource shallowClone() {
1590+
TsFileResource cloned = new TsFileResource();
1591+
cloned.file = this.file;
1592+
cloned.timeIndex = this.timeIndex;
1593+
cloned.maxPlanIndex = this.maxPlanIndex;
1594+
cloned.minPlanIndex = this.minPlanIndex;
1595+
cloned.exclusiveModFileFuture = this.exclusiveModFileFuture;
1596+
cloned.sharedModFilePathFuture = this.sharedModFilePathFuture;
1597+
cloned.modFileManagement = this.modFileManagement;
1598+
cloned.exclusiveModFile = this.exclusiveModFile;
1599+
cloned.sharedModFile = this.sharedModFile;
1600+
cloned.sharedModFileOffset = this.sharedModFileOffset;
1601+
cloned.compactionModFile = this.compactionModFile;
1602+
cloned.isSeq = this.isSeq;
1603+
cloned.tsFileRepairStatus = this.tsFileRepairStatus;
1604+
cloned.settleTsFileCallBack = this.settleTsFileCallBack;
1605+
cloned.deviceTimeIndexRamSize = this.deviceTimeIndexRamSize;
1606+
cloned.tsFileSize = this.tsFileSize;
1607+
cloned.processor = this.processor;
1608+
cloned.originTsFileResource = this.originTsFileResource;
1609+
cloned.isGeneratedByPipeConsensus = this.isGeneratedByPipeConsensus;
1610+
cloned.isGeneratedByPipe = this.isGeneratedByPipe;
1611+
cloned.insertionCompactionCandidateStatus = this.insertionCompactionCandidateStatus;
1612+
cloned.tierLevel = this.tierLevel;
1613+
cloned.pathToChunkMetadataListMap = this.pathToChunkMetadataListMap;
1614+
cloned.pathToReadOnlyMemChunkMap = this.pathToReadOnlyMemChunkMap;
1615+
cloned.pathToTimeSeriesMetadataMap = this.pathToTimeSeriesMetadataMap;
1616+
cloned.lastValues = this.lastValues;
1617+
cloned.maxProgressIndex.set(this.maxProgressIndex.get());
1618+
cloned.atomicStatus.set(this.atomicStatus.get());
1619+
cloned.isEmpty.set(this.isEmpty.get());
1620+
cloned.tsFileID = this.tsFileID;
1621+
cloned.prev = null;
1622+
cloned.next = null;
1623+
return cloned;
1624+
}
1625+
1626+
public TsFileResource shallowCloneForNative() throws CloneNotSupportedException {
1627+
return (TsFileResource) clone();
1628+
}
15881629
}

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/disk/MinIOSelector.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ public File selectTargetDirectory(
8181
throws DiskSpaceInsufficientException, LoadFileException {
8282
String fileDirRoot = null;
8383
try {
84-
fileDirRoot =
85-
Optional.ofNullable(FileStoreUtils.getFileStore(sourceDirectory.getCanonicalPath()))
86-
.map(Object::toString)
87-
.orElse(null);
84+
if (sourceDirectory != null) {
85+
fileDirRoot =
86+
Optional.ofNullable(FileStoreUtils.getFileStore(sourceDirectory.getCanonicalPath()))
87+
.map(Object::toString)
88+
.orElse(null);
89+
}
8890
} catch (Exception e) {
8991
logger.warn(
9092
"Exception occurs when reading target file's mount point {}",

0 commit comments

Comments
 (0)