Fixed issue with RepositoryBase and Friend/MysqlRepo

Each db call on login gets its own async after the first two account calls are finished.
This commit is contained in:
Jonathan Williams 2014-07-19 13:56:03 -07:00
parent de5c0a77ef
commit 2ae3d89496
10 changed files with 125 additions and 80 deletions

View File

@ -122,7 +122,6 @@ public class CoreClientManager extends MiniPlugin
@EventHandler(priority = EventPriority.LOWEST)
public void AsyncLogin(AsyncPlayerPreLoginEvent event)
{
TimingManager.start(event.getName() + " logging in ASYNC.");
try
{
LoadClient(Add(event.getName()), event.getUniqueId(), event.getAddress().getHostAddress());
@ -132,6 +131,8 @@ public class CoreClientManager extends MiniPlugin
Logger.Instance.log(exception);
event.disallow(Result.KICK_OTHER, "Error retrieving information from web, please retry in a minute.");
System.out.println(exception.getMessage());
}
if (Bukkit.hasWhitelist() && !Get(event.getName()).GetRank().Has(Rank.MODERATOR))
@ -146,12 +147,12 @@ public class CoreClientManager extends MiniPlugin
event.disallow(Result.KICK_WHITELIST, "You are not whitelisted my friend.");
}
TimingManager.stop(event.getName() + " logging in ASYNC.");
}
private void LoadClient(CoreClient client, UUID uuid, String ipAddress)
{
TimingManager.start(client.GetPlayerName() + " LoadClient Total.");
// Prep for mysql
ClientToken token = null;
@ -171,29 +172,37 @@ public class CoreClientManager extends MiniPlugin
// Load client in miniplugins
Bukkit.getServer().getPluginManager().callEvent(new AsyncClientLoadEvent(token, client));
RetrieveClientInformationEvent clientInformationEvent = new RetrieveClientInformationEvent(client.GetPlayerName(), uuid);
try
{
// Mysql
Bukkit.getServer().getPluginManager().callEvent(new RetrieveClientInformationEvent(client.GetPlayerName(), uuid));
Bukkit.getServer().getPluginManager().callEvent(clientInformationEvent);
}
catch (Exception exception)
{
Logger.Instance.log(exception);
System.out.println("Error running RetrieveClientInformationEvent" + exception.getMessage());
}
while (clientInformationEvent.processing())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void LoginTiming(PlayerLoginEvent event)
{
TimingManager.stop(event.getPlayer().getName() + " logging in SYNC.");
TimingManager.stop(client.GetPlayerName() + " LoadClient Total.");
}
@EventHandler(priority = EventPriority.LOWEST)
public void Login(PlayerLoginEvent event)
{
TimingManager.start(event.getPlayer().getName() + " logging in SYNC.");
synchronized(_clientLock)
{
if (!_clientList.containsKey(event.getPlayer().getName()))

View File

@ -11,6 +11,7 @@ public class RetrieveClientInformationEvent extends Event
private String _playerName;
private UUID _uuid;
private int _processingCount;
public RetrieveClientInformationEvent(String playerName, UUID uuid)
{
@ -37,4 +38,19 @@ public class RetrieveClientInformationEvent extends Event
{
return _uuid;
}
public void incrementProcessingCount()
{
_processingCount++;
}
public boolean processing()
{
return _processingCount > 0;
}
public void decreaseProcessingCount()
{
_processingCount--;
}
}

View File

@ -8,7 +8,8 @@ import mineplex.core.database.column.ColumnVarChar;
public class MysqlAccountRepository extends RepositoryBase
{
private static String CREATE_ACCOUNT_TABLE = "CREATE TABLE IF NOT EXISTS accounts (id INT NOT NULL AUTO_INCREMENT, uuid VARCHAR(100), name VARCHAR(40), gems INT, rank VARCHAR(40), rankPerm BOOL, rankExpire LONG, lastLogin LONG, totalPlayTime LONG, PRIMARY KEY (id), UNIQUE INDEX uuidIndex (uuid), UNIQUE INDEX nameIndex (name), INDEX rankIndex (rank));";
private static String ACCOUNT_LOGIN = "INSERT INTO accounts (uuid, name, lastLogin) values(?, ?, now()) ON DUPLICATE KEY UPDATE name=VALUES(name), lastLogin=VALUES(lastLogin);";
private static String ACCOUNT_LOGIN_NEW = "INSERT INTO accounts (uuid, name, lastLogin) values(?, ?, now()) ON DUPLICATE KEY UPDATE name=VALUES(name), lastLogin=VALUES(lastLogin);";
private static String ACCOUNT_LOGIN_UPDATE = "UPDATE accounts SET uuid=?, name=?, lastLogin=now() WHERE uuid = ?;";
public MysqlAccountRepository(JavaPlugin plugin)
{
@ -26,6 +27,12 @@ public class MysqlAccountRepository extends RepositoryBase
public void login(String uuid, String name)
{
executeUpdate(ACCOUNT_LOGIN, new ColumnVarChar("uuid", 100, uuid), new ColumnVarChar("name", 40, name));
int affectedRows = executeUpdate(ACCOUNT_LOGIN_UPDATE, new ColumnVarChar("uuid", 100, uuid), new ColumnVarChar("name", 40, name), new ColumnVarChar("uuid", 100, uuid));
if (affectedRows == 0)
{
executeUpdate(ACCOUNT_LOGIN_NEW, new ColumnVarChar("uuid", 100, uuid), new ColumnVarChar("name", 40, name));
System.out.println("Executed LOGIN_NEW");
}
}
}

View File

@ -61,15 +61,16 @@ public abstract class RepositoryBase
protected int executeUpdate(String query, Column<?>...columns)
{
Connection connection = null;
PreparedStatement preparedStatement = null;
int affectedRows = 0;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(query);
if (_connection == null || !_connection.isValid(1))
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(query);
for (int i=0; i < columns.length; i++)
{
@ -95,18 +96,6 @@ public abstract class RepositoryBase
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return affectedRows;
@ -149,13 +138,14 @@ public abstract class RepositoryBase
protected void executeQuery(String query, ResultSetCallable callable, Column<?>...columns)
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(query);
if (_connection == null || !_connection.isValid(1))
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(query);
executeQuery(preparedStatement, callable, columns);
}
@ -176,31 +166,15 @@ public abstract class RepositoryBase
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
protected int executeUpdate(PreparedStatement preparedStatement, Column<?>...columns)
{
Connection connection = null;
int affectedRows = 0;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
for (int i=0; i < columns.length; i++)
{
columns[i].setValue(preparedStatement, i+1);
@ -212,20 +186,6 @@ public abstract class RepositoryBase
{
exception.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return affectedRows;
}

View File

@ -5,12 +5,16 @@ import java.util.UUID;
import mineplex.core.MiniPlugin;
import mineplex.core.account.event.RetrieveClientInformationEvent;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.timing.TimingManager;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
public class EloManager extends MiniPlugin
{
private static Object _playerEloLock = new Object();
private EloRepository _repository;
private EloRatingSystem _ratingSystem;
private NautHashMap<String, NautHashMap<String, Integer>> _playerElos;
@ -41,15 +45,33 @@ public class EloManager extends MiniPlugin
}
@EventHandler
public void retrievePlayersElos(RetrieveClientInformationEvent event)
public void retrievePlayersElos(final RetrieveClientInformationEvent event)
{
_playerElos.put(event.getUniqueId().toString(), _repository.loadClientInformation(event.getUniqueId()));
event.incrementProcessingCount();
Bukkit.getServer().getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
TimingManager.start(event.getPlayerName() + " elo Account call.");
NautHashMap<String, Integer> eloMap = _repository.loadClientInformation(event.getUniqueId());
TimingManager.stop(event.getPlayerName() + " elo Account call.");
synchronized (_playerEloLock)
{
_playerElos.put(event.getUniqueId().toString(), eloMap);
}
event.decreaseProcessingCount();
}
});
}
public int getElo(UUID uuid, String gameType)
{
int elo = 1000;
synchronized (_playerEloLock)
{
if (_playerElos.containsKey(uuid.toString()))
{
if (_playerElos.get(uuid.toString()).containsKey(gameType))
@ -57,6 +79,7 @@ public class EloManager extends MiniPlugin
elo = _playerElos.get(uuid.toString()).get(gameType);
}
}
}
return elo;
}
@ -94,10 +117,16 @@ public class EloManager extends MiniPlugin
saveElo(uuid.toString(), gameType, elo);
}
public void saveElo(String uuid, String gameType, int elo)
public void saveElo(final String uuid, final String gameType, final int elo)
{
Bukkit.getServer().getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
_repository.saveElo(uuid, gameType, elo);
synchronized (_playerEloLock)
{
if (_playerElos.containsKey(uuid))
{
if (_playerElos.get(uuid).containsKey(gameType))
@ -107,3 +136,6 @@ public class EloManager extends MiniPlugin
}
}
}
});
}
}

