Cache permission lookups

This commit is contained in:
Luck 2016-09-16 21:11:12 +01:00
parent f6aa20c300
commit f2e06b56e7
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
8 changed files with 229 additions and 47 deletions

View File

@ -46,11 +46,12 @@ public class LPPermissible extends PermissibleBase {
private final CommandSender parent; private final CommandSender parent;
private final LuckPermsPlugin plugin; private final LuckPermsPlugin plugin;
@Getter
private final Map<String, Boolean> luckPermsPermissions = new ConcurrentHashMap<>();
private final List<PermissionAttachment> attachments = new LinkedList<>(); private final List<PermissionAttachment> attachments = new LinkedList<>();
private final Map<String, PermissionAttachmentInfo> attachmentPermissions = new HashMap<>(); private final Map<String, PermissionAttachmentInfo> attachmentPermissions = new HashMap<>();
@Getter private final Map<String, Tristate> lookupCache = new HashMap<>();
private final Map<String, Boolean> luckPermsPermissions = new ConcurrentHashMap<>();
public LPPermissible(@NonNull CommandSender sender, LuckPermsPlugin plugin) { public LPPermissible(@NonNull CommandSender sender, LuckPermsPlugin plugin) {
super(sender); super(sender);
@ -58,24 +59,10 @@ public class LPPermissible extends PermissibleBase {
this.plugin = plugin; this.plugin = plugin;
} }
@Override public void invalidateCache() {
public boolean isOp() { synchronized (lookupCache) {
return parent.isOp(); lookupCache.clear();
} }
@Override
public void setOp(boolean value) {
parent.setOp(value);
}
@Override
public boolean isPermissionSet(@NonNull String name) {
return luckPermsPermissions.containsKey(name.toLowerCase()) || attachmentPermissions.containsKey(name.toLowerCase());
}
@Override
public boolean isPermissionSet(@NonNull Permission perm) {
return isPermissionSet(perm.getName());
} }
private Tristate getPermissionValue(String permission) { private Tristate getPermissionValue(String permission) {
@ -84,7 +71,18 @@ public class LPPermissible extends PermissibleBase {
} }
permission = permission.toLowerCase(); permission = permission.toLowerCase();
synchronized (lookupCache) {
if (lookupCache.containsKey(permission)) {
return lookupCache.get(permission);
} else {
Tristate t = lookupPermissionValue(permission);
lookupCache.put(permission, t);
return t;
}
}
}
private Tristate lookupPermissionValue(String permission) {
if (luckPermsPermissions.containsKey(permission)) { if (luckPermsPermissions.containsKey(permission)) {
return Tristate.fromBoolean(luckPermsPermissions.get(permission)); return Tristate.fromBoolean(luckPermsPermissions.get(permission));
} }
@ -124,6 +122,26 @@ public class LPPermissible extends PermissibleBase {
return Tristate.UNDEFINED; return Tristate.UNDEFINED;
} }
@Override
public boolean isOp() {
return parent.isOp();
}
@Override
public void setOp(boolean value) {
parent.setOp(value);
}
@Override
public boolean isPermissionSet(@NonNull String name) {
return luckPermsPermissions.containsKey(name.toLowerCase()) || attachmentPermissions.containsKey(name.toLowerCase());
}
@Override
public boolean isPermissionSet(@NonNull Permission perm) {
return isPermissionSet(perm.getName());
}
@Override @Override
public boolean hasPermission(@NonNull String name) { public boolean hasPermission(@NonNull String name) {
Tristate ts = getPermissionValue(name); Tristate ts = getPermissionValue(name);
@ -239,6 +257,8 @@ public class LPPermissible extends PermissibleBase {
for (PermissionAttachment attachment : attachments) { for (PermissionAttachment attachment : attachments) {
calculateChildPermissions(attachment.getPermissions(), false, attachment); calculateChildPermissions(attachment.getPermissions(), false, attachment);
} }
invalidateCache();
} }
@Override @Override

View File

@ -87,6 +87,7 @@ public class BukkitUser extends User {
if (!different) return; if (!different) return;
existing.clear(); existing.clear();
lpPermissible.invalidateCache();
existing.putAll(toApply); existing.putAll(toApply);
if (plugin.getConfiguration().getAutoOp()) { if (plugin.getConfiguration().getAutoOp()) {

View File

@ -33,8 +33,6 @@ import net.md_5.bungee.api.event.*;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
import java.util.Collections;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -51,30 +49,18 @@ public class BungeeListener extends AbstractListener implements Listener {
@EventHandler @EventHandler
public void onPlayerPermissionCheck(PermissionCheckEvent e) { public void onPlayerPermissionCheck(PermissionCheckEvent e) {
if (!(e.getSender() instanceof ProxiedPlayer)) { if (!(e.getSender() instanceof ProxiedPlayer)) {
e.setHasPermission(true);
return; return;
} }
final ProxiedPlayer player = ((ProxiedPlayer) e.getSender()); final ProxiedPlayer player = ((ProxiedPlayer) e.getSender());
final User user = plugin.getUserManager().get(plugin.getUuidCache().getUUID(player.getUniqueId()));
if (user == null) return;
BungeePlayerCache playerCache = plugin.getPlayerCache().get(player.getUniqueId());
final String server = player.getServer() == null ? null : (player.getServer().getInfo() == null ? null : player.getServer().getInfo().getName()); if (playerCache == null) {
Map<String, Boolean> local = user.exportNodes( return;
plugin.getConfiguration().getServer(),
server,
null,
plugin.getConfiguration().getIncludeGlobalPerms(),
true,
Collections.singletonList(e.getPermission())
);
for (Map.Entry<String, Boolean> en : local.entrySet()) {
if (en.getKey().equalsIgnoreCase(e.getPermission())) {
e.setHasPermission(en.getValue());
return;
}
} }
e.setHasPermission(playerCache.getPermissionValue(e.getPermission()));
} }
@EventHandler @EventHandler
@ -116,17 +102,20 @@ public class BungeeListener extends AbstractListener implements Listener {
@EventHandler @EventHandler
public void onPlayerPostLogin(PostLoginEvent e) { public void onPlayerPostLogin(PostLoginEvent e) {
final ProxiedPlayer player = e.getPlayer(); final ProxiedPlayer player = e.getPlayer();
final User user = plugin.getUserManager().get(plugin.getUuidCache().getUUID(e.getPlayer().getUniqueId())); final UUID internal = plugin.getUuidCache().getUUID(e.getPlayer().getUniqueId());
final User user = plugin.getUserManager().get(internal);
if (user == null) { if (user == null) {
plugin.getProxy().getScheduler().schedule(plugin, () -> player.sendMessage(WARN_MESSAGE), 3, TimeUnit.SECONDS); plugin.getProxy().getScheduler().schedule(plugin, () -> player.sendMessage(WARN_MESSAGE), 3, TimeUnit.SECONDS);
} else { } else {
plugin.getPlayerCache().put(internal, new BungeePlayerCache(plugin, internal, e.getPlayer().getName()));
user.refreshPermissions(); user.refreshPermissions();
} }
} }
@EventHandler @EventHandler
public void onPlayerQuit(PlayerDisconnectEvent e) { public void onPlayerQuit(PlayerDisconnectEvent e) {
plugin.getPlayerCache().remove(plugin.getUuidCache().getUUID(e.getPlayer().getUniqueId()));
onLeave(e.getPlayer().getUniqueId()); onLeave(e.getPlayer().getUniqueId());
} }

View File

@ -0,0 +1,97 @@
/*
* 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;
import com.google.common.base.Splitter;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@RequiredArgsConstructor
public class BungeePlayerCache {
private final LuckPermsPlugin plugin;
private final UUID uuid;
private final String name;
@Getter
private final Map<String, Boolean> permissions = new ConcurrentHashMap<>();
private final Map<String, Boolean> lookupCache = new HashMap<>();
public void invalidateCache() {
synchronized (lookupCache) {
lookupCache.clear();
}
}
public boolean getPermissionValue(String permission) {
if (plugin.getConfiguration().getDebugPermissionChecks()) {
plugin.getLog().info("Checking if " + name + " has permission: " + permission);
}
permission = permission.toLowerCase();
synchronized (lookupCache) {
if (lookupCache.containsKey(permission)) {
return lookupCache.get(permission);
} else {
boolean t = lookupPermissionValue(permission);
lookupCache.put(permission, t);
return t;
}
}
}
private boolean lookupPermissionValue(String permission) {
if (permissions.containsKey(permission)) {
return permissions.get(permission);
}
if (plugin.getConfiguration().getApplyWildcards()) {
if (permissions.containsKey("*")) {
return permissions.get("*");
}
if (permissions.containsKey("'*'")) {
return permissions.get("'*'");
}
String node = "";
Iterable<String> permParts = Splitter.on('.').split(permission);
for (String s : permParts) {
if (node.equals("")) {
node = s;
} else {
node = node + "." + s;
}
if (permissions.containsKey(node + ".*")) {
return permissions.get(node + ".*");
}
}
}
return false;
}
}

View File

@ -47,16 +47,14 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import java.io.File; import java.io.File;
import java.util.Collections; import java.util.*;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Getter @Getter
public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
private final Map<UUID, BungeePlayerCache> playerCache = new ConcurrentHashMap<>();
private final Set<UUID> ignoringLogs = ConcurrentHashMap.newKeySet(); private final Set<UUID> ignoringLogs = ConcurrentHashMap.newKeySet();
private LPConfiguration configuration; private LPConfiguration configuration;
private UserManager userManager; private UserManager userManager;

View File

@ -22,21 +22,74 @@
package me.lucko.luckperms.users; package me.lucko.luckperms.users;
import me.lucko.luckperms.BungeePlayerCache;
import me.lucko.luckperms.LPBungeePlugin; import me.lucko.luckperms.LPBungeePlugin;
import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent;
import me.lucko.luckperms.api.implementation.internal.UserLink;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.Collections;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class BungeeUser extends User { public class BungeeUser extends User {
private final LPBungeePlugin plugin;
BungeeUser(UUID uuid, LPBungeePlugin plugin) { BungeeUser(UUID uuid, LPBungeePlugin plugin) {
super(uuid, plugin); super(uuid, plugin);
this.plugin = plugin;
} }
BungeeUser(UUID uuid, String username, LPBungeePlugin plugin) { BungeeUser(UUID uuid, String username, LPBungeePlugin plugin) {
super(uuid, username, plugin); super(uuid, username, plugin);
this.plugin = plugin;
} }
@Override @Override
public void refreshPermissions() { public void refreshPermissions() {
// Do nothing. Permissions are applied when needed in a listener. ProxiedPlayer player = plugin.getProxy().getPlayer(plugin.getUuidCache().getExternalUUID(getUuid()));
if (player == null) {
return;
}
BungeePlayerCache playerCache = plugin.getPlayerCache().get(getUuid());
if (playerCache == null) {
return;
}
final String server = player.getServer() == null ? null : (player.getServer().getInfo() == null ? null : player.getServer().getInfo().getName());
// Calculate the permissions that should be applied. This is done async.
Map<String, Boolean> toApply = exportNodes(
plugin.getConfiguration().getServer(),
server,
null,
plugin.getConfiguration().getIncludeGlobalPerms(),
true,
Collections.emptyList()
);
Map<String, Boolean> existing = playerCache.getPermissions();
boolean different = false;
if (toApply.size() != existing.size()) {
different = true;
} else {
for (Map.Entry<String, Boolean> e : existing.entrySet()) {
if (toApply.containsKey(e.getKey()) && toApply.get(e.getKey()) == e.getValue()) {
continue;
}
different = true;
break;
}
}
if (!different) return;
existing.clear();
playerCache.invalidateCache();
existing.putAll(toApply);
plugin.getApiProvider().fireEventAsync(new UserPermissionRefreshEvent(new UserLink(this)));
} }
} }

View File

@ -29,6 +29,7 @@ import me.lucko.luckperms.users.User;
import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.util.Tristate; import org.spongepowered.api.util.Tristate;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -44,11 +45,20 @@ public class LuckPermsUserSubject extends LuckPermsSubject {
@Getter @Getter
private final Map<String, Boolean> permissionCache = new ConcurrentHashMap<>(); private final Map<String, Boolean> permissionCache = new ConcurrentHashMap<>();
@Getter
private final Map<String, Tristate> lookupCache = new HashMap<>();
private LuckPermsUserSubject(User user, LuckPermsService service) { private LuckPermsUserSubject(User user, LuckPermsService service) {
super(user, service); super(user, service);
this.user = user; this.user = user;
} }
public void invalidateCache() {
synchronized (lookupCache) {
lookupCache.clear();
}
}
// TODO don't ignore context // TODO don't ignore context
@Override @Override
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String permission) { public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String permission) {
@ -57,7 +67,18 @@ public class LuckPermsUserSubject extends LuckPermsSubject {
} }
permission = permission.toLowerCase(); permission = permission.toLowerCase();
synchronized (lookupCache) {
if (lookupCache.containsKey(permission)) {
return lookupCache.get(permission);
} else {
Tristate t = lookupPermissionValue(contexts, permission);
lookupCache.put(permission, t);
return t;
}
}
}
private Tristate lookupPermissionValue(Set<Context> contexts, String permission) {
if (permissionCache.containsKey(permission)) { if (permissionCache.containsKey(permission)) {
return Tristate.fromBoolean(permissionCache.get(permission)); return Tristate.fromBoolean(permissionCache.get(permission));
} }

View File

@ -25,6 +25,7 @@ package me.lucko.luckperms.users;
import me.lucko.luckperms.LPSpongePlugin; import me.lucko.luckperms.LPSpongePlugin;
import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent; import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent;
import me.lucko.luckperms.api.implementation.internal.UserLink; import me.lucko.luckperms.api.implementation.internal.UserLink;
import me.lucko.luckperms.api.sponge.LuckPermsUserSubject;
import me.lucko.luckperms.api.sponge.collections.UserCollection; import me.lucko.luckperms.api.sponge.collections.UserCollection;
import java.util.Collections; import java.util.Collections;
@ -62,7 +63,8 @@ class SpongeUser extends User {
); );
try { try {
Map<String, Boolean> existing = uc.getUsers().get(getUuid()).getPermissionCache(); LuckPermsUserSubject us = uc.getUsers().get(getUuid());
Map<String, Boolean> existing = us.getPermissionCache();
boolean different = false; boolean different = false;
if (toApply.size() != existing.size()) { if (toApply.size() != existing.size()) {
@ -80,6 +82,7 @@ class SpongeUser extends User {
if (!different) return; if (!different) return;
existing.clear(); existing.clear();
us.invalidateCache();
existing.putAll(toApply); existing.putAll(toApply);
plugin.getApiProvider().fireEventAsync(new UserPermissionRefreshEvent(new UserLink(this))); plugin.getApiProvider().fireEventAsync(new UserPermissionRefreshEvent(new UserLink(this)));