From 1b7a6c45e675b5e7ff5a54891df1bfc2bfd4db0e Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 27 May 2013 08:41:06 +0200 Subject: [PATCH] Adding in a sync-log. This makes it easier to find colls that sync to much --- .../massivecraft/mcore/InternalListener.java | 2 +- src/com/massivecraft/mcore/MCore.java | 6 ++- src/com/massivecraft/mcore/MPlugin.java | 5 +-- .../mcorecmd/CmdMCoreMStoreListcolls.java | 2 +- .../mcore/mcorecmd/CmdMCoreMStoreStats.java | 8 ++++ src/com/massivecraft/mcore/store/Coll.java | 45 ++++++++++++++++++- .../mcore/store/CollInterface.java | 1 + .../mcore/store/ExamineThread.java | 2 +- .../massivecraft/mcore/store/SenderColl.java | 2 +- 9 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/com/massivecraft/mcore/InternalListener.java b/src/com/massivecraft/mcore/InternalListener.java index 4f91b826..71ea2fa6 100644 --- a/src/com/massivecraft/mcore/InternalListener.java +++ b/src/com/massivecraft/mcore/InternalListener.java @@ -263,7 +263,7 @@ public class InternalListener implements Listener public void syncAllForPlayer(Player player) { String playerName = player.getName(); - for (Coll coll : Coll.instances) + for (Coll coll : Coll.getInstances()) { if (!(coll instanceof SenderColl)) continue; SenderColl pcoll = (SenderColl)coll; diff --git a/src/com/massivecraft/mcore/MCore.java b/src/com/massivecraft/mcore/MCore.java index a1c95b2d..3a123220 100644 --- a/src/com/massivecraft/mcore/MCore.java +++ b/src/com/massivecraft/mcore/MCore.java @@ -103,7 +103,7 @@ public class MCore extends MPlugin { public void run() { - for (Coll coll : Coll.instances) + for (Coll coll : Coll.getInstances()) { coll.onTick(); } @@ -119,7 +119,9 @@ public class MCore extends MPlugin { // This is safe since all plugins using Persist should bukkit-depend this plugin. // Note this one must be before preEnable. dooh. - Coll.instances.clear(); + // TODO: Create something like "deinit all" (perhaps a forloop) to readd this. + // TODO: Test and ensure reload compat. + // Coll.instances.clear(); // Start the examine thread ExamineThread.get().start(); diff --git a/src/com/massivecraft/mcore/MPlugin.java b/src/com/massivecraft/mcore/MPlugin.java index b3606f6c..5351be15 100644 --- a/src/com/massivecraft/mcore/MPlugin.java +++ b/src/com/massivecraft/mcore/MPlugin.java @@ -53,11 +53,10 @@ public abstract class MPlugin extends JavaPlugin implements Listener public void onDisable() { // Collection shutdowns. - for (Coll coll : Coll.instances) + for (Coll coll : Coll.getInstances()) { if (coll.getPlugin() != this) continue; - coll.syncAll(); // TODO: Save outwards only? We may want to avoid loads at this stage... - Coll.instances.remove(coll); + coll.deinit(); } log("Disabled"); diff --git a/src/com/massivecraft/mcore/mcorecmd/CmdMCoreMStoreListcolls.java b/src/com/massivecraft/mcore/mcorecmd/CmdMCoreMStoreListcolls.java index 6dabc91f..3df7b4a9 100644 --- a/src/com/massivecraft/mcore/mcorecmd/CmdMCoreMStoreListcolls.java +++ b/src/com/massivecraft/mcore/mcorecmd/CmdMCoreMStoreListcolls.java @@ -49,7 +49,7 @@ public class CmdMCoreMStoreListcolls extends MCoreCommand Coll coll = null; - for (Coll collCandidate : Coll.instances) + for (Coll collCandidate : Coll.getInstances()) { if (!collCandidate.getName().equals(collname)) continue; if (collCandidate.getDb() != db) continue; diff --git a/src/com/massivecraft/mcore/mcorecmd/CmdMCoreMStoreStats.java b/src/com/massivecraft/mcore/mcorecmd/CmdMCoreMStoreStats.java index 5725814b..3c87b6f7 100644 --- a/src/com/massivecraft/mcore/mcorecmd/CmdMCoreMStoreStats.java +++ b/src/com/massivecraft/mcore/mcorecmd/CmdMCoreMStoreStats.java @@ -4,6 +4,7 @@ import java.util.List; import com.massivecraft.mcore.MCorePerm; import com.massivecraft.mcore.cmd.req.ReqHasPerm; +import com.massivecraft.mcore.store.Coll; import com.massivecraft.mcore.store.ExamineThread; import com.massivecraft.mcore.util.Txt; @@ -21,6 +22,13 @@ public class CmdMCoreMStoreStats extends MCoreCommand { msg(Txt.titleize("MStore Statistics")); msg("Last Examine Duration: %dms", ExamineThread.get().getLastDurationMillis()); + msg("== Coll | Sync Count In | Sync Count Out =="); + for (String name : Coll.getNames()) + { + long in = Coll.getSyncCount(name, true); + long out = Coll.getSyncCount(name, false); + msg("%s | %d | %d", name, in, out); + } } } diff --git a/src/com/massivecraft/mcore/store/Coll.java b/src/com/massivecraft/mcore/store/Coll.java index 49731723..2d04e49f 100644 --- a/src/com/massivecraft/mcore/store/Coll.java +++ b/src/com/massivecraft/mcore/store/Coll.java @@ -8,6 +8,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.CopyOnWriteArrayList; @@ -16,6 +18,7 @@ import org.bukkit.plugin.Plugin; import com.massivecraft.mcore.MCore; import com.massivecraft.mcore.MPlugin; +import com.massivecraft.mcore.NaturalOrderComparator; import com.massivecraft.mcore.Predictate; import com.massivecraft.mcore.store.accessor.Accessor; import com.massivecraft.mcore.xlib.gson.Gson; @@ -27,7 +30,31 @@ public class Coll implements CollInterface // GLOBAL REGISTRY // -------------------------------------------- // - public static List> instances = new CopyOnWriteArrayList>(); + // All instances registered here are considered inited. + private static List> instances = new CopyOnWriteArrayList>(); + public static List> getInstances() { return instances; } + + private static TreeSet names = new TreeSet(NaturalOrderComparator.get()); + public static TreeSet getNames() { return names; } + + // Log database syncronization for display in the "/mcore mstore stats" command. + private static Map name2out = new TreeMap(String.CASE_INSENSITIVE_ORDER); + private static Map name2in = new TreeMap(String.CASE_INSENSITIVE_ORDER); + + public static long getSyncCount(String name, boolean in) + { + Long count = (in ? name2in.get(name) : name2out.get(name)); + if (count == null) return 0; + return count; + } + + public static void addSyncCount(String name, boolean in) + { + long count = getSyncCount(name, in); + count++; + Map map = (in ? name2in : name2out); + map.put(name, count); + } // -------------------------------------------- // // WHAT DO WE HANDLE? @@ -516,16 +543,20 @@ public class Coll implements CollInterface case LOCAL_ALTER: case LOCAL_ATTACH: this.saveToRemote(id); + if (this.inited()) addSyncCount(this.getName(), false); break; case LOCAL_DETACH: this.removeAtRemote(id); + if (this.inited()) addSyncCount(this.getName(), false); break; case REMOTE_ALTER: case REMOTE_ATTACH: this.loadFromRemote(id); + if (this.inited()) addSyncCount(this.getName(), true); break; case REMOTE_DETACH: this.removeAtLocal(id); + if (this.inited()) addSyncCount(this.getName(), true); break; default: this.clearIdentifiedChanges(id); @@ -657,8 +688,20 @@ public class Coll implements CollInterface public void init() { if (this.inited()) return; + // TODO: Could this be more efficient by considering it's the first sync? this.syncAll(); instances.add(this); + names.add(this.getName()); + } + + @Override + public void deinit() + { + if (!this.inited()) return; + // TODO: Save outwards only? We may want to avoid loads at this stage... + this.syncAll(); + instances.remove(this); + names.remove(this.getName()); } @Override diff --git a/src/com/massivecraft/mcore/store/CollInterface.java b/src/com/massivecraft/mcore/store/CollInterface.java index 223e06a2..f516e558 100644 --- a/src/com/massivecraft/mcore/store/CollInterface.java +++ b/src/com/massivecraft/mcore/store/CollInterface.java @@ -141,6 +141,7 @@ public interface CollInterface // -------------------------------------------- // public void init(); + public void deinit(); public boolean inited(); diff --git a/src/com/massivecraft/mcore/store/ExamineThread.java b/src/com/massivecraft/mcore/store/ExamineThread.java index 6562e272..e07968b7 100644 --- a/src/com/massivecraft/mcore/store/ExamineThread.java +++ b/src/com/massivecraft/mcore/store/ExamineThread.java @@ -41,7 +41,7 @@ public class ExamineThread extends Thread try { long before = System.currentTimeMillis(); - for (Coll coll: Coll.instances) + for (Coll coll : Coll.getInstances()) { coll.findSuspects(); } diff --git a/src/com/massivecraft/mcore/store/SenderColl.java b/src/com/massivecraft/mcore/store/SenderColl.java index 6fb149a1..e0433771 100644 --- a/src/com/massivecraft/mcore/store/SenderColl.java +++ b/src/com/massivecraft/mcore/store/SenderColl.java @@ -123,7 +123,7 @@ public class SenderColl> extends Coll implements Se public static void setSenderRefferences(String senderId, CommandSender sender) { - for (Coll coll : Coll.instances) + for (Coll coll : Coll.getInstances()) { if (!(coll instanceof SenderColl)) continue; SenderColl senderColl = (SenderColl)coll;