From ada2fe01a234327fbc28ce456009b5048194d9a3 Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sun, 25 May 2014 01:38:37 +0100 Subject: [PATCH] Utilize write transactions for delaylogin task. --- .../com/earth2me/essentials/Essentials.java | 1 + .../earth2me/essentials/EssentialsConf.java | 33 +++++++++---- .../essentials/EssentialsPlayerListener.java | 49 ++++++++++++------- .../src/com/earth2me/essentials/UserData.java | 10 ++++ 4 files changed, 64 insertions(+), 29 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index e600f2cec..4fb2731a1 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -312,6 +312,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials user.setVanished(false); user.sendMessage(tl("unvanishedReload")); } + user.stopTransaction(); } cleanupOpenInventories(); if (i18n != null) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsConf.java b/Essentials/src/com/earth2me/essentials/EssentialsConf.java index 2ebb2b53e..d59ab9e84 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsConf.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsConf.java @@ -15,6 +15,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -35,9 +36,10 @@ public class EssentialsConf extends YamlConfiguration protected final File configFile; protected String templateName = null; protected static final Charset UTF8 = Charset.forName("UTF-8"); - private Class resourceClass = EssentialsConf.class; + private Class resourceClass = EssentialsConf.class; private static final ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor(); private final AtomicInteger pendingDiskWrites = new AtomicInteger(0); + private final AtomicBoolean transaction = new AtomicBoolean(false); public EssentialsConf(final File configFile) { @@ -95,7 +97,7 @@ public class EssentialsConf extends YamlConfiguration LOGGER.log(Level.SEVERE, null, ex); } } - + if (!configFile.exists()) { if (legacyFileExists()) @@ -187,22 +189,22 @@ public class EssentialsConf extends YamlConfiguration LOGGER.log(Level.SEVERE, "The file " + configFile.toString() + " is broken, it has been renamed to " + broken.toString(), ex.getCause()); } } - + public boolean legacyFileExists() { return false; } - + public void convertLegacyFile() { LOGGER.log(Level.SEVERE, "Unable to import legacy config file."); } - + public boolean altFileExists() { return false; } - + public void convertAltFile() { LOGGER.log(Level.SEVERE, "Unable to import alt config file."); @@ -277,6 +279,17 @@ public class EssentialsConf extends YamlConfiguration this.resourceClass = resClass; } + public void startTransaction() + { + transaction.set(true); + } + + public void stopTransaction() + { + transaction.set(false); + save(); + } + public void save() { try @@ -297,7 +310,10 @@ public class EssentialsConf extends YamlConfiguration @Override public synchronized void save(final File file) throws IOException { - delayedSave(file); + if (!transaction.get()) + { + delayedSave(file); + } } public synchronized void forceSave() @@ -322,7 +338,6 @@ public class EssentialsConf extends YamlConfiguration private Future delayedSave(final File file) { - //long startTime = System.nanoTime(); if (file == null) { throw new IllegalArgumentException("File cannot be null"); @@ -339,8 +354,6 @@ public class EssentialsConf extends YamlConfiguration Future future = EXECUTOR_SERVICE.submit(new WriteRunner(configFile, data, pendingDiskWrites)); - //LOGGER.log(Level.INFO, configFile + " prepared for writing in " + (System.nanoTime() - startTime) + " nsec."); - return future; } diff --git a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java index 94feafa89..6eb4eb6cc 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java @@ -220,6 +220,29 @@ public class EssentialsPlayerListener implements Listener dUser.checkMuteTimeout(currentTime); dUser.updateActivity(false); + IText tempInput = null; + + if (!ess.getSettings().isCommandDisabled("motd")) + { + try + { + tempInput = new TextInput(dUser.getSource(), "motd", true, ess); + } + catch (IOException ex) + { + if (ess.getSettings().isDebug()) + { + LOGGER.log(Level.WARNING, ex.getMessage(), ex); + } + else + { + LOGGER.log(Level.WARNING, ex.getMessage()); + } + } + } + + final IText input = tempInput; + class DelayJoinTask implements Runnable { @Override @@ -232,6 +255,8 @@ public class EssentialsPlayerListener implements Listener return; } + user.startTransaction(); + user.setLastAccountName(user.getBase().getName()); user.setLastLogin(currentTime); user.setDisplayNick(); @@ -270,26 +295,11 @@ public class EssentialsPlayerListener implements Listener ess.getServer().broadcastMessage(message); } - if (!ess.getSettings().isCommandDisabled("motd") && user.isAuthorized("essentials.motd")) + if (input != null && user.isAuthorized("essentials.motd")) { - try - { - final IText input = new TextInput(user.getSource(), "motd", true, ess); - final IText output = new KeywordReplacer(input, user.getSource(), ess); - final TextPager pager = new TextPager(output, true); - pager.showPage("1", null, "motd", user.getSource()); - } - catch (IOException ex) - { - if (ess.getSettings().isDebug()) - { - LOGGER.log(Level.WARNING, ex.getMessage(), ex); - } - else - { - LOGGER.log(Level.WARNING, ex.getMessage()); - } - } + final IText output = new KeywordReplacer(input, user.getSource(), ess); + final TextPager pager = new TextPager(output, true); + pager.showPage("1", null, "motd", user.getSource()); } if (!ess.getSettings().isCommandDisabled("mail") && user.isAuthorized("essentials.mail")) @@ -318,6 +328,7 @@ public class EssentialsPlayerListener implements Listener user.getBase().setFlySpeed(0.1f); user.getBase().setWalkSpeed(0.2f); + user.stopTransaction(); } } diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index d70f7c459..c312fbe84 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -964,4 +964,14 @@ public abstract class UserData extends PlayerExtension implements IConf { config.save(); } + + public void startTransaction() + { + config.startTransaction(); + } + + public void stopTransaction() + { + config.stopTransaction(); + } }