Improve player cleaning system

This commit is contained in:
Magnus Ulf Jørgensen 2017-07-22 17:01:05 +02:00
parent 81ca676c08
commit 1d02942085
14 changed files with 194 additions and 132 deletions

View File

@ -18,7 +18,7 @@ permissions:
massivecore.store.stats: {description: show mstore statistics, default: false}
massivecore.store.listcolls: {description: list collections in a database, default: false}
massivecore.store.copydb: {description: copy database content, default: false}
massivecore.store.playerclean: {description: clean inactive players, default: false}
massivecore.store.clean: {description: clean database, default: false}
massivecore.usys: {description: use the usys command, default: false}
massivecore.usys.multiverse: {description: manage multiverses, default: false}
massivecore.usys.multiverse.list: {description: list multiverses, default: false}
@ -63,7 +63,7 @@ permissions:
massivecore.store.stats: true
massivecore.store.listcolls: true
massivecore.store.copydb: true
massivecore.store.playerclean: true
massivecore.store.clean: true
massivecore.usys: true
massivecore.usys.multiverse: true
massivecore.usys.multiverse.list: true
@ -103,7 +103,7 @@ permissions:
massivecore.kit.rank3:
default: false
children:
massivecore.store.playerclean: true
massivecore.store.clean: true
massivecore.kit.rank2: true
massivecore.kit.rank2:
default: false

View File

@ -128,25 +128,25 @@ public class MassiveCoreMConf extends Entity<MassiveCoreMConf>
public boolean warnOnLocalAlter = false;
// -------------------------------------------- //
// PLAYERCLEAN
// CLEAN
// -------------------------------------------- //
// How often should the task run?
// When set to 0 this feature is disabled. Meaning no cleaning will be done.
// Default: 1 day (Per default once a day.)
public long playercleanPeriodMillis = TimeUnit.MILLIS_PER_DAY;
public long cleanTaskPeriodMillis = TimeUnit.MILLIS_PER_DAY;
// This is used to decide at what time of the day the task will run.
// For Example: If the taskPeriodMillis is 24 hours:
// Set it to 0 for UTC midnight.
// Set it to 3600000 for UTC midnight + 1 hour.
public long playercleanOffsetMillis = 0;
public long cleanTaskOffsetMillis = 0;
// When did the task last run?
// This need not be modified by the server owner.
// It will be set for you automatically.
// 0 means it never ran before.
public long playercleanLastMillis = 0;
public long cleanTaskLastMillis = 0;
// -------------------------------------------- //
// MONGODB

View File

@ -18,7 +18,7 @@ public enum MassiveCorePerm implements Identified
STORE_STATS,
STORE_LISTCOLLS,
STORE_COPYDB,
STORE_PLAYERCLEAN,
STORE_CLEAN,
USYS,
USYS_MULTIVERSE,
USYS_MULTIVERSE_LIST,

View File

@ -20,6 +20,6 @@ public class CmdMassiveCoreStore extends MassiveCoreCommand
public CmdMassiveCoreStoreStats cmdMassiveCoreStoreStats = new CmdMassiveCoreStoreStats();
public CmdMassiveCoreStoreListcolls cmdMassiveCoreStoreListcolls = new CmdMassiveCoreStoreListcolls();
public CmdMassiveCoreStoreCopydb cmdMassiveCoreStoreCopydb = new CmdMassiveCoreStoreCopydb();
public CmdMassiveCoreStorePlayerclean cmdMassiveCoreStorePlayerclean = new CmdMassiveCoreStorePlayerclean();
public CmdMassiveCoreStoreClean cmdMassiveCoreStoreClean = new CmdMassiveCoreStoreClean();
}

View File

