This is an automated email from the ASF dual-hosted git repository.
HTHou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 157efaef283 Use provider-compatible file deletion and truncation APIs
(#18349)
157efaef283 is described below
commit 157efaef283450d9187e96caead05c7ed5913d5f
Author: libo <[email protected]>
AuthorDate: Mon Aug 3 16:40:43 2026 +0800
Use provider-compatible file deletion and truncation APIs (#18349)
---
.../ConsensusSubscriptionWalFileAnalyzer.java | 5 ++-
.../org/apache/iotdb/it/env/cluster/EnvUtils.java | 9 +++--
.../iotdb/it/env/cluster/env/AbstractEnv.java | 9 +++--
.../datastructure/SerializableList.java | 28 ++++++---------
.../confignode/persistence/ProcedureInfo.java | 40 ++++++++++------------
.../confignode/persistence/node/NodeInfo.java | 3 +-
.../persistence/partition/PartitionInfo.java | 3 +-
.../persistence/schema/ClusterSchemaInfo.java | 3 +-
.../persistence/schema/TemplatePreSetTable.java | 3 +-
.../persistence/schema/TemplateTable.java | 3 +-
.../confignode/procedure/store/ProcedureWAL.java | 10 ++----
.../writelog/io/SingleFileLogReader.java | 8 ++---
.../consensus/iot/IoTConsensusServerImpl.java | 7 ++--
.../ratis/ApplicationStateMachineProxy.java | 12 +++++--
.../deletion/persist/PageCacheDeletionBuffer.java | 22 +++++++-----
.../consensus/deletion/recover/DeletionReader.java | 7 ++--
.../iotconsensusv2/IoTConsensusV2Receiver.java | 3 +-
...aNodeHardlinkOrCopiedFileDirStartupCleaner.java | 4 +--
.../batch/PipeTabletEventTsFileBatch.java | 3 +-
.../thrift/async/IoTDBDataRegionAsyncSink.java | 5 ++-
.../util/builder/PipeTableModelTsFileBuilder.java | 9 ++---
.../builder/PipeTableModelTsFileBuilderV2.java | 8 +++--
.../util/builder/PipeTreeModelTsFileBuilder.java | 6 ++--
.../util/builder/PipeTreeModelTsFileBuilderV2.java | 5 +--
.../pipe/sink/util/builder/PipeTsFileBuilder.java | 20 ++++++-----
.../plan/analyze/load/LoadTsFileAnalyzer.java | 3 +-
.../schemaregion/logfile/SchemaLogReader.java | 14 ++++----
.../schemaregion/logfile/SchemaLogWriter.java | 28 +++++++++++----
.../mtree/impl/pbtree/schemafile/SchemaFile.java | 21 ++++++++----
.../pbtree/schemafile/log/SchemaFileLogReader.java | 3 +-
.../pbtree/schemafile/pagemgr/PageIOChannel.java | 12 ++++---
.../schemaengine/schemaregion/tag/TagManager.java | 4 +--
.../db/storageengine/dataregion/DataRegion.java | 2 +-
.../dataregion/flush/CompressionRatio.java | 5 +--
.../modification/v1/ModificationFileV1.java | 4 ++-
.../v1/io/LocalTextModificationAccessor.java | 37 ++++++++++++++------
.../dataregion/tsfile/TsFileResource.java | 2 +-
.../FileTimeIndexCacheWriter.java | 30 +++++++++++-----
.../storageengine/dataregion/wal/io/LogWriter.java | 12 ++++---
.../storageengine/dataregion/wal/node/WALNode.java | 2 +-
.../file/AbstractTsFileRecoverPerformer.java | 2 +-
...leStatementDataTypeConvertExecutionVisitor.java | 12 ++++---
...eeStatementDataTypeConvertExecutionVisitor.java | 12 ++++---
.../rescon/disk/DirectoryChecker.java | 36 +++++++++++++------
.../ConsensusSubscriptionCommitManager.java | 17 +++++----
.../batch/SubscriptionPipeTsFileEventBatch.java | 3 +-
.../org/apache/iotdb/db/utils/ObjectWriter.java | 31 +++++++++++++----
.../apache/iotdb/db/utils/writelog/LogWriter.java | 25 +++++++++-----
.../commons/auth/role/LocalFileRoleAccessor.java | 5 ++-
.../commons/auth/user/LocalFileUserAccessor.java | 3 +-
.../commons/executable/ExecutableManager.java | 14 ++++----
.../commons/file/SystemPropertiesHandler.java | 8 ++---
.../queue/serializer/PlainQueueSerializer.java | 32 ++++++++---------
.../commons/pipe/receiver/IoTDBFileReceiver.java | 7 ++--
.../org/apache/iotdb/commons/utils/FileUtils.java | 17 ++++++++-
.../org/apache/iotdb/commons/utils/IOUtils.java | 2 +-
.../apache/iotdb/commons/utils/FileUtilsTest.java | 10 ++++++
pom.xml | 28 +++++++++++++++
src/main/forbidden-apis/secure-erase | 23 +++++++++++++
59 files changed, 448 insertions(+), 253 deletions(-)
diff --git
a/example/session/src/main/java/org/apache/iotdb/ConsensusSubscriptionWalFileAnalyzer.java
b/example/session/src/main/java/org/apache/iotdb/ConsensusSubscriptionWalFileAnalyzer.java
index 0b8c165f9fd..ee8bf55f588 100644
---
a/example/session/src/main/java/org/apache/iotdb/ConsensusSubscriptionWalFileAnalyzer.java
+++
b/example/session/src/main/java/org/apache/iotdb/ConsensusSubscriptionWalFileAnalyzer.java
@@ -23,10 +23,10 @@ import
org.apache.tsfile.file.metadata.enums.CompressionType;
import java.io.File;
import java.io.IOException;
-import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
+import java.nio.file.StandardOpenOption;
import java.util.Locale;
/**
@@ -79,8 +79,7 @@ public class ConsensusSubscriptionWalFileAnalyzer {
}
private static WalFileAnalysis analyze(final File walFile) throws
IOException {
- try (RandomAccessFile raf = new RandomAccessFile(walFile, "r");
- FileChannel channel = raf.getChannel()) {
+ try (FileChannel channel = FileChannel.open(walFile.toPath(),
StandardOpenOption.READ)) {
final long totalBytes = channel.size();
final String version = detectVersion(channel, totalBytes);
final int headMagicBytes = getHeadMagicBytes(version);
diff --git
a/integration-test/src/main/java/org/apache/iotdb/it/env/cluster/EnvUtils.java
b/integration-test/src/main/java/org/apache/iotdb/it/env/cluster/EnvUtils.java
index 53d7140a6f4..63c2041199d 100644
---
a/integration-test/src/main/java/org/apache/iotdb/it/env/cluster/EnvUtils.java
+++
b/integration-test/src/main/java/org/apache/iotdb/it/env/cluster/EnvUtils.java
@@ -25,6 +25,7 @@ import org.apache.tsfile.external.commons.lang3.SystemUtils;
import org.apache.tsfile.utils.Pair;
import java.io.*;
+import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -92,8 +93,12 @@ public class EnvUtils {
// ignore
}
// Delete the lock file if the ports can't be used or some error happens
- if (lockFile.exists() && !lockFile.delete()) {
- IoTDBTestLogger.logger.error("Delete lockfile {} failed",
lockFilePath);
+ try {
+ if (lockFile.exists() && !Files.deleteIfExists(lockFile.toPath())) {
+ IoTDBTestLogger.logger.error("Delete lockfile {} failed",
lockFilePath);
+ }
+ } catch (IOException e) {
+ IoTDBTestLogger.logger.error("Delete lockfile {} failed",
lockFilePath, e);
}
}
}
diff --git
a/integration-test/src/main/java/org/apache/iotdb/it/env/cluster/env/AbstractEnv.java
b/integration-test/src/main/java/org/apache/iotdb/it/env/cluster/env/AbstractEnv.java
index 3819bd32a8d..af673a402d2 100644
---
a/integration-test/src/main/java/org/apache/iotdb/it/env/cluster/env/AbstractEnv.java
+++
b/integration-test/src/main/java/org/apache/iotdb/it/env/cluster/env/AbstractEnv.java
@@ -78,6 +78,7 @@ import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
@@ -680,8 +681,12 @@ public abstract class AbstractEnv implements BaseEnv {
nodeWrapper.stopForcibly();
nodeWrapper.destroyDir();
final String lockPath = EnvUtils.getLockFilePath(nodeWrapper.getPort());
- if (!new File(lockPath).delete()) {
- logger.error("Delete lock file {} failed", lockPath);
+ try {
+ if (!Files.deleteIfExists(new File(lockPath).toPath())) {
+ logger.error("Delete lock file {} failed", lockPath);
+ }
+ } catch (IOException e) {
+ logger.error("Delete lock file {} failed", lockPath, e);
}
}
if (clientManager != null) {
diff --git
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/datastructure/SerializableList.java
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/datastructure/SerializableList.java
index fb1b86ad8bb..8365de016f7 100644
---
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/datastructure/SerializableList.java
+++
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/datastructure/SerializableList.java
@@ -27,9 +27,9 @@ import org.apache.iotdb.commons.utils.IOUtils;
import org.apache.tsfile.utils.PublicBAOS;
import java.io.IOException;
-import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
+import java.nio.file.StandardOpenOption;
public interface SerializableList {
@@ -87,7 +87,6 @@ public interface SerializableList {
protected int serializedElementSize;
protected String fileName;
- protected RandomAccessFile file;
protected FileChannel fileChannel;
public SerializationRecorder(String queryId) {
@@ -127,28 +126,21 @@ public interface SerializableList {
return serializedElementSize;
}
- public RandomAccessFile getFile() throws IOException {
- if (file == null) {
- if (fileName == null) {
- fileName =
AbstractTemporaryQueryDataFileService.getInstance().register(this);
- }
- file = new
RandomAccessFile(SystemFileFactory.INSTANCE.getFile(fileName), "rw");
- }
- return file;
- }
-
public void closeFile() throws IOException {
- if (file == null) {
- return;
- }
closeFileChannel();
- file.close();
- file = null;
}
public FileChannel getFileChannel() throws IOException {
if (fileChannel == null) {
- fileChannel = getFile().getChannel();
+ if (fileName == null) {
+ fileName =
AbstractTemporaryQueryDataFileService.getInstance().register(this);
+ }
+ fileChannel =
+ FileChannel.open(
+ SystemFileFactory.INSTANCE.getFile(fileName).toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.READ,
+ StandardOpenOption.WRITE);
}
return fileChannel;
}
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/ProcedureInfo.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/ProcedureInfo.java
index f8e20e360ce..7409258b8c9 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/ProcedureInfo.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/ProcedureInfo.java
@@ -163,31 +163,29 @@ public class ProcedureInfo implements SnapshotProcessor {
}
private static Optional<Procedure> loadProcedure(Path procedureFilePath) {
- try (FileInputStream fis = new
FileInputStream(procedureFilePath.toFile())) {
+ try (FileChannel channel = FileChannel.open(procedureFilePath)) {
Procedure procedure = null;
- try (FileChannel channel = fis.getChannel()) {
- final long fileSize = channel.size();
- if (fileSize > PROCEDURE_LOAD_BUFFER_SIZE) {
- throw new IOException(
- String.format(
- ConfigNodeMessages
-
.EXCEPTION_PROCEDURE_FILE_ARG_EXCEEDS_THE_LOAD_BUFFER_LIMIT_ARG_ACTUAL_SIZE_ARG_62375B4C,
- procedureFilePath,
- PROCEDURE_LOAD_BUFFER_SIZE,
- fileSize));
- }
- ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileSize);
- if (fileSize > 0) {
- IOUtils.readFully(channel, byteBuffer);
- byteBuffer.flip();
- procedure = ProcedureFactory.getInstance().create(byteBuffer);
- byteBuffer.clear();
- }
- return Optional.ofNullable(procedure);
+ final long fileSize = channel.size();
+ if (fileSize > PROCEDURE_LOAD_BUFFER_SIZE) {
+ throw new IOException(
+ String.format(
+ ConfigNodeMessages
+
.EXCEPTION_PROCEDURE_FILE_ARG_EXCEEDS_THE_LOAD_BUFFER_LIMIT_ARG_ACTUAL_SIZE_ARG_62375B4C,
+ procedureFilePath,
+ PROCEDURE_LOAD_BUFFER_SIZE,
+ fileSize));
}
+ ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileSize);
+ if (fileSize > 0) {
+ IOUtils.readFully(channel, byteBuffer);
+ byteBuffer.flip();
+ procedure = ProcedureFactory.getInstance().create(byteBuffer);
+ byteBuffer.clear();
+ }
+ return Optional.ofNullable(procedure);
} catch (Exception e) {
LOGGER.error(ConfigNodeMessages.LOAD_FAILED_IT_WILL_BE_DELETED,
procedureFilePath, e);
- if (!procedureFilePath.toFile().delete()) {
+ if (!FileUtils.deleteFileIfExist(procedureFilePath.toFile())) {
LOGGER.error(
ConfigNodeMessages.DELETED_FAILED_TAKE_APPROPRIATE_ACTION,
procedureFilePath, e);
}
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/node/NodeInfo.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/node/NodeInfo.java
index e2d3ed3e162..d98a05c8a8c 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/node/NodeInfo.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/node/NodeInfo.java
@@ -676,7 +676,8 @@ public class NodeInfo implements SnapshotProcessor {
dataNodeInfoReadWriteLock.readLock().unlock();
configNodeInfoReadWriteLock.readLock().unlock();
for (int retry = 0; retry < 5; retry++) {
- if (!tmpFile.exists() || tmpFile.delete()) {
+ if (!tmpFile.exists()
+ ||
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tmpFile)) {
break;
} else {
LOGGER.warn(
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/partition/PartitionInfo.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/partition/PartitionInfo.java
index e71b61d7b33..da2466c1e24 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/partition/PartitionInfo.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/partition/PartitionInfo.java
@@ -1042,7 +1042,8 @@ public class PartitionInfo implements SnapshotProcessor {
} finally {
// with or without success, delete temporary files anyway
for (int retry = 0; retry < 5; retry++) {
- if (!tmpFile.exists() || tmpFile.delete()) {
+ if (!tmpFile.exists()
+ ||
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tmpFile)) {
break;
} else {
LOGGER.warn(
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java
index 9c599da5840..6553fa6566e 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java
@@ -781,7 +781,8 @@ public class ClusterSchemaInfo implements SnapshotProcessor
{
return tmpFile.renameTo(snapshotFile);
} finally {
for (int retry = 0; retry < 5; retry++) {
- if (!tmpFile.exists() || tmpFile.delete()) {
+ if (!tmpFile.exists()
+ ||
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tmpFile)) {
break;
} else {
LOGGER.warn(
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TemplatePreSetTable.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TemplatePreSetTable.java
index 985e005e967..3ebfc5f2c51 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TemplatePreSetTable.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TemplatePreSetTable.java
@@ -130,7 +130,8 @@ public class TemplatePreSetTable {
return tmpFile.renameTo(snapshotFile);
} finally {
for (int retry = 0; retry < 5; retry++) {
- if (!tmpFile.exists() || tmpFile.delete()) {
+ if (!tmpFile.exists()
+ ||
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tmpFile)) {
break;
} else {
LOGGER.warn(
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TemplateTable.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TemplateTable.java
index 29878b47dfe..45b8264c40f 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TemplateTable.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TemplateTable.java
@@ -237,7 +237,8 @@ public class TemplateTable {
return tmpFile.renameTo(snapshotFile);
} finally {
for (int retry = 0; retry < 5; retry++) {
- if (!tmpFile.exists() || tmpFile.delete()) {
+ if (!tmpFile.exists()
+ ||
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tmpFile)) {
break;
} else {
LOGGER.warn(
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureWAL.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureWAL.java
index d6ff7e4891d..25053529146 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureWAL.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureWAL.java
@@ -27,8 +27,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.DataOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
@@ -56,19 +54,17 @@ public class ProcedureWAL {
*/
@TestOnly
public void save(Procedure procedure) throws IOException {
- File walTmp = new File(walFilePath + ".tmp");
- Path walTmpPath = walTmp.toPath();
+ Path walTmpPath = Path.of(walFilePath + ".tmp");
Files.deleteIfExists(walTmpPath);
Files.createFile(walTmpPath);
- try (FileOutputStream fos = new FileOutputStream(walTmp);
- FileChannel channel = fos.getChannel();
+ try (FileChannel channel =
+ FileChannel.open(walTmpPath,
java.nio.file.StandardOpenOption.WRITE);
PublicBAOS publicBAOS = new PublicBAOS();
DataOutputStream dataOutputStream = new DataOutputStream(publicBAOS)) {
procedure.serialize(dataOutputStream);
channel.write(ByteBuffer.wrap(publicBAOS.getBuf(), 0,
publicBAOS.size()));
channel.force(true);
- fos.getFD().sync();
}
Files.deleteIfExists(walFilePath);
Files.move(walTmpPath, walFilePath);
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/writelog/io/SingleFileLogReader.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/writelog/io/SingleFileLogReader.java
index d54b48f75d6..6681d213704 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/writelog/io/SingleFileLogReader.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/writelog/io/SingleFileLogReader.java
@@ -18,6 +18,7 @@
*/
package org.apache.iotdb.confignode.writelog.io;
+import org.apache.iotdb.commons.utils.FileUtils;
import org.apache.iotdb.confignode.consensus.request.ConfigPhysicalPlan;
import org.apache.iotdb.confignode.i18n.ConfigNodeMessages;
@@ -29,10 +30,8 @@ import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
import java.util.NoSuchElementException;
import java.util.zip.CRC32;
@@ -147,9 +146,8 @@ public class SingleFileLogReader implements ILogReader {
}
private void truncateBrokenLogs() {
- try (FileOutputStream outputStream = new FileOutputStream(filepath, true);
- FileChannel channel = outputStream.getChannel()) {
- channel.truncate(unbrokenLogsSize);
+ try {
+ FileUtils.truncateFile(new File(filepath), unbrokenLogsSize);
} catch (IOException e) {
logger.error(ConfigNodeMessages.FAIL_TO_TRUNCATE_LOG_FILE_TO_SIZE,
unbrokenLogsSize, e);
}
diff --git
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerImpl.java
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerImpl.java
index b0bf44decb6..66d917bebc2 100644
---
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerImpl.java
+++
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerImpl.java
@@ -85,13 +85,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -530,8 +530,9 @@ public class IoTConsensusServerImpl {
if (!Files.exists(parentDir)) {
Files.createDirectories(parentDir);
}
- try (FileOutputStream fos = new
FileOutputStream(targetFile.getAbsolutePath(), true);
- FileChannel channel = fos.getChannel()) {
+ try (FileChannel channel =
+ FileChannel.open(
+ targetFile.toPath(), StandardOpenOption.CREATE,
StandardOpenOption.WRITE)) {
channel.write(fileChunk.slice(), fileOffset);
}
}
diff --git
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
index 6c0366c546f..67b1fdf7da9 100644
---
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
+++
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
@@ -276,10 +276,16 @@ public class ApplicationStateMachineProxy extends
BaseStateMachine {
private void deleteIncompleteSnapshot(File snapshotDir) throws IOException {
// this takeSnapshot failed, clean up files and directories
// statemachine is supposed to clear snapshotDir on failure
- boolean isEmpty = snapshotDir.delete();
- if (!isEmpty) {
+ try {
+ Files.deleteIfExists(snapshotDir.toPath());
+ } catch (IOException deleteException) {
logger.info(RatisMessages.SNAPSHOT_DIR_INCOMPLETE_DELETING,
snapshotDir.getAbsolutePath());
- FileUtils.deleteFully(snapshotDir);
+ try {
+ FileUtils.deleteFully(snapshotDir);
+ } catch (IOException cleanupException) {
+ deleteException.addSuppressed(cleanupException);
+ throw deleteException;
+ }
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/persist/PageCacheDeletionBuffer.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/persist/PageCacheDeletionBuffer.java
index 8b7946998ac..d58c65158ac 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/persist/PageCacheDeletionBuffer.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/persist/PageCacheDeletionBuffer.java
@@ -36,11 +36,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
+import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.BlockingQueue;
@@ -93,7 +93,6 @@ public class PageCacheDeletionBuffer implements
DeletionBuffer {
private volatile ByteBuffer serializeBuffer;
// Current Logging file.
private volatile File logFile;
- private volatile FileOutputStream logStream;
private volatile FileChannel logChannel;
// Max progressIndex among current .deletion file. Used by PersistTask for
naming .deletion file.
// Since deletions are written serially, DAL is also written serially. This
ensures that the
@@ -121,8 +120,12 @@ public class PageCacheDeletionBuffer implements
DeletionBuffer {
new File(
baseDirectory,
String.format("_%d-%d%s", 0, 0,
DeletionResourceManager.DELETION_FILE_SUFFIX));
- this.logStream = new FileOutputStream(logFile, true);
- this.logChannel = logStream.getChannel();
+ this.logChannel =
+ FileChannel.open(
+ logFile.toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.APPEND);
// Create file && write magic string
if (!logFile.exists() || logFile.length() == 0) {
this.logChannel.write(
@@ -186,9 +189,6 @@ public class PageCacheDeletionBuffer implements
DeletionBuffer {
private void closeCurrentLoggingFile(boolean notifySuccess) throws
IOException {
LOGGER.info(DataNodePipeMessages.DELETION_PERSIST_CURRENT_FILE_HAS_BEEN_CLOSED,
dataRegionId);
// Close old resource to fsync.
- if (this.logStream != null) {
- this.logStream.close();
- }
if (this.logChannel != null) {
this.logChannel.close();
}
@@ -243,8 +243,12 @@ public class PageCacheDeletionBuffer implements
DeletionBuffer {
progressIndex.getRebootTimes(),
progressIndex.getMemTableFlushOrderId(),
DeletionResourceManager.DELETION_FILE_SUFFIX));
- this.logStream = new FileOutputStream(logFile, true);
- this.logChannel = logStream.getChannel();
+ this.logChannel =
+ FileChannel.open(
+ logFile.toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.APPEND);
// Create file && write magic string
if (!logFile.exists() || logFile.length() == 0) {
this.logChannel.write(
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/recover/DeletionReader.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/recover/DeletionReader.java
index 8477e1e8059..b11805471a8 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/recover/DeletionReader.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/recover/DeletionReader.java
@@ -29,11 +29,11 @@ import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
+import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
@@ -45,15 +45,13 @@ public class DeletionReader implements Closeable {
private final int regionId;
private final Consumer<DeletionResource> removeHook;
private final File logFile;
- private final FileInputStream fileInputStream;
private final FileChannel fileChannel;
public DeletionReader(File logFile, int regionId, Consumer<DeletionResource>
removeHook)
throws IOException {
this.logFile = logFile;
this.regionId = regionId;
- this.fileInputStream = new FileInputStream(logFile);
- this.fileChannel = fileInputStream.getChannel();
+ this.fileChannel = FileChannel.open(logFile.toPath(),
StandardOpenOption.READ);
this.removeHook = removeHook;
}
@@ -95,6 +93,5 @@ public class DeletionReader implements Closeable {
@Override
public void close() throws IOException {
this.fileChannel.close();
- this.fileInputStream.close();
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/iotconsensusv2/IoTConsensusV2Receiver.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/iotconsensusv2/IoTConsensusV2Receiver.java
index e185a424947..9303e4c01f4 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/iotconsensusv2/IoTConsensusV2Receiver.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/iotconsensusv2/IoTConsensusV2Receiver.java
@@ -365,7 +365,8 @@ public class IoTConsensusV2Receiver {
// filename. However, for other files (mod, snapshot, etc.) the
content varies for the
// same name in different times, then we must rewrite the file to
apply the newest
// version.
- writingFileWriter.setLength(0);
+ org.apache.iotdb.commons.utils.FileUtils.truncateFile(writingFile,
0);
+ writingFileWriter.seek(0);
}
final TSStatus status =
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/PipeDataNodeHardlinkOrCopiedFileDirStartupCleaner.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/PipeDataNodeHardlinkOrCopiedFileDirStartupCleaner.java
index db60fc4645b..8dfc4c1a8e6 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/PipeDataNodeHardlinkOrCopiedFileDirStartupCleaner.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/PipeDataNodeHardlinkOrCopiedFileDirStartupCleaner.java
@@ -24,7 +24,6 @@ import
org.apache.iotdb.commons.pipe.resource.snapshot.PipeSnapshotResourceManag
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.i18n.DataNodePipeMessages;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -114,10 +113,11 @@ public class
PipeDataNodeHardlinkOrCopiedFileDirStartupCleaner {
} catch (final IOException e) {
LOGGER.warn(
DataNodePipeMessages.PIPE_HARDLINK_DIR_MOVE_FAILED_DELETING_SYNC,
pipeHardLinkDir, e);
+
org.apache.iotdb.commons.utils.FileUtils.deleteFileOrDirectory(pipeHardLinkDir,
true);
LOGGER.info(
DataNodePipeMessages.PIPE_HARDLINK_DIR_FOUND_DELETING_IT_RESULT,
pipeHardLinkDir,
- FileUtils.deleteQuietly(pipeHardLinkDir));
+ !pipeHardLinkDir.exists());
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/payload/evolvable/batch/PipeTabletEventTsFileBatch.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/payload/evolvable/batch/PipeTabletEventTsFileBatch.java
index 59493ef3327..a0046e82e3e 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/payload/evolvable/batch/PipeTabletEventTsFileBatch.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/payload/evolvable/batch/PipeTabletEventTsFileBatch.java
@@ -31,7 +31,6 @@ import
org.apache.iotdb.db.pipe.sink.util.sorter.PipeTreeModelTabletEventSorter;
import org.apache.iotdb.pipe.api.event.dml.insertion.TabletInsertionEvent;
import org.apache.tsfile.exception.write.WriteProcessException;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.utils.Pair;
import org.apache.tsfile.write.record.Tablet;
import org.slf4j.Logger;
@@ -251,7 +250,7 @@ public class PipeTabletEventTsFileBatch extends
PipeTabletEventBatch {
} finally {
if (!sealedSuccessfully) {
for (final Pair<String, File> sealedFile : list) {
- if (sealedFile.right.exists() &&
!FileUtils.deleteQuietly(sealedFile.right)) {
+ if
(!org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(sealedFile.right))
{
LOGGER.warn(DataNodePipeMessages.FAILED_TO_DELETE_BATCH_FILE_THIS_FILE,
sealedFile);
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/protocol/thrift/async/IoTDBDataRegionAsyncSink.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/protocol/thrift/async/IoTDBDataRegionAsyncSink.java
index 480ade16546..689a5bb1b7b 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/protocol/thrift/async/IoTDBDataRegionAsyncSink.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/protocol/thrift/async/IoTDBDataRegionAsyncSink.java
@@ -72,7 +72,6 @@ import org.apache.iotdb.service.rpc.thrift.TPipeTransferReq;
import com.google.common.collect.ImmutableSet;
import org.apache.tsfile.exception.write.WriteProcessException;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -302,8 +301,8 @@ public class IoTDBDataRegionAsyncSink extends IoTDBSink
implements PipeSinkWithS
} catch (final Exception e) {
for (int i = transferredFileCount; i < dbTsFilePairs.size(); i++) {
final Pair<String, File> untransferredFile = dbTsFilePairs.get(i);
- if (untransferredFile.right.exists()
- && !FileUtils.deleteQuietly(untransferredFile.right)) {
+ if (!org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(
+ untransferredFile.right)) {
LOGGER.warn(
DataNodePipeMessages.FAILED_TO_DELETE_BATCH_FILE_THIS_FILE,
untransferredFile);
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTableModelTsFileBuilder.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTableModelTsFileBuilder.java
index ce7e81e3481..09499155a25 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTableModelTsFileBuilder.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTableModelTsFileBuilder.java
@@ -25,7 +25,6 @@ import org.apache.iotdb.pipe.api.exception.PipeException;
import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.exception.write.WriteProcessException;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.file.metadata.IDeviceID;
import org.apache.tsfile.file.metadata.TableSchema;
import org.apache.tsfile.utils.BitMap;
@@ -89,7 +88,8 @@ public class PipeTableModelTsFileBuilder extends
PipeTsFileBuilder {
}
return pairList;
} catch (final IOException | RuntimeException e) {
- pairList.forEach(pair -> FileUtils.deleteQuietly(pair.right));
+ pairList.forEach(
+ pair ->
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(pair.right));
throw e;
}
}
@@ -159,7 +159,7 @@ public class PipeTableModelTsFileBuilder extends
PipeTsFileBuilder {
try {
fileWriter = new TsFileWriter(file);
} catch (final IOException | RuntimeException e) {
- FileUtils.deleteQuietly(file);
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(file);
throw e;
}
}
@@ -189,7 +189,8 @@ public class PipeTableModelTsFileBuilder extends
PipeTsFileBuilder {
}
for (final Pair<String, File> sealedFile : sealedFiles) {
- final boolean deleteSuccess =
FileUtils.deleteQuietly(sealedFile.right);
+ final boolean deleteSuccess =
+
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(sealedFile.right);
LOGGER.warn(
DataNodePipeMessages.BATCH_ID_DELETE_THE_TSFILE_AFTER_FAILED,
currentBatchId.get(),
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTableModelTsFileBuilderV2.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTableModelTsFileBuilderV2.java
index eb20a33e147..8163d79ad51 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTableModelTsFileBuilderV2.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTableModelTsFileBuilderV2.java
@@ -32,7 +32,6 @@ import
org.apache.iotdb.db.storageengine.dataregion.memtable.PrimitiveMemTable;
import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.exception.write.WriteProcessException;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.file.metadata.TableSchema;
import org.apache.tsfile.utils.BitMap;
import org.apache.tsfile.utils.DateUtils;
@@ -101,7 +100,8 @@ public class PipeTableModelTsFileBuilderV2 extends
PipeTsFileBuilder {
}
return pairList;
} catch (final Exception e) {
- pairList.forEach(pair -> FileUtils.deleteQuietly(pair.right));
+ pairList.forEach(
+ pair ->
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(pair.right));
LOGGER.warn(
DataNodePipeMessages
.EXCEPTION_OCCURRED_WHEN_PIPETABLEMODELTSFILEBUILDERV2_WRITING_TABLETS_TO,
@@ -142,7 +142,9 @@ public class PipeTableModelTsFileBuilderV2 extends
PipeTsFileBuilder {
sealedFiles.add(new Pair<>(dataBase, writer.getFile()));
}
} catch (final Exception e) {
- FileUtils.deleteQuietly(file);
+ if (file != null) {
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(file);
+ }
LOGGER.warn(
DataNodePipeMessages.BATCH_ID_FAILED_TO_WRITE_TABLETS_INTO,
currentBatchId.get(),
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTreeModelTsFileBuilder.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTreeModelTsFileBuilder.java
index 7d6967db7ec..b68dc4315f6 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTreeModelTsFileBuilder.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTreeModelTsFileBuilder.java
@@ -23,7 +23,6 @@ import org.apache.iotdb.db.i18n.DataNodePipeMessages;
import org.apache.iotdb.db.pipe.event.common.tablet.PipeTabletUtils;
import org.apache.tsfile.exception.write.WriteProcessException;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.file.metadata.IDeviceID;
import org.apache.tsfile.read.common.Path;
import org.apache.tsfile.utils.BitMap;
@@ -151,7 +150,7 @@ public class PipeTreeModelTsFileBuilder extends
PipeTsFileBuilder {
try {
fileWriter = new TsFileWriter(file);
} catch (final IOException | RuntimeException e) {
- FileUtils.deleteQuietly(file);
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(file);
throw e;
}
}
@@ -180,7 +179,8 @@ public class PipeTreeModelTsFileBuilder extends
PipeTsFileBuilder {
}
for (final Pair<String, File> sealedFile : sealedFiles) {
- final boolean deleteSuccess =
FileUtils.deleteQuietly(sealedFile.right);
+ final boolean deleteSuccess =
+
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(sealedFile.right);
LOGGER.warn(
DataNodePipeMessages.BATCH_ID_DELETE_THE_TSFILE_AFTER_FAILED,
currentBatchId.get(),
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTreeModelTsFileBuilderV2.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTreeModelTsFileBuilderV2.java
index 4aedf542ea4..a325f2cbdfb 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTreeModelTsFileBuilderV2.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTreeModelTsFileBuilderV2.java
@@ -30,7 +30,6 @@ import
org.apache.iotdb.db.storageengine.dataregion.memtable.PrimitiveMemTable;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.exception.write.WriteProcessException;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.utils.BitMap;
import org.apache.tsfile.utils.DateUtils;
import org.apache.tsfile.utils.Pair;
@@ -129,7 +128,9 @@ public class PipeTreeModelTsFileBuilderV2 extends
PipeTsFileBuilder {
sealedFiles.add(new Pair<>(null, writer.getFile()));
}
} catch (final Exception e) {
- FileUtils.deleteQuietly(file);
+ if (file != null) {
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(file);
+ }
LOGGER.warn(
DataNodePipeMessages.BATCH_ID_FAILED_TO_WRITE_TABLETS_INTO,
currentBatchId.get(),
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTsFileBuilder.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTsFileBuilder.java
index c34f1fa96b4..414d2fad231 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTsFileBuilder.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/builder/PipeTsFileBuilder.java
@@ -28,7 +28,6 @@ import org.apache.iotdb.pipe.api.exception.PipeException;
import org.apache.tsfile.common.constant.TsFileConstant;
import org.apache.tsfile.exception.write.WriteProcessException;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.utils.Pair;
import org.apache.tsfile.write.TsFileWriter;
import org.apache.tsfile.write.record.Tablet;
@@ -37,6 +36,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@@ -91,18 +91,22 @@ public abstract class PipeTsFileBuilder {
.getNextWithRetry(
folder -> {
File dir = new File(folder,
Long.toString(currentBatchId.get()));
- FileUtils.deleteQuietly(dir);
- if (dir.mkdirs()) {
+
org.apache.iotdb.commons.utils.FileUtils.deleteFileOrDirectory(dir, true);
+ try {
+ Files.createDirectories(dir.toPath().getParent());
+ Files.createDirectory(dir.toPath());
LOGGER.info(
DataNodePipeMessages.BATCH_ID_CREATE_BATCH_DIR_SUCCESSFULLY_BATCH,
currentBatchId.get(),
dir.getPath());
return dir;
+ } catch (final IOException e) {
+ LOGGER.warn(
+
DataNodePipeMessages.BATCH_ID_FAILED_TO_CREATE_BATCH_FILE,
+ currentBatchId.get(),
+ dir.getPath(),
+ e);
}
- LOGGER.warn(
-
DataNodePipeMessages.BATCH_ID_FAILED_TO_CREATE_BATCH_FILE,
- currentBatchId.get(),
- dir.getPath());
return null;
});
if (baseDir != null) {
@@ -143,7 +147,7 @@ public abstract class PipeTsFileBuilder {
}
try {
- FileUtils.delete(fileWriter.getIOWriter().getFile());
+ Files.deleteIfExists(fileWriter.getIOWriter().getFile().toPath());
} catch (final Exception e) {
LOGGER.info(
DataNodePipeMessages.BATCH_ID_FAILED_TO_DELETE_THE_TSFILE,
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzer.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzer.java
index 68b23ae5d00..3cf619d7130 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzer.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzer.java
@@ -55,7 +55,6 @@ import org.apache.iotdb.rpc.TSStatusCode;
import org.apache.tsfile.common.conf.TSFileDescriptor;
import org.apache.tsfile.encrypt.EncryptParameter;
import org.apache.tsfile.encrypt.EncryptUtils;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.file.metadata.IDeviceID;
import org.apache.tsfile.file.metadata.TableSchema;
import org.apache.tsfile.file.metadata.TimeseriesMetadata;
@@ -489,7 +488,7 @@ public class LoadTsFileAnalyzer implements AutoCloseable {
DataNodeQueryMessages.EMPTY_FILE_DETECTED_WILL_SKIP_LOADING_THIS_FILE,
tsFile.getAbsolutePath());
if (isDeleteAfterLoad) {
- FileUtils.deleteQuietly(tsFile);
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tsFile);
}
} finally {
// reset the session info to the original one
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/SchemaLogReader.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/SchemaLogReader.java
index ae7ae7d0adf..b3b26e3ac31 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/SchemaLogReader.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/SchemaLogReader.java
@@ -20,6 +20,7 @@
package org.apache.iotdb.db.schemaengine.schemaregion.logfile;
import org.apache.iotdb.commons.file.SystemFileFactory;
+import org.apache.iotdb.commons.utils.FileUtils;
import org.apache.iotdb.db.i18n.DataNodeSchemaMessages;
import org.apache.tsfile.utils.ReadWriteIOUtils;
@@ -32,10 +33,8 @@ import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.nio.channels.FileChannel;
import java.util.NoSuchElementException;
/**
@@ -143,16 +142,15 @@ public class SchemaLogReader<T> implements AutoCloseable {
}
private void truncateBrokenLogs() {
- try (FileOutputStream outputStream = new FileOutputStream(logFile, true);
- FileChannel channel = outputStream.getChannel()) {
- if (currentIndex != channel.size()) {
+ try {
+ final long fileSize = logFile.length();
+ if (currentIndex != fileSize) {
LOGGER.warn(
DataNodeSchemaMessages.LOG_FILE_END_CORRUPTED_TRUNCATE,
logFile.getName(),
currentIndex,
- channel.size());
- channel.truncate(currentIndex);
- channel.force(true);
+ fileSize);
+ FileUtils.truncateFile(logFile, currentIndex);
}
isFileCorrupted = false;
} catch (IOException e) {
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/SchemaLogWriter.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/SchemaLogWriter.java
index f4cc8fdd766..0e63a4d38e5 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/SchemaLogWriter.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/SchemaLogWriter.java
@@ -26,9 +26,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
/**
* This class provides the common ability to write a log storing T.
@@ -41,7 +44,8 @@ public class SchemaLogWriter<T> implements AutoCloseable {
private final File logFile;
- private final FileOutputStream fileOutputStream;
+ private final FileChannel fileChannel;
+ private final OutputStream fileOutputStream;
private final ISerializer<T> serializer;
@@ -62,7 +66,8 @@ public class SchemaLogWriter<T> implements AutoCloseable {
}
logFile = SystemFileFactory.INSTANCE.getFile(schemaDir + File.separator +
logFileName);
- fileOutputStream = new FileOutputStream(logFile, true);
+ fileChannel = openFileChannel(logFile);
+ fileOutputStream = Channels.newOutputStream(fileChannel);
this.serializer = serializer;
this.forceEachWrite = forceEachWrite;
@@ -71,7 +76,8 @@ public class SchemaLogWriter<T> implements AutoCloseable {
public SchemaLogWriter(String logFilePath, ISerializer<T> serializer,
boolean forceEachWrite)
throws IOException {
logFile = SystemFileFactory.INSTANCE.getFile(logFilePath);
- fileOutputStream = new FileOutputStream(logFile, true);
+ fileChannel = openFileChannel(logFile);
+ fileOutputStream = Channels.newOutputStream(fileChannel);
this.serializer = serializer;
this.forceEachWrite = forceEachWrite;
@@ -91,11 +97,11 @@ public class SchemaLogWriter<T> implements AutoCloseable {
return;
}
hasSynced = true;
- fileOutputStream.getFD().sync();
+ fileChannel.force(true);
}
private void syncBufferToDisk() throws IOException {
- fileOutputStream.getFD().sync();
+ fileChannel.force(true);
hasSynced = true;
}
@@ -113,6 +119,14 @@ public class SchemaLogWriter<T> implements AutoCloseable {
}
public long position() throws IOException {
- return fileOutputStream.getChannel().position();
+ return fileChannel.position();
+ }
+
+ private static FileChannel openFileChannel(File file) throws IOException {
+ return FileChannel.open(
+ file.toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.APPEND);
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/SchemaFile.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/SchemaFile.java
index 665bafb956f..968a4792e9a 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/SchemaFile.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/SchemaFile.java
@@ -23,6 +23,7 @@ import org.apache.iotdb.commons.file.SystemFileFactory;
import org.apache.iotdb.commons.schema.SchemaConstant;
import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode;
import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory;
+import org.apache.iotdb.commons.utils.FileUtils;
import org.apache.iotdb.commons.utils.IOUtils;
import org.apache.iotdb.commons.utils.PathUtils;
import org.apache.iotdb.commons.utils.TestOnly;
@@ -45,11 +46,11 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
-import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
import java.util.Iterator;
/**
@@ -123,7 +124,7 @@ public class SchemaFile implements ISchemaFile {
pmtFile.createNewFile();
}
- this.channel = new RandomAccessFile(pmtFile, "rw").getChannel();
+ this.channel = openReadWriteChannel(pmtFile);
this.headerContent =
ByteBuffer.allocate(SchemaFileConfig.FILE_HEADER_SIZE);
// will be overwritten if to init
this.dataTTL = ttl;
@@ -138,7 +139,7 @@ public class SchemaFile implements ISchemaFile {
pmtFile = file;
filePath = pmtFile.getPath();
logPath = file.getParent() + File.separator +
SchemaConstant.PBTREE_LOG_FILE_NAME;
- channel = new RandomAccessFile(file, "rw").getChannel();
+ channel = openReadWriteChannel(file);
headerContent = ByteBuffer.allocate(SchemaFileConfig.FILE_HEADER_SIZE);
if (channel.size() <= 0) {
@@ -293,11 +294,19 @@ public class SchemaFile implements ISchemaFile {
}
pmtFile.createNewFile();
- channel = new RandomAccessFile(pmtFile, "rw").getChannel();
+ channel = openReadWriteChannel(pmtFile);
headerContent = ByteBuffer.allocate(SchemaFileConfig.FILE_HEADER_SIZE);
initFileHeader();
}
+ private static FileChannel openReadWriteChannel(File file) throws
IOException {
+ return FileChannel.open(
+ file.toPath(),
+ StandardOpenOption.READ,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.CREATE);
+ }
+
public String inspect() throws MetadataException, IOException {
return inspect(null);
}
@@ -464,7 +473,7 @@ public class SchemaFile implements ISchemaFile {
SystemFileFactory.INSTANCE.getFile(snapshotDir,
SchemaConstant.PBTREE_SNAPSHOT);
try {
sync();
- if (schemaFileSnapshot.exists() && !schemaFileSnapshot.delete()) {
+ if (schemaFileSnapshot.exists() &&
!FileUtils.deleteFileIfExist(schemaFileSnapshot)) {
logger.error(
DataNodeSchemaMessages.FAILED_TO_DELETE_OLD_PBTREE_SNAPSHOT,
schemaFileSnapshot.getName());
@@ -474,7 +483,7 @@ public class SchemaFile implements ISchemaFile {
return true;
} catch (IOException e) {
logger.error(DataNodeSchemaMessages.FAILED_TO_CREATE_SCHEMA_FILE_SNAPSHOT,
e.getMessage(), e);
- schemaFileSnapshot.delete();
+ FileUtils.deleteFileIfExist(schemaFileSnapshot);
return false;
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/log/SchemaFileLogReader.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/log/SchemaFileLogReader.java
index d2300384fa7..a763ffe5565 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/log/SchemaFileLogReader.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/log/SchemaFileLogReader.java
@@ -32,6 +32,7 @@ import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
+import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -54,7 +55,7 @@ public class SchemaFileLogReader {
}
public List<byte[]> collectUpdatedEntries() throws IOException,
SchemaFileLogCorruptedException {
- if (inputStream == null || inputStream.getChannel().size() == 0) {
+ if (inputStream == null || Files.size(logFile.toPath()) == 0) {
return Collections.emptyList();
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/pagemgr/PageIOChannel.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/pagemgr/PageIOChannel.java
index 4e9d6524f19..f56e0c15d1b 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/pagemgr/PageIOChannel.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/pagemgr/PageIOChannel.java
@@ -26,7 +26,6 @@ import
org.apache.iotdb.db.schemaengine.schemaregion.mtree.impl.pbtree.schemafil
import
org.apache.iotdb.db.schemaengine.schemaregion.mtree.impl.pbtree.schemafile.log.SchemaFileLogWriter;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
@@ -95,9 +94,14 @@ public class PageIOChannel {
// complete log file
if (!res.isEmpty()) {
- try (FileOutputStream outputStream = new FileOutputStream(logPath,
true)) {
- outputStream.write(new byte[] {SchemaFileConfig.SF_COMMIT_MARK});
- return outputStream.getChannel().size();
+ try (FileChannel logChannel =
+ FileChannel.open(
+ new File(logPath).toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.APPEND)) {
+ logChannel.write(ByteBuffer.wrap(new byte[]
{SchemaFileConfig.SF_COMMIT_MARK}));
+ return logChannel.size();
}
}
return 0L;
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/tag/TagManager.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/tag/TagManager.java
index 69e21e442de..314e971e3ac 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/tag/TagManager.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/tag/TagManager.java
@@ -137,7 +137,7 @@ public class TagManager {
File tagSnapshot =
SystemFileFactory.INSTANCE.getFile(snapshotDir,
SchemaConstant.TAG_LOG_SNAPSHOT);
File tagFile = SystemFileFactory.INSTANCE.getFile(sgSchemaDirPath,
SchemaConstant.TAG_LOG);
- if (tagFile.exists() && !tagFile.delete()) {
+ if (tagFile.exists() && !FileUtils.deleteFileIfExist(tagFile)) {
logger.warn(DataNodeSchemaMessages.FAILED_TO_DELETE_EXISTING_WHEN_LOADING,
tagFile.getName());
}
@@ -145,7 +145,7 @@ public class TagManager {
org.apache.tsfile.external.commons.io.FileUtils.copyFile(tagSnapshot,
tagFile);
return new TagManager(sgSchemaDirPath, regionStatistics);
} catch (IOException e) {
- if (!tagFile.delete()) {
+ if (!FileUtils.deleteFileIfExist(tagFile)) {
logger.warn(
DataNodeSchemaMessages.FAILED_TO_DELETE_EXISTING_WHEN_COPY_FAILURE,
tagFile.getName());
}
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 eb63312ebb1..cfdc63edbdf 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
@@ -3233,7 +3233,7 @@ public class DataRegion implements IDataRegionForQuery {
} catch (IOException e) {
logger.error(StorageEngineMessages.FAILED_TO_CHECK_OBJECT_FILES,
e.getMessage());
}
- FileUtils.deleteQuietly(objectTableDir);
+
org.apache.iotdb.commons.utils.FileUtils.deleteFileOrDirectory(objectTableDir,
true);
}
FileMetrics.getInstance().decreaseObjectFileNum(count.get());
FileMetrics.getInstance().decreaseObjectFileSize(totalSize.get());
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/flush/CompressionRatio.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/flush/CompressionRatio.java
index a243912240f..daf9de6489d 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/flush/CompressionRatio.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/flush/CompressionRatio.java
@@ -155,7 +155,8 @@ public class CompressionRatio {
dataRegionCompressionRatio.getRight())
+ "."
+ dataRegionId);
- if (!oldDataRegionFile.delete() && oldDataRegionFile.exists()) {
+ if
(!org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(oldDataRegionFile)
+ && oldDataRegionFile.exists()) {
LOGGER.warn(StorageEngineMessages.CANNOT_DELETE_OLD_COMPRESSION_FILE,
oldDataRegionFile);
}
}
@@ -237,7 +238,7 @@ public class CompressionRatio {
for (File ratioFile : ratioFiles) {
if (ratioFile != null) {
- if (!ratioFile.delete()) {
+ if
(!org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(ratioFile)) {
LOGGER.warn(StorageEngineMessages.CANNOT_DELETE_RATIO_FILE,
ratioFile.getAbsolutePath());
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/v1/ModificationFileV1.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/v1/ModificationFileV1.java
index a0d625f016d..5b938db9d8f 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/v1/ModificationFileV1.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/v1/ModificationFileV1.java
@@ -154,7 +154,9 @@ public class ModificationFileV1 implements AutoCloseable {
public void remove() throws IOException {
close();
- boolean deleted =
FSFactoryProducer.getFSFactory().getFile(filePath).delete();
+ boolean deleted =
+ FSFactoryProducer.getFSFactory()
+
.deleteIfExists(FSFactoryProducer.getFSFactory().getFile(filePath));
if (!deleted) {
logger.warn(StorageEngineMessages.DELETE_MODIFICATION_FILE_FAILED,
filePath);
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/v1/io/LocalTextModificationAccessor.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/v1/io/LocalTextModificationAccessor.java
index ecc4be4f909..2f1d5599a07 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/v1/io/LocalTextModificationAccessor.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/v1/io/LocalTextModificationAccessor.java
@@ -31,12 +31,16 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
+import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
-import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.NoSuchFileException;
+import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -201,11 +205,11 @@ public class LocalTextModificationAccessor
@Override
public void truncate(long size) {
- try (FileOutputStream outputStream =
- new
FileOutputStream(FSFactoryProducer.getFSFactory().getFile(filePath), true)) {
- outputStream.getChannel().truncate(size);
+ try {
+ org.apache.iotdb.commons.utils.FileUtils.truncateFile(
+ FSFactoryProducer.getFSFactory().getFile(filePath), size);
logger.warn(StorageEngineMessages.MODIFICATIONS_WILL_BE_TRUNCATED,
filePath, size);
- } catch (FileNotFoundException e) {
+ } catch (NoSuchFileException e) {
logger.debug(NO_MODIFICATION_MSG, filePath);
} catch (IOException e) {
logger.error(
@@ -219,19 +223,20 @@ public class LocalTextModificationAccessor
@Override
public void mayTruncateLastLine() {
- try (RandomAccessFile file = new RandomAccessFile(filePath, "r")) {
- long filePointer = file.length() - 1;
+ try (FileChannel channel =
+ FileChannel.open(
+ FSFactoryProducer.getFSFactory().getFile(filePath).toPath(),
StandardOpenOption.READ)) {
+ long filePointer = channel.size() - 1;
if (filePointer <= 0) {
return;
}
- file.seek(filePointer);
- byte lastChar = file.readByte();
+ ByteBuffer byteBuffer = ByteBuffer.allocate(Byte.BYTES);
+ byte lastChar = readByte(channel, byteBuffer, filePointer);
if (lastChar != '\n') {
while (filePointer > -1 && lastChar != '\n') {
- file.seek(filePointer);
+ lastChar = readByte(channel, byteBuffer, filePointer);
filePointer--;
- lastChar = file.readByte();
}
logger.warn(StorageEngineMessages.LAST_LINE_OF_MODS_INCOMPLETE);
truncate(filePointer + 2);
@@ -241,6 +246,16 @@ public class LocalTextModificationAccessor
}
}
+ private static byte readByte(FileChannel channel, ByteBuffer byteBuffer,
long position)
+ throws IOException {
+ byteBuffer.clear();
+ if (channel.read(byteBuffer, position) < Byte.BYTES) {
+ throw new EOFException();
+ }
+ byteBuffer.flip();
+ return byteBuffer.get();
+ }
+
private static String encodeModification(Modification mod) {
if (mod instanceof Deletion) {
return encodeDeletion((Deletion) mod);
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java
index a5ec8a555eb..4b33759af1c 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/tsfile/TsFileResource.java
@@ -1619,7 +1619,7 @@ public class TsFileResource implements
PersistentResource, Cloneable {
@SuppressWarnings({"java:S4042", "java:S899", "ResultOfMethodCallIgnored"})
private ModificationFile doUpgradeModFile(ModificationFileV1 oldModFile)
throws IOException {
ModificationFile newMFile = ModificationFile.getExclusiveMods(this);
- newMFile.getFile().delete();
+ FileUtils.deleteFileIfExist(newMFile.getFile());
try {
for (Modification oldMod : oldModFile.getModifications()) {
newMFile.write(new TreeDeletionEntry((Deletion) oldMod));
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/utils/fileTimeIndexCache/FileTimeIndexCacheWriter.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/utils/fileTimeIndexCache/FileTimeIndexCacheWriter.java
index 7cef1dc4792..b11c7da451a 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/utils/fileTimeIndexCache/FileTimeIndexCacheWriter.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/utils/fileTimeIndexCache/FileTimeIndexCacheWriter.java
@@ -26,18 +26,17 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
public class FileTimeIndexCacheWriter implements ILogWriter {
private static final Logger logger =
LoggerFactory.getLogger(FileTimeIndexCacheWriter.class);
private final File logFile;
- private FileOutputStream fileOutputStream;
private FileChannel channel;
private final boolean forceEachWrite;
@@ -46,8 +45,7 @@ public class FileTimeIndexCacheWriter implements ILogWriter {
this.logFile = logFile;
this.forceEachWrite = forceEachWrite;
- fileOutputStream = new FileOutputStream(logFile, true);
- channel = fileOutputStream.getChannel();
+ channel = openChannel(logFile);
}
@Override
@@ -82,8 +80,6 @@ public class FileTimeIndexCacheWriter implements ILogWriter {
if (channel.isOpen()) {
channel.force(false);
}
- fileOutputStream.close();
- fileOutputStream = null;
channel.close();
channel = null;
}
@@ -96,8 +92,12 @@ public class FileTimeIndexCacheWriter implements ILogWriter {
logger.warn(
StorageEngineMessages.PARTITION_LOG_FILE_ALREADY_EXISTS,
logFile.getAbsolutePath());
}
- fileOutputStream = new FileOutputStream(logFile, true);
- channel = fileOutputStream.getChannel();
+ channel =
+ FileChannel.open(
+ logFile.toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.APPEND);
}
@Override
@@ -108,4 +108,18 @@ public class FileTimeIndexCacheWriter implements
ILogWriter {
public File getLogFile() {
return logFile;
}
+
+ private static FileChannel openChannel(File file) throws
FileNotFoundException {
+ try {
+ return FileChannel.open(
+ file.toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.APPEND);
+ } catch (IOException e) {
+ FileNotFoundException exception = new
FileNotFoundException(e.getMessage());
+ exception.initCause(e);
+ throw exception;
+ }
+ }
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/LogWriter.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/LogWriter.java
index 37ccc3cb6cd..52675dae37e 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/LogWriter.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/LogWriter.java
@@ -31,11 +31,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
+import java.nio.file.StandardOpenOption;
/**
* LogWriter writes the binary logs into a file, including writing {@link
WALEntry} into .wal file
@@ -45,7 +45,6 @@ public abstract class LogWriter implements ILogWriter {
private static final Logger logger =
LoggerFactory.getLogger(LogWriter.class);
protected final File logFile;
- protected final FileOutputStream logStream;
protected final FileChannel logChannel;
protected long originalSize = 0;
@@ -68,8 +67,12 @@ public abstract class LogWriter implements ILogWriter {
protected LogWriter(File logFile, WALFileVersion version) throws IOException
{
this.logFile = logFile;
- this.logStream = new FileOutputStream(logFile, true);
- this.logChannel = this.logStream.getChannel();
+ this.logChannel =
+ FileChannel.open(
+ logFile.toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.APPEND);
if ((!logFile.exists() || logFile.length() == 0)
&& (version == WALFileVersion.V2 || version == WALFileVersion.V3)) {
this.logChannel.write(ByteBuffer.wrap(version.getVersionBytes()));
@@ -174,7 +177,6 @@ public abstract class LogWriter implements ILogWriter {
}
} finally {
logChannel.close();
- logStream.close();
}
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/node/WALNode.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/node/WALNode.java
index 728b3356b88..a59ebeaa21d 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/node/WALNode.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/node/WALNode.java
@@ -398,7 +398,7 @@ public class WALNode implements IWALNode {
long versionId = WALFileUtils.parseVersionId(currentWal.getName());
if (canDeleteFile(fileArrIdx, walFileStatus, versionId)) {
long fileSize = currentWal.length();
- if (currentWal.delete()) {
+ if
(org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(currentWal)) {
deleteFileSize += fileSize;
buffer.removeMemTableIdsOfWal(versionId);
successfullyDeleted.add(versionId);
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/recover/file/AbstractTsFileRecoverPerformer.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/recover/file/AbstractTsFileRecoverPerformer.java
index 46fe0129c2b..5b470ee1c2b 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/recover/file/AbstractTsFileRecoverPerformer.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/recover/file/AbstractTsFileRecoverPerformer.java
@@ -98,7 +98,7 @@ public abstract class AbstractTsFileRecoverPerformer
implements Closeable {
if (versionNumber != TSFileConfig.VERSION_NUMBER) {
// cannot rewrite a file with V3 header, delete it first
writer.close();
- tsFile.delete();
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tsFile);
writer =
new RestorableTsFileIOWriter(
tsFile,
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/converter/LoadTableStatementDataTypeConvertExecutionVisitor.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/converter/LoadTableStatementDataTypeConvertExecutionVisitor.java
index 89854e23f04..ce3ddccde92 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/converter/LoadTableStatementDataTypeConvertExecutionVisitor.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/converter/LoadTableStatementDataTypeConvertExecutionVisitor.java
@@ -32,7 +32,6 @@ import org.apache.iotdb.db.storageengine.load.util.LoadUtil;
import org.apache.iotdb.pipe.api.event.dml.insertion.TabletInsertionEvent;
import org.apache.iotdb.rpc.TSStatusCode;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -125,11 +124,14 @@ public class
LoadTableStatementDataTypeConvertExecutionVisitor
.getTsFiles()
.forEach(
tsfile -> {
- FileUtils.deleteQuietly(tsfile);
+
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tsfile);
final String tsFilePath = tsfile.getAbsolutePath();
- FileUtils.deleteQuietly(new
File(LoadUtil.getTsFileResourcePath(tsFilePath)));
- FileUtils.deleteQuietly(new
File(LoadUtil.getTsFileModsV1Path(tsFilePath)));
- FileUtils.deleteQuietly(new
File(LoadUtil.getTsFileModsV2Path(tsFilePath)));
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(
+ new File(LoadUtil.getTsFileResourcePath(tsFilePath)));
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(
+ new File(LoadUtil.getTsFileModsV1Path(tsFilePath)));
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(
+ new File(LoadUtil.getTsFileModsV2Path(tsFilePath)));
});
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/converter/LoadTreeStatementDataTypeConvertExecutionVisitor.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/converter/LoadTreeStatementDataTypeConvertExecutionVisitor.java
index 1922e335a29..d96f5c1cd09 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/converter/LoadTreeStatementDataTypeConvertExecutionVisitor.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/converter/LoadTreeStatementDataTypeConvertExecutionVisitor.java
@@ -33,7 +33,6 @@ import
org.apache.iotdb.db.storageengine.load.memory.LoadTsFileMemoryManager;
import org.apache.iotdb.db.storageengine.load.util.LoadUtil;
import org.apache.iotdb.rpc.TSStatusCode;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.utils.Pair;
import org.apache.tsfile.write.record.Tablet;
import org.slf4j.Logger;
@@ -172,11 +171,14 @@ public class
LoadTreeStatementDataTypeConvertExecutionVisitor
.getTsFiles()
.forEach(
tsfile -> {
- FileUtils.deleteQuietly(tsfile);
+
org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(tsfile);
final String tsFilePath = tsfile.getAbsolutePath();
- FileUtils.deleteQuietly(new
File(LoadUtil.getTsFileResourcePath(tsFilePath)));
- FileUtils.deleteQuietly(new
File(LoadUtil.getTsFileModsV1Path(tsFilePath)));
- FileUtils.deleteQuietly(new
File(LoadUtil.getTsFileModsV2Path(tsFilePath)));
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(
+ new File(LoadUtil.getTsFileResourcePath(tsFilePath)));
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(
+ new File(LoadUtil.getTsFileModsV1Path(tsFilePath)));
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(
+ new File(LoadUtil.getTsFileModsV2Path(tsFilePath)));
});
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/disk/DirectoryChecker.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/disk/DirectoryChecker.java
index e7076cfbe18..77bf6f45729 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/disk/DirectoryChecker.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/disk/DirectoryChecker.java
@@ -29,20 +29,22 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
-import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
public class DirectoryChecker {
private static final Logger logger =
LoggerFactory.getLogger(DirectoryChecker.class);
private static final String LOCK_FILE_NAME = ".iotdb-lock";
- private final List<RandomAccessFile> randomAccessFileList = new
ArrayList<>();
+ private final List<FileChannel> fileChannelList = new ArrayList<>();
private final List<File> fileList = new ArrayList<>();
private DirectoryChecker() {}
@@ -51,7 +53,7 @@ public class DirectoryChecker {
return DirectoryCheckerHolder.INSTANCE;
}
- @SuppressWarnings("java:S2095") // will be closed by randomAccessFileList
+ @SuppressWarnings("java:S2095") // will be closed by fileChannelList
public void registerDirectory(File dir) throws ConfigurationException,
IOException {
if (dir.exists() && !dir.isDirectory()) {
throw new ConfigurationException(
@@ -69,8 +71,12 @@ public class DirectoryChecker {
}
}
File file = new File(dir, LOCK_FILE_NAME);
- RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
- FileChannel channel = randomAccessFile.getChannel();
+ FileChannel channel =
+ FileChannel.open(
+ file.toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.READ,
+ StandardOpenOption.WRITE);
FileLock lock = null;
try {
// Try acquiring the lock without blocking. This method returns
@@ -81,18 +87,26 @@ public class DirectoryChecker {
}
// File is already locked other virtual machine
if (lock == null) {
- randomAccessFile.close();
+ String lockOwner = Files.readString(file.toPath());
+ channel.close();
throw new ConfigurationException(
String.format(
StorageEngineMessages
.STORAGE_EXCEPTION_CONFLICT_IS_DETECTED_IN_DIRECTORY_S_WHICH_MAY_BE_BEING_USED_CB5C77FC,
dir.getAbsolutePath(),
- randomAccessFile.readLine()));
+ lockOwner));
}
- randomAccessFile.writeBytes(ProcessIdUtils.getProcessId());
+ channel.truncate(0);
+ channel.position(0);
+ ByteBuffer processId =
+
ByteBuffer.wrap(ProcessIdUtils.getProcessId().getBytes(StandardCharsets.UTF_8));
+ while (processId.hasRemaining()) {
+ channel.write(processId);
+ }
+ channel.force(true);
// add to list
fileList.add(file);
- randomAccessFileList.add(randomAccessFile);
+ fileChannelList.add(channel);
}
public boolean isCrossDisk(String[] dirs) throws IOException {
@@ -125,8 +139,8 @@ public class DirectoryChecker {
public void deregisterAll() {
try {
- for (RandomAccessFile randomAccessFile : randomAccessFileList) {
- randomAccessFile.close();
+ for (FileChannel fileChannel : fileChannelList) {
+ fileChannel.close();
// it will release lock automatically after close
}
for (File file : fileList) {
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusSubscriptionCommitManager.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusSubscriptionCommitManager.java
index 0e85f4d2072..4daa503ce1a 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusSubscriptionCommitManager.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusSubscriptionCommitManager.java
@@ -58,12 +58,13 @@ import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
import java.util.Base64;
import java.util.Collections;
import java.util.Iterator;
@@ -1154,7 +1155,7 @@ public class ConsensusSubscriptionCommitManager {
}
private static void deleteFileIfExists(final File file) {
- if (file.exists() && !file.delete()) {
+ if (file.exists() &&
!org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(file)) {
LOGGER.warn(
DataNodePipeMessages
.PIPE_LOG_FAILED_TO_DELETE_CONSENSUS_SUBSCRIPTION_PROGRESS_FILE_51C57096,
@@ -1220,11 +1221,15 @@ public class ConsensusSubscriptionCommitManager {
private void overwriteRegionPayload(
final File metaFile, final long payloadOffset, final byte[] payload)
throws IOException {
- try (final RandomAccessFile randomAccessFile = new
RandomAccessFile(metaFile, "rw")) {
- randomAccessFile.seek(payloadOffset);
- randomAccessFile.write(payload);
+ try (final FileChannel channel =
+ FileChannel.open(metaFile.toPath(), StandardOpenOption.WRITE)) {
+ channel.position(payloadOffset);
+ final ByteBuffer payloadBuffer = ByteBuffer.wrap(payload);
+ while (payloadBuffer.hasRemaining()) {
+ channel.write(payloadBuffer);
+ }
if
(SubscriptionConfig.getInstance().isSubscriptionConsensusCommitFsyncEnabled()) {
- randomAccessFile.getFD().sync();
+ channel.force(true);
}
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionPipeTsFileEventBatch.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionPipeTsFileEventBatch.java
index f05c1e467ba..a8735b59edd 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionPipeTsFileEventBatch.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionPipeTsFileEventBatch.java
@@ -33,7 +33,6 @@ import
org.apache.iotdb.pipe.api.event.dml.insertion.TabletInsertionEvent;
import org.apache.iotdb.pipe.api.event.dml.insertion.TsFileInsertionEvent;
import
org.apache.iotdb.rpc.subscription.payload.poll.SubscriptionCommitContext;
-import org.apache.tsfile.external.commons.io.FileUtils;
import org.apache.tsfile.utils.Pair;
import org.apache.tsfile.write.record.Tablet;
import org.slf4j.Logger;
@@ -88,7 +87,7 @@ public class SubscriptionPipeTsFileEventBatch extends
SubscriptionPipeEventBatch
} finally {
for (final Pair<String, File> sealedFilePair : sealedFilePairs) {
final File sealedFile = sealedFilePair.right;
- if (sealedFile.exists() && !FileUtils.deleteQuietly(sealedFile)) {
+ if
(!org.apache.iotdb.commons.utils.FileUtils.deleteFileIfExist(sealedFile)) {
LOGGER.warn(DataNodePipeMessages.FAILED_TO_DELETE_BATCH_FILE_THIS_FILE,
sealedFilePair);
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/ObjectWriter.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/ObjectWriter.java
index 67c61fcc1bc..b85b136af61 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/ObjectWriter.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/ObjectWriter.java
@@ -29,9 +29,11 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
public class ObjectWriter implements AutoCloseable {
@@ -39,7 +41,7 @@ public class ObjectWriter implements AutoCloseable {
private static final IoTDBConfig config =
IoTDBDescriptor.getInstance().getConfig();
- private final FileOutputStream fos;
+ private final FileChannel channel;
private final File file;
@@ -58,14 +60,15 @@ public class ObjectWriter implements AutoCloseable {
}
}
file = filePath;
- fos = new FileOutputStream(filePath, true);
+ channel = openChannel(filePath);
}
public void write(boolean isGeneratedByConsensus, long offset, byte[]
content)
throws IOException {
if (file.length() != offset) {
if (isGeneratedByConsensus || offset == 0) {
- fos.getChannel().truncate(offset);
+ org.apache.iotdb.commons.utils.FileUtils.truncateFile(file, offset);
+ channel.position(offset);
} else {
throw new IOException(
String.format(
@@ -78,11 +81,27 @@ public class ObjectWriter implements AutoCloseable {
if (file.length() + content.length > config.getMaxObjectSizeInByte()) {
throw new IOException(DataNodeMiscMessages.FILE_LENGTH_LARGER_THAN_MAX);
}
- fos.write(content);
+ ByteBuffer buffer = ByteBuffer.wrap(content);
+ while (buffer.hasRemaining()) {
+ channel.write(buffer);
+ }
}
@Override
public void close() throws Exception {
- fos.close();
+ channel.close();
+ }
+
+ private static FileChannel openChannel(File file) throws
FileNotFoundException {
+ try {
+ FileChannel channel =
+ FileChannel.open(file.toPath(), StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
+ channel.position(channel.size());
+ return channel;
+ } catch (IOException e) {
+ FileNotFoundException exception = new
FileNotFoundException(e.getMessage());
+ exception.initCause(e);
+ throw exception;
+ }
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/LogWriter.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/LogWriter.java
index 5421dd76651..a68cf17b668 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/LogWriter.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/LogWriter.java
@@ -25,11 +25,11 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
+import java.nio.file.StandardOpenOption;
import java.util.zip.CRC32;
/**
@@ -40,7 +40,6 @@ public class LogWriter implements ILogWriter {
private static final Logger logger =
LoggerFactory.getLogger(LogWriter.class);
private final File logFile;
- private FileOutputStream fileOutputStream;
private FileChannel channel;
private final CRC32 checkSummer = new CRC32();
private final ByteBuffer lengthBuffer = ByteBuffer.allocate(4);
@@ -51,15 +50,13 @@ public class LogWriter implements ILogWriter {
this.logFile = logFile;
this.forceEachWrite = forceEachWrite;
- fileOutputStream = new FileOutputStream(logFile, true);
- channel = fileOutputStream.getChannel();
+ channel = openChannel(logFile);
}
@Override
public void write(ByteBuffer logBuffer) throws IOException {
if (channel == null) {
- fileOutputStream = new FileOutputStream(logFile, true);
- channel = fileOutputStream.getChannel();
+ channel = openChannel(logFile);
}
logBuffer.flip();
int logSize = logBuffer.limit();
@@ -104,8 +101,6 @@ public class LogWriter implements ILogWriter {
if (channel.isOpen()) {
channel.force(true);
}
- fileOutputStream.close();
- fileOutputStream = null;
channel.close();
channel = null;
}
@@ -115,4 +110,18 @@ public class LogWriter implements ILogWriter {
public String toString() {
return "LogWriter{" + "logFile=" + logFile + '}';
}
+
+ private static FileChannel openChannel(File file) throws
FileNotFoundException {
+ try {
+ return FileChannel.open(
+ file.toPath(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.APPEND);
+ } catch (IOException e) {
+ FileNotFoundException exception = new
FileNotFoundException(e.getMessage());
+ exception.initCause(e);
+ throw exception;
+ }
+ }
}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java
index cda9271bb04..df7d7f28427 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java
@@ -307,8 +307,7 @@ public class LocalFileRoleAccessor implements
IEntityAccessor {
if (!roleProfile.exists() && !backFile.exists()) {
return false;
}
- if ((roleProfile.exists() && !roleProfile.delete())
- || (backFile.exists() && !backFile.delete())) {
+ if (!FileUtils.deleteFileIfExist(roleProfile) ||
!FileUtils.deleteFileIfExist(backFile)) {
throw new
IOException(String.format(AuthMessages.CANNOT_DELETE_ROLE_FILE, entityName));
}
return true;
@@ -352,7 +351,7 @@ public class LocalFileRoleAccessor implements
IEntityAccessor {
result = FileUtils.copyDir(roleFolder, roleTmpSnapshotDir);
result &= roleTmpSnapshotDir.renameTo(roleSnapshotDir);
} finally {
- if (roleTmpSnapshotDir.exists() && !roleTmpSnapshotDir.delete()) {
+ if (roleTmpSnapshotDir.exists() &&
!FileUtils.deleteFileIfExist(roleTmpSnapshotDir)) {
FileUtils.deleteFileOrDirectory(roleTmpSnapshotDir);
}
}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java
index b29c064ba62..5ad0d08fc42 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java
@@ -230,8 +230,7 @@ public class LocalFileUserAccessor extends
LocalFileRoleAccessor {
if (!uRoleProfile.exists() && !backProfile.exists()) {
return true;
}
- if ((uRoleProfile.exists() && !uRoleProfile.delete())
- || (backProfile.exists() && !backProfile.delete())) {
+ if (!FileUtils.deleteFileIfExist(uRoleProfile) ||
!FileUtils.deleteFileIfExist(backProfile)) {
throw new
IOException(String.format(AuthMessages.CATCH_ERROR_DELETE_USER_ROLE, username));
}
return true;
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/executable/ExecutableManager.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/executable/ExecutableManager.java
index a9f8ffc4229..51d4671f36b 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/executable/ExecutableManager.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/executable/ExecutableManager.java
@@ -30,7 +30,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -128,7 +127,8 @@ public class ExecutableManager {
}
private void removeFromTemporaryLibRoot(long requestId) {
- FileUtils.deleteQuietly(getDirUnderTempRootByRequestId(requestId));
+ org.apache.iotdb.commons.utils.FileUtils.deleteFileOrDirectory(
+ getDirUnderTempRootByRequestId(requestId), true);
}
public void saveTextAsFileUnderTemporaryRoot(String text, String fileName)
throws IOException {
@@ -228,12 +228,10 @@ public class ExecutableManager {
}
Files.createFile(path);
}
- // FileOutPutStream is not in append mode by default, so the file will be
- // overridden if it
- // already exists.
- try (FileOutputStream outputStream = new FileOutputStream(destination)) {
- outputStream.getChannel().write(byteBuffer);
- outputStream.getFD().sync();
+ try (FileChannel channel =
+ FileChannel.open(path, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
+ channel.write(byteBuffer);
+ channel.force(true);
}
} catch (IOException e) {
LOGGER.warn(
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/file/SystemPropertiesHandler.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/file/SystemPropertiesHandler.java
index 5d465e7376e..c82e41a49aa 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/file/SystemPropertiesHandler.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/file/SystemPropertiesHandler.java
@@ -161,7 +161,7 @@ public abstract class SystemPropertiesHandler {
return;
}
if (formalFile.exists() && tmpFile.exists()) {
- if (!tmpFile.delete()) {
+ if (!FileUtils.deleteFileIfExist(tmpFile)) {
LOGGER.warn(
CommonMessages
.LOG_DELETE_SYSTEM_PROPERTIES_TMP_FILE_FAIL_YOU_MAY_MANUALLY_DELETE_F81C4A53,
@@ -182,7 +182,7 @@ public abstract class SystemPropertiesHandler {
CommonMessages
.EXCEPTION_TMP_SYSTEM_PROPERTIES_FILE_MUST_EXIST_CALL_REPLACEFORMALFILE_FA63B976);
}
- if (formalFile.exists() && !formalFile.delete()) {
+ if (formalFile.exists() && !FileUtils.deleteFileIfExist(formalFile)) {
String msg =
String.format(
"Delete formal system properties file fail: %s",
formalFile.getAbsoluteFile());
@@ -198,8 +198,8 @@ public abstract class SystemPropertiesHandler {
}
public void delete() {
- this.formalFile.delete();
- this.tmpFile.delete();
+ FileUtils.deleteFileIfExist(this.formalFile);
+ FileUtils.deleteFileIfExist(this.tmpFile);
if (this.formalFile.exists() || this.tmpFile.exists()) {
LOGGER.warn(
CommonMessages
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/datastructure/queue/serializer/PlainQueueSerializer.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/datastructure/queue/serializer/PlainQueueSerializer.java
index 10087878d93..ddc41bdeec0 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/datastructure/queue/serializer/PlainQueueSerializer.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/datastructure/queue/serializer/PlainQueueSerializer.java
@@ -21,15 +21,14 @@ package
org.apache.iotdb.commons.pipe.datastructure.queue.serializer;
import org.apache.iotdb.commons.i18n.PipeMessages;
import
org.apache.iotdb.commons.pipe.datastructure.queue.ConcurrentIterableLinkedQueue;
-import org.apache.iotdb.commons.utils.IOUtils;
import org.apache.tsfile.utils.ReadWriteIOUtils;
+import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
import java.util.Objects;
import java.util.function.Function;
@@ -62,22 +61,21 @@ public class PlainQueueSerializer<E> implements
QueueSerializer<E> {
ConcurrentIterableLinkedQueue<E> queue,
Function<ByteBuffer, E> elementDeserializationFunction)
throws IOException {
- try (FileChannel channel = inputStream.getChannel()) {
- queue.setFirstIndex(ReadWriteIOUtils.readLong(inputStream));
- while (true) {
- if (inputStream.available() == 0) {
- return;
- }
- int capacity = ReadWriteIOUtils.readInt(inputStream);
- ByteBuffer buffer = ByteBuffer.allocate(capacity);
- IOUtils.readFully(channel, buffer);
- buffer.flip();
- E element = elementDeserializationFunction.apply(buffer);
- if (element == null) {
- throw new IOException(PipeMessages.FAILED_TO_LOAD_SNAPSHOT);
- }
- queue.add(element);
+ queue.setFirstIndex(ReadWriteIOUtils.readLong(inputStream));
+ while (true) {
+ if (inputStream.available() == 0) {
+ return;
+ }
+ int capacity = ReadWriteIOUtils.readInt(inputStream);
+ byte[] bytes = inputStream.readNBytes(capacity);
+ if (bytes.length != capacity) {
+ throw new EOFException();
+ }
+ E element = elementDeserializationFunction.apply(ByteBuffer.wrap(bytes));
+ if (element == null) {
+ throw new IOException(PipeMessages.FAILED_TO_LOAD_SNAPSHOT);
}
+ queue.add(element);
}
}
}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/receiver/IoTDBFileReceiver.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/receiver/IoTDBFileReceiver.java
index af2e327ad42..806b5d0c5ee 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/receiver/IoTDBFileReceiver.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/receiver/IoTDBFileReceiver.java
@@ -434,7 +434,9 @@ public abstract class IoTDBFileReceiver implements
IoTDBReceiver {
// of the file. So the receiver should reset the offset of the writing
file to the beginning
// of the file.
if (isRequestThroughAirGap && req.getStartWritingOffset() <
writingFileWriter.length()) {
- writingFileWriter.setLength(req.getStartWritingOffset());
+ org.apache.iotdb.commons.utils.FileUtils.truncateFile(
+ writingFile, req.getStartWritingOffset());
+ writingFileWriter.seek(req.getStartWritingOffset());
}
if (!isWritingFileOffsetCorrect(req.getStartWritingOffset())) {
@@ -442,7 +444,8 @@ public abstract class IoTDBFileReceiver implements
IoTDBReceiver {
// If the file is a tsFile, then the content will not be changed for
a specific filename.
// However, for other files (mod, snapshot, etc.) the content varies
for the same name in
// different times, then we must rewrite the file to apply the
newest version.
- writingFileWriter.setLength(0);
+ org.apache.iotdb.commons.utils.FileUtils.truncateFile(writingFile,
0);
+ writingFileWriter.seek(0);
}
final TSStatus status =
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
index dd799b0f9a7..a0611a8cfc6 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
@@ -35,6 +35,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.channels.FileChannel;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystems;
@@ -42,6 +43,7 @@ import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
@@ -92,6 +94,19 @@ public class FileUtils {
}
}
+ /**
+ * Truncate a file through its NIO file system provider.
+ *
+ * <p>Using {@link FileChannel#open(Path, java.nio.file.OpenOption...)}
allows a configured
+ * default {@link java.nio.file.spi.FileSystemProvider} to intercept the
truncation.
+ */
+ public static void truncateFile(File file, long size) throws IOException {
+ try (FileChannel channel = FileChannel.open(file.toPath(),
StandardOpenOption.WRITE)) {
+ channel.truncate(size);
+ channel.force(true);
+ }
+ }
+
public static void createLink(Path link, Path existing, boolean
fallBackToCopy)
throws IOException {
try {
@@ -197,7 +212,7 @@ public class FileUtils {
File[] files = parentFolder.listFiles();
if (parentFolder.isDirectory() && (files == null || files.length == 0)) {
acquireRemovePermit(parentFolder, deleteRateLimiter);
- if (!parentFolder.delete()) {
+ if (!deleteFileIfExist(parentFolder)) {
LOGGER.warn(UtilMessages.DELETE_FOLDER_FAILED,
parentFolder.getAbsolutePath());
}
}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/IOUtils.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/IOUtils.java
index 88c929e7092..4adebea8645 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/IOUtils.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/IOUtils.java
@@ -267,7 +267,7 @@ public class IOUtils {
public static void replaceFile(File newFile, File oldFile) throws
IOException {
if (!newFile.renameTo(oldFile)) {
// some OSs need to delete the old file before renaming to it
- if (!oldFile.delete()) {
+ if (!FileUtils.deleteFileIfExist(oldFile)) {
throw new IOException(
String.format(UtilMessages.CANNOT_DELETE_OLD_USER_FILE,
oldFile.getPath()));
}
diff --git
a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/FileUtilsTest.java
b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/FileUtilsTest.java
index 402634b084b..875a5a2bfcd 100644
---
a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/FileUtilsTest.java
+++
b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/FileUtilsTest.java
@@ -74,6 +74,16 @@ public class FileUtilsTest {
Assert.assertNull(FileUtils.getIllegalError4Directory("valid_dir"));
}
+ @Test
+ public void testTruncateFile() throws IOException {
+ File file = new File(tmpDir, "truncate-file");
+ Files.write(file.toPath(), new byte[] {1, 2, 3, 4});
+
+ FileUtils.truncateFile(file, 2);
+
+ Assert.assertArrayEquals(new byte[] {1, 2},
Files.readAllBytes(file.toPath()));
+ }
+
@Test
public void testDeleteFileOrDirectoryWithRateLimiter() throws IOException {
File deleteDir = new File(tmpDir, "deleteWithRateLimiter");
diff --git a/pom.xml b/pom.xml
index dbca5078c33..4496cc23e5d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -77,6 +77,7 @@
<enforcer.skip>true</enforcer.skip>
<felix.version>5.1.9</felix.version>
<findbugs.jsr305.version>3.0.2</findbugs.jsr305.version>
+ <forbiddenapis.version>3.10</forbiddenapis.version>
<fusesource-mqtt-client.version>1.16</fusesource-mqtt-client.version>
<google.java.format.version>1.28.0</google.java.format.version>
<gson.version>2.13.1</gson.version>
@@ -1024,6 +1025,33 @@
<skip>${spotless.skip}</skip>
</configuration>
</plugin>
+ <!--
+ Enforce provider-compatible file APIs in every Maven module.
The configured excludes
+ are paths relative to each module's target/classes directory
and are limited to
+ APIs without a direct provider-compatible lifecycle equivalent.
+ -->
+ <plugin>
+ <groupId>de.thetaphi</groupId>
+ <artifactId>forbiddenapis</artifactId>
+ <version>${forbiddenapis.version}</version>
+ <configuration>
+ <failOnUnsupportedJava>true</failOnUnsupportedJava>
+ <!-- Some modules reference optional profile-specific
classes, such as AINode thrift types. -->
+ <failOnMissingClasses>false</failOnMissingClasses>
+ <signaturesFiles>
+
<signaturesFile>${maven.multiModuleProjectDirectory}/src/main/forbidden-apis/secure-erase</signaturesFile>
+ </signaturesFiles>
+ </configuration>
+ <executions>
+ <execution>
+ <id>check-secure-erase-file-apis</id>
+ <phase>process-classes</phase>
+ <goals>
+ <goal>check</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
diff --git a/src/main/forbidden-apis/secure-erase
b/src/main/forbidden-apis/secure-erase
new file mode 100644
index 00000000000..462402c3f7c
--- /dev/null
+++ b/src/main/forbidden-apis/secure-erase
@@ -0,0 +1,23 @@
+# 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.
+
+@defaultMessage Use Files or FileChannel APIs so SecureFileSystemProvider can
intercept the operation.
+java.io.File#delete()
+@ignoreMissingClasses
+org.apache.tsfile.external.commons.io.FileUtils#deleteQuietly(java.io.File)
+java.io.FileInputStream#getChannel()
+java.io.FileOutputStream#getChannel()
+java.io.RandomAccessFile#getChannel()
+java.io.RandomAccessFile#setLength(long)