2021-08-26 04:16:27 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
2022-09-26 10:02:51 +02:00
|
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
2021-08-26 04:16:27 +02:00
|
|
|
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
|
2024-01-23 15:43:48 +01:00
|
|
|
index 1ec0f3a7148c2f412421772f6e1dff0bb92a51bc..d45fe762941dd6da45c890706bc9e4c0438522c3 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
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -60,6 +60,11 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
@Nullable
|
|
|
|
private Executor executor;
|
|
|
|
|
2024-01-19 12:30:04 +01:00
|
|
|
+ // Paper start - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
+ 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();
|
2024-01-19 12:30:04 +01:00
|
|
|
+ // Paper end - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
+
|
|
|
|
public GameProfileCache(GameProfileRepository profileRepository, File cacheFile) {
|
|
|
|
this.profileRepository = profileRepository;
|
|
|
|
this.file = cacheFile;
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -67,11 +72,13 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void safeAdd(GameProfileCache.GameProfileInfo entry) {
|
2024-01-19 12:30:04 +01:00
|
|
|
+ try { this.stateLock.lock(); // Paper - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
GameProfile gameprofile = entry.getProfile();
|
|
|
|
|
|
|
|
entry.setLastAccess(this.getNextOperation());
|
2023-09-22 15:32:32 +02:00
|
|
|
this.profilesByName.put(gameprofile.getName().toLowerCase(Locale.ROOT), entry);
|
|
|
|
this.profilesByUUID.put(gameprofile.getId(), entry);
|
2024-01-19 12:30:04 +01:00
|
|
|
+ } finally { this.stateLock.unlock(); } // Paper - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 15:32:32 +02:00
|
|
|
private static Optional<GameProfile> lookupGameProfile(GameProfileRepository repository, String name) {
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -128,17 +135,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) {
|
2024-01-19 12:30:04 +01:00
|
|
|
+ try { this.stateLock.lock(); // Paper - Fix GameProfileCache concurrency
|
2022-03-18 03:44:45 +01:00
|
|
|
GameProfileCache.GameProfileInfo entry = this.profilesByName.get(name.toLowerCase(Locale.ROOT));
|
|
|
|
if (entry == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
entry.setLastAccess(this.getNextOperation());
|
|
|
|
return entry.getProfile();
|
2024-01-19 12:30:04 +01:00
|
|
|
+ } finally { this.stateLock.unlock(); } // Paper - Fix GameProfileCache 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);
|
2024-01-19 12:30:04 +01:00
|
|
|
+ boolean stateLocked = true; try { this.stateLock.lock(); // Paper - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByName.get(s1);
|
|
|
|
boolean flag = false;
|
|
|
|
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -154,8 +164,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());
|
2024-01-19 12:30:04 +01:00
|
|
|
+ stateLocked = false; this.stateLock.unlock(); // Paper - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
} else {
|
2024-01-19 12:30:04 +01:00
|
|
|
+ stateLocked = false; this.stateLock.unlock(); // Paper - Fix GameProfileCache concurrency
|
|
|
|
+ try { this.lookupLock.lock(); // Paper - Fix GameProfileCache concurrency
|
2023-12-28 21:47:57 +01:00
|
|
|
optional = GameProfileCache.lookupGameProfile(this.profileRepository, name); // CraftBukkit - use correct case for offline players
|
2024-01-19 12:30:04 +01:00
|
|
|
+ } finally { this.lookupLock.unlock(); } // Paper - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
if (optional.isPresent()) {
|
|
|
|
this.add((GameProfile) optional.get());
|
|
|
|
flag = false;
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -167,6 +181,7 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return optional;
|
2024-01-19 12:30:04 +01:00
|
|
|
+ } finally { if (stateLocked) { this.stateLock.unlock(); } } // Paper - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 15:32:32 +02:00
|
|
|
public CompletableFuture<Optional<GameProfile>> getAsync(String username) {
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -191,6 +206,7 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<GameProfile> get(UUID uuid) {
|
2024-01-19 12:30:04 +01:00
|
|
|
+ try { this.stateLock.lock(); // Paper - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByUUID.get(uuid);
|
|
|
|
|
|
|
|
if (usercache_usercacheentry == null) {
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -199,6 +215,7 @@ public class GameProfileCache {
|
2021-08-26 04:16:27 +02:00
|
|
|
usercache_usercacheentry.setLastAccess(this.getNextOperation());
|
|
|
|
return Optional.of(usercache_usercacheentry.getProfile());
|
|
|
|
}
|
2024-01-19 12:30:04 +01:00
|
|
|
+ } finally { this.stateLock.unlock(); } // Paper - Fix GameProfileCache concurrency
|
2021-08-26 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setExecutor(Executor executor) {
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -279,7 +296,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
|
2024-01-19 12:30:04 +01:00
|
|
|
+ this.listTopMRUProfiles(org.spigotmc.SpigotConfig.userCacheCap).forEach((usercache_usercacheentry) -> { // Spigot // Paper - Fix GameProfileCache concurrency
|
2022-03-18 03:44:45 +01:00
|
|
|
jsonarray.add(GameProfileCache.writeGameProfile(usercache_usercacheentry, dateformat));
|
|
|
|
});
|
|
|
|
String s = this.gson.toJson(jsonarray);
|
2023-12-28 21:47:57 +01:00
|
|
|
@@ -320,8 +337,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);
|
2024-01-19 12:30:04 +01:00
|
|
|
+ // Paper start - Fix GameProfileCache concurrency
|
2022-03-18 03:44:45 +01:00
|
|
|
+ return this.listTopMRUProfiles(limit).stream();
|
2023-03-23 22:57:03 +01:00
|
|
|
+ }
|
|
|
|
+
|
2022-03-18 03:44:45 +01:00
|
|
|
+ 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();
|
|
|
|
+ }
|
2023-03-23 22:57:03 +01:00
|
|
|
}
|
2024-01-19 12:30:04 +01:00
|
|
|
+ // Paper end - Fix GameProfileCache concurrency
|
2023-03-23 22:57:03 +01:00
|
|
|
|
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();
|