@ -2,25 +2,26 @@ package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.container.TypeSet;
import com.massivecraft.massivecore.command.type.store.TypeSenderColl;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.store.inactive.InactiveUtil;
import com.massivecraft.massivecore.command.type.store.TypeColl;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.cleanable.Cleanable;
import com.massivecraft.massivecore.store.cleanable.CleaningUtil;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
import org.bukkit.command.CommandSender;
import java.util.Set;
public class CmdMassiveCoreStorePlayerclean extends MassiveCoreCommand
public class CmdMassiveCoreStoreClean extends MassiveCoreCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdMassiveCoreStorePlayerclean()
public CmdMassiveCoreStoreClean()
{
// Parameters
this.addParameter(TypeSet.get(TypeSenderColl.get()), "player coll", true).setDesc("the coll to clean inactive players from");
this.addParameter(TypeSet.get(TypeColl.get()), "coll", true).setDesc("the coll to clean");
}
// -------------------------------------------- //
@ -30,13 +31,13 @@ public class CmdMassiveCoreStorePlayerclean extends MassiveCoreCommand
@Override
public void perform() throws MassiveException
{
Set<SenderColl<?>> colls = this.readArg();
Set<Coll<? extends Cleanable>> colls = this.readArg();
Set<CommandSender> receivers = MUtil.set(sender, IdUtil.getConsole());
for (SenderColl<?> coll : colls)
for (Coll<? extends Cleanable> coll : colls)
{
InactiveUtil.considerRemoveInactive(coll, receivers);
CleaningUtil.considerClean(coll, receivers);
}
}

View File

@ -3,9 +3,9 @@ package com.massivecraft.massivecore.engine;
import com.massivecraft.massivecore.Engine;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.event.EventMassiveCorePlayercleanToleranceMillis;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.store.inactive.InactiveUtil;
import com.massivecraft.massivecore.event.EventMassiveCorePlayerCleanInactivityToleranceMillis;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.cleanable.CleaningUtil;
import com.massivecraft.massivecore.util.IdUtil;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
@ -14,15 +14,15 @@ import org.bukkit.event.EventPriority;
import java.util.Collections;
import java.util.List;
public class EngineMassiveCorePlayerclean extends Engine
public class EngineMassiveCoreClean extends Engine
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static EngineMassiveCorePlayerclean i = new EngineMassiveCorePlayerclean();
public static EngineMassiveCorePlayerclean get() { return i; }
public EngineMassiveCorePlayerclean()
private static EngineMassiveCoreClean i = new EngineMassiveCoreClean();
public static EngineMassiveCoreClean get() { return i; }
public EngineMassiveCoreClean()
{
// Just check once a minute
this.setPeriod(60L * 20L);
@ -44,7 +44,7 @@ public class EngineMassiveCorePlayerclean extends Engine
final long currentInvocation = getInvocationFromMillis(now);
// ... and the last invocation ...
final long lastInvocation = getInvocationFromMillis(MassiveCoreMConf.get().playercleanLastMillis);
final long lastInvocation = getInvocationFromMillis(MassiveCoreMConf.get().cleanTaskLastMillis);
// ... are different ...
if (currentInvocation == lastInvocation) return;
@ -56,14 +56,14 @@ public class EngineMassiveCorePlayerclean extends Engine
public void invoke(long now)
{
// Update lastMillis
MassiveCoreMConf.get().playercleanLastMillis = now;
MassiveCoreMConf.get().cleanTaskLastMillis = now;
MassiveCoreMConf.get().changed();
List<CommandSender> recipients = Collections.<CommandSender>singletonList(IdUtil.getConsole());
for (SenderColl<?> coll : SenderColl.getSenderInstances())
for (Coll<?> coll : Coll.getInstances())
{
if (!coll.isPlayercleanTaskEnabled()) continue;
InactiveUtil.considerRemoveInactive(now, coll, recipients);
if (!coll.isCleanTaskEnabled()) continue;
CleaningUtil.considerClean(now, coll, recipients);
}
}
@ -77,7 +77,7 @@ public class EngineMassiveCorePlayerclean extends Engine
// Here we accept millis from inside the period by rounding down.
private static long getInvocationFromMillis(long millis)
{
return (millis - MassiveCoreMConf.get().playercleanOffsetMillis) / MassiveCoreMConf.get().playercleanPeriodMillis;
return (millis - MassiveCoreMConf.get().cleanTaskOffsetMillis) / MassiveCoreMConf.get().cleanTaskPeriodMillis;
}
// -------------------------------------------- //
@ -85,9 +85,9 @@ public class EngineMassiveCorePlayerclean extends Engine
// -------------------------------------------- //
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void defaultMillis(EventMassiveCorePlayercleanToleranceMillis event)
public void defaultMillis(EventMassiveCorePlayerCleanInactivityToleranceMillis event)
{
event.getToleranceCauseMillis().put("Default", event.getColl().getPlayercleanToleranceMillis());
event.getToleranceCauseMillis().put("Default", event.getColl().getCleanInactivityToleranceMillis());
}
}

