Removed DataContainerCache due to its memory impact vs cpu gain being too high

This commit is contained in:
Rsl1122 2018-08-15 10:17:16 +03:00
parent 782e3a07ed
commit 497b37aa3b
7 changed files with 7 additions and 126 deletions

View File

@ -7,9 +7,6 @@ package com.djrapitops.plan.system.cache;
import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.SubSystem; import com.djrapitops.plan.system.SubSystem;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
/** /**
@ -21,7 +18,6 @@ public class CacheSystem implements SubSystem {
private final DataCache dataCache; private final DataCache dataCache;
private final GeolocationCache geolocationCache; private final GeolocationCache geolocationCache;
private final DataContainerCache dataContainerCache;
public CacheSystem(PlanSystem system) { public CacheSystem(PlanSystem system) {
this(new DataCache(system), system); this(new DataCache(system), system);
@ -30,7 +26,6 @@ public class CacheSystem implements SubSystem {
protected CacheSystem(DataCache dataCache, PlanSystem system) { protected CacheSystem(DataCache dataCache, PlanSystem system) {
this.dataCache = dataCache; this.dataCache = dataCache;
geolocationCache = new GeolocationCache(() -> system.getLocaleSystem().getLocale()); geolocationCache = new GeolocationCache(() -> system.getLocaleSystem().getLocale());
dataContainerCache = new DataContainerCache();
} }
public static CacheSystem getInstance() { public static CacheSystem getInstance() {
@ -43,19 +38,11 @@ public class CacheSystem implements SubSystem {
public void enable() throws EnableException { public void enable() throws EnableException {
dataCache.enable(); dataCache.enable();
geolocationCache.enable(); geolocationCache.enable();
RunnableFactory.createNew("DataContainer cache clean task", new AbsRunnable() {
@Override
public void run() {
dataContainerCache.clear();
}
}).runTaskTimerAsynchronously(TimeAmount.MINUTE.ticks(), TimeAmount.MINUTE.ms());
} }
@Override @Override
public void disable() { public void disable() {
geolocationCache.clearCache(); geolocationCache.clearCache();
dataContainerCache.clear();
} }
public DataCache getDataCache() { public DataCache getDataCache() {
@ -66,7 +53,4 @@ public class CacheSystem implements SubSystem {
return geolocationCache; return geolocationCache;
} }
public DataContainerCache getDataContainerCache() {
return dataContainerCache;
}
} }

View File

@ -1,67 +0,0 @@
package com.djrapitops.plan.system.cache;
import com.djrapitops.plan.data.store.Key;
import com.djrapitops.plan.data.store.containers.AnalysisContainer;
import com.djrapitops.plan.data.store.containers.DataContainer;
import com.djrapitops.plan.data.store.containers.PlayerContainer;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plugin.api.TimeAmount;
import java.util.UUID;
import java.util.function.Supplier;
/**
* Cache to aid Bungee in case SQL is causing cpu thread starvation, leading to mysql connection drops.
*
* @author Rsl1122
*/
public class DataContainerCache extends DataContainer {
public DataContainerCache() {
super(TimeAmount.SECOND.ms() * 10L);
}
public PlayerContainer getPlayerContainer(UUID uuid) {
return getAndCacheSupplier(Keys.playerContainer(uuid), Suppliers.playerContainer(uuid));
}
public AnalysisContainer getAnalysisContainer(UUID serverUUID) {
return getAndCacheSupplier(Keys.analysisContainer(serverUUID), Suppliers.analysisContainer(serverUUID));
}
private <T> T getAndCacheSupplier(Key<T> key, Supplier<T> ifNotPresent) {
if (!supports(key)) {
putSupplier(key, ifNotPresent);
}
return getUnsafe(key);
}
private static class Keys {
static Key<AnalysisContainer> analysisContainer(UUID serverUUID) {
return new Key<>(AnalysisContainer.class, "ANALYSIS_CONTAINER:" + serverUUID);
}
static Key<PlayerContainer> playerContainer(UUID uuid) {
return new Key<>(PlayerContainer.class, "PLAYER_CONTAINER:" + uuid);
}
private Keys() {
// Static utility class
}
}
private static class Suppliers {
static Supplier<AnalysisContainer> analysisContainer(UUID serverUUID) {
return () -> new AnalysisContainer(Database.getActive().fetch().getServerContainer(serverUUID));
}
static Supplier<PlayerContainer> playerContainer(UUID uuid) {
return () -> Database.getActive().fetch().getPlayerContainer(uuid);
}
private Suppliers() {
// Static utility class
}
}
}

