2021-08-26 04:16:27 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Spottedleaf <spottedleaf@spottedleaf.dev>
|
|
|
|
Date: Sat, 11 Jul 2020 05:09:28 -0700
|
2022-03-18 04:53:36 +01:00
|
|
|
Subject: [PATCH] Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
|
2022-03-18 04:53:36 +01:00
|
|
|
Separate lookup and state access locks prevent lookups
|
|
|
|
from stalling simple state access/write calls
|
2021-08-26 04:16:27 +02:00
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/players/GameProfileCache.java b/src/main/java/net/minecraft/server/players/GameProfileCache.java
|
2022-09-20 03:39:46 +02:00
|
|
|
index 2a4f8aa6697ed6144440970c9abaf9f6e1a2c2ce..8d86645ef264287d01203afd7bba516e78be5743 100644
|
2021-08-26 04:16:27 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/players/GameProfileCache.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/players/GameProfileCache.java
|
|
|
|
@@ -62,6 +62,11 @@ public class GameProfileCache {
|
|
|
|
@Nullable
|
|
|
|
private Executor executor;
|
|
|
|
|
|
|
|
+ // Paper start
|
|
|
|
+ protected final java.util.concurrent.locks.ReentrantLock stateLock = new java.util.concurrent.locks.ReentrantLock();
|
|
|
|
+ protected final java.util.concurrent.locks.ReentrantLock lookupLock = new java.util.concurrent.locks.ReentrantLock();
|
|
|
|
+ // Paper end
|
|
|
|
+
|
|
|
|
public GameProfileCache(GameProfileRepository profileRepository, File cacheFile) {
|
|
|
|
this.profileRepository = profileRepository;
|
|
|
|
this.file = cacheFile;
|
|
|
|
@@ -69,6 +74,7 @@ public class GameProfileCache {
|
|
|
|
}
|
|
|
|
|
|
|
|
private void safeAdd(GameProfileCache.GameProfileInfo entry) {
|
|
|
|
+ try { this.stateLock.lock(); // Paper - allow better concurrency
|
|
|
|
GameProfile gameprofile = entry.getProfile();
|
|
|
|
|
|
|
|
entry.setLastAccess(this.getNextOperation());
|
|
|
|
@@ -83,6 +89,7 @@ public class GameProfileCache {
|
|
|
|
if (uuid != null) {
|
|
|
|
this.profilesByUUID.put(uuid, entry);
|
|
|
|
}
|
|
|
|
+ } finally { this.stateLock.unlock(); } // Paper - allow better concurrency
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-09 23:37:26 +02:00
|
|
|
@@ -138,17 +145,20 @@ public class GameProfileCache {
|
2022-03-18 03:44:45 +01:00
|
|
|
|
|
|
|
// Paper start
|
2022-03-18 04:53:36 +01:00
|
|
|
public @Nullable GameProfile getProfileIfCached(String name) {
|
2022-03-18 03:44:45 +01:00
|
|
|
+ try { this.stateLock.lock(); // Paper - allow better concurrency
|
|
|
|
GameProfileCache.GameProfileInfo entry = this.profilesByName.get(name.toLowerCase(Locale.ROOT));
|
|
|
|
if (entry == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
entry.setLastAccess(this.getNextOperation());
|
|
|
|
return entry.getProfile();
|
|
|
|
+ } finally { this.stateLock.unlock(); } // Paper - allow better concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
// Paper end
|
|
|
|
|
2022-03-18 04:53:36 +01:00
|
|
|
public Optional<GameProfile> get(String name) {
|
2021-08-26 04:16:27 +02:00
|
|
|
String s1 = name.toLowerCase(Locale.ROOT);
|
|
|
|
+ boolean stateLocked = true; try { this.stateLock.lock(); // Paper - allow better concurrency
|
|
|
|
GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByName.get(s1);
|
|
|
|
boolean flag = false;
|
|
|
|
|
2022-06-09 23:37:26 +02:00
|
|
|
@@ -164,8 +174,12 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
if (usercache_usercacheentry != null) {
|
|
|
|
usercache_usercacheentry.setLastAccess(this.getNextOperation());
|
|
|
|
optional = Optional.of(usercache_usercacheentry.getProfile());
|
|
|
|
+ stateLocked = false; this.stateLock.unlock(); // Paper - allow better concurrency
|
|
|
|
} else {
|
|
|
|
+ stateLocked = false; this.stateLock.unlock(); // Paper - allow better concurrency
|
|
|
|
+ try { this.lookupLock.lock(); // Paper - allow better concurrency
|
|
|
|
optional = GameProfileCache.lookupGameProfile(this.profileRepository, name); // Spigot - use correct case for offline players
|
|
|
|
+ } finally { this.lookupLock.unlock(); } // Paper - allow better concurrency
|
|
|
|
if (optional.isPresent()) {
|
|
|
|
this.add((GameProfile) optional.get());
|
|
|
|
flag = false;
|
2022-06-09 23:37:26 +02:00
|
|
|
@@ -177,6 +191,7 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return optional;
|
|
|
|
+ } finally { if (stateLocked) { this.stateLock.unlock(); } } // Paper - allow better concurrency
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getAsync(String username, Consumer<Optional<GameProfile>> consumer) {
|
2022-06-09 23:37:26 +02:00
|
|
|
@@ -203,6 +218,7 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<GameProfile> get(UUID uuid) {
|
|
|
|
+ try { this.stateLock.lock(); // Paper - allow better concurrency
|
|
|
|
GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByUUID.get(uuid);
|
|
|
|
|
|
|
|
if (usercache_usercacheentry == null) {
|
2022-06-09 23:37:26 +02:00
|
|
|
@@ -211,6 +227,7 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
usercache_usercacheentry.setLastAccess(this.getNextOperation());
|
|
|
|
return Optional.of(usercache_usercacheentry.getProfile());
|
|
|
|
}
|
|
|
|
+ } finally { this.stateLock.unlock(); } // Paper - allow better concurrency
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setExecutor(Executor executor) {
|
2022-06-09 23:37:26 +02:00
|
|
|
@@ -291,7 +308,7 @@ public class GameProfileCache {
|
2022-03-18 03:44:45 +01:00
|
|
|
JsonArray jsonarray = new JsonArray();
|
|
|
|
DateFormat dateformat = GameProfileCache.createDateFormat();
|
|
|
|
|
|
|
|
- this.getTopMRUProfiles(org.spigotmc.SpigotConfig.userCacheCap).forEach((usercache_usercacheentry) -> { // Spigot
|
|
|
|
+ this.listTopMRUProfiles(org.spigotmc.SpigotConfig.userCacheCap).forEach((usercache_usercacheentry) -> { // Spigot // Paper - allow better concurrency
|
|
|
|
jsonarray.add(GameProfileCache.writeGameProfile(usercache_usercacheentry, dateformat));
|
|
|
|
});
|
|
|
|
String s = this.gson.toJson(jsonarray);
|
2022-06-09 23:37:26 +02:00
|
|
|
@@ -332,8 +349,19 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private Stream<GameProfileCache.GameProfileInfo> getTopMRUProfiles(int limit) {
|
2022-03-18 03:44:45 +01:00
|
|
|
- return ImmutableList.copyOf(this.profilesByUUID.values()).stream().sorted(Comparator.comparing(GameProfileCache.GameProfileInfo::getLastAccess).reversed()).limit((long) limit);
|
|
|
|
+ // Paper start - allow better concurrency
|
|
|
|
+ return this.listTopMRUProfiles(limit).stream();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<GameProfileCache.GameProfileInfo> listTopMRUProfiles(int limit) {
|
|
|
|
+ try {
|
|
|
|
+ this.stateLock.lock();
|
|
|
|
+ return this.profilesByUUID.values().stream().sorted(Comparator.comparing(GameProfileCache.GameProfileInfo::getLastAccess).reversed()).limit(limit).toList();
|
|
|
|
+ } finally {
|
|
|
|
+ this.stateLock.unlock();
|
|
|
|
+ }
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
2022-03-18 03:44:45 +01:00
|
|
|
+ // Paper end
|
2021-08-26 04:16:27 +02:00
|
|
|
|
|
|
|
private static JsonElement writeGameProfile(GameProfileCache.GameProfileInfo entry, DateFormat dateFormat) {
|
2022-03-18 03:44:45 +01:00
|
|
|
JsonObject jsonobject = new JsonObject();
|