View File

@ -2,13 +2,12 @@ package com.massivecraft.massivecore.event;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.store.SenderEntity;
import com.massivecraft.massivecore.store.inactive.Inactive;
import org.bukkit.event.HandlerList;
import java.util.LinkedHashMap;
import java.util.Map;
public class EventMassiveCorePlayercleanToleranceMillis extends EventMassiveCore
public class EventMassiveCorePlayerCleanInactivityToleranceMillis extends EventMassiveCore
{
// -------------------------------------------- //
// REQUIRED EVENT CODE
@ -22,6 +21,9 @@ public class EventMassiveCorePlayercleanToleranceMillis extends EventMassiveCore
// FIELD
// -------------------------------------------- //
private final long lastActivityMillis;
public long getLastActivityMillis() { return lastActivityMillis; }
private final long now;
public long getNow() { return now; }
@ -37,13 +39,14 @@ public class EventMassiveCorePlayercleanToleranceMillis extends EventMassiveCore
// CONSTRUCT
// -------------------------------------------- //
public EventMassiveCorePlayercleanToleranceMillis(SenderEntity entity)
public EventMassiveCorePlayerCleanInactivityToleranceMillis(long lastActivityMillis, SenderEntity entity)
{
this(System.currentTimeMillis(), entity);
this(lastActivityMillis, System.currentTimeMillis(), entity);
}
public EventMassiveCorePlayercleanToleranceMillis(long now, SenderEntity entity)
public EventMassiveCorePlayerCleanInactivityToleranceMillis(long lastActivityMillis, long now, SenderEntity entity)
{
this.lastActivityMillis = lastActivityMillis;
this.now = now;
this.entity = entity;
}
@ -64,12 +67,15 @@ public class EventMassiveCorePlayercleanToleranceMillis extends EventMassiveCore
return ret;
}
public boolean shouldBeRemoved()
public boolean shouldBeCleaned()
{
long toleranceMillis = getToleranceMillis();
long now = this.getNow();
long lastActivityMillis = ((Inactive)this.getEntity()).getLastActivityMillis();
long lastActivityMillis = this.getLastActivityMillis();
// If unknown don't remove
if (lastActivityMillis <= 0) return false;
long removeTime = lastActivityMillis + toleranceMillis;

View File

@ -142,6 +142,13 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
protected final int entityTargetVersion;
@Override public int getEntityTargetVersion() { return this.entityTargetVersion; }
// This should be false under most circumstances.
// In some cases such as Factions we want it though.
// Especially so we don't change the years old way Factions does it.
private boolean cleanTaskEnabled = false;
public boolean isCleanTaskEnabled() { return this.cleanTaskEnabled; }
public void setCleanTaskEnabled(boolean cleanTaskEnabled) { this.cleanTaskEnabled = cleanTaskEnabled; }
// -------------------------------------------- //
// IDENTIFIED MODIFICATIONS
// -------------------------------------------- //

View File

@ -16,13 +16,6 @@ import java.util.List;
public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements SenderIdSource
{
// This should be false under most circumstances.
// In some cases such as Factions we want it though.
// Especially so we don't change the years old way Factions does it.
private boolean playercleanTaskEnabled = false;
public boolean isPlayercleanTaskEnabled() { return this.playercleanTaskEnabled; }
public void setPlayercleanTaskEnabled(boolean playercleanTaskEnabled) { this.playercleanTaskEnabled = playercleanTaskEnabled; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
@ -203,11 +196,11 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements Se
}
// -------------------------------------------- //
// ACTIVITY MILLIS
// CLEAN
// -------------------------------------------- //
// Must be overriden if they want to use it.
public long getPlayercleanToleranceMillis()
public long getCleanInactivityToleranceMillis()
{
return -1;
}

View File

@ -4,11 +4,13 @@ import com.google.common.base.Objects;
import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.PlayerState;
import com.massivecraft.massivecore.event.EventMassiveCoreAknowledge;
import com.massivecraft.massivecore.event.EventMassiveCorePlayerCleanInactivityToleranceMillis;
import com.massivecraft.massivecore.mixin.MixinDisplayName;
import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.mixin.MixinPlayed;
import com.massivecraft.massivecore.mixin.MixinVisibility;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.store.cleanable.Cleanable;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.PermissionUtil;
import org.bukkit.GameMode;
@ -19,7 +21,7 @@ import org.bukkit.entity.Player;
import java.util.Collection;
import java.util.UUID;
public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E> implements Named
public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E> implements Named, Cleanable
{
// -------------------------------------------- //
// FIELDS
@ -65,6 +67,40 @@ public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E>
return (SenderColl<E>) super.getColl();
}
// -------------------------------------------- //
// OVERRIDE: CLEANABLE
// -------------------------------------------- //
@Override
public boolean shouldBeCleaned(long now)
{
// If unknown don't clean
Long lastActivityMillis = this.getLastPlayed();
if (lastActivityMillis == null) return false;
return this.shouldBeCleaned(now, lastActivityMillis);
}
protected boolean shouldBeCleaned(long now, long lastActivityMillis)
{
// This means it is disabled for this coll
if (this.getColl().getCleanInactivityToleranceMillis() < 0) return false;
EventMassiveCorePlayerCleanInactivityToleranceMillis event = new EventMassiveCorePlayerCleanInactivityToleranceMillis(lastActivityMillis, now, this);
event.run();
return event.shouldBeCleaned();
}
public void preClean()
{
}
public void postClean()
{
}
// -------------------------------------------- //
// CONVENIENCE: DATABASE
// -------------------------------------------- //

View File

@ -0,0 +1,14 @@
package com.massivecraft.massivecore.store.cleanable;
import com.massivecraft.massivecore.store.Coll;
// This interface only really makes sense for SenderEntity's
// But by using this conversion to non-senders should be easier when that is done
public interface Cleanable
{
boolean shouldBeCleaned(long now);
Coll<?> getColl();
void preClean();
void postClean();
}

View File

@ -0,0 +1,87 @@
package com.massivecraft.massivecore.store.cleanable;
import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.Entity;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.command.CommandSender;
import java.util.Collection;
public class CleaningUtil
{
// -------------------------------------------- //
// WHAT DO WE HANDLE?
// -------------------------------------------- //
// The standard used for non-crucial data that can easily be replaced.
public static final long CLEAN_INACTIVITY_TOLERANCE_MILLIS_STANDARD = 3 * TimeUnit.MILLIS_PER_MONTH;
// The standard for important information that can not easily be replaced.
public static final long CLEAN_INACTIVITY_TOLERANCE_MILLIS_IMPORTANT = 15 * TimeUnit.MILLIS_PER_MONTH;
// -------------------------------------------- //
// LOGIC
// -------------------------------------------- //
public static void considerClean(final Coll<? extends Cleanable> coll, final Iterable<CommandSender> recipients)
{
considerClean(System.currentTimeMillis(), coll, recipients);
}
public static void considerClean(long now, final Coll<?> coll, final Iterable<CommandSender> recipients)
{
final long start = System.nanoTime();
int count = 0;
final Collection<? extends Entity<?>> entities = coll.getAll();
// For each entity ...
for (Entity<?> entity : entities)
{
// ... see if they should be cleaned.
boolean result = considerClean(now, entity);
if (result) count++;
}
long nano = System.nanoTime() - start;
int current = coll.getIds().size();
int total = current + count;
double percentage = (((double) count) / total) * 100D;
if (!MUtil.isFinite(percentage)) percentage = 0D;
String message = Txt.parse("<i>Removed <h>%d<i>/<h>%d (%.2f%%) <i>entities from <h>%s <i>took <v>%.2fms<i>.", count, total, percentage, coll.getName(), nano/1000D);
for (CommandSender recipient : recipients)
{
MixinMessage.get().messageOne(recipient, message);
}
}
public static boolean considerClean(long now, Entity<?> entity)
{
if (entity.detached()) return false;
// Consider
if (!shouldBeCleaned(now, entity)) return false;
// Apply
clean(entity);
return true;
}
public static void clean(Entity<?> entity)
{
((Cleanable) entity).preClean();
entity.detach();
((Cleanable) entity).postClean();
}
public static boolean shouldBeCleaned(long now, Entity<?> entity)
{
if (!(entity instanceof Cleanable)) return false;
return ((Cleanable) entity).shouldBeCleaned(now);
}
}

View File

@ -1,6 +0,0 @@
package com.massivecraft.massivecore.store.inactive;
public interface Inactive
{
long getLastActivityMillis();
}

View File

@ -1,76 +0,0 @@
package com.massivecraft.massivecore.store.inactive;
import com.massivecraft.massivecore.event.EventMassiveCorePlayercleanToleranceMillis;
import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.store.SenderEntity;
import org.bukkit.command.CommandSender;
import java.util.Collection;
public class InactiveUtil
{
public static void considerRemoveInactive(final SenderColl<? extends SenderEntity<?>> coll, final Iterable<CommandSender> recipients)
{
considerRemoveInactive(System.currentTimeMillis(), coll, recipients);
}
public static void considerRemoveInactive(long now, final SenderColl<? extends SenderEntity<?>> coll, final Iterable<CommandSender> recipients)
{
final long playercleanToleranceMillis = coll.getPlayercleanToleranceMillis();
if (playercleanToleranceMillis <= 0)
{
for (CommandSender recipient : recipients)
{
MixinMessage.get().msgOne(recipient, "<h>%s<b> does not support player cleaning.", coll.getName());
}
return;
}
final long start = System.currentTimeMillis();
int count = 0;
final Collection<? extends SenderEntity<?>> senderEntitiesOffline = coll.getAllOffline();
// For each offline player ...
for (SenderEntity entity : senderEntitiesOffline)
{
// ... see if they should be removed.
boolean result = considerRemoveInactive(now, entity, recipients);
if (result) count++;
}
long time = System.currentTimeMillis() - start;
for (CommandSender recipient : recipients)
{
int current = coll.getIds().size();
int total = current + count;
double percentage = (((double) count) / total) * 100D;
MixinMessage.get().msgOne(recipient, "<i>Removed <h>%d<i>/<h>%d (%.2f%%) <i>players from <h>%s <i>took <v>%dms<i>.", count, total, percentage, coll.getName(), time);
}
}
public static boolean considerRemoveInactive(long now, SenderEntity<?> entity, Iterable<CommandSender> recipients)
{
if ( ! (entity instanceof Inactive)) return false;
if (entity.detached()) return false;
// Consider
if (!shouldBeRemoved(now, entity)) return false;
//String message = Txt.parse("<i>Player <h>%s<i> with id %s was removed due to inactivity.", entity.getName(), entity.getId());
// Apply
entity.detach();
return true;
}
public static boolean shouldBeRemoved(long now, SenderEntity entity)
{
EventMassiveCorePlayercleanToleranceMillis event = new EventMassiveCorePlayercleanToleranceMillis(now, entity);
event.run();
return event.shouldBeRemoved();
}
}