szetszwo commented on code in PR #1100:
URL: https://github.com/apache/ratis/pull/1100#discussion_r1613099528


##########
ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java:
##########
@@ -19,14 +19,26 @@
 
 import org.apache.ratis.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.proto.RaftProtos.TermIndexProto;
+import org.apache.ratis.thirdparty.com.google.common.cache.Cache;
+import org.apache.ratis.thirdparty.com.google.common.cache.CacheBuilder;
 
 import java.util.Comparator;
+import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
 /** The term and the log index defined in the Raft consensus algorithm. */
 public interface TermIndex extends Comparable<TermIndex> {
   TermIndex[] EMPTY_ARRAY = {};
 
+  /** Shared Guava Cache for TermIndex instances */
+  Cache<TermIndexKey, TermIndex> CACHE = CacheBuilder.newBuilder()
+      .maximumSize(10000)

Review Comment:
   Let's use a 64k buffer, i.e. `1 << 16`.



##########
ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndexImpl.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.server.protocol;
+
+public class TermIndexImpl implements TermIndex {

Review Comment:
   Just use the key.  This class is not needed.



##########
ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java:
##########
@@ -19,14 +19,26 @@
 
 import org.apache.ratis.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.proto.RaftProtos.TermIndexProto;
+import org.apache.ratis.thirdparty.com.google.common.cache.Cache;
+import org.apache.ratis.thirdparty.com.google.common.cache.CacheBuilder;
 
 import java.util.Comparator;
+import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
 /** The term and the log index defined in the Raft consensus algorithm. */
 public interface TermIndex extends Comparable<TermIndex> {
   TermIndex[] EMPTY_ARRAY = {};
 
+  /** Shared Guava Cache for TermIndex instances */
+  Cache<TermIndexKey, TermIndex> CACHE = CacheBuilder.newBuilder()
+      .maximumSize(10000)
+      .expireAfterAccess(10, TimeUnit.MINUTES)

Review Comment:
   How about 1min?



##########
ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java:
##########
@@ -60,43 +72,46 @@ static TermIndex valueOf(LogEntryProto proto) {
 
   /** @return a {@link TermIndex} object. */
   static TermIndex valueOf(long term, long index) {
-    return new TermIndex() {
-      @Override
-      public long getTerm() {
-        return term;
-      }
+    TermIndexKey key = new TermIndexKey(term, index);
+    try {
+      return CACHE.get(key, () -> new TermIndexImpl(term, index));
+    } catch (ExecutionException e) {
+      throw new IllegalStateException("Failed to valueOf(" + term + ", " + 
index + ")", e);
+    }
+  }
 
-      @Override
-      public long getIndex() {
-        return index;
-      }
+  class TermIndexKey {

Review Comment:
   Let's keep the anonymous class.



##########
ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java:
##########
@@ -60,43 +72,46 @@ static TermIndex valueOf(LogEntryProto proto) {
 
   /** @return a {@link TermIndex} object. */
   static TermIndex valueOf(long term, long index) {
-    return new TermIndex() {
-      @Override
-      public long getTerm() {
-        return term;
-      }
+    TermIndexKey key = new TermIndexKey(term, index);
+    try {
+      return CACHE.get(key, () -> new TermIndexImpl(term, index));
+    } catch (ExecutionException e) {
+      throw new IllegalStateException("Failed to valueOf(" + term + ", " + 
index + ")", e);
+    }
+  }
 
-      @Override
-      public long getIndex() {
-        return index;
-      }
+  class TermIndexKey {
+    private final long term;
+    private final long index;
 
-      @Override
-      public boolean equals(Object obj) {
-        if (obj == this) {
-          return true;
-        } else if (!(obj instanceof TermIndex)) {
-          return false;
-        }
-
-        final TermIndex that = (TermIndex) obj;
-        return this.getTerm() == that.getTerm()
-            && this.getIndex() == that.getIndex();
-      }
+    public TermIndexKey(long term, long index) {
+      this.term = term;
+      this.index = index;
+    }
 
-      @Override
-      public int hashCode() {
-        return Long.hashCode(term) ^ Long.hashCode(index);
-      }
+    public long getTerm() {
+      return term;
+    }
 
-      private String longToString(long n) {
-        return n >= 0L? String.valueOf(n) : "~";
-      }
+    public long getIndex() {
+      return index;
+    }
 
-      @Override
-      public String toString() {
-        return String.format("(t:%s, i:%s)", longToString(term), 
longToString(index));
+    @Override
+    public boolean equals(Object o) {
+      if (this == o) {
+        return true;
+      }
+      if (o == null || getClass() != o.getClass()) {
+        return false;
       }
-    };
+      TermIndexKey that = (TermIndexKey) o;
+      return term == that.term && index == that.index;
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(term, index);

Review Comment:
   Let's don't change the `hashCode()` and keep `toString()`; otherwise it 
becomes an incompatible change.



##########
ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java:
##########
@@ -19,14 +19,26 @@
 
 import org.apache.ratis.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.proto.RaftProtos.TermIndexProto;
+import org.apache.ratis.thirdparty.com.google.common.cache.Cache;
+import org.apache.ratis.thirdparty.com.google.common.cache.CacheBuilder;
 
 import java.util.Comparator;
+import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
 /** The term and the log index defined in the Raft consensus algorithm. */
 public interface TermIndex extends Comparable<TermIndex> {
   TermIndex[] EMPTY_ARRAY = {};
 
+  /** Shared Guava Cache for TermIndex instances */
+  Cache<TermIndexKey, TermIndex> CACHE = CacheBuilder.newBuilder()

Review Comment:
   Let's call it `PRIVATE_CACHE` and update the javadoc as below.
   ```java
     /** An LRU Cache for {@link TermIndex} instances */
     Cache<TermIndex, TermIndex> PRIVATE_CACHE = CacheBuilder.newBuilder()
   ```



-- 
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: issues-unsubscr...@ratis.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to