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

fix(s3stream/wal): check isBlockDev by prefix in some rare cases #2278

Merged
merged 1 commit into from
Jan 21, 2025
Merged
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 @@ -16,6 +16,9 @@
import com.automq.stream.utils.CommandResult;
import com.automq.stream.utils.CommandUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
Expand All @@ -24,6 +27,7 @@
import java.util.zip.CRC32;

import io.netty.buffer.ByteBuf;
import jnr.posix.POSIX;
import jnr.posix.POSIXFactory;

import static com.automq.stream.s3.wal.common.RecordHeader.RECORD_HEADER_MAGIC_CODE;
Expand All @@ -36,6 +40,8 @@ public class WALUtil {
"4096"
));

private static final Logger LOGGER = LoggerFactory.getLogger(WALUtil.class);

public static Record generateRecord(ByteBuf body, ByteBuf emptyHeader, int crc, long start) {
return generateRecord(body, emptyHeader, crc, start, true);
}
Expand Down Expand Up @@ -158,15 +164,25 @@ public static boolean isBlockDevice(String path) {
if (!new File(path).exists()) {
return false;
}
boolean isBlockDevice;

POSIX posix;
try {
isBlockDevice = POSIXFactory.getPOSIX()
.stat(path)
.isBlockDev();
posix = POSIXFactory.getNativePOSIX();
} catch (Exception e) {
LOGGER.warn("Failed to get native POSIX, fallback to check by prefix", e);
return isBlockDeviceByPrefix(path);
}

try {
return posix.stat(path).isBlockDev();
} catch (Exception e) {
// In some OS (like Windows), the isBlockDev() method may throw an IllegalStateException.
isBlockDevice = false;
LOGGER.warn("Failed to check if {} is a block device, fallback to check by prefix", path, e);
return isBlockDeviceByPrefix(path);
}
return isBlockDevice;
}

private static boolean isBlockDeviceByPrefix(String path) {
return path.startsWith("/dev/");
}
}
Loading