Refactored inventory repository to use 1 second queue

Added 15 second timeout for server connection if db freezes.
This commit is contained in:
Jonathan Williams 2014-12-31 09:38:28 -05:00
parent f0e3520343
commit 4b937dfade
3 changed files with 81 additions and 7 deletions

View File

@ -149,7 +149,9 @@ public class CoreClientManager extends MiniPlugin
try
{
_clientsProcessing.incrementAndGet();
LoadClient(Add(event.getName()), event.getUniqueId(), event.getAddress().getHostAddress());
if (!LoadClient(Add(event.getName()), event.getUniqueId(), event.getAddress().getHostAddress()))
event.disallow(Result.KICK_OTHER, "There was a problem logging you in.");
}
catch(Exception exception)
{
@ -221,9 +223,10 @@ public class CoreClientManager extends MiniPlugin
});
}
private void LoadClient(final CoreClient client, final UUID uuid, String ipAddress)
private boolean LoadClient(final CoreClient client, final UUID uuid, String ipAddress)
{
TimingManager.start(client.GetPlayerName() + " LoadClient Total.");
long timeStart = System.currentTimeMillis();
_clientLoginLock.put(client.GetPlayerName(), new Object());
ClientToken token = null;
@ -252,7 +255,7 @@ public class CoreClientManager extends MiniPlugin
// JSON sql response
Bukkit.getServer().getPluginManager().callEvent(new ClientWebResponseEvent(response, uuid));
while (_clientLoginLock.containsKey(client.GetPlayerName()))
while (_clientLoginLock.containsKey(client.GetPlayerName()) && System.currentTimeMillis() - timeStart < 15000)
{
try
{
@ -264,7 +267,14 @@ public class CoreClientManager extends MiniPlugin
}
}
if (_clientLoginLock.containsKey(client.GetPlayerName()))
{
System.out.println("MYSQL TOO LONG TO LOGIN....");
}
TimingManager.stop(client.GetPlayerName() + " LoadClient Total.");
return !_clientLoginLock.containsKey(client.GetPlayerName());
}
@EventHandler(priority = EventPriority.LOWEST)

View File

@ -7,6 +7,7 @@ import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniDbClientPlugin;
@ -17,6 +18,8 @@ import mineplex.core.inventory.command.GiveItemCommand;
import mineplex.core.inventory.data.Category;
import mineplex.core.inventory.data.InventoryRepository;
import mineplex.core.inventory.data.Item;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
public class InventoryManager extends MiniDbClientPlugin<ClientInventory>
{
@ -27,6 +30,8 @@ public class InventoryManager extends MiniDbClientPlugin<ClientInventory>
private NautHashMap<String, Item> _items = new NautHashMap<String, Item>();
private NautHashMap<String, Category> _categories = new NautHashMap<String, Category>();
private NautHashMap<Player, NautHashMap<String, NautHashMap<String, Integer>>> _inventoryQueue = new NautHashMap<Player, NautHashMap<String, NautHashMap<String, Integer>>>();
public InventoryManager(JavaPlugin plugin, CoreClientManager clientManager)
{
super("Inventory Manager", plugin, clientManager);
@ -76,11 +81,22 @@ public class InventoryManager extends MiniDbClientPlugin<ClientInventory>
Get(player).addItem(new ClientItem(_items.get(item), count));
}
addItemToInventory(null, player, category, item, count);
if (!_inventoryQueue.containsKey(player))
_inventoryQueue.put(player, new NautHashMap<String, NautHashMap<String, Integer>>());
if (!_inventoryQueue.get(player).containsKey(category))
_inventoryQueue.get(player).put(category, new NautHashMap<String, Integer>());
int totalAmount = count;
if (_inventoryQueue.get(player).get(category).containsKey(item))
totalAmount += _inventoryQueue.get(player).get(category).get(item);
_inventoryQueue.get(player).get(category).put(item, totalAmount);
}
public void addItemToInventory(final Callback<Boolean> callback, final Player player, String category, final String item, final int count)
{
{
addItemToInventoryForOffline(new Callback<Boolean>()
{
public void run(Boolean success)
@ -99,6 +115,7 @@ public class InventoryManager extends MiniDbClientPlugin<ClientInventory>
callback.run(success);
}
}, player.getUniqueId().toString(), category, item, count);
}
public boolean validCategory(String category)
@ -179,6 +196,47 @@ public class InventoryManager extends MiniDbClientPlugin<ClientInventory>
});
}
@EventHandler
public void updateInventoryQueue(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC)
return;
for (final Player player : _inventoryQueue.keySet())
{
for (final String category : _inventoryQueue.get(player).keySet())
{
for (final String item : _inventoryQueue.get(player).get(category).keySet())
{
final int count = _inventoryQueue.get(player).get(category).get(item);
addItemToInventoryForOffline(new Callback<Boolean>()
{
public void run(Boolean success)
{
if (!success)
{
System.out.println("Add item to Inventory FAILED for " + player);
if (_items.containsKey(item))
{
Get(player).addItem(new ClientItem(_items.get(item), -count));
}
}
}
}, player.getUniqueId().toString(), category, item, count);
}
}
//Clean
_inventoryQueue.get(player).clear();
}
//Clean
_inventoryQueue.clear();
}
@Override
protected ClientInventory AddPlayer(String player)
{

View File

@ -27,7 +27,8 @@ public class InventoryRepository extends RepositoryBase
private static String RETRIEVE_CATEGORIES = "SELECT id, name FROM itemCategories;";
private static String INSERT_CLIENT_INVENTORY = "INSERT INTO accountInventory (accountId, itemId, count) SELECT accounts.id, ?, ? FROM accounts WHERE accounts.uuid = ? ON DUPLICATE KEY UPDATE count=count + VALUES(count);";
private static String UPDATE_CLIENT_INVENTORY = "UPDATE accountInventory AS AI INNER JOIN accounts ON AI.accountId = accounts.id SET AI.count = AI.count + ? WHERE accounts.uuid = ? AND AI.itemId = ?;";
public InventoryRepository(JavaPlugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
@ -36,9 +37,11 @@ public class InventoryRepository extends RepositoryBase
@Override
protected void initialize()
{
/*
executeUpdate(CREATE_INVENTORY_CATEGORY_TABLE);
executeUpdate(CREATE_INVENTORY_TABLE);
executeUpdate(CREATE_INVENTORY_RELATION_TABLE);
*/
}
@Override
@ -94,7 +97,10 @@ public class InventoryRepository extends RepositoryBase
public boolean incrementClientInventoryItem(String uuid, int itemId, int count)
{
return executeUpdate(INSERT_CLIENT_INVENTORY, new ColumnInt("itemid", itemId), new ColumnInt("count", count), new ColumnVarChar("uuid", 100, uuid)) > 0;
if (executeUpdate(UPDATE_CLIENT_INVENTORY, new ColumnInt("count", count), new ColumnVarChar("uuid", 100, uuid), new ColumnInt("itemid", itemId)) < 1)
return executeUpdate(INSERT_CLIENT_INVENTORY, new ColumnInt("itemid", itemId), new ColumnInt("count", count), new ColumnVarChar("uuid", 100, uuid)) > 0;
else
return true;
}
public ClientInventory loadClientInformation(ResultSet resultSet) throws SQLException