Skip to content

Commit

Permalink
Revert "RATIS-2099. Cache TermIndexImpl instead of using anonymous cl…
Browse files Browse the repository at this point in the history
…ass (apache#1100)"

This reverts commit 428ce4a.
  • Loading branch information
szetszwo committed Aug 26, 2024
1 parent da5d508 commit 520ecab
Showing 1 changed file with 2 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@

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> {
/** An LRU Cache for {@link TermIndex} instances */
Cache<TermIndex, TermIndex> PRIVATE_CACHE = CacheBuilder.newBuilder()
.maximumSize(1 << 16)
.expireAfterAccess(1, TimeUnit.MINUTES)
.build();
TermIndex[] EMPTY_ARRAY = {};

/** @return the term. */
long getTerm();
Expand Down Expand Up @@ -68,7 +60,7 @@ static TermIndex valueOf(LogEntryProto proto) {

/** @return a {@link TermIndex} object. */
static TermIndex valueOf(long term, long index) {
final TermIndex key = new TermIndex() {
return new TermIndex() {
@Override
public long getTerm() {
return term;
Expand Down Expand Up @@ -106,10 +98,5 @@ public String toString() {
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);
}
}
}

0 comments on commit 520ecab

Please sign in to comment.