View File

@ -14,8 +14,8 @@ import mineplex.core.database.column.ColumnVarChar;
public class FriendRepository extends RepositoryBase
{
private static String CREATE_FRIEND_TABLE = "CREATE TABLE IF NOT EXISTS accountFriend (id INT NOT NULL AUTO_INCREMENT, uuidSource VARCHAR(100), uuidTarget VARCHAR(100), mutual BOOL, PRIMARY KEY (id), INDEX uuidTargetIndex (uuidTarget));";
private static String RETRIEVE_MULTIPLE_FRIEND_RECORDS = "SELECT uuidSource, tA.Name, mutual FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource IN ";
private static String CREATE_FRIEND_TABLE = "CREATE TABLE IF NOT EXISTS accountFriend (id INT NOT NULL AUTO_INCREMENT, uuidSource VARCHAR(100), uuidTarget VARCHAR(100), mutual BOOL, PRIMARY KEY (id), INDEX uuidIndex (uuidSource, uuidTarget));";
private static String RETRIEVE_MULTIPLE_FRIEND_RECORDS = "SELECT uuidSource, tA.Name, mutual, serverName, tA.lastLogin FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource IN ";
private static String RETRIEVE_FRIEND_RECORDS = "SELECT tA.Name, mutual, serverName, tA.lastLogin FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource = ?;";
private static String ADD_FRIEND_RECORD = "INSERT INTO accountFriend (uuidSource, uuidTarget) SELECT fA.uuid AS uuidSource, tA.uuid AS uuidTarget FROM accounts as fA LEFT JOIN accounts AS tA ON tA.name = ? WHERE fA.uuid = ?;";
private static String DELETE_FRIEND_RECORD = "DELETE aF FROM accountFriend AS aF INNER JOIN accounts ON accounts.name = ? WHERE uuidSource = ? AND uuidTarget = accounts.uuid;";

View File

@ -2,6 +2,7 @@ package mineplex.core.preferences;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@ -19,6 +20,7 @@ import mineplex.core.donation.DonationManager;
import mineplex.core.itemstack.ItemStackFactory;
import mineplex.core.preferences.command.PreferencesCommand;
import mineplex.core.preferences.ui.PreferencesShop;
import mineplex.core.timing.TimingManager;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
@ -64,9 +66,19 @@ public class PreferencesManager extends MiniClientPlugin<UserPreferences>
}
@Override
protected void loadClientInformation(RetrieveClientInformationEvent event)
protected void loadClientInformation(final RetrieveClientInformationEvent event)
{
event.incrementProcessingCount();
Bukkit.getServer().getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
TimingManager.start(event.getPlayerName() + " pref Account call.");
Set(event.getPlayerName(), _repository.loadClientInformation(event.getUniqueId()));
TimingManager.stop(event.getPlayerName() + " pref Account call.");
event.decreaseProcessingCount();
}
});
}
public void savePreferences(Player caller)

View File

@ -8,6 +8,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerQuitEvent;
@ -126,8 +127,16 @@ public class StatsManager extends MiniPlugin
}
@EventHandler
public void loadPlayerStats(RetrieveClientInformationEvent event)
public void loadPlayerStats(final RetrieveClientInformationEvent event)
{
event.incrementProcessingCount();
Bukkit.getServer().getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
event.decreaseProcessingCount();
}
});
/* HA YOU THOUGHT I WAS GOING TO RUN DIDN'T YOU? */
if (true)

View File

@ -91,7 +91,7 @@ public class Hub extends JavaPlugin implements IRelation
//Main Modules
ServerStatusManager serverStatusManager = new ServerStatusManager(this, new LagMeter(this, clientManager));
PacketHandler packetHandler = new PacketHandler(this);
new FriendManager(this, packetHandler);
//new FriendManager(this, packetHandler);
PartyManager partyManager = new PartyManager(this, clientManager, preferenceManager);
Portal portal = new Portal(this, serverStatusManager.getCurrentServerName());
AntiHack.Initialize(this, punish, portal);

Binary file not shown.