Improve memory footprint in low throughput caches

This commit is contained in:
Luck 2016-12-20 12:42:02 +00:00
parent 829eacb0fb
commit 8a692200d5
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
14 changed files with 178 additions and 1 deletions

View File

@ -58,6 +58,7 @@ import me.lucko.luckperms.common.managers.impl.GenericUserManager;
import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask;
import me.lucko.luckperms.common.utils.BufferedRequest;
@ -252,6 +253,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
// register tasks
getServer().getScheduler().runTaskTimerAsynchronously(this, new ExpireTemporaryTask(this), 60L, 60L);
getServer().getScheduler().runTaskTimerAsynchronously(this, new CacheHousekeepingTask(this), 2400L, 2400L);
// register permissions
registerPermissions(getConfiguration().isCommandsAllowOp() ? PermissionDefault.OP : PermissionDefault.FALSE);

View File

@ -51,6 +51,7 @@ import me.lucko.luckperms.common.managers.impl.GenericUserManager;
import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask;
import me.lucko.luckperms.common.utils.BufferedRequest;
@ -187,6 +188,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
// register tasks
getProxy().getScheduler().schedule(this, new ExpireTemporaryTask(this), 3L, 3L, TimeUnit.SECONDS);
getProxy().getScheduler().schedule(this, new CacheHousekeepingTask(this), 2L, 2L, TimeUnit.MINUTES);
getLog().info("Successfully loaded.");
}

View File

