duongkame commented on code in PR #998:
URL: https://github.com/apache/ratis/pull/998#discussion_r1443335072


##########
ratis-test/src/test/java/org/apache/ratis/server/raftlog/segmented/TestLogSegment.java:
##########
@@ -235,18 +238,21 @@ public void testAppendWithGap() throws Exception {
     final StateMachineLogEntryProto m = op.getLogEntryContent();
     try {
       LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1001);
-      segment.appendToOpenSegment(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+      segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry)
+      );

Review Comment:
   ```suggestion
         
segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry));
   ```



##########
ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLog.java:
##########
@@ -421,14 +423,17 @@ protected CompletableFuture<Long> 
appendEntryImpl(LogEntryProto entry, Transacti
       // If the entry has state machine data, then the entry should be inserted
       // to statemachine first and then to the cache. Not following the order
       // will leave a spurious entry in the cache.
-      CompletableFuture<Long> writeFuture =
-          fileLogWorker.writeLogEntry(entry, context).getFuture();
+      final CompletableFuture<Long> writeFuture = 
fileLogWorker.writeLogEntry(entry, context).getFuture();
+      final Function<LogEntryProto, ReferenceCountedObject<LogEntryProto>> 
wrap = context != null?

Review Comment:
   ```suggestion
         final Function<LogEntryProto, ReferenceCountedObject<LogEntryProto>> 
wrap = context != null ?
   ```



##########
ratis-test/src/test/java/org/apache/ratis/server/raftlog/segmented/TestLogSegment.java:
##########
@@ -261,7 +267,8 @@ public void testTruncate() throws Exception {
     for (int i = 0; i < 100; i++) {
       LogEntryProto entry = LogProtoUtils.toLogEntryProto(
           new SimpleOperation("m" + i).getLogEntryContent(), term, i + start);
-      segment.appendToOpenSegment(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+      segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry)
+      );

Review Comment:
   ```suggestion
         
segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry));
   ```



##########
ratis-test/src/test/java/org/apache/ratis/server/raftlog/segmented/TestLogSegment.java:
##########
@@ -202,8 +204,9 @@ public void testAppendEntries() throws Exception {
     while (size < max) {
       SimpleOperation op = new SimpleOperation("m" + i);
       LogEntryProto entry = 
LogProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i++ + start);
-      size += getEntrySize(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
-      segment.appendToOpenSegment(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+      size += getEntrySize(entry, Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+      segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry)
+      );

Review Comment:
   ```suggestion
         
segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry));
   ```



##########
ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java:
##########
@@ -246,13 +247,48 @@ public LogEntryProto load(LogRecord key) throws 
IOException {
     }
   }
 
+  static class EntryCache {
+    private final Map<TermIndex, ReferenceCountedObject<LogEntryProto>> map = 
new ConcurrentHashMap<>();
+    private final AtomicLong size = new AtomicLong();
+
+    long size() {
+      return size.get();
+    }
+
+    LogEntryProto get(TermIndex ti) {
+      return Optional.ofNullable(map.get(ti))
+          .map(ReferenceCountedObject::get)
+          .orElse(null);
+    }
+
+    void clear() {
+      map.values().forEach(ReferenceCountedObject::release);
+      map.clear();
+      size.set(0);
+    }
+
+    void put(TermIndex key, ReferenceCountedObject<LogEntryProto> valueRef, Op 
op) {
+      valueRef.retain();
+      Optional.ofNullable(map.put(key, valueRef)).ifPresent(this::release);
+      size.getAndAdd(getEntrySize(valueRef.get(), op));
+    }
+
+    private void release(ReferenceCountedObject<LogEntryProto> entry) {
+      size.getAndAdd(-getEntrySize(entry.get(), Op.REMOVE_CACHE));
+      entry.release();

Review Comment:
   `getEntrySize` may result in different results based on `op`. If `put` and 
`release` use different `op`, may the `size` calculation be wrong?



##########
ratis-test/src/test/java/org/apache/ratis/server/raftlog/segmented/TestLogSegment.java:
##########
@@ -235,18 +238,21 @@ public void testAppendWithGap() throws Exception {
     final StateMachineLogEntryProto m = op.getLogEntryContent();
     try {
       LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1001);
-      segment.appendToOpenSegment(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+      segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry)
+      );
       Assert.fail("should fail since the entry's index needs to be 1000");
     } catch (IllegalStateException e) {
       // the exception is expected.
     }
 
     LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1000);
-    segment.appendToOpenSegment(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+    segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry)
+    );
 
     try {
       entry = LogProtoUtils.toLogEntryProto(m, 0, 1002);
-      segment.appendToOpenSegment(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+      segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry)
+      );

Review Comment:
   ```suggestion
         
segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry));
   ```



##########
ratis-test/src/test/java/org/apache/ratis/server/raftlog/segmented/TestLogSegment.java:
##########
@@ -235,18 +238,21 @@ public void testAppendWithGap() throws Exception {
     final StateMachineLogEntryProto m = op.getLogEntryContent();
     try {
       LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1001);
-      segment.appendToOpenSegment(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+      segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry)
+      );
       Assert.fail("should fail since the entry's index needs to be 1000");
     } catch (IllegalStateException e) {
       // the exception is expected.
     }
 
     LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1000);
-    segment.appendToOpenSegment(entry, 
LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
+    segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry)
+    );

Review Comment:
   ```suggestion
       segment.appendToOpenSegment(Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE, 
ReferenceCountedObject.wrap(entry));
   ```



-- 
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]

Reply via email to