This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git
The following commit(s) were added to refs/heads/master by this push:
new 1782cd9f1 RATIS-2099. Cache TermIndexImpl instead of using anonymous
class (#1100)
1782cd9f1 is described below
commit 1782cd9f171519c8fd9006f24cbfe6fab91a1daf
Author: Symious <[email protected]>
AuthorDate: Fri May 24 17:51:52 2024 +0800
RATIS-2099. Cache TermIndexImpl instead of using anonymous class (#1100)
---
.../org/apache/ratis/server/protocol/TermIndex.java | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git
a/ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java
b/ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java
index 7def686bb..6a9bd1cfb 100644
---
a/ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java
+++
b/ratis-server-api/src/main/java/org/apache/ratis/server/protocol/TermIndex.java
@@ -19,13 +19,21 @@ package org.apache.ratis.server.protocol;
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.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 = {};
+ /** An LRU Cache for {@link TermIndex} instances */
+ Cache<TermIndex, TermIndex> PRIVATE_CACHE = CacheBuilder.newBuilder()
+ .maximumSize(1 << 16)
+ .expireAfterAccess(1, TimeUnit.MINUTES)
+ .build();
/** @return the term. */
long getTerm();
@@ -60,7 +68,7 @@ public interface TermIndex extends Comparable<TermIndex> {
/** @return a {@link TermIndex} object. */
static TermIndex valueOf(long term, long index) {
- return new TermIndex() {
+ final TermIndex key = new TermIndex() {
@Override
public long getTerm() {
return term;
@@ -98,5 +106,10 @@ public interface TermIndex extends Comparable<TermIndex> {
return String.format("(t:%s, i:%s)", longToString(term),
longToString(index));
}
};
+ try {
+ return PRIVATE_CACHE.get(key, () -> key);
+ } catch (ExecutionException e) {
+ throw new IllegalStateException("Failed to valueOf(" + term + ", " +
index + "), key=" + key, e);
+ }
}
}
\ No newline at end of file