@ -41,6 +41,7 @@ import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Holds an easily accessible cache of a user's data in a number of contexts
@ -59,6 +60,7 @@ public class UserCache implements UserData {
private final CalculatorFactory calculatorFactory;
private final LoadingCache<Contexts, PermissionCache> permission = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<Contexts, PermissionCache>() {
@Override
public PermissionCache load(Contexts contexts) {
@ -73,6 +75,7 @@ public class UserCache implements UserData {
});
private final LoadingCache<Contexts, MetaCache> meta = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<Contexts, MetaCache>() {
@Override
public MetaCache load(Contexts contexts) {
@ -153,4 +156,9 @@ public class UserCache implements UserData {
permission.asMap().values().forEach(PermissionData::invalidateCache);
}
public void cleanup() {
permission.cleanUp();
meta.cleanUp();
}
}

View File

@ -76,6 +76,7 @@ import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
@ -142,6 +143,7 @@ public abstract class PermissionHolder {
/* External Caches - may depend on the state of other instances. */
private LoadingCache<GetAllNodesHolder, SortedSet<LocalizedNode>> getAllNodesCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<GetAllNodesHolder, SortedSet<LocalizedNode>>() {
@Override
public SortedSet<LocalizedNode> load(GetAllNodesHolder getAllNodesHolder) {
@ -149,6 +151,7 @@ public abstract class PermissionHolder {
}
});
private LoadingCache<ExtractedContexts, Set<LocalizedNode>> getAllNodesFilteredCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<ExtractedContexts, Set<LocalizedNode>>() {
@Override
public Set<LocalizedNode> load(ExtractedContexts extractedContexts) throws Exception {
@ -156,6 +159,7 @@ public abstract class PermissionHolder {
}
});
private LoadingCache<ExportNodesHolder, Map<String, Boolean>> exportNodesCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<ExportNodesHolder, Map<String, Boolean>>() {
@Override
public Map<String, Boolean> load(ExportNodesHolder exportNodesHolder) throws Exception {
@ -167,6 +171,12 @@ public abstract class PermissionHolder {
/* Caching apply methods. Are just called by the caching instances to gather data about the instance. */
protected void forceCleanup() {
getAllNodesCache.cleanUp();
getAllNodesFilteredCache.cleanUp();
exportNodesCache.cleanUp();
}
private void invalidateCache(boolean enduring) {
if (enduring) {
enduringCache.invalidate();

View File

@ -149,4 +149,12 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
super.clearNodes();
getPlugin().getUserManager().giveDefaultIfNeeded(this, false);
}
public void cleanup() {
UserCache cache = userData;
if (cache != null) {
cache.cleanup();
}
forceCleanup();
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.tasks;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.core.model.User;
@RequiredArgsConstructor
public class CacheHousekeepingTask implements Runnable {
private final LuckPermsPlugin plugin;
@Override
public void run() {
for (User user : plugin.getUserManager().getAll().values()) {
user.cleanup();
}
}
}

View File

@ -48,6 +48,7 @@ import me.lucko.luckperms.common.managers.impl.GenericTrackManager;
import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask;
import me.lucko.luckperms.common.utils.BufferedRequest;
@ -60,6 +61,7 @@ import me.lucko.luckperms.sponge.contexts.WorldCalculator;
import me.lucko.luckperms.sponge.managers.SpongeGroupManager;
import me.lucko.luckperms.sponge.managers.SpongeUserManager;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import me.lucko.luckperms.sponge.service.ServiceCacheHousekeepingTask;
import me.lucko.luckperms.sponge.timings.LPTimings;
import me.lucko.luckperms.sponge.utils.VersionData;
@ -250,6 +252,9 @@ public class LPSpongePlugin implements LuckPermsPlugin {
// register tasks
scheduler.createTaskBuilder().async().intervalTicks(60L).execute(new ExpireTemporaryTask(this)).submit(this);
scheduler.createTaskBuilder().async().intervalTicks(2400L).execute(new CacheHousekeepingTask(this)).submit(this);
scheduler.createTaskBuilder().async().intervalTicks(2400L).execute(new ServiceCacheHousekeepingTask(service)).submit(this);
scheduler.createTaskBuilder().async().intervalTicks(2400L).execute(() -> userManager.performCleanup()).submit(this);
getLog().info("Successfully loaded.");
}

View File

@ -52,6 +52,7 @@ import org.spongepowered.api.service.permission.PermissionService;
import co.aikar.timings.Timing;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@ -83,6 +84,17 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection {
new SpongeUser(id.getUuid(), id.getUsername(), plugin);
}
public void performCleanup() {
Set<UserIdentifier> set = new HashSet<>();
for (Map.Entry<UserIdentifier, SpongeUser> user : objects.asMap().entrySet()) {
if (user.getValue().getSpongeData().shouldCleanup()) {
set.add(user.getKey());
}
}
objects.invalidateAll(set);
}
/* ------------------------------------------
* Manager methods
* ------------------------------------------ */
@ -109,7 +121,6 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection {
@Override
public void unload(User t) {
// TODO override
if (t != null) {
objects.invalidate(t.getId());
}

View File

@ -54,6 +54,7 @@ import co.aikar.timings.Timing;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class SpongeGroup extends Group {
@ -77,6 +78,7 @@ public class SpongeGroup extends Group {
private final LuckPermsSubjectData transientSubjectData;
private final LoadingCache<ContextSet, NodeTree> permissionCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<ContextSet, NodeTree>() {
@Override
public NodeTree load(ContextSet contexts) {
@ -90,6 +92,7 @@ public class SpongeGroup extends Group {
});
private final LoadingCache<ContextSet, Set<SubjectReference>> parentCache = CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(new CacheLoader<ContextSet, Set<SubjectReference>>() {
@Override
public Set<SubjectReference> load(ContextSet contexts) {
@ -120,6 +123,12 @@ public class SpongeGroup extends Group {
});
}
@Override
public void performCleanup() {
permissionCache.cleanUp();
parentCache.cleanUp();
}
@Override
public String getIdentifier() {
return parent.getObjectName();

View File

@ -74,6 +74,8 @@ public class SpongeUser extends User {
@Getter
private final LuckPermsSubjectData transientSubjectData;
private long lastUse = System.currentTimeMillis();
private UserSubject(LPSpongePlugin plugin, SpongeUser parent) {
this.parent = parent;
this.plugin = plugin;
@ -81,6 +83,16 @@ public class SpongeUser extends User {
this.transientSubjectData = new LuckPermsSubjectData(false, plugin.getService(), parent, this);
}
private void logUsage() {
lastUse = System.currentTimeMillis();
}
public boolean shouldCleanup() {
long now = System.currentTimeMillis();
// Expire after 10 minutes of idle
return (now - lastUse) > 600000;
}
private boolean hasData() {
return parent.getUserData() != null;
}
@ -114,6 +126,7 @@ public class SpongeUser extends User {
@Override
public Tristate getPermissionValue(ContextSet contexts, String permission) {
logUsage();
try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_PERMISSION_VALUE)) {
if (!hasData()) {
return Tristate.UNDEFINED;
@ -125,6 +138,7 @@ public class SpongeUser extends User {
@Override
public boolean isChildOf(ContextSet contexts, SubjectReference parent) {
logUsage();
try (Timing ignored = plugin.getTimings().time(LPTiming.USER_IS_CHILD_OF)) {
return parent.getCollection().equals(PermissionService.SUBJECTS_GROUP) && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean();
}
@ -132,6 +146,7 @@ public class SpongeUser extends User {
@Override
public Set<SubjectReference> getParents(ContextSet contexts) {
logUsage();
try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_PARENTS)) {
ImmutableSet.Builder<SubjectReference> subjects = ImmutableSet.builder();
@ -157,6 +172,7 @@ public class SpongeUser extends User {
@Override
public Optional<String> getOption(ContextSet contexts, String s) {
logUsage();
try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_OPTION)) {
if (hasData()) {
MetaData data = parent.getUserData().getMetaData(plugin.getService().calculateContexts(contexts));
@ -188,6 +204,7 @@ public class SpongeUser extends User {
@Override
public ContextSet getActiveContextSet() {
logUsage();
try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_ACTIVE_CONTEXTS)) {
return plugin.getContextManager().getApplicableContext(this);
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.sponge.service;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.sponge.service.base.LPSubject;
import me.lucko.luckperms.sponge.service.base.LPSubjectCollection;
@RequiredArgsConstructor
public class ServiceCacheHousekeepingTask implements Runnable {
private final LuckPermsService service;
@Override
public void run() {
for (LPSubjectCollection collection : service.getCollections().values()) {
for (LPSubject subject : collection.getSubjects()) {
subject.performCleanup();
}
}
}
}

View File

@ -60,6 +60,10 @@ public interface LPSubject extends Subject {
LuckPermsService getService();
default void performCleanup() {
}
default SubjectReference toReference() {
return SubjectReference.of(getParentCollection().getCollection(), getIdentifier());
}

View File

@ -52,6 +52,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
public class CalculatedSubjectData implements LPSubjectData {
@ -102,6 +103,7 @@ public class CalculatedSubjectData implements LPSubjectData {
private final Map<ContextSet, Map<String, Boolean>> permissions = new ConcurrentHashMap<>();
private final LoadingCache<ContextSet, CalculatorHolder> permissionCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<ContextSet, CalculatorHolder>() {
@Override
public CalculatorHolder load(ContextSet contexts) {
@ -119,6 +121,10 @@ public class CalculatedSubjectData implements LPSubjectData {
private final Map<ContextSet, Set<SubjectReference>> parents = new ConcurrentHashMap<>();
private final Map<ContextSet, Map<String, String>> options = new ConcurrentHashMap<>();
public void cleanup() {
permissionCache.cleanUp();
}
public Tristate getPermissionValue(ContextSet contexts, String permission) {
return permissionCache.getUnchecked(contexts).getCalculator().getPermissionValue(permission);
}

View File

@ -51,6 +51,7 @@ import java.io.IOException;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* A simple persistable Subject implementation
@ -66,6 +67,7 @@ public class PersistedSubject implements LPSubject {
private final CalculatedSubjectData transientSubjectData;
private final LoadingCache<PermissionLookup, Tristate> permissionLookupCache = CacheBuilder.newBuilder()
.expireAfterAccess(20, TimeUnit.MINUTES)
.build(new CacheLoader<PermissionLookup, Tristate>() {
@Override
public Tristate load(PermissionLookup lookup) {
@ -73,6 +75,7 @@ public class PersistedSubject implements LPSubject {
}
});
private final LoadingCache<ImmutableContextSet, Set<SubjectReference>> parentLookupCache = CacheBuilder.newBuilder()
.expireAfterAccess(20, TimeUnit.MINUTES)
.build(new CacheLoader<ImmutableContextSet, Set<SubjectReference>>() {
@Override
public Set<SubjectReference> load(ImmutableContextSet contexts) {
@ -80,6 +83,7 @@ public class PersistedSubject implements LPSubject {
}
});
private final LoadingCache<OptionLookup, Optional<String>> optionLookupCache = CacheBuilder.newBuilder()
.expireAfterAccess(20, TimeUnit.MINUTES)
.build(new CacheLoader<OptionLookup, Optional<String>>() {
@Override
public Optional<String> load(OptionLookup lookup) {
@ -115,6 +119,15 @@ public class PersistedSubject implements LPSubject {
service.getLocalOptionCaches().add(optionLookupCache);
}
@Override
public void performCleanup() {
this.subjectData.cleanup();
this.transientSubjectData.cleanup();
this.permissionLookupCache.cleanUp();
this.parentLookupCache.cleanUp();
this.optionLookupCache.cleanUp();
}
public void loadData(SubjectDataHolder dataHolder) {
subjectData.setSave(false);
dataHolder.copyTo(subjectData);