View File

@ -9,7 +9,7 @@ import com.djrapitops.plan.api.exceptions.connection.InternalErrorException;
import com.djrapitops.plan.api.exceptions.connection.WebException; import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.database.DBOpException; import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.data.store.containers.AnalysisContainer; import com.djrapitops.plan.data.store.containers.AnalysisContainer;
import com.djrapitops.plan.system.cache.CacheSystem; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.InfoSystem; import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.response.DefaultResponses; import com.djrapitops.plan.system.webserver.response.DefaultResponses;
@ -79,7 +79,7 @@ public class GenerateAnalysisPageRequest extends InfoRequestWithVariables implem
try { try {
runningAnalysis = true; runningAnalysis = true;
UUID serverUUID = ServerInfo.getServerUUID(); UUID serverUUID = ServerInfo.getServerUUID();
AnalysisContainer analysisContainer = CacheSystem.getInstance().getDataContainerCache().getAnalysisContainer(serverUUID); AnalysisContainer analysisContainer = new AnalysisContainer(Database.getActive().fetch().getServerContainer(serverUUID));
return new AnalysisPage(analysisContainer).toHtml(); return new AnalysisPage(analysisContainer).toHtml();
} catch (DBOpException e) { } catch (DBOpException e) {
if (!e.getCause().getMessage().contains("Connection is closed")) { if (!e.getCause().getMessage().contains("Connection is closed")) {

View File

@ -1,6 +1,6 @@
package com.djrapitops.plan.system.webserver.response.pages; package com.djrapitops.plan.system.webserver.response.pages;
import com.djrapitops.plan.system.cache.CacheSystem; import com.djrapitops.plan.system.database.databases.Database;
import java.util.UUID; import java.util.UUID;
@ -12,6 +12,6 @@ import java.util.UUID;
public class RawPlayerDataResponse extends RawDataResponse { public class RawPlayerDataResponse extends RawDataResponse {
public RawPlayerDataResponse(UUID uuid) { public RawPlayerDataResponse(UUID uuid) {
super(CacheSystem.getInstance().getDataContainerCache().getPlayerContainer(uuid)); super(Database.getActive().fetch().getPlayerContainer(uuid));
} }
} }

View File

@ -1,6 +1,6 @@
package com.djrapitops.plan.system.webserver.response.pages; package com.djrapitops.plan.system.webserver.response.pages;
import com.djrapitops.plan.system.cache.CacheSystem; import com.djrapitops.plan.system.database.databases.Database;
import java.util.UUID; import java.util.UUID;
@ -12,6 +12,6 @@ import java.util.UUID;
public class RawServerDataResponse extends RawDataResponse { public class RawServerDataResponse extends RawDataResponse {
public RawServerDataResponse(UUID serverUUID) { public RawServerDataResponse(UUID serverUUID) {
super(CacheSystem.getInstance().getDataContainerCache().getAnalysisContainer(serverUUID).getServerContainer()); super(Database.getActive().fetch().getServerContainer(serverUUID));
} }
} }

View File

@ -2,13 +2,10 @@ package com.djrapitops.plan.utilities.html.pages;
import com.djrapitops.plan.PlanPlugin; import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.data.container.Session; import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.CachingSupplier;
import com.djrapitops.plan.data.store.Key;
import com.djrapitops.plan.data.store.keys.SessionKeys; import com.djrapitops.plan.data.store.keys.SessionKeys;
import com.djrapitops.plan.data.store.mutators.formatting.Formatter; import com.djrapitops.plan.data.store.mutators.formatting.Formatter;
import com.djrapitops.plan.data.store.mutators.formatting.Formatters; import com.djrapitops.plan.data.store.mutators.formatting.Formatters;
import com.djrapitops.plan.data.store.objects.DateHolder; import com.djrapitops.plan.data.store.objects.DateHolder;
import com.djrapitops.plan.system.cache.CacheSystem;
import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.connection.ConnectionLog; import com.djrapitops.plan.system.info.connection.ConnectionLog;
@ -32,7 +29,6 @@ import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean; import java.lang.management.RuntimeMXBean;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.*; import java.util.*;
import java.util.function.Supplier;
/** /**
* Html parsing for the Debug page. * Html parsing for the Debug page.
@ -68,7 +64,6 @@ public class DebugPage implements Page {
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
appendResponseCache(content); appendResponseCache(content);
appendSessionCache(content); appendSessionCache(content);
appendDataContainerCache(content);
return content.toString(); return content.toString();
} }
@ -110,36 +105,6 @@ public class DebugPage implements Page {
} }
} }
private void appendDataContainerCache(StringBuilder content) {
try {
content.append("<pre>### DataContainer Cache:<br><br>");
content.append("Key | Is Cached | Cache Time <br>")
.append("-- | -- | -- <br>");
Formatter<Long> timeStamp = Formatters.yearLongValue();
Set<Map.Entry<Key, Supplier>> dataContainers = CacheSystem.getInstance().getDataContainerCache().getMap().entrySet();
if (dataContainers.isEmpty()) {
content.append("Empty");
}
for (Map.Entry<Key, Supplier> entry : dataContainers) {
String keyName = entry.getKey().getKeyName();
Supplier supplier = entry.getValue();
if (supplier instanceof CachingSupplier) {
CachingSupplier cachingSupplier = (CachingSupplier) supplier;
boolean isCached = cachingSupplier.isCached();
String cacheText = isCached ? "Yes" : "No";
String cacheTime = isCached ? timeStamp.apply(cachingSupplier.getCacheTime()) : "-";
content.append(keyName).append(" | ").append(cacheText).append(" | ").append(cacheTime).append("<br>");
} else {
content.append(keyName).append(" | ").append("Non-caching Supplier").append(" | ").append("-").append("<br>");
}
}
content.append("</pre>");
} catch (Exception e) {
Log.toLog(this.getClass(), e);
}
}
private String createConfigContent() { private String createConfigContent() {
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
appendConfig(content); appendConfig(content);

View File

@ -14,7 +14,6 @@ import com.djrapitops.plan.data.store.mutators.formatting.Formatter;
import com.djrapitops.plan.data.store.mutators.formatting.Formatters; import com.djrapitops.plan.data.store.mutators.formatting.Formatters;
import com.djrapitops.plan.data.store.mutators.formatting.PlaceholderReplacer; import com.djrapitops.plan.data.store.mutators.formatting.PlaceholderReplacer;
import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.system.cache.CacheSystem;
import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
@ -60,7 +59,7 @@ public class InspectPage implements Page {
} }
Benchmark.start("Inspect Parse, Fetch"); Benchmark.start("Inspect Parse, Fetch");
Database db = Database.getActive(); Database db = Database.getActive();
PlayerContainer container = CacheSystem.getInstance().getDataContainerCache().getPlayerContainer(uuid); PlayerContainer container = Database.getActive().fetch().getPlayerContainer(uuid);
if (!container.getValue(PlayerKeys.REGISTERED).isPresent()) { if (!container.getValue(PlayerKeys.REGISTERED).isPresent()) {
throw new IllegalStateException("Player is not registered"); throw new IllegalStateException("Player is not registered");
} }