Skip to content

Commit

Permalink
move newDigest to MD5Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
133tosakarin committed Sep 13, 2024
1 parent 92b8447 commit 4cafe38
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 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 @@ -89,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);
renewMd5Digest();
digester = MD5Hash.newDigester();
} else {
if (!exists) {
throw new FileNotFoundException("Chunk offset is non-zero but file is not found: " + tmpSnapshotFile
Expand Down Expand Up @@ -172,14 +172,6 @@ public void installSnapshot(InstallSnapshotRequestProto request, StateMachine st
}
}

private void renewMd5Digest() throws IOException {
try {
digester = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IOException("could not find MD5 digest algorithm");
}
}

private static void rename(File tmpDir, File stateMachineDir) throws IOException {
LOG.info("Installed snapshot, renaming temporary dir {} to {}", tmpDir, stateMachineDir);

Expand Down

0 comments on commit 4cafe38

Please sign in to comment.