Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use jdk api to create hardlink when rename file when compaction. #3876

Merged
merged 13 commits into from
Jan 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ private void createNewCompactionLog() throws IOException {
private void removeCurCompactionLog() {
synchronized (compactionLogLock) {
if (compactionLogChannel != null) {
if (!compactionLogChannel.getLogFile().delete()) {
if (compactionLogChannel.getLogFile().exists() && !compactionLogChannel.getLogFile().delete()) {
LOG.warn("Could not delete compaction log file {}", compactionLogChannel.getLogFile());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Class for creating hardlinks.
Expand All @@ -42,7 +47,7 @@
* efficient - and minimizes the impact of the extra buffer creations.
*/
public class HardLink {

private static final Logger LOG = LoggerFactory.getLogger(HardLink.class);
/**
* OS Types.
*/
Expand Down Expand Up @@ -395,6 +400,8 @@ protected static int getMaxAllowedCmdArgLength() {
return getHardLinkCommand.getMaxAllowedCmdArgLength();
}

private static final AtomicBoolean CREATE_LINK_SUPPORTED = new AtomicBoolean(true);

/*
* ****************************************************
* Complexity is above. User-visible functionality is below
Expand All @@ -416,6 +423,23 @@ public static void createHardLink(File file, File linkName)
throw new IOException(
"invalid arguments to createHardLink: link name is null");
}

// if createLink available try first, else fall back to shell command.
if (CREATE_LINK_SUPPORTED.get()) {
lifepuzzlefun marked this conversation as resolved.
Show resolved Hide resolved
try {
Path newFile = Files.createLink(linkName.toPath(), file.toPath());
if (newFile.toFile().exists()) {
return;
}
} catch (UnsupportedOperationException e) {
LOG.error("createLink not supported", e);
CREATE_LINK_SUPPORTED.set(false);
} catch (IOException e) {
LOG.error("error when create hard link use createLink", e);
CREATE_LINK_SUPPORTED.set(false);
}
}

// construct and execute shell command
String[] hardLinkCommand = getHardLinkCommand.linkOne(file, linkName);
Process process = Runtime.getRuntime().exec(hardLinkCommand);
Expand Down