Skip to content

Commit 319d5fa

Browse files
committed
Fix state locking for getTopMRUProfiles & getProfileIfCached
Also add missing last access update in getProfileIfCached
1 parent 2eeca6f commit 319d5fa

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

patches/server/0139-Basic-PlayerProfile-API.patch

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,17 +599,21 @@ index b49528d8a2c577def4f74ee694ffd53b481acb32..8f5784ed4df46f3c7d4c6b4ff76ad839
599599
GameProfileRepository gameprofilerepository = yggdrasilauthenticationservice.createProfileRepository();
600600
GameProfileCache usercache = new GameProfileCache(gameprofilerepository, new File(file, MinecraftServer.USERID_CACHE_FILE.getName()));
601601
diff --git a/src/main/java/net/minecraft/server/players/GameProfileCache.java b/src/main/java/net/minecraft/server/players/GameProfileCache.java
602-
index c3e3a9950ee05dc97f15ab128e40854901f38a2f..95974d78196397136179f8d6acf1597c557e5a23 100644
602+
index c3e3a9950ee05dc97f15ab128e40854901f38a2f..d7eba4190110b92641664c827c6bc50f62d2ae15 100644
603603
--- a/src/main/java/net/minecraft/server/players/GameProfileCache.java
604604
+++ b/src/main/java/net/minecraft/server/players/GameProfileCache.java
605-
@@ -135,6 +135,13 @@ public class GameProfileCache {
605+
@@ -135,6 +135,17 @@ public class GameProfileCache {
606606
return this.operationCount.incrementAndGet();
607607
}
608608

609609
+ // Paper start
610610
+ @Nullable public GameProfile getProfileIfCached(String name) {
611611
+ GameProfileCache.GameProfileInfo entry = this.profilesByName.get(name.toLowerCase(Locale.ROOT));
612-
+ return entry == null ? null : entry.getProfile();
612+
+ if (entry == null) {
613+
+ return null;
614+
+ }
615+
+ entry.setLastAccess(this.getNextOperation());
616+
+ return entry.getProfile();
613617
+ }
614618
+ // Paper end
615619
+

patches/server/0729-Separate-lookup-locking-from-state-access-in-UserCac.patch

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Subject: [PATCH] Separate lookup locking from state access in UserCache
66
Prevent lookups from stalling simple state access/write calls
77

88
diff --git a/src/main/java/net/minecraft/server/players/GameProfileCache.java b/src/main/java/net/minecraft/server/players/GameProfileCache.java
9-
index 95974d78196397136179f8d6acf1597c557e5a23..6349b33939909435120fdef5e506480108cfbfc1 100644
9+
index d7eba4190110b92641664c827c6bc50f62d2ae15..534373a1e305942d63ffb0fc97ea3f165eca76b5 100644
1010
--- a/src/main/java/net/minecraft/server/players/GameProfileCache.java
1111
+++ b/src/main/java/net/minecraft/server/players/GameProfileCache.java
1212
@@ -62,6 +62,11 @@ public class GameProfileCache {
@@ -46,7 +46,18 @@ index 95974d78196397136179f8d6acf1597c557e5a23..6349b33939909435120fdef5e5064801
4646
Calendar calendar = Calendar.getInstance();
4747

4848
calendar.setTime(new Date());
49-
@@ -142,8 +149,9 @@ public class GameProfileCache {
49+
@@ -137,17 +144,20 @@ public class GameProfileCache {
50+
51+
// Paper start
52+
@Nullable public GameProfile getProfileIfCached(String name) {
53+
+ try { this.stateLock.lock(); // Paper - allow better concurrency
54+
GameProfileCache.GameProfileInfo entry = this.profilesByName.get(name.toLowerCase(Locale.ROOT));
55+
if (entry == null) {
56+
return null;
57+
}
58+
entry.setLastAccess(this.getNextOperation());
59+
return entry.getProfile();
60+
+ } finally { this.stateLock.unlock(); } // Paper - allow better concurrency
5061
}
5162
// Paper end
5263

@@ -57,7 +68,7 @@ index 95974d78196397136179f8d6acf1597c557e5a23..6349b33939909435120fdef5e5064801
5768
GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByName.get(s1);
5869
boolean flag = false;
5970

60-
@@ -159,8 +167,12 @@ public class GameProfileCache {
71+
@@ -163,8 +173,12 @@ public class GameProfileCache {
6172
if (usercache_usercacheentry != null) {
6273
usercache_usercacheentry.setLastAccess(this.getNextOperation());
6374
optional = Optional.of(usercache_usercacheentry.getProfile());
@@ -70,37 +81,57 @@ index 95974d78196397136179f8d6acf1597c557e5a23..6349b33939909435120fdef5e5064801
7081
if (optional.isPresent()) {
7182
this.add((GameProfile) optional.get());
7283
flag = false;
73-
@@ -172,6 +184,7 @@ public class GameProfileCache {
84+
@@ -176,6 +190,7 @@ public class GameProfileCache {
7485
}
7586

7687
return optional;
7788
+ } finally { if (stateLocked) { this.stateLock.unlock(); } } // Paper - allow better concurrency
7889
}
7990

8091
public void getAsync(String username, Consumer<Optional<GameProfile>> consumer) {
81-
@@ -198,6 +211,7 @@ public class GameProfileCache {
92+
@@ -202,6 +217,7 @@ public class GameProfileCache {
8293
}
8394

8495
public Optional<GameProfile> get(UUID uuid) {
8596
+ try { this.stateLock.lock(); // Paper - allow better concurrency
8697
GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByUUID.get(uuid);
8798

8899
if (usercache_usercacheentry == null) {
89-
@@ -206,6 +220,7 @@ public class GameProfileCache {
100+
@@ -210,6 +226,7 @@ public class GameProfileCache {
90101
usercache_usercacheentry.setLastAccess(this.getNextOperation());
91102
return Optional.of(usercache_usercacheentry.getProfile());
92103
}
93104
+ } finally { this.stateLock.unlock(); } // Paper - allow better concurrency
94105
}
95106

96107
public void setExecutor(Executor executor) {
97-
@@ -326,7 +341,9 @@ public class GameProfileCache {
108+
@@ -290,7 +307,7 @@ public class GameProfileCache {
109+
JsonArray jsonarray = new JsonArray();
110+
DateFormat dateformat = GameProfileCache.createDateFormat();
111+
112+
- this.getTopMRUProfiles(org.spigotmc.SpigotConfig.userCacheCap).forEach((usercache_usercacheentry) -> { // Spigot
113+
+ this.listTopMRUProfiles(org.spigotmc.SpigotConfig.userCacheCap).forEach((usercache_usercacheentry) -> { // Spigot // Paper - allow better concurrency
114+
jsonarray.add(GameProfileCache.writeGameProfile(usercache_usercacheentry, dateformat));
115+
});
116+
String s = this.gson.toJson(jsonarray);
117+
@@ -330,8 +347,19 @@ public class GameProfileCache {
98118
}
99119

100120
private Stream<GameProfileCache.GameProfileInfo> getTopMRUProfiles(int limit) {
101-
+ try { this.stateLock.lock(); // Paper - allow better concurrency
102-
return ImmutableList.copyOf(this.profilesByUUID.values()).stream().sorted(Comparator.comparing(GameProfileCache.GameProfileInfo::getLastAccess).reversed()).limit((long) limit);
103-
+ } finally { this.stateLock.unlock(); } // Paper - allow better concurrency
121+
- return ImmutableList.copyOf(this.profilesByUUID.values()).stream().sorted(Comparator.comparing(GameProfileCache.GameProfileInfo::getLastAccess).reversed()).limit((long) limit);
122+
+ // Paper start - allow better concurrency
123+
+ return this.listTopMRUProfiles(limit).stream();
124+
+ }
125+
+
126+
+ private List<GameProfileCache.GameProfileInfo> listTopMRUProfiles(int limit) {
127+
+ try {
128+
+ this.stateLock.lock();
129+
+ return this.profilesByUUID.values().stream().sorted(Comparator.comparing(GameProfileCache.GameProfileInfo::getLastAccess).reversed()).limit(limit).toList();
130+
+ } finally {
131+
+ this.stateLock.unlock();
132+
+ }
104133
}
134+
+ // Paper end
105135

106136
private static JsonElement writeGameProfile(GameProfileCache.GameProfileInfo entry, DateFormat dateFormat) {
137+
JsonObject jsonobject = new JsonObject();

patches/server/0733-Do-not-submit-profile-lookups-to-worldgen-threads.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ index c072819c1ebc772f524649c6c2f8b48d919bf805..bbe69b5b2b1b7ccd3358325c9a65e7e1
3737
public static LongSupplier timeSource = System::nanoTime;
3838
public static final Ticker TICKER = new Ticker() {
3939
diff --git a/src/main/java/net/minecraft/server/players/GameProfileCache.java b/src/main/java/net/minecraft/server/players/GameProfileCache.java
40-
index 6349b33939909435120fdef5e506480108cfbfc1..ea739fae40e819fd82bc275c0af5c8269e12d656 100644
40+
index 534373a1e305942d63ffb0fc97ea3f165eca76b5..e3fbada96b530784fa1de63a322b484865ed194f 100644
4141
--- a/src/main/java/net/minecraft/server/players/GameProfileCache.java
4242
+++ b/src/main/java/net/minecraft/server/players/GameProfileCache.java
43-
@@ -200,7 +200,7 @@ public class GameProfileCache {
43+
@@ -206,7 +206,7 @@ public class GameProfileCache {
4444
} else {
4545
this.requests.put(username, CompletableFuture.supplyAsync(() -> {
4646
return this.get(username);

0 commit comments

Comments
 (0)