You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by ru...@apache.org on 2020/12/07 13:34:11 UTC

[incubator-ratis] branch master updated: RATIS-1215. Allow put duplicate key in LogSegment#entryCache (#333)

This is an automated email from the ASF dual-hosted git repository.

runzhiwang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 002b2ec  RATIS-1215. Allow put duplicate key in LogSegment#entryCache (#333)
002b2ec is described below

commit 002b2ec4d638d50d882eddfb8e3209abfba0dcc5
Author: runzhiwang <51...@users.noreply.github.com>
AuthorDate: Mon Dec 7 21:29:21 2020 +0800

    RATIS-1215. Allow put duplicate key in LogSegment#entryCache (#333)
---
 .../org/apache/ratis/server/raftlog/segmented/LogSegment.java     | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
index df3b6a3..0510b14 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
@@ -428,8 +428,12 @@ public class LogSegment implements Comparable<Long> {
 
   void putEntryCache(TermIndex key, LogEntryProto value, Op op) {
     final LogEntryProto previous = entryCache.put(key, value);
-    Preconditions.assertNull(previous, "entryCache shouldn't contains duplicated entry");
-    totalCacheSize.getAndAdd(getEntrySize(value, op));
+    long previousSize = 0;
+    if (previous != null) {
+      // Different threads maybe load LogSegment file into cache at the same time, so duplicate maybe happen
+      previousSize = getEntrySize(value, Op.REMOVE_CACHE);
+    }
+    totalCacheSize.getAndAdd(getEntrySize(value, op) - previousSize);
   }
 
   void removeEntryCache(TermIndex key, Op op) {