Skip to content

Commit

Permalink
Improve user permission data caching; update upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlatemp committed Mar 4, 2022
1 parent 417671d commit ae37832
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 32 deletions.
2 changes: 1 addition & 1 deletion LuckPerms
69 changes: 66 additions & 3 deletions patchs.patch
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,34 @@ index 0be4e97e1..bf66ed15d 100644
prefixed(text()
.color(AQUA)
.append(text("- ", WHITE))
diff --git a/common/src/main/java/me/lucko/luckperms/common/model/manager/user/UserHousekeeper.java b/common/src/main/java/me/lucko/luckperms/common/model/manager/user/UserHousekeeper.java
index f1ae13665..bbbd40edc 100644
--- a/common/src/main/java/me/lucko/luckperms/common/model/manager/user/UserHousekeeper.java
+++ b/common/src/main/java/me/lucko/luckperms/common/model/manager/user/UserHousekeeper.java
@@ -75,7 +75,7 @@ public class UserHousekeeper implements Runnable {

public void cleanup(UUID uuid) {
// unload users which aren't online and who haven't been online (or tried to login) recently
- if (this.recentlyUsed.contains(uuid) || this.recentlyUsedApi.contains(uuid) || this.plugin.getBootstrap().isPlayerOnline(uuid)) {
+ if (this.recentlyUsed.contains(uuid) || this.recentlyUsedApi.contains(uuid) || uuid.getMostSignificantBits() == 0L) {
return;
}

@@ -108,4 +108,14 @@ public class UserHousekeeper implements Runnable {
this.unit = unit;
}
}
+
+ public static class Access {
+ public static ExpiringSet<UUID> recentlyUsed(UserHousekeeper i) {
+ return i.recentlyUsed;
+ }
+
+ public static ExpiringSet<UUID> recentlyUsedApi(UserHousekeeper i) {
+ return i.recentlyUsedApi;
+ }
+ }
}
diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/bootstrap/LuckPermsBootstrap.java b/common/src/main/java/me/lucko/luckperms/common/plugin/bootstrap/LuckPermsBootstrap.java
index 2680ab00e..c904d6fbd 100644
--- a/common/src/main/java/me/lucko/luckperms/common/plugin/bootstrap/LuckPermsBootstrap.java
Expand Down Expand Up @@ -284,6 +312,38 @@ index a4287a7d0..df176f333 100644
+
+ default boolean isHoverEventSupported() { return false; }
}
diff --git a/common/src/main/java/me/lucko/luckperms/common/util/ExpiringSet.java b/common/src/main/java/me/lucko/luckperms/common/util/ExpiringSet.java
index 8ed2535d9..aef840178 100644
--- a/common/src/main/java/me/lucko/luckperms/common/util/ExpiringSet.java
+++ b/common/src/main/java/me/lucko/luckperms/common/util/ExpiringSet.java
@@ -27,6 +27,9 @@ package me.lucko.luckperms.common.util;

import com.github.benmanes.caffeine.cache.Cache;

+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
@@ -57,4 +60,17 @@ public class ExpiringSet<E> {
public void remove(E item) {
this.cache.invalidate(item);
}
+
+ public Collection<E> snapshot() {
+ ArrayList<E> rsp = new ArrayList<>();
+ long currentTime = System.currentTimeMillis();
+ for (Map.Entry<E, Long> e : cache.asMap().entrySet()) {
+ E k = e.getKey();
+ Long v = e.getValue();
+ if (v != null && v > currentTime) {
+ rsp.add(k);
+ }
+ }
+ return rsp;
+ }
}
diff --git a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java
index 22c082368..063585fce 100644
--- a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java
Expand All @@ -298,7 +358,7 @@ index 22c082368..063585fce 100644
return;
}
diff --git a/settings.gradle b/settings.gradle
index db3bdea62..b88ed5914 100644
index 17a0c9c99..b88ed5914 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1,7 +1,6 @@
Expand All @@ -309,7 +369,7 @@ index db3bdea62..b88ed5914 100644
maven {
url 'https://maven.fabricmc.net/'
}
@@ -14,15 +13,4 @@ include (
@@ -14,18 +13,4 @@ include (
'api',
'common',
'common:loader-utils',
Expand All @@ -322,6 +382,9 @@ index db3bdea62..b88ed5914 100644
- 'fabric',
- 'nukkit',
- 'nukkit:loader',
- 'sponge', 'sponge:sponge-service', 'sponge:sponge-service-api8',
- 'sponge',
- 'sponge:loader',
- 'sponge:sponge-service',
- 'sponge:sponge-service-api8',
- 'velocity'
)
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,21 @@ object LPMiraiBootstrap : KotlinPlugin(
}

override fun getPlayerCount(): Int {
return CACHED_USERS.size
return 0
}

override fun getPlayerList(): MutableCollection<String> {
return CACHED_USERS.keys().asSequence()
.map { it.toString() }
.toHashSet()
return mutableListOf()
}

override fun getOnlinePlayers(): MutableCollection<UUID> {
return CACHED_USERS.keys().asSequence()
.map { UUID(MAGIC_UUID_HIGH_BITS, it) }
.toHashSet()
return mutableListOf()
}

override fun isPlayerOnline(uniqueId: UUID): Boolean {
if (uniqueId.mostSignificantBits == 0L) { // System
return true
}
return uniqueId.mostSignificantBits == MAGIC_UUID_HIGH_BITS
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import io.github.karlatemp.luckperms.mirai.internal.LPPermissionService
import io.github.karlatemp.luckperms.mirai.internal.LPPermissionService.uuid
import io.github.karlatemp.luckperms.mirai.internal.Magic_NO_PERMISSION_CHECK
import io.github.karlatemp.luckperms.mirai.internal.OpenApiImpl
import io.github.karlatemp.luckperms.mirai.logging.DebugKit
import me.lucko.luckperms.common.api.LuckPermsApiProvider
import me.lucko.luckperms.common.calculator.CalculatorFactory
import me.lucko.luckperms.common.command.CommandManager
Expand All @@ -49,6 +50,8 @@ import me.lucko.luckperms.common.tasks.CacheHousekeepingTask
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask
import me.lucko.luckperms.common.util.MoreFiles
import net.luckperms.api.LuckPerms
import net.luckperms.api.event.user.UserLoadEvent
import net.luckperms.api.event.user.UserUnloadEvent
import net.luckperms.api.query.QueryOptions
import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
Expand Down Expand Up @@ -359,5 +362,11 @@ object LPMiraiPlugin : AbstractLuckPermsPlugin() {
override fun performFinalSetup() {
LPMiraiBootstrap.logger.info { "Registering internal users...." }
connectionListener0.loadInternalUsers()
apiProvider.eventBus.subscribe(UserLoadEvent::class.java) { event ->
DebugKit.log { "[S] User Loaded: ${event.user.friendlyName}(${event.user.uniqueId})" }
}
apiProvider.eventBus.subscribe(UserUnloadEvent::class.java) { event ->
DebugKit.log { "[S] User Unloaded: ${event.user.friendlyName}(${event.user.uniqueId})" }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
package io.github.karlatemp.luckperms.mirai

import me.lucko.luckperms.common.sender.Sender
import me.lucko.luckperms.common.util.ExpiringSet
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit

const val MAGIC_UUID_HIGH_BITS: Long =
0x14768AEEFFA88746L

val CACHED_USERS = ConcurrentHashMap<Long, Unit>()
val UUID_CONSOLE = Sender.CONSOLE_UUID!!
val UUID_ANY_MEMBER_SELECTOR = UUID(0, 1)
val UUID_ANY_GROUP_SELECTOR = UUID(0, 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ class MiraiConnectionListener : AbstractConnectionListener(LPMiraiPlugin) {

fun recUsr(id: Long) {
val uid = UUID(MAGIC_UUID_HIGH_BITS, id)
DebugKit.log { "Checking user object for $id..." }
val loaded = LPMiraiPlugin.apiProvider.userManager.getUser(uid)
if (loaded == null) {
DebugKit.log { "User object of $id not create. Creating..." }
DebugKit.log { "Registered $id with $uid" }
loadUser(uid, id.toString())
recordConnection(uid)
}
}

override fun recordConnection(uniqueId: UUID?) {
// noop
}

internal fun loadInternalUsers() {
fun rec(uid: UUID, name: String) {
// UUID_ANY_MEMBER_SELECTOR
Expand All @@ -58,16 +61,5 @@ class MiraiConnectionListener : AbstractConnectionListener(LPMiraiPlugin) {
}

fun registerListeners() {
LPMiraiBootstrap.globalEventChannel().subscribeAlways<MessageEvent>(
priority = EventPriority.HIGHEST
) {
loadInternalUsers()
recUsr(sender.id)
message.forEach { elm ->
if (elm is At) {
recUsr(elm.target)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package io.github.karlatemp.luckperms.mirai.commands

import io.github.karlatemp.luckperms.mirai.LPMiraiPlugin
import io.github.karlatemp.luckperms.mirai.MAGIC_UUID_HIGH_BITS
import io.github.karlatemp.luckperms.mirai.internal.LPPermissionService
import io.github.karlatemp.luckperms.mirai.internal.LuckPermsPermission
import io.github.karlatemp.luckperms.mirai.internal.OpenApiImpl
Expand All @@ -21,12 +23,11 @@ import me.lucko.luckperms.common.command.abstraction.SingleCommand
import me.lucko.luckperms.common.command.access.CommandPermission
import me.lucko.luckperms.common.command.spec.CommandSpec
import me.lucko.luckperms.common.command.utils.ArgumentList
import me.lucko.luckperms.common.model.manager.user.UserHousekeeper
import me.lucko.luckperms.common.plugin.LuckPermsPlugin
import me.lucko.luckperms.common.sender.Sender
import me.lucko.luckperms.common.util.Predicates
import net.mamoe.mirai.console.permission.AbstractPermitteeId
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.permission.PermitteeId

internal object SwitchDebugCommand : SingleCommand(
CommandSpec.LPM_DEBUG,
Expand All @@ -48,7 +49,19 @@ internal object SwitchDebugCommand : SingleCommand(
newLine("status - View LuckPermsMirai status")
newLine("logger - Enable/Disable debug logger.")
newLine("trust - Temporarily trust full access to another user. (UNSAFE)")
newLine("check - check [mirai user identity] [lp permission node] Check a permission via mirai permission api")
})
"loadedUsr" -> {
sender.sendmsg(buildString {
LPMiraiPlugin.userManager.all.forEach { (uid, usr) ->
newLine {
append(uid)
append(" -> ")
append(usr.plainDisplayName)
}
}
})
}
"status" -> {
sender.sendmsg(buildString {
newLine("LuckPerms Mirai - Dev Kit")
Expand All @@ -67,6 +80,27 @@ internal object SwitchDebugCommand : SingleCommand(
newLine()
newLine("LPM DebugLogger: " + (if (DebugKit.isDebugEnabled) "ON" else "OFF"))
newLine("Trusted Users: " + DebugKit.trustedUsers)
val keeper = LPMiraiPlugin.userManager.houseKeeper
val ru = UserHousekeeper.Access.recentlyUsed(keeper)
val rua = UserHousekeeper.Access.recentlyUsedApi(keeper)
newLine("UserHousekeeper.recentlyUsed:")
ru.snapshot().forEach { uid ->
newLine {
append(uid)
if (uid.mostSignificantBits == MAGIC_UUID_HIGH_BITS) {
append(" -> ").append(uid.leastSignificantBits)
}
}
}
newLine("UserHousekeeper.recentlyUsedApi:")
rua.snapshot().forEach { uid ->
newLine {
append(uid)
if (uid.mostSignificantBits == MAGIC_UUID_HIGH_BITS) {
append(" -> ").append(uid.leastSignificantBits)
}
}
}
})
}
"trust" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import me.lucko.luckperms.common.cacheddata.result.TristateResult
import me.lucko.luckperms.common.cacheddata.type.PermissionCache
import me.lucko.luckperms.common.verbose.VerboseCheckTarget
import me.lucko.luckperms.common.verbose.event.CheckOrigin
import me.lucko.luckperms.common.verbose.event.PermissionCheckEvent
import net.luckperms.api.query.QueryOptions
import net.luckperms.api.util.Tristate
import net.mamoe.mirai.console.extensions.PermissionServiceProvider
import net.mamoe.mirai.console.permission.*
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.utils.debug
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass
Expand Down Expand Up @@ -298,7 +298,21 @@ internal object LPPermissionService : PermissionService<LuckPermsPermission> {
val permissionData = usr.cachedData.getPermissionData(
LPMiraiPlugin.contextManager.getQueryOptions(permitteeId)
)
DebugKit.log { "UUID $user with lnum ${user.leastSignificantBits}, permissions = ${permissionData.permissionMap}" }
DebugKit.ifDebug {
val logger = LPMiraiBootstrap.logger
logger.debug { "UUID $user with lnum ${user.leastSignificantBits}, permissions: " }
permissionData.permissionMap.forEach { (k, v) ->
logger.debug { " $k -> $v" }
}
logger.debug { "Query options: " }
val opts = permissionData.queryOptions
logger.debug { " `- mode: ${opts.mode()}, flags: ${opts.flags()}" }
logger.debug { " `- options: ${opts.options()}" }
logger.debug { " `- contexts:" }
opts.context().forEach { ctx ->
logger.debug { " `- ${ctx.key} -> ${ctx.value}" }
}
}
return testPermission(permission, permissionData, permission)
|| DebugKit.isTrusted(permitteeId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

package io.github.karlatemp.luckperms.mirai.logging

import io.github.karlatemp.luckperms.mirai.LPMiraiPlugin
import io.github.karlatemp.luckperms.mirai.LPMiraiBootstrap
import net.mamoe.mirai.console.permission.AbstractPermitteeId
import net.mamoe.mirai.console.permission.PermitteeId
import net.mamoe.mirai.utils.debug
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
Expand All @@ -29,7 +30,15 @@ internal object DebugKit {
inline fun log(msg: () -> String) {
contract { callsInPlace(msg, InvocationKind.AT_MOST_ONCE) }
if (isDebugEnabled) {
LPMiraiPlugin.logger.info("[DEBUG] ${msg()}")
LPMiraiBootstrap.logger.debug { "[DEBUG] ${msg()}" }
}
}

@OptIn(ExperimentalContracts::class)
inline fun ifDebug(act: () -> Unit) {
contract { callsInPlace(act, InvocationKind.AT_MOST_ONCE) }
if (isDebugEnabled) {
act()
}
}

Expand Down

0 comments on commit ae37832

Please sign in to comment.