From 8b506b6a95c9733b954d3e882ef01dc52137f56b Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 3 Jul 2020 15:18:58 +0100 Subject: [PATCH] Don't re-use PermissionCache instances --- .../cacheddata/AbstractCachedDataManager.java | 58 +++++-------------- .../cacheddata/HolderCachedDataManager.java | 4 +- .../common/cacheddata/type/MetaCache.java | 4 +- .../cacheddata/type/PermissionCache.java | 19 +----- .../cacheddata/type/SimpleMetaCache.java | 42 +++++++------- .../common/model/PermissionHolder.java | 18 +++--- .../model/calculated/CalculatedSubject.java | 7 +-- .../CalculatedSubjectCachedDataManager.java | 4 +- 8 files changed, 54 insertions(+), 102 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/cacheddata/AbstractCachedDataManager.java b/common/src/main/java/me/lucko/luckperms/common/cacheddata/AbstractCachedDataManager.java index 4791afcfc..20dbf2efd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cacheddata/AbstractCachedDataManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/cacheddata/AbstractCachedDataManager.java @@ -46,12 +46,12 @@ import net.luckperms.api.node.ChatMetaType; import net.luckperms.api.query.QueryOptions; import org.checkerframework.checker.nullness.qual.NonNull; -import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; /** @@ -135,10 +135,10 @@ public abstract class AbstractCachedDataManager implements CachedDataManager { /** * Resolves the owners permissions data for the given {@link QueryOptions}. * + * @param accumulator the accumulator to add resolved permissions to * @param queryOptions the query options - * @return a map of permissions to back the {@link PermissionCache} */ - protected abstract Map resolvePermissions(QueryOptions queryOptions); + protected abstract void resolvePermissions(Map accumulator, QueryOptions queryOptions); /** * Resolves the owners meta data for the given {@link QueryOptions}. @@ -148,31 +148,24 @@ public abstract class AbstractCachedDataManager implements CachedDataManager { */ protected abstract void resolveMeta(MetaAccumulator accumulator, QueryOptions queryOptions); - private PermissionCache calculatePermissions(QueryOptions queryOptions, PermissionCache data) { + private PermissionCache calculatePermissions(QueryOptions queryOptions) { Objects.requireNonNull(queryOptions, "queryOptions"); + CacheMetadata metadata = getMetadataForQueryOptions(queryOptions); - if (data == null) { - CacheMetadata metadata = getMetadataForQueryOptions(queryOptions); - data = new PermissionCache(queryOptions, metadata, getCalculatorFactory()); - } + ConcurrentHashMap sourcePermissions = new ConcurrentHashMap<>(); + resolvePermissions(sourcePermissions, queryOptions); - data.setPermissions(resolvePermissions(queryOptions)); - return data; + return new PermissionCache(queryOptions, metadata, getCalculatorFactory(), sourcePermissions); } - private MetaCache calculateMeta(QueryOptions queryOptions, MetaCache data) { + private MetaCache calculateMeta(QueryOptions queryOptions) { Objects.requireNonNull(queryOptions, "queryOptions"); - - if (data == null) { - CacheMetadata metadata = getMetadataForQueryOptions(queryOptions); - data = new MetaCache(this.plugin, queryOptions, metadata); - } + CacheMetadata metadata = getMetadataForQueryOptions(queryOptions); MetaAccumulator accumulator = newAccumulator(queryOptions); resolveMeta(accumulator, queryOptions); - data.loadMeta(accumulator); - return data; + return new MetaCache(this.plugin, queryOptions, metadata, accumulator); } @Override @@ -236,25 +229,15 @@ public abstract class AbstractCachedDataManager implements CachedDataManager { public @NonNull CompletableFuture reload(@NonNull QueryOptions queryOptions) { Objects.requireNonNull(queryOptions, "queryOptions"); - // get the previous value - we can reuse the same instance - C previous = this.cache.getIfPresent(queryOptions); - // invalidate the previous value until we're done recalculating this.cache.invalidate(queryOptions); clearRecent(); // request recalculation from the cache - if (previous != null) { - return CompletableFuture.supplyAsync( - () -> this.cache.get(queryOptions, c -> this.cacheLoader.reload(c, previous)), - CaffeineFactory.executor() - ); - } else { - return CompletableFuture.supplyAsync( - () -> this.cache.get(queryOptions), - CaffeineFactory.executor() - ); - } + return CompletableFuture.supplyAsync( + () -> this.cache.get(queryOptions), + CaffeineFactory.executor() + ); } @Override @@ -284,17 +267,8 @@ public abstract class AbstractCachedDataManager implements CachedDataManager { } private interface Loader extends CacheLoader { - @NonNull V load(@NonNull K key, @Nullable V oldValue); - @Override - default @NonNull V load(@NonNull K key) { - return load(key, null); - } - - @Override - default @NonNull V reload(@NonNull K key, @NonNull V oldValue) { - return load(key, oldValue); - } + @NonNull V load(@NonNull K key); } private MetaStackDefinition getMetaStackDefinition(QueryOptions queryOptions, ChatMetaType type) { diff --git a/common/src/main/java/me/lucko/luckperms/common/cacheddata/HolderCachedDataManager.java b/common/src/main/java/me/lucko/luckperms/common/cacheddata/HolderCachedDataManager.java index d7fa0e9a6..5f0b3562c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cacheddata/HolderCachedDataManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/cacheddata/HolderCachedDataManager.java @@ -74,8 +74,8 @@ public abstract class HolderCachedDataManager extend } @Override - protected Map resolvePermissions(QueryOptions queryOptions) { - return this.holder.exportPermissions(queryOptions, true, getPlugin().getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND)); + protected void resolvePermissions(Map accumulator, QueryOptions queryOptions) { + this.holder.exportPermissions(accumulator, queryOptions, true, getPlugin().getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND)); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/MetaCache.java b/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/MetaCache.java index 8e5419925..22af9e28c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/MetaCache.java +++ b/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/MetaCache.java @@ -56,8 +56,8 @@ public class MetaCache extends SimpleMetaCache implements CachedMetaData { /** The object name passed to the verbose handler when checks are made */ private final String verboseCheckTarget; - public MetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions, CacheMetadata metadata) { - super(plugin, queryOptions); + public MetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions, CacheMetadata metadata, MetaAccumulator sourceMeta) { + super(plugin, queryOptions, sourceMeta); this.plugin = plugin; this.metadata = metadata; diff --git a/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/PermissionCache.java b/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/PermissionCache.java index 25b4bfc3c..d0b8ff855 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/PermissionCache.java +++ b/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/PermissionCache.java @@ -68,13 +68,13 @@ public class PermissionCache implements CachedPermissionData { */ private final PermissionCalculator calculator; - public PermissionCache(QueryOptions queryOptions, CacheMetadata metadata, CalculatorFactory calculatorFactory) { + public PermissionCache(QueryOptions queryOptions, CacheMetadata metadata, CalculatorFactory calculatorFactory, ConcurrentHashMap sourcePermissions) { this.queryOptions = queryOptions; - this.permissions = new ConcurrentHashMap<>(); + this.permissions = sourcePermissions; this.permissionsUnmodifiable = Collections.unmodifiableMap(this.permissions); this.calculator = calculatorFactory.build(queryOptions, metadata); - this.calculator.setSourcePermissions(this.permissions); // Initial setup. + this.calculator.setSourcePermissions(this.permissions); } @Override @@ -82,19 +82,6 @@ public class PermissionCache implements CachedPermissionData { this.calculator.invalidateCache(); } - private void setPermissionsInternal(Map permissions) { - this.permissions.clear(); - this.permissions.putAll(permissions); - this.calculator.setSourcePermissions(this.permissions); - invalidateCache(); - } - - public void setPermissions(Map toApply) { - if (!this.permissions.equals(toApply)) { - setPermissionsInternal(toApply); - } - } - public PermissionCalculator getCalculator() { return this.calculator; } diff --git a/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/SimpleMetaCache.java b/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/SimpleMetaCache.java index 69beb500d..f17193788 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/SimpleMetaCache.java +++ b/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/SimpleMetaCache.java @@ -58,24 +58,22 @@ public class SimpleMetaCache implements CachedMetaData { private final QueryOptions queryOptions; /* The data */ - protected Map> meta = ImmutableMap.of(); - protected Map flattenedMeta = ImmutableMap.of(); - protected SortedMap prefixes = ImmutableSortedMap.of(); - protected SortedMap suffixes = ImmutableSortedMap.of(); - protected int weight = 0; - protected String primaryGroup = null; - private MetaStackDefinition prefixDefinition = null; - private MetaStackDefinition suffixDefinition = null; - private String prefix = null; - private String suffix = null; + protected final Map> meta; + protected final Map flattenedMeta; + protected final SortedMap prefixes; + protected final SortedMap suffixes; + protected final int weight; + protected final String primaryGroup; + private final MetaStackDefinition prefixDefinition; + private final MetaStackDefinition suffixDefinition; + private final String prefix; + private final String suffix; - public SimpleMetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions) { + public SimpleMetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions, MetaAccumulator sourceMeta) { this.plugin = plugin; this.queryOptions = queryOptions; - } - public void loadMeta(MetaAccumulator meta) { - this.meta = Multimaps.asMap(ImmutableListMultimap.copyOf(meta.getMeta())); + this.meta = Multimaps.asMap(ImmutableListMultimap.copyOf(sourceMeta.getMeta())); MetaValueSelector metaValueSelector = this.queryOptions.option(MetaValueSelector.KEY) .orElseGet(() -> this.plugin.getConfiguration().get(ConfigKeys.META_VALUE_SELECTOR)); @@ -95,14 +93,14 @@ public class SimpleMetaCache implements CachedMetaData { } this.flattenedMeta = builder.build(); - this.prefixes = ImmutableSortedMap.copyOfSorted(meta.getPrefixes()); - this.suffixes = ImmutableSortedMap.copyOfSorted(meta.getSuffixes()); - this.weight = meta.getWeight(); - this.primaryGroup = meta.getPrimaryGroup(); - this.prefixDefinition = meta.getPrefixDefinition(); - this.suffixDefinition = meta.getSuffixDefinition(); - this.prefix = meta.getPrefix(); - this.suffix = meta.getSuffix(); + this.prefixes = ImmutableSortedMap.copyOfSorted(sourceMeta.getPrefixes()); + this.suffixes = ImmutableSortedMap.copyOfSorted(sourceMeta.getSuffixes()); + this.weight = sourceMeta.getWeight(); + this.primaryGroup = sourceMeta.getPrimaryGroup(); + this.prefixDefinition = sourceMeta.getPrefixDefinition(); + this.suffixDefinition = sourceMeta.getSuffixDefinition(); + this.prefix = sourceMeta.getPrefix(); + this.suffix = sourceMeta.getSuffix(); } public String getMetaValue(String key, MetaCheckEvent.Origin origin) { diff --git a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java index 48ec76bdf..652ddfc52 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java @@ -58,7 +58,6 @@ import java.time.Instant; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -358,18 +357,17 @@ public abstract class PermissionHolder { return (List) inheritanceTree; } - public Map exportPermissions(QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) { + public void exportPermissions(Map accumulator, QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) { List entries = resolveInheritedNodes(queryOptions); - return processExportedPermissions(entries, convertToLowercase, resolveShorthand); + processExportedPermissions(accumulator, entries, convertToLowercase, resolveShorthand); } - private static Map processExportedPermissions(List entries, boolean convertToLowercase, boolean resolveShorthand) { - Map map = new HashMap<>(entries.size()); + private static void processExportedPermissions(Map accumulator, List entries, boolean convertToLowercase, boolean resolveShorthand) { for (Node node : entries) { if (convertToLowercase) { - map.putIfAbsent(node.getKey().toLowerCase(), node.getValue()); + accumulator.putIfAbsent(node.getKey().toLowerCase(), node.getValue()); } else { - map.putIfAbsent(node.getKey(), node.getValue()); + accumulator.putIfAbsent(node.getKey(), node.getValue()); } } @@ -378,15 +376,13 @@ public abstract class PermissionHolder { Collection shorthand = node.resolveShorthand(); for (String s : shorthand) { if (convertToLowercase) { - map.putIfAbsent(s.toLowerCase(), node.getValue()); + accumulator.putIfAbsent(s.toLowerCase(), node.getValue()); } else { - map.putIfAbsent(s, node.getValue()); + accumulator.putIfAbsent(s, node.getValue()); } } } } - - return map; } public MetaAccumulator accumulateMeta(QueryOptions queryOptions) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubject.java index ab8150adc..e8870b80d 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubject.java @@ -89,18 +89,15 @@ public abstract class CalculatedSubject implements LPSubject { return permissions; } - public Map resolveAllPermissions(QueryOptions filter) { + public void resolveAllPermissions(Map accumulator, QueryOptions filter) { SubjectInheritanceGraph graph = new SubjectInheritanceGraph(filter); - Map result = new HashMap<>(); Iterable traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this); for (CalculatedSubject subject : traversal) { for (Map.Entry entry : subject.getCombinedPermissions(filter).entrySet()) { - result.putIfAbsent(entry.getKey(), entry.getValue()); + accumulator.putIfAbsent(entry.getKey(), entry.getValue()); } } - - return result; } public Set getCombinedParents(QueryOptions filter) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubjectCachedDataManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubjectCachedDataManager.java index a5ee5e735..bb7e796ad 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubjectCachedDataManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubjectCachedDataManager.java @@ -83,8 +83,8 @@ public class CalculatedSubjectCachedDataManager extends AbstractCachedDataManage } @Override - protected Map resolvePermissions(QueryOptions queryOptions) { - return this.subject.resolveAllPermissions(queryOptions); + protected void resolvePermissions(Map accumulator, QueryOptions queryOptions) { + this.subject.resolveAllPermissions(accumulator, queryOptions); } @Override