Ensure NameManager caches stay consistent with multiple threads accessing them

This commit is contained in:
Phoenix616 2023-03-24 00:08:48 +01:00
parent d52c329618
commit 013a21159f
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
2 changed files with 68 additions and 57 deletions

View File

@ -1,20 +1,21 @@
package com.Acrobot.Breeze.Collection; package com.Acrobot.Breeze.Collection;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
public class SimpleCache<K, V> { public class SimpleCache<K, V> {
private final LinkedHashMap<K, V> map; private final Map<K, V> map;
public SimpleCache(int cacheSize) { public SimpleCache(int cacheSize) {
map = new LinkedHashMap<K, V>(cacheSize * 10/9, 0.7f, true) { map = Collections.synchronizedMap(new LinkedHashMap<K, V>(cacheSize * 10/9, 0.7f, true) {
@Override @Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > cacheSize; return size() > cacheSize;
} }
}; });
} }
public V put(K key, V value) { public V put(K key, V value) {

View File

@ -36,6 +36,8 @@ import java.util.logging.Level;
*/ */
@SuppressWarnings("UnusedAssignment") // I deliberately set the variables to null while initializing @SuppressWarnings("UnusedAssignment") // I deliberately set the variables to null while initializing
public class NameManager implements Listener { public class NameManager implements Listener {
private static final Object accountsLock = new Object();
private static Dao<Account, String> accounts; private static Dao<Account, String> accounts;
private static SimpleCache<String, Account> usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE); private static SimpleCache<String, Account> usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
@ -97,6 +99,7 @@ public class NameManager implements Listener {
*/ */
public static Account getAccount(UUID uuid) { public static Account getAccount(UUID uuid) {
try { try {
synchronized (accountsLock) {
return uuidToAccount.get(uuid, () -> { return uuidToAccount.get(uuid, () -> {
try { try {
Account account = accounts.queryBuilder().orderBy("lastSeen", false).where().eq("uuid", new SelectArg(uuid)).queryForFirst(); Account account = accounts.queryBuilder().orderBy("lastSeen", false).where().eq("uuid", new SelectArg(uuid)).queryForFirst();
@ -111,6 +114,7 @@ public class NameManager implements Listener {
} }
throw new Exception("Could not find account for " + uuid); throw new Exception("Could not find account for " + uuid);
}); });
}
} catch (ExecutionException ignored) { } catch (ExecutionException ignored) {
return null; return null;
} }
@ -127,6 +131,7 @@ public class NameManager implements Listener {
Preconditions.checkNotNull(fullName, "fullName cannot be null!"); Preconditions.checkNotNull(fullName, "fullName cannot be null!");
Preconditions.checkArgument(!fullName.isEmpty(), "fullName cannot be empty!"); Preconditions.checkArgument(!fullName.isEmpty(), "fullName cannot be empty!");
try { try {
synchronized (accountsLock) {
return usernameToAccount.get(fullName, () -> { return usernameToAccount.get(fullName, () -> {
try { try {
Account account = accounts.queryBuilder().orderBy("lastSeen", false).where().eq("name", new SelectArg(fullName)).queryForFirst(); Account account = accounts.queryBuilder().orderBy("lastSeen", false).where().eq("name", new SelectArg(fullName)).queryForFirst();
@ -140,6 +145,7 @@ public class NameManager implements Listener {
} }
throw new Exception("Could not find account for " + fullName); throw new Exception("Could not find account for " + fullName);
}); });
}
} catch (ExecutionException ignored) { } catch (ExecutionException ignored) {
return null; return null;
} }
@ -167,6 +173,7 @@ public class NameManager implements Listener {
Account account = null; Account account = null;
try { try {
synchronized (accountsLock) {
account = shortToAccount.get(shortName, () -> { account = shortToAccount.get(shortName, () -> {
try { try {
Account a = accounts.queryBuilder().where().eq("shortName", new SelectArg(shortName)).queryForFirst(); Account a = accounts.queryBuilder().where().eq("shortName", new SelectArg(shortName)).queryForFirst();
@ -179,6 +186,7 @@ public class NameManager implements Listener {
} }
throw new Exception("Could not find account for " + shortName); throw new Exception("Could not find account for " + shortName);
}); });
}
} catch (ExecutionException ignored) {} } catch (ExecutionException ignored) {}
return account; return account;
} }
@ -222,6 +230,7 @@ public class NameManager implements Listener {
final UUID uuid = player.getUniqueId(); final UUID uuid = player.getUniqueId();
Account latestAccount = null; Account latestAccount = null;
synchronized (accountsLock) {
try { try {
latestAccount = accounts.queryBuilder().where().eq("uuid", new SelectArg(uuid)).and().eq("name", new SelectArg(player.getName())).queryForFirst(); latestAccount = accounts.queryBuilder().where().eq("uuid", new SelectArg(uuid)).and().eq("name", new SelectArg(player.getName())).queryForFirst();
} catch (SQLException e) { } catch (SQLException e) {
@ -243,6 +252,7 @@ public class NameManager implements Listener {
usernameToAccount.put(latestAccount.getName(), latestAccount); usernameToAccount.put(latestAccount.getName(), latestAccount);
uuidToAccount.put(uuid, latestAccount); uuidToAccount.put(uuid, latestAccount);
shortToAccount.put(latestAccount.getShortName(), latestAccount); shortToAccount.put(latestAccount.getShortName(), latestAccount);
}
return latestAccount; return latestAccount;
} }