szetszwo commented on code in PR #986:
URL: https://github.com/apache/ratis/pull/986#discussion_r1424600259
##########
ratis-common/src/main/java/org/apache/ratis/protocol/RaftPeerId.java:
##########
@@ -40,7 +40,8 @@ public final class RaftPeerId {
private static final Map<String, RaftPeerId> STRING_MAP = new
ConcurrentHashMap<>();
public static RaftPeerId valueOf(ByteString id) {
- return BYTE_STRING_MAP.computeIfAbsent(id, RaftPeerId::new);
+ ByteString cloned = ByteString.copyFrom(id.asReadOnlyByteBuffer());
Review Comment:
Let's check the map before copying.
```java
final RaftPeerId cached = BYTE_STRING_MAP.get(id);
if (cached != null) {
return cached;
}
```
##########
ratis-server/src/main/java/org/apache/ratis/server/impl/CommitInfoCache.java:
##########
@@ -42,8 +42,9 @@ CommitInfoProto update(RaftPeer peer, long newCommitIndex) {
}
CommitInfoProto update(CommitInfoProto newInfo) {
+ CommitInfoProto cloned = ProtoUtils.copyFrom(newInfo);
return map.compute(RaftPeerId.valueOf(newInfo.getServer().getId()),
- (id, old) -> old == null || newInfo.getCommitIndex() >
old.getCommitIndex()? newInfo: old);
+ (id, old) -> old == null || cloned.getCommitIndex() >
old.getCommitIndex()? cloned: old);
}
Review Comment:
Let's change `CommitInfoCache` separately. If we are going to copy the
proto every time, it probably is better to change the map to
`ConcurrentMap<RaftPeerId, Long>`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]