Always load user data caches alongside user instances

This commit is contained in:
Luck 2017-05-02 16:55:08 +01:00
parent f56bb251e9
commit 163e6e0f2a
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
18 changed files with 59 additions and 162 deletions

View File

@ -214,7 +214,7 @@ user-info-general: >
{PREFIX}&f- &3Meta: &a{8}
user-info-data: >
{PREFIX}&f- &aCached Data:\n
{PREFIX}&f- &3Is Loaded: {0}\n
{PREFIX}&f- &3Has contextual data: {0}\n
{PREFIX}&f- &3Current Contexts: {1}\n
{PREFIX}&f- &3Current Prefix: {2}\n
{PREFIX}&f- &3Current Suffix: {3}

View File

@ -29,7 +29,6 @@ import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.bukkit.model.Injector;
import me.lucko.luckperms.bukkit.model.LPPermissible;
import me.lucko.luckperms.common.caching.UserCache;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.core.model.User;
@ -182,14 +181,7 @@ public class BukkitListener implements Listener {
// We assume all users are not op, but those who are need extra calculation.
if (player.isOp()) {
plugin.doAsync(() -> {
UserCache userData = user.getUserData();
if (userData == null) {
return;
}
userData.preCalculate(plugin.getPreProcessContexts(true));
});
plugin.doAsync(() -> user.getUserData().preCalculate(plugin.getPreProcessContexts(true)));
}
}

View File

@ -42,7 +42,6 @@ import me.lucko.luckperms.bukkit.model.LPPermissible;
import me.lucko.luckperms.bukkit.vault.VaultHook;
import me.lucko.luckperms.common.api.ApiHandler;
import me.lucko.luckperms.common.api.ApiProvider;
import me.lucko.luckperms.common.caching.UserCache;
import me.lucko.luckperms.common.caching.handlers.CachedStateManager;
import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.commands.sender.Sender;
@ -474,12 +473,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
return;
}
UserCache userData = user.getUserData();
if (userData == null) {
return;
}
Map<String, Boolean> backing = userData.getPermissionData(permissible.calculateContexts()).getImmutableBacking();
Map<String, Boolean> backing = user.getUserData().getPermissionData(permissible.calculateContexts()).getImmutableBacking();
boolean op = Optional.ofNullable(backing.get("luckperms.autoop")).orElse(false);
player.setOp(op);
} catch (Exception ignored) {}

View File

