Expire UserMap cache more aggressively and add cache debugging (#5331)

This commit is contained in:
pop4959 2023-05-05 15:44:22 -07:00 committed by GitHub
parent 8194d1f747
commit 991bc61b0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View File

@ -282,6 +282,8 @@ public interface ISettings extends IConf {
int getMaxUserCacheCount(); int getMaxUserCacheCount();
long getMaxUserCacheValueExpiry();
boolean allowSilentJoinQuit(); boolean allowSilentJoinQuit();
boolean isCustomJoinMessage(); boolean isCustomJoinMessage();

View File

@ -1469,10 +1469,16 @@ public class Settings implements net.ess3.api.ISettings {
// #easteregg // #easteregg
@Override @Override
public int getMaxUserCacheCount() { public int getMaxUserCacheCount() {
final long count = Runtime.getRuntime().maxMemory() / 1024 / 96; final long count = Runtime.getRuntime().maxMemory() / 1024 / 1024;
return config.getInt("max-user-cache-count", (int) count); return config.getInt("max-user-cache-count", (int) count);
} }
// #easteregg
@Override
public long getMaxUserCacheValueExpiry() {
return config.getLong("max-user-cache-value-expiry", 600);
}
@Override @Override
public boolean isLastMessageReplyRecipient() { public boolean isLastMessageReplyRecipient() {
return config.getBoolean("last-message-reply-recipient", false); return config.getBoolean("last-message-reply-recipient", false);

View File

@ -16,6 +16,7 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level; import java.util.logging.Level;
@ -26,6 +27,7 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
private final boolean debugPrintStackWithWarn; private final boolean debugPrintStackWithWarn;
private final long debugMaxWarnsPerType; private final long debugMaxWarnsPerType;
private final boolean debugLogCache;
private final ConcurrentMap<String, AtomicLong> debugNonPlayerWarnCounts; private final ConcurrentMap<String, AtomicLong> debugNonPlayerWarnCounts;
public ModernUserMap(final IEssentials ess) { public ModernUserMap(final IEssentials ess) {
@ -33,6 +35,7 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
this.uuidCache = new ModernUUIDCache(ess); this.uuidCache = new ModernUUIDCache(ess);
this.userCache = CacheBuilder.newBuilder() this.userCache = CacheBuilder.newBuilder()
.maximumSize(ess.getSettings().getMaxUserCacheCount()) .maximumSize(ess.getSettings().getMaxUserCacheCount())
.expireAfterAccess(ess.getSettings().getMaxUserCacheValueExpiry(), TimeUnit.SECONDS)
.softValues() .softValues()
.build(this); .build(this);
@ -40,9 +43,12 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
final String printStackProperty = System.getProperty("net.essentialsx.usermap.print-stack", "false"); final String printStackProperty = System.getProperty("net.essentialsx.usermap.print-stack", "false");
// -Dnet.essentialsx.usermap.max-warns=20 // -Dnet.essentialsx.usermap.max-warns=20
final String maxWarnProperty = System.getProperty("net.essentialsx.usermap.max-warns", "100"); final String maxWarnProperty = System.getProperty("net.essentialsx.usermap.max-warns", "100");
// -Dnet.essentialsx.usermap.log-cache=true
final String logCacheProperty = System.getProperty("net.essentialsx.usermap.log-cache", "false");
this.debugMaxWarnsPerType = NumberUtil.isLong(maxWarnProperty) ? Long.parseLong(maxWarnProperty) : -1; this.debugMaxWarnsPerType = NumberUtil.isLong(maxWarnProperty) ? Long.parseLong(maxWarnProperty) : -1;
this.debugPrintStackWithWarn = Boolean.parseBoolean(printStackProperty); this.debugPrintStackWithWarn = Boolean.parseBoolean(printStackProperty);
this.debugLogCache = Boolean.parseBoolean(logCacheProperty);
this.debugNonPlayerWarnCounts = new ConcurrentHashMap<>(); this.debugNonPlayerWarnCounts = new ConcurrentHashMap<>();
} }
@ -81,6 +87,7 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
public User getUser(final Player base) { public User getUser(final Player base) {
final User user = loadUncachedUser(base); final User user = loadUncachedUser(base);
userCache.put(user.getUUID(), user); userCache.put(user.getUUID(), user);
debugLogCache(user);
return user; return user;
} }
@ -114,6 +121,7 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
public User load(final UUID uuid) throws Exception { public User load(final UUID uuid) throws Exception {
final User user = loadUncachedUser(uuid); final User user = loadUncachedUser(uuid);
if (user != null) { if (user != null) {
debugLogCache(user);
return user; return user;
} }
@ -168,6 +176,7 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
public void addCachedUser(final User user) { public void addCachedUser(final User user) {
userCache.put(user.getUUID(), user); userCache.put(user.getUUID(), user);
debugLogCache(user);
} }
@Override @Override
@ -196,6 +205,14 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
uuidCache.shutdown(); uuidCache.shutdown();
} }
private void debugLogCache(final User user) {
if (!debugLogCache) {
return;
}
final Throwable throwable = new Throwable();
ess.getLogger().log(Level.INFO, String.format("Caching user %s (%s)", user.getName(), user.getUUID()), throwable);
}
private void debugLogUncachedNonPlayer(final Player base) { private void debugLogUncachedNonPlayer(final Player base) {
final String typeName = base.getClass().getName(); final String typeName = base.getClass().getName();
final long count = debugNonPlayerWarnCounts.computeIfAbsent(typeName, name -> new AtomicLong(0)).getAndIncrement(); final long count = debugNonPlayerWarnCounts.computeIfAbsent(typeName, name -> new AtomicLong(0)).getAndIncrement();