Skip to content

Commit

Permalink
RATIS-2147. Md5 mismatch when snapshot install (#1142)
Browse files Browse the repository at this point in the history
  • Loading branch information
133tosakarin authored Sep 14, 2024
1 parent 37386c4 commit 0c9e299
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
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 @@ -27,7 +27,6 @@
import org.apache.ratis.statemachine.StateMachine;
import org.apache.ratis.statemachine.StateMachineStorage;
import org.apache.ratis.util.FileUtils;
import org.apache.ratis.util.JavaUtils;
import org.apache.ratis.util.MD5FileUtil;
import org.apache.ratis.util.MemoizedSupplier;
import org.apache.ratis.util.Preconditions;
Expand Down Expand Up @@ -63,7 +62,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;

SnapshotManager(RaftPeerId selfId, Supplier<RaftStorageDirectory> dir, StateMachineStorage smStorage) {
this.selfId = selfId;
Expand All @@ -88,7 +87,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 +113,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 +126,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 +142,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

0 comments on commit 0c9e299

Please sign in to comment.