@ -76,7 +76,7 @@ public class LPPermissible extends PermissibleBase {
private final Map<String, PermissionAttachmentInfo> attachmentPermissions = new ConcurrentHashMap<>();
private final List<PermissionAttachment> attachments = Collections.synchronizedList(new LinkedList<>());
public LPPermissible(@NonNull Player parent, User user, LPBukkitPlugin plugin) {
public LPPermissible(@NonNull Player parent, @NonNull User user, @NonNull LPBukkitPlugin plugin) {
super(parent);
this.user = user;
this.parent = parent;
@ -139,10 +139,6 @@ public class LPPermissible extends PermissibleBase {
);
}
private boolean hasData() {
return user != null && user.getUserData() != null;
}
@Override
public void setOp(boolean value) {
parent.setOp(value);
@ -150,7 +146,7 @@ public class LPPermissible extends PermissibleBase {
@Override
public boolean isPermissionSet(@NonNull String name) {
return hasData() && user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(name) != Tristate.UNDEFINED;
return user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(name) != Tristate.UNDEFINED;
}
@Override
@ -160,11 +156,9 @@ public class LPPermissible extends PermissibleBase {
@Override
public boolean hasPermission(@NonNull String name) {
if (hasData()) {
Tristate ts = user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(name);
if (ts != Tristate.UNDEFINED) {
return ts.asBoolean();
}
Tristate ts = user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(name);
if (ts != Tristate.UNDEFINED) {
return ts.asBoolean();
}
return Permission.DEFAULT_PERMISSION.getValue(isOp());
@ -172,11 +166,9 @@ public class LPPermissible extends PermissibleBase {
@Override
public boolean hasPermission(@NonNull Permission perm) {
if (hasData()) {
Tristate ts = user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(perm.getName());
if (ts != Tristate.UNDEFINED) {
return ts.asBoolean();
}
Tristate ts = user.getUserData().getPermissionData(calculateContexts()).getPermissionValue(perm.getName());
if (ts != Tristate.UNDEFINED) {
return ts.asBoolean();
}
return perm.getDefault().getValue(isOp());
@ -187,13 +179,11 @@ public class LPPermissible extends PermissibleBase {
Set<PermissionAttachmentInfo> perms = new HashSet<>();
perms.addAll(attachmentPermissions.values());
if (hasData()) {
perms.addAll(
user.getUserData().getPermissionData(calculateContexts()).getImmutableBacking().entrySet().stream()
.map(e -> new PermissionAttachmentInfo(parent, e.getKey(), null, e.getValue()))
.collect(Collectors.toList())
);
}
perms.addAll(
user.getUserData().getPermissionData(calculateContexts()).getImmutableBacking().entrySet().stream()
.map(e -> new PermissionAttachmentInfo(parent, e.getKey(), null, e.getValue()))
.collect(Collectors.toList())
);
return perms;
}
@ -288,7 +278,7 @@ public class LPPermissible extends PermissibleBase {
calculateChildPermissions(attachment.getPermissions(), false, attachment);
}
if (hasData() && invalidate) {
if (invalidate) {
user.getUserData().invalidatePermissionCalculators();
}
}

View File

@ -126,10 +126,6 @@ public class VaultChatHook extends Chat {
perms.log("Getting meta: '" + node + "' for user " + user.getFriendlyName() + " on world " + world + ", server " + perms.getServer());
if (user.getUserData() == null) {
return defaultValue;
}
String ret = user.getUserData().getMetaData(perms.createContextForWorld(world)).getMeta().get(node);
if (ret == null) {
return defaultValue;
@ -144,10 +140,6 @@ public class VaultChatHook extends Chat {
perms.log("Getting " + (prefix ? "prefix" : "suffix") + " for user " + user.getFriendlyName() + " on world " + world + ", server " + perms.getServer());
if (user.getUserData() == null) {
return "";
}
MetaData data = user.getUserData().getMetaData(perms.createContextForWorld(world));
String v = prefix ? data.getPrefix() : data.getSuffix();
return v == null ? "" : unescapeCharacters(v);

View File

@ -164,10 +164,6 @@ public class VaultPermissionHook extends Permission {
User user = plugin.getUserManager().getByUsername(player);
if (user == null) return false;
if (user.getUserData() == null) {
return false;
}
// Effectively fallback to the standard Bukkit #hasPermission check.
return user.getUserData().getPermissionData(createContextForWorld(world)).getPermissionValue(permission).asBoolean();
}

View File

@ -29,7 +29,6 @@ import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.caching.UserData;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.constants.Message;
@ -166,14 +165,6 @@ public class BungeeListener implements Listener {
return;
}
UserData userData = user.getUserData();
if (userData == null) {
plugin.getLog().warn("Player " + player.getName() + " does not have any user data setup.");
plugin.doAsync(() -> user.setupData(false));
e.setHasPermission(false);
return;
}
Contexts contexts = new Contexts(
plugin.getContextManager().getApplicableContext(player),
plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS),
@ -184,7 +175,7 @@ public class BungeeListener implements Listener {
false
);
Tristate result = userData.getPermissionData(contexts).getPermissionValue(e.getPermission());
Tristate result = user.getUserData().getPermissionData(contexts).getPermissionValue(e.getPermission());
if (result == Tristate.UNDEFINED && plugin.getConfiguration().get(ConfigKeys.APPLY_BUNGEE_CONFIG_PERMISSIONS)) {
return; // just use the result provided by the proxy when the event was created
}
@ -207,10 +198,6 @@ public class BungeeListener implements Listener {
if (user == null) {
return;
}
UserData userData = user.getUserData();
if (userData == null) {
return;
}
Contexts contexts = new Contexts(
set.makeImmutable(),
@ -222,7 +209,7 @@ public class BungeeListener implements Listener {
false
);
userData.preCalculate(contexts);
user.getUserData().preCalculate(contexts);
});
}
}

View File

@ -97,12 +97,13 @@ public final class UserDelegate extends PermissionHolderDelegate implements User
@Override
public Optional<UserData> getUserDataCache() {
return Optional.ofNullable(handle.getUserData());
// TODO Deprecate this and return a nonnull instance
return Optional.of(handle.getUserData());
}
@Override
public void setupDataCache() {
handle.setupData(false);
handle.preCalculateData(false);
}
@Override

View File

@ -37,7 +37,6 @@ import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.caching.MetaData;
import me.lucko.luckperms.api.caching.PermissionData;
import me.lucko.luckperms.api.caching.UserData;
import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.utils.ExtractedContexts;
@ -50,17 +49,11 @@ import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
public class UserCache implements UserData {
/**
* The user whom this data instance is representing
*/
private final User user;
/**
* A provider of {@link me.lucko.luckperms.common.calculators.PermissionCalculator}s for the instance
*/
private final CalculatorFactory calculatorFactory;
private final LoadingCache<Contexts, PermissionCache> permission = Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<Contexts, PermissionCache>() {
@ -103,7 +96,7 @@ public class UserCache implements UserData {
@Override
public PermissionCache calculatePermissions(@NonNull Contexts contexts) {
PermissionCache data = new PermissionCache(contexts, user, calculatorFactory);
PermissionCache data = new PermissionCache(contexts, user, user.getPlugin().getCalculatorFactory());
data.setPermissions(user.exportNodes(ExtractedContexts.generate(contexts), true));
return data;
}
@ -163,4 +156,9 @@ public class UserCache implements UserData {
meta.cleanUp();
}
public void clear() {
permission.invalidateAll();
meta.invalidateAll();
}
}

View File

@ -26,7 +26,6 @@
package me.lucko.luckperms.common.commands.impl.misc;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.caching.UserCache;
import me.lucko.luckperms.common.commands.Arg;
import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
@ -71,13 +70,7 @@ public class CheckCommand extends SingleCommand {
return CommandResult.STATE_ERROR;
}
UserCache data = user.getUserData();
if (data == null) {
Message.USER_NO_DATA.send(sender, user.getFriendlyName());
return CommandResult.STATE_ERROR;
}
Tristate tristate = data.getPermissionData(plugin.getContextForUser(user)).getPermissionValue(permission);
Tristate tristate = user.getUserData().getPermissionData(plugin.getContextForUser(user)).getPermissionValue(permission);
Message.CHECK_RESULT.send(sender, user.getFriendlyName(), permission, Util.formatTristate(tristate));
return CommandResult.SUCCESS;
}

View File

@ -26,7 +26,6 @@
package me.lucko.luckperms.common.commands.impl.misc;
import me.lucko.luckperms.api.caching.PermissionData;
import me.lucko.luckperms.common.caching.UserCache;
import me.lucko.luckperms.common.commands.Arg;
import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
@ -90,13 +89,7 @@ public class TreeCommand extends SingleCommand {
return CommandResult.STATE_ERROR;
}
UserCache data = user.getUserData();
if (data == null) {
Message.USER_NO_DATA.send(sender, user.getFriendlyName());
return CommandResult.STATE_ERROR;
}
PermissionData permissionData = data.getPermissionData(plugin.getContextForUser(user));
PermissionData permissionData = user.getUserData().getPermissionData(plugin.getContextForUser(user));
TreeView view = TreeViewBuilder.newBuilder().rootPosition(selection).maxLevels(maxLevel).build(plugin.getPermissionVault());
if (!view.hasData()) {

View File

@ -28,7 +28,6 @@ package me.lucko.luckperms.common.commands.impl.user;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.caching.MetaData;
import me.lucko.luckperms.api.caching.UserData;
import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
@ -90,28 +89,25 @@ public class UserInfo extends SubCommand<User> {
}
}
UserData data = user.getUserData();
String context = "&bNone";
String prefix = "&bNone";
String suffix = "&bNone";
if (data != null) {
Contexts contexts = plugin.getContextForUser(user);
if (contexts != null) {
context = contexts.getContexts().toSet().stream()
.map(e -> Util.contextToString(e.getKey(), e.getValue()))
.collect(Collectors.joining(" "));
Contexts contexts = plugin.getContextForUser(user);
if (contexts != null) {
context = contexts.getContexts().toSet().stream()
.map(e -> Util.contextToString(e.getKey(), e.getValue()))
.collect(Collectors.joining(" "));
MetaData meta = data.getMetaData(contexts);
if (meta.getPrefix() != null) {
prefix = "&f\"" + meta.getPrefix() + "&f\"";
}
if (meta.getSuffix() != null) {
suffix = "&f\"" + meta.getSuffix() + "&f\"";
}
MetaData meta = user.getUserData().getMetaData(contexts);
if (meta.getPrefix() != null) {
prefix = "&f\"" + meta.getPrefix() + "&f\"";
}
if (meta.getSuffix() != null) {
suffix = "&f\"" + meta.getSuffix() + "&f\"";
}
}
Message.USER_INFO_DATA.send(sender, Util.formatBoolean(data != null), context, prefix, suffix);
Message.USER_INFO_DATA.send(sender, Util.formatBoolean(contexts != null), context, prefix, suffix);
return CommandResult.SUCCESS;
}
}

View File

@ -294,7 +294,7 @@ public enum Message {
USER_INFO_DATA(
"{PREFIX}&f- &aCached Data:" + "\n" +
"{PREFIX}&f- &3Is Loaded: {0}" + "\n" +
"{PREFIX}&f- &3Has contextual data: {0}" + "\n" +
"{PREFIX}&f- &3Current Contexts: {1}" + "\n" +
"{PREFIX}&f- &3Current Prefix: {2}" + "\n" +
"{PREFIX}&f- &3Current Suffix: {3}",

View File

@ -29,7 +29,6 @@ import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import me.lucko.luckperms.api.caching.UserData;
import me.lucko.luckperms.common.api.delegates.UserDelegate;
import me.lucko.luckperms.common.caching.UserCache;
import me.lucko.luckperms.common.caching.handlers.HolderReference;
@ -69,7 +68,7 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
* The users data cache instance, if present.
*/
@Getter
private UserCache userData = null;
private final UserCache userData;
@Getter
private BufferedRequest<Void> refreshBuffer = new BufferedRequest<Void>(1000L, r -> getPlugin().doAsync(r)) {
@ -86,15 +85,20 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
public User(UUID uuid, LuckPermsPlugin plugin) {
super(uuid.toString(), plugin);
this.uuid = uuid;
this.primaryGroup = plugin.getConfiguration().get(ConfigKeys.PRIMARY_GROUP_CALCULATION).apply(this);
this.userData = new UserCache(this);
getPlugin().getApiProvider().getEventFactory().handleUserCacheLoad(this, userData);
}
public User(UUID uuid, String name, LuckPermsPlugin plugin) {
super(uuid.toString(), plugin);
this.uuid = uuid;
setName(name, true);
setName(name, true);
this.primaryGroup = plugin.getConfiguration().get(ConfigKeys.PRIMARY_GROUP_CALCULATION).apply(this);
this.userData = new UserCache(this);
getPlugin().getApiProvider().getEventFactory().handleUserCacheLoad(this, userData);
}
@Override
@ -156,15 +160,8 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
* Sets up the UserData cache
* Blocking call.
*/
public synchronized void setupData(boolean op) {
if (userData != null) {
return;
}
userData = new UserCache(this, getPlugin().getCalculatorFactory());
public synchronized void preCalculateData(boolean op) {
userData.preCalculate(getPlugin().getPreProcessContexts(op));
getPlugin().getApiProvider().getEventFactory().handleUserCacheLoad(this, userData);
getPlugin().onUserRefresh(this);
}
@ -172,7 +169,7 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
* Removes the UserData cache from this user
*/
public void unregisterData() {
userData = null;
userData.clear();
}
/**
@ -180,14 +177,9 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
* Blocking call.
*/
private synchronized void refreshPermissions() {
UserData ud = userData;
if (ud == null) {
return;
}
ud.recalculatePermissions();
ud.recalculateMeta();
getPlugin().getApiProvider().getEventFactory().handleUserDataRecalculate(this, ud);
userData.recalculatePermissions();
userData.recalculateMeta();
getPlugin().getApiProvider().getEventFactory().handleUserDataRecalculate(this, userData);
getPlugin().onUserRefresh(this);
}
@ -213,9 +205,6 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
}
public void cleanup() {
UserCache cache = userData;
if (cache != null) {
cache.cleanup();
}
userData.cleanup();
}
}

View File

@ -91,7 +91,7 @@ public class LoginHelper {
plugin.getStorage().force().saveUser(user).join();
}
user.setupData(false); // Pretty nasty calculation call. Sets up the caching system so data is ready when the user joins.
user.preCalculateData(false); // Pretty nasty calculation call. Sets up the caching system so data is ready when the user joins.
}
final long time = System.currentTimeMillis() - startTime;

View File

@ -83,12 +83,8 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection {
.expireAfterWrite(1, TimeUnit.MINUTES)
.build(u -> {
if (isLoaded(UserIdentifier.of(u, null))) {
SpongeUser user = get(u);
if (user.getUserData() == null) {
user.setupData(false);
}
user.preCalculateData(false);
return get(u).getSpongeData();
}
@ -101,12 +97,7 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection {
throw new RuntimeException();
}
user.setupData(false);
if (user.getUserData() == null) {
getPlugin().getLog().warn("User data not present for requested user id: " + u);
}
user.preCalculateData(false);
return user.getSpongeData();
});

View File

@ -96,13 +96,6 @@ public class SpongeUser extends User {
return (now - lastUse) > 600000;
}
private synchronized void checkData() {
if (parent.getUserData() == null) {
plugin.getLog().warn("User " + parent.getName().orElse("unknown") + " - " + parent.getUuid() + " does not have any data loaded.");
parent.setupData(false);
}
}
@Override
public String getIdentifier() {
return plugin.getUuidCache().getExternalUUID(parent.getUuid()).toString();
@ -134,7 +127,6 @@ public class SpongeUser extends User {
public Tristate getPermissionValue(ContextSet contexts, String permission) {
logUsage();
try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_PERMISSION_VALUE)) {
checkData();
return parent.getUserData().getPermissionData(plugin.getService().calculateContexts(contexts)).getPermissionValue(permission);
}
}
@ -153,7 +145,6 @@ public class SpongeUser extends User {
try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_PARENTS)) {
ImmutableSet.Builder<SubjectReference> subjects = ImmutableSet.builder();
checkData();
for (String perm : parent.getUserData().getPermissionData(plugin.getService().calculateContexts(contexts)).getImmutableBacking().keySet()) {
if (!perm.startsWith("group.")) {
continue;
@ -176,8 +167,6 @@ public class SpongeUser extends User {
public Optional<String> getOption(ContextSet contexts, String s) {
logUsage();
try (Timing ignored = plugin.getTimings().time(LPTiming.USER_GET_OPTION)) {
checkData();
MetaData data = parent.getUserData().getMetaData(plugin.getService().calculateContexts(contexts));
if (s.equalsIgnoreCase("prefix")) {
if (data.getPrefix() != null) {

View File

@ -280,8 +280,6 @@ public class LuckPermsService implements PermissionService {
for (User user : plugin.getUserManager().getAll().values()) {
UserCache userCache = user.getUserData();
if (userCache == null) continue;
userCache.invalidateCache();
}
@ -305,8 +303,6 @@ public class LuckPermsService implements PermissionService {
for (User user : plugin.getUserManager().getAll().values()) {
UserCache userCache = user.getUserData();
if (userCache == null) continue;
userCache.invalidateCache();
}
}