Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ subprojects {
configurations.all {
resolutionStrategy {
force group: 'com.google.guava', name: 'guava', version: '30.1-jre'
force group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.9.3'
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ dependencies {
compile 'org.aspectj:aspectjrt:1.8.13'
compile 'org.aspectj:aspectjweaver:1.8.13'
compile 'org.aspectj:aspectjtools:1.8.13'
compile group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.9.3'

compile group: 'io.github.tronprotocol', name: 'libp2p', version: '2.1.0',{
exclude group: 'io.grpc', module: 'grpc-context'
exclude group: 'io.grpc', module: 'grpc-core'
Expand Down
80 changes: 80 additions & 0 deletions common/src/main/java/org/tron/common/cache/CommonCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.tron.common.cache;

import com.google.errorprone.annotations.CompatibleWith;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.parameter.CommonParameter;

import java.util.concurrent.ConcurrentMap;

@Slf4j(topic = "app")
public class CommonCache<K, V> {

private com.github.benmanes.caffeine.cache.Cache caffeineCache;
private com.google.common.cache.Cache guavaCache;
private boolean isCaffeine = CommonParameter.getInstance().caffeineCacheActive;

public CommonCache(com.github.benmanes.caffeine.cache.Cache caffeineCache,
com.google.common.cache.Cache guavaCache) {
if (isCaffeine) {
this.caffeineCache = caffeineCache;
this.guavaCache = null;
} else {
this.caffeineCache = null;
this.guavaCache = guavaCache;
}
}

public V getIfPresent(@CompatibleWith("K") Object key) {
if (isCaffeine) {
logger.info("isCaffeine get {}", caffeineCache.getIfPresent(key) == null?" null":" not null");
return (V)caffeineCache.getIfPresent(key);
} else {
logger.info("isGuava get {}", guavaCache.getIfPresent(key) == null?" null":" not null");
return (V)guavaCache.getIfPresent(key);
}
}

public void put(K key, V value) {
if (isCaffeine) {
logger.info("isCaffeine put");
caffeineCache.put(key, value);
} else {
logger.info("isGuava put");
guavaCache.put(key, value);
}
}

public void invalidate(K key) {
if (isCaffeine) {
caffeineCache.invalidate(key);
} else {
guavaCache.invalidate(key);
}
}

public ConcurrentMap<K, V> asMap() {
if (isCaffeine) {
return caffeineCache.asMap();
} else {
return guavaCache.asMap();
}
}


public void cleanUp() {
if (isCaffeine) {
caffeineCache.cleanUp();
} else {
guavaCache.cleanUp();
}
}

public long size() {
if (isCaffeine) {
return caffeineCache.estimatedSize();
} else {
return guavaCache.size();
}
}

}
102 changes: 102 additions & 0 deletions common/src/main/java/org/tron/common/cache/CommonCacheBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.tron.common.cache;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.cache.CacheBuilder;
import org.tron.common.parameter.CommonParameter;

import java.util.concurrent.TimeUnit;

public class CommonCacheBuilder<K, V> {
private boolean isCaffeine = CommonParameter.getInstance().caffeineCacheActive;
private int initialCapacity = 0;
private long maximumSize = 0L;
private long expireAfterWrite = 0L;
private TimeUnit timeUnit;
private boolean recordStats = false;

public CommonCacheBuilder initialCapacity(int initialCapacity) {
this.initialCapacity = initialCapacity;
return this;
}

public CommonCacheBuilder maximumSize(long maximumSize) {
this.maximumSize = maximumSize;
return this;
}

public CommonCacheBuilder expireAfterWrite(long expireAfterWrite,
TimeUnit timeUnit) {
this.expireAfterWrite = expireAfterWrite;
this.timeUnit = timeUnit;
return this;
}

public CommonCacheBuilder recordStats() {
this.recordStats = true;
return this;
}

public CommonCache<K, V> build() {
//check
if (isCaffeine) {
com.github.benmanes.caffeine.cache.Caffeine caffeine = initCaffeineCacheBuilder(
initialCapacity, maximumSize, expireAfterWrite, timeUnit, recordStats
);
com.github.benmanes.caffeine.cache.Cache caffeineCache = caffeine.build();
return new CommonCache(caffeineCache, null);
} else {
com.google.common.cache.CacheBuilder cacheBuilder = initGuavaCacheBuilder(
initialCapacity, maximumSize, expireAfterWrite, timeUnit, recordStats
);
com.google.common.cache.Cache guavaCache = cacheBuilder.build();
return new CommonCache(null, guavaCache);
}
}


private com.google.common.cache.CacheBuilder initGuavaCacheBuilder(int initialCapacity
, long maximumSize
, long expireAfterWrite
, TimeUnit timeUnit
, boolean recordStats) {
com.google.common.cache.CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
if (initialCapacity > 0) {
cacheBuilder = cacheBuilder.initialCapacity(initialCapacity);
}
if (maximumSize > 0) {
cacheBuilder = cacheBuilder.maximumSize(maximumSize);
}
if (expireAfterWrite > 0) {
cacheBuilder = cacheBuilder.expireAfterWrite(expireAfterWrite, timeUnit);
}
if (recordStats) {
cacheBuilder = cacheBuilder.recordStats();
}
return cacheBuilder;
}

private com.github.benmanes.caffeine.cache.Caffeine initCaffeineCacheBuilder(int initialCapacity
, long maximumSize
, long expireAfterWrite
, TimeUnit timeUnit
, boolean recordStats) {
com.github.benmanes.caffeine.cache.Caffeine caffeine = Caffeine.newBuilder();
if (initialCapacity > 0) {
caffeine = caffeine.initialCapacity(initialCapacity);
}
if (maximumSize > 0) {
caffeine = caffeine.maximumSize(maximumSize);
}
if (expireAfterWrite > 0) {
caffeine = caffeine.expireAfterWrite(expireAfterWrite, timeUnit);
}
if (recordStats) {
caffeine = caffeine.recordStats();
}
return caffeine;
}

public static CommonCacheBuilder<Object, Object> newBuilder() {
return new CommonCacheBuilder<>();
}
}
26 changes: 26 additions & 0 deletions common/src/main/java/org/tron/common/cache/CommonCacheStat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.tron.common.cache;

import com.google.common.cache.CacheStats;
import lombok.Getter;

public class CommonCacheStat {
@Getter
private CacheStats guavaCacheStats;
@Getter
private com.github.benmanes.caffeine.cache.stats.CacheStats caffeineCacheStats;
public CommonCacheStat(CacheStats guavaCacheStats,
com.github.benmanes.caffeine.cache.stats.CacheStats caffeineCacheStats) {
this.guavaCacheStats = guavaCacheStats;
this.caffeineCacheStats = caffeineCacheStats;
}


public static CacheStats castCaffeineToGuavaStat(com.github.benmanes.caffeine.cache.stats.CacheStats caffeineCacheStats) {
CacheStats guavaCacheStats = new CacheStats(
caffeineCacheStats.hitCount(), caffeineCacheStats.missCount(),
caffeineCacheStats.loadSuccessCount(), caffeineCacheStats.loadFailureCount(),
caffeineCacheStats.totalLoadTime(), caffeineCacheStats.evictionCount()
);
return guavaCacheStats;
}
}
61 changes: 55 additions & 6 deletions common/src/main/java/org/tron/common/cache/TronCache.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,90 @@
package org.tron.common.cache;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.base.Objects;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.parameter.CommonParameter;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

import lombok.Getter;

@Slf4j(topic = "app")
public class TronCache<K, V> {

@Getter
private final CacheType name;
private final Cache<K, V> cache;
private final com.github.benmanes.caffeine.cache.Cache<K, V> caffeineCache;
private boolean isCaffeine = CommonParameter.getInstance().caffeineCacheActive;

TronCache(CacheType name, String strategy) {
this.name = name;
this.cache = CacheBuilder.from(strategy).build();
if (isCaffeine) {
this.cache = null;
strategy = castGuavaSpec2Caffeine(strategy);
this.caffeineCache = Caffeine.from(strategy).build();
} else {
this.cache = CacheBuilder.from(strategy).build();
this.caffeineCache = null;
}
}

TronCache(CacheType name, String strategy, CacheLoader<K, V> loader) {
this.name = name;
this.caffeineCache = null;
this.cache = CacheBuilder.from(strategy).build(loader);
}

public void put(K k, V v) {
this.cache.put(k, v);
if(isCaffeine) {
logger.info("isCaffeine put");
this.caffeineCache.put(k, v);
} else {
logger.info("isGuava put");
this.cache.put(k, v);
}
}

public V getIfPresent(K k) {
return this.cache.getIfPresent(k);
if(isCaffeine) {
logger.info("isCaffeine {}", this.caffeineCache.getIfPresent(k) == null?" null":" not null");
return this.caffeineCache.getIfPresent(k);
} else {
logger.info("isGuava {}", this.cache.getIfPresent(k) == null?" null":" not null");
return this.cache.getIfPresent(k);
}
}

public V get(K k, Callable<? extends V> loader) throws ExecutionException {
return this.cache.get(k, loader);
if(isCaffeine) {
logger.info("isCaffeine {}", this.caffeineCache.get(k, (Function<? super K, ? extends V>) loader) == null?" null":" not null");
return this.caffeineCache.get(k, (Function<? super K, ? extends V>) loader);
} else {
logger.info("isGuava {}", this.cache.get(k, loader) == null?" null":" not null");
return this.cache.get(k, loader);
}
}

public CacheStats stats() {
return this.cache.stats();
if(isCaffeine){
return CommonCacheStat.castCaffeineToGuavaStat(this.caffeineCache.stats());
} else {
return this.cache.stats();
}
}

public void invalidateAll() {
this.cache.invalidateAll();
if (isCaffeine) {
this.caffeineCache.invalidateAll();
} else {
this.cache.invalidateAll();
}
}

@Override
Expand All @@ -61,4 +103,11 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hashCode(name);
}

private static String castGuavaSpec2Caffeine(String guavaSpec) {
int beforeConcurrencyLevel = guavaSpec.indexOf(",concurrencyLevel");
int beforeRecordStats = guavaSpec.indexOf(",recordStats");
String ret = guavaSpec.substring(0, beforeConcurrencyLevel) + guavaSpec.substring(beforeRecordStats, guavaSpec.length());
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ public class CommonParameter {
@Setter
public long allowCancelAllUnfreezeV2;

@Getter
@Setter
public boolean caffeineCacheActive;

private static double calcMaxTimeRatio() {
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
return 5.0;
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,6 @@ public class Constant {
public static final String DYNAMIC_CONFIG_CHECK_INTERVAL = "node.dynamicConfig.checkInterval";

public static final String COMMITTEE_ALLOW_TVM_SHANGHAI = "committee.allowTvmShangHai";

public static final String NODE_CACHE_CAFFEINE_ACTIVE = "node.cache.caffeine.active";
}
Loading