Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

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;

/**
Expand Down Expand Up @@ -79,8 +79,7 @@ private static void printUsage() {
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -92,8 +93,12 @@ public static int[] searchAvailablePorts() {
// 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

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;
Expand Down Expand Up @@ -680,8 +681,12 @@ public void cleanClusterEnvironment() {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
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 {

Expand Down Expand Up @@ -87,7 +87,6 @@ class SerializationRecorder {
protected int serializedElementSize;

protected String fileName;
protected RandomAccessFile file;
protected FileChannel fileChannel;

public SerializationRecorder(String queryId) {
Expand Down Expand Up @@ -127,28 +126,21 @@ public int getSerializedElementSize() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,31 +163,29 @@ public TSStatus deleteProcedure(DeleteProcedurePlan deleteProcedurePlan) {
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,8 @@ public boolean processTakeSnapshot(File snapshotDir) throws IOException, TExcept
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,8 @@ public boolean processTakeSnapshot(File snapshotDir) throws TException, IOExcept
} 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,8 @@ public boolean processDatabaseSchemaSnapshot(
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public boolean processTakeSnapshot(File snapshotDir) throws IOException {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ public boolean processTakeSnapshot(File snapshotDir) throws IOException {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
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;
Expand Down Expand Up @@ -56,19 +54,17 @@ public ProcedureWAL(Path walFilePath, IProcedureFactory procedureFactory) {
*/
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,10 +30,8 @@
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;

Expand Down Expand Up @@ -147,9 +146,8 @@ public boolean isFileCorrupted() {
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@
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;
Expand Down Expand Up @@ -529,8 +529,9 @@ private void writeSnapshotFragment(File targetFile, ByteBuffer fileChunk, long f
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,16 @@ public long takeSnapshot() throws IOException {
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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -121,8 +120,12 @@ public void start() {
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(
Expand Down Expand Up @@ -186,9 +189,6 @@ private void fsyncCurrentLoggingFile() throws IOException {
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();
}
Expand Down Expand Up @@ -243,8 +243,12 @@ private void switchLoggingFile() throws IOException {
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(
Expand Down
Loading