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

hadoop: checksum fix for files with size close to blockSize #2333

Merged
merged 2 commits into from
Jul 6, 2022
Merged
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
19 changes: 11 additions & 8 deletions sdk/java/src/main/java/io/juicefs/JuiceFileSystemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1185,41 +1185,44 @@ public FileChecksum getFileChecksum(Path f, long length) throws IOException {
int bytesPerCrc = getConf().getInt("io.bytes.per.checksum", 512);
DataChecksum summer = DataChecksum.newDataChecksum(ctype, bytesPerCrc);

long crcPerBlock = 0;
DataOutputBuffer checksumBuf = new DataOutputBuffer();
DataOutputBuffer crcBuf = new DataOutputBuffer();
byte[] buf = new byte[bytesPerCrc];
FSDataInputStream in = open(f, 1 << 20);
while (length > 0) {
for (int i = 0; i < blocksize / bytesPerCrc && length > 0; i++) {
boolean eof = false;
long got = 0;
while (got < length && !eof) {
for (int i = 0; i < blocksize / bytesPerCrc && got < length; i++) {
int n;
if (length < bytesPerCrc) {
n = in.read(buf, 0, (int) length);
} else {
n = in.read(buf);
}
if (n <= 0) {
length = 0; // EOF
eof = true;
break;
} else {
summer.update(buf, 0, n);
summer.writeValue(crcBuf, true);
length -= n;
got += n;
}
}
if (crcBuf.getLength() > 0) {
MD5Hash blockMd5 = MD5Hash.digest(crcBuf.getData(), 0, crcBuf.getLength());
blockMd5.write(checksumBuf);
crcBuf.reset();
if (length > 0) { // more than one block
crcPerBlock = blocksize / bytesPerCrc;
}
}
}
in.close();
if (checksumBuf.getLength() == 0) { // empty file
return new MD5MD5CRC32GzipFileChecksum(0, 0, MD5Hash.digest(new byte[32]));
}
MD5Hash md5 = MD5Hash.digest(checksumBuf.getData());
long crcPerBlock = 0;
if (got > blocksize) { // more than one block
crcPerBlock = blocksize / bytesPerCrc;
}
if (ctype == DataChecksum.Type.CRC32C) {
return new MD5MD5CRC32CastagnoliFileChecksum(bytesPerCrc, crcPerBlock, md5);
} else {
Expand Down
10 changes: 10 additions & 0 deletions sdk/java/src/test/java/io/juicefs/JuiceFileSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ public void testChecksum() throws IOException {
assertEquals(new MD5MD5CRC32CastagnoliFileChecksum(512, 0, new MD5Hash("05a157db1cc7549c82ec6f31f63fdb46")),
sum);

f = new Path("/medium");
out = fs.create(f, true);
byte[] bytes = new byte[(128 << 20) - 1];
out.write(bytes);
out.close();
sum = fs.getFileChecksum(f);
assertEquals(
new MD5MD5CRC32CastagnoliFileChecksum(512, 0, new MD5Hash("1cf326bae8274fd824ec69ece3e4082f")),
sum);

f = new Path("/big");
out = fs.create(f, true);
byte[] zeros = new byte[1024 * 1000];
Expand Down