-
Notifications
You must be signed in to change notification settings - Fork 421
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-2158. Let the snapshot sender and receiver use a new digester each time #1151
RATIS-2158. Let the snapshot sender and receiver use a new digester each time #1151
Conversation
@szetszwo PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@133tosakarin , @OneSizeFitsQuorum , on a second thought, we may not need any synchronization. The code must be already synchronized. If not, two different threads accessing digester
can change order. Then the final digester.digest()
will be different.
The reason is similar to OutputStream
, whose subclasses (e.g. FileOutputStream
) usually are NOT thread-safe. If there is a need, the caller should synchronize it; see https://stackoverflow.com/questions/8422278/write-to-fileoutputstream-from-multiple-threads-in-java
If we really want to make a change, how about simply add volatile
?
@@ -87,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 = MD5Hash.newDigester(); | |||
digester.set(MD5Hash.getDigester()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@133tosakarin , why changing back to getDigester
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I mistakenly thought that every time I use DIGESTER_FACTORY (), newDigest () will be called, so I want to try changing it to getDigester ().
I have changed back to newDigester.
@@ -62,7 +63,7 @@ public class SnapshotManager { | |||
private final Supplier<File> snapshotDir; | |||
private final Supplier<File> snapshotTmpDir; | |||
private final Function<FileChunkProto, String> getRelativePath; | |||
private MessageDigest digester; | |||
private final AtomicReference<MessageDigest> digester; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about simply add a volatile
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -62,7 +63,7 @@ public class SnapshotManager { | |||
private final Supplier<File> snapshotDir; | |||
private final Supplier<File> snapshotTmpDir; | |||
private final Function<FileChunkProto, String> getRelativePath; | |||
private MessageDigest digester; | |||
private volatile AtomicReference<MessageDigest> digester; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@133tosakarin , sorry, I mean that don't use AtomicReference
, i.e.
private volatile MessageDigest digester;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 the change looks good.
Let's also update the title?
Hi, @szetszwo @133tosakarin ,
|
@OneSizeFitsQuorum , if the failure is related to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@133tosakarin , thanks for the update! Please see the comment inlined.
@@ -34,7 +34,9 @@ public class MD5Hash { | |||
|
|||
public static MessageDigest newDigester() { | |||
try { | |||
return MessageDigest.getInstance("MD5"); | |||
MessageDigest digester = MessageDigest.getInstance("MD5"); | |||
digester.reset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the API, MessageDigest.getInstance(..) returns a new object. So, reset()
is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 the change looks good.
see https://issues.apache.org/jira/browse/RATIS-2158