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

RATIS-2147. Md5 mismatch when snapshot install #1142

Merged
merged 8 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 5 additions & 3 deletions ratis-common/src/main/java/org/apache/ratis/io/MD5Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public class MD5Hash {
public static final int MD5_LEN = 16;

private static final ThreadLocal<MessageDigest> DIGESTER_FACTORY =
ThreadLocal.withInitial(() -> {
ThreadLocal.withInitial(MD5Hash::newDigester);

public static MessageDigest newDigester() {
try {
return MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
throw new IllegalStateException("Failed to create MessageDigest for MD5", e);
}
});
}

private byte[] digest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
Expand All @@ -63,7 +64,7 @@ public class SnapshotManager {
private final Supplier<File> snapshotDir;
private final Supplier<File> snapshotTmpDir;
private final Function<FileChunkProto, String> getRelativePath;
private final Supplier<MessageDigest> digester = JavaUtils.memoize(MD5Hash::getDigester);
private MessageDigest digester;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move it to a local variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explained it in the code snippet you provided, please take a look.


SnapshotManager(RaftPeerId selfId, Supplier<RaftStorageDirectory> dir, StateMachineStorage smStorage) {
this.selfId = selfId;
Expand All @@ -88,7 +89,7 @@ private FileChannel open(FileChunkProto chunk, File tmpSnapshotFile) throws IOEx
}
// create the temp snapshot file and put padding inside
out = FileUtils.newFileChannel(tmpSnapshotFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
digester.get().reset();
digester = MD5Hash.newDigester();
} else {
if (!exists) {
throw new FileNotFoundException("Chunk offset is non-zero but file is not found: " + tmpSnapshotFile
Expand All @@ -114,7 +115,6 @@ public void installSnapshot(InstallSnapshotRequestProto request, StateMachine st

// TODO: Make sure that subsequent requests for the same installSnapshot are coming in order,
// and are not lost when whole request cycle is done. Check requestId and requestIndex here

for (FileChunkProto chunk : snapshotChunkRequest.getFileChunksList()) {
SnapshotInfo pi = stateMachine.getLatestSnapshot();
if (pi != null && pi.getTermIndex().getIndex() >= lastIncludedIndex) {
Expand All @@ -128,7 +128,7 @@ public void installSnapshot(InstallSnapshotRequestProto request, StateMachine st

try (FileChannel out = open(chunk, tmpSnapshotFile)) {
final ByteBuffer data = chunk.getData().asReadOnlyByteBuffer();
digester.get().update(data.duplicate());
digester.update(data.duplicate());

int written = 0;
for(; data.remaining() > 0; ) {
Expand All @@ -144,7 +144,7 @@ public void installSnapshot(InstallSnapshotRequestProto request, StateMachine st
new MD5Hash(chunk.getFileDigest().toByteArray());
// calculate the checksum of the snapshot file and compare it with the
// file digest in the request
final MD5Hash digest = new MD5Hash(digester.get().digest());
final MD5Hash digest = new MD5Hash(digester.digest());
if (!digest.equals(expectedDigest)) {
LOG.warn("The snapshot md5 digest {} does not match expected {}",
digest, expectedDigest);
Expand Down
Loading