Made shutdown session save less disruptive

- Session save is now properly waited for when plugin disables
    The session save attempt times out after 4 seconds instead of
    waiting forever
- If shutdown save is not performed, instead of attempting again on JVM death,
  the sessions are placed into a file that is read next time the plugin
  enables.

Affects issues:
- Fixed #1770
This commit is contained in:
Risto Lahtela 2021-03-13 10:15:29 +02:00
parent 4f411e648b
commit a9ce521c0c
13 changed files with 308 additions and 34 deletions

View File

@ -34,6 +34,11 @@ import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -118,16 +123,30 @@ public class Plan extends JavaPlugin implements PlanPlugin {
return PlanColorScheme.create(system.getConfigSystem().getConfig(), logger);
}
/**
* Disables the plugin.
*/
@Override
public void onDisable() {
if (serverShutdownSave != null) serverShutdownSave.performSave();
storeSessionsOnShutdown();
cancelAllTasks();
if (system != null) system.disable();
logger.info(locale != null ? locale.getString(PluginLang.DISABLED) : PluginLang.DISABLED.getDefault());
logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED));
}
private void storeSessionsOnShutdown() {
if (serverShutdownSave != null) {
Optional<Future<?>> complete = serverShutdownSave.performSave();
if (complete.isPresent()) {
try {
complete.get().get(4, TimeUnit.SECONDS); // wait for completion for 4s
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.error("Failed to save sessions to database on shutdown: " + e.getCause().getMessage());
} catch (TimeoutException e) {
logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED_UNSAVED_SESSIONS_TIMEOUT));
}
}
}
}
public void cancelAllTasks() {

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.modules.bukkit;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage;
import com.djrapitops.plan.extension.ExtensionServerDataUpdater;
import com.djrapitops.plan.gathering.ShutdownDataPreservation;
import com.djrapitops.plan.gathering.ShutdownHook;
import com.djrapitops.plan.gathering.timed.BukkitPingCounter;
import com.djrapitops.plan.gathering.timed.ServerTPSCounter;
@ -74,4 +75,8 @@ public interface BukkitTaskModule {
@IntoSet
TaskSystem.Task bindJSONFileStorageCleanTask(JSONFileStorage.CleanTask cleanTask);
@Binds
@IntoSet
TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation);
}

View File

@ -0,0 +1,159 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.gathering;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.gathering.cache.SessionCache;
import com.djrapitops.plan.gathering.domain.FinishedSession;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.PluginLang;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.LargeStoreQueries;
import com.djrapitops.plan.storage.database.transactions.Transaction;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import net.playeranalytics.plugin.scheduling.RunnableFactory;
import net.playeranalytics.plugin.server.PluginLogger;
import org.apache.commons.text.TextStringBuilder;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Singleton
public class ShutdownDataPreservation extends TaskSystem.Task {
private final Locale locale;
private final DBSystem dbSystem;
private final PluginLogger logger;
private final ErrorLogger errorLogger;
private final Path storeLocation;
@Inject
public ShutdownDataPreservation(
PlanFiles files,
Locale locale,
DBSystem dbSystem,
PluginLogger logger,
ErrorLogger errorLogger
) {
this.locale = locale;
this.dbSystem = dbSystem;
storeLocation = files.getDataDirectory().resolve("unsaved-sessions.csv");
this.logger = logger;
this.errorLogger = errorLogger;
}
public void storePreviouslyPreservedSessions() {
if (storeLocation.toFile().exists()) {
try {
logger.info(locale.getString(PluginLang.ENABLE_NOTIFY_STORING_PRESERVED_SESSIONS));
List<FinishedSession> finishedSessions = loadFinishedSessions();
storeInDB(finishedSessions);
deleteStorageFile();
} catch (IllegalStateException e) {
errorLogger.error(e, ErrorContext.builder().related(storeLocation).build());
}
}
}
private void storeInDB(List<FinishedSession> finishedSessions) {
if (!finishedSessions.isEmpty()) {
try {
dbSystem.getDatabase().executeTransaction(new Transaction() {
@Override
protected void performOperations() {
execute(LargeStoreQueries.storeAllSessionsWithKillAndWorldData(finishedSessions));
}
}).get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
throw new IllegalStateException("Failed to store unsaved sessions in database: " + e.getCause().getMessage(), e);
}
}
}
@Override
public void run() {
storePreviouslyPreservedSessions();
}
@Override
public void register(RunnableFactory runnableFactory) {
runnableFactory.create(this).runTaskAsynchronously();
}
private void deleteStorageFile() {
try {
Files.delete(storeLocation);
} catch (IOException e) {
throw new IllegalStateException("Could not delete " + storeLocation.toFile().getAbsolutePath() + ", " + e.getMessage());
}
}
private List<FinishedSession> loadFinishedSessions() {
try (Stream<String> lines = Files.lines(storeLocation)) {
return lines.map(FinishedSession::deserializeCSV)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
} catch (IOException e) {
throw new IllegalStateException("Could not read " + storeLocation.toFile().getAbsolutePath() + ", " + e.getMessage(), e);
}
}
public void preserveSessionsInCache() {
long now = System.currentTimeMillis();
List<FinishedSession> finishedSessions = SessionCache.getActiveSessions().stream()
.map(session -> session.toFinishedSession(now))
.collect(Collectors.toList());
storeFinishedSessions(finishedSessions);
}
private void storeFinishedSessions(List<FinishedSession> sessions) {
if (sessions.isEmpty()) return;
if (storeLocation.toFile().exists()) {
sessions.addAll(loadFinishedSessions());
}
List<String> lines = sessions.stream().map(FinishedSession::serializeCSV).collect(Collectors.toList());
try {
Files.write(storeLocation, lines, StandardCharsets.UTF_8,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE
);
} catch (IOException e) {
logger.warn("Place this to " + storeLocation.toFile().getAbsolutePath() + " if you wish to store missing sessions:");
logger.warn(new TextStringBuilder().appendWithSeparators(lines, "\n").build());
throw new IllegalStateException("Could not write " + storeLocation.toFile().getAbsolutePath() + ", " + e.getMessage(), e);
}
}
}

View File

@ -21,10 +21,6 @@ import net.playeranalytics.plugin.scheduling.RunnableFactory;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Thread that is run when JVM shuts down.
@ -38,11 +34,11 @@ public class ShutdownHook extends Thread {
private static ShutdownHook activated;
private final ServerShutdownSave serverShutdownSave;
private final ShutdownDataPreservation dataPreservation;
@Inject
public ShutdownHook(ServerShutdownSave serverShutdownSave) {
this.serverShutdownSave = serverShutdownSave;
public ShutdownHook(ShutdownDataPreservation dataPreservation) {
this.dataPreservation = dataPreservation;
}
private static boolean isActivated() {
@ -68,18 +64,7 @@ public class ShutdownHook extends Thread {
@Override
public void run() {
serverShutdownSave.serverIsKnownToBeShuttingDown();
serverShutdownSave.performSave().ifPresent(this::waitForSave);
}
private void waitForSave(Future<?> sessionsAreSavedFuture) {
try {
sessionsAreSavedFuture.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
Logger.getGlobal().log(Level.SEVERE, "Plan failed to save sessions on JVM shutdown.", e);
}
dataPreservation.preserveSessionsInCache();
}
@Singleton

View File

@ -23,18 +23,18 @@ import java.util.Optional;
public class DataMap {
private final Map<Class<?>, Object> data;
private final Map<String, Object> data;
public DataMap() {
this.data = new HashMap<>();
}
public <T> void put(Class<T> type, T value) {
data.put(type, value);
data.put(type.getName(), value);
}
public <T> Optional<T> get(Class<T> ofType) {
return Optional.ofNullable(ofType.cast(data.get(ofType)));
return Optional.ofNullable(ofType.cast(data.get(ofType.getName())));
}

View File

@ -18,6 +18,8 @@ package com.djrapitops.plan.gathering.domain;
import com.djrapitops.plan.delivery.domain.DateHolder;
import com.djrapitops.plan.identification.ServerUUID;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
@ -154,4 +156,51 @@ public class FinishedSession implements DateHolder {
return id;
}
}
/**
* Deserialize csv format of the session.
*
* @param serialized Serialized version of the session
* @return Proper session if the csv had 9 columns or more
* @throws com.google.gson.JsonSyntaxException if serialized format has a json syntax error
*/
public static Optional<FinishedSession> deserializeCSV(String serialized) {
String[] asArray = StringUtils.split(serialized, ';');
if (asArray.length < 9) return Optional.empty();
// Note for the future: Use length to determine version of serialized class
Gson gson = new Gson();
UUID playerUUID = UUID.fromString(asArray[0]);
ServerUUID serverUUID = ServerUUID.fromString(asArray[1]);
long start = Long.parseLong(asArray[2]);
long end = Long.parseLong(asArray[3]);
long afkTime = Long.parseLong(asArray[4]);
DataMap extraData = new DataMap();
extraData.put(WorldTimes.class, gson.fromJson(asArray[5], WorldTimes.class));
extraData.put(PlayerKills.class, gson.fromJson(asArray[6], PlayerKills.class));
extraData.put(MobKillCounter.class, gson.fromJson(asArray[7], MobKillCounter.class));
extraData.put(DeathCounter.class, gson.fromJson(asArray[8], DeathCounter.class));
return Optional.of(new FinishedSession(playerUUID, serverUUID, start, end, afkTime, extraData));
}
/**
* Serialize into csv format.
*
* @return Serialized format
*/
public String serializeCSV() {
Gson gson = new Gson();
return String.valueOf(playerUUID) + ';' +
serverUUID + ';' +
start + ';' +
end + ';' +
afkTime + ';' +
gson.toJson(getExtraData(WorldTimes.class).orElseGet(WorldTimes::new)) + ';' +
gson.toJson(getExtraData(PlayerKills.class).orElseGet(PlayerKills::new)) + ';' +
gson.toJson(getExtraData(MobKillCounter.class).orElseGet(MobKillCounter::new)) + ';' +
gson.toJson(getExtraData(DeathCounter.class).orElseGet(DeathCounter::new));
}
}

View File

@ -40,6 +40,10 @@ public class Locale extends HashMap<Lang, Message> {
return forLangCode(LangCode.fromString(code), files);
}
public static String getStringNullSafe(Locale locale, Lang lang) {
return locale != null ? locale.getString(lang) : lang.getDefault();
}
private LangCode langCode;
public Locale() {

View File

@ -28,6 +28,7 @@ public enum PluginLang implements Lang {
API_ADD_RESOURCE_JS("API - js+", "PageExtension: ${0} added javascript(s) to ${1}, ${2}"),
API_ADD_RESOURCE_CSS("API - css+", "PageExtension: ${0} added stylesheet(s) to ${1}, ${2}"),
ENABLE_NOTIFY_STORING_PRESERVED_SESSIONS("Enable - Storing preserved sessions", "Storing sessions that were preserved before previous shutdown."),
ENABLE_NOTIFY_EMPTY_IP("Enable - Notify Empty IP", "IP in server.properties is empty & Alternative_IP is not in use. Incorrect links might be given!"),
ENABLE_NOTIFY_BAD_IP("Enable - Notify Bad IP", "0.0.0.0 is not a valid address, set up Alternative_IP settings. Incorrect links might be given!"),
ENABLE_NOTIFY_WEB_SERVER_DISABLED("Enable - Notify Webserver disabled", "WebServer was not initialized. (WebServer.DisableWebServer: true)"),
@ -56,6 +57,7 @@ public enum PluginLang implements Lang {
DISABLED_PROCESSING("Disable - Processing", "Processing critical unprocessed tasks. (${0})"),
DISABLED_PROCESSING_COMPLETE("Disable - Processing Complete", "Processing complete."),
DISABLED_UNSAVED_SESSIONS("Disable - Unsaved Session Save", "Saving unfinished sessions.."),
DISABLED_UNSAVED_SESSIONS_TIMEOUT("Disable - Unsaved Session Save Timeout", "Timeout hit, storing the unfinished sessions on next enable instead."),
VERSION_NEWEST("Version - Latest", "You're using the latest version."),
VERSION_AVAILABLE("Version - New", "New Release (${0}) is available ${1}"),

View File

@ -16,6 +16,7 @@
*/
package com.djrapitops.plan.storage.database.transactions.events;
import com.djrapitops.plan.gathering.cache.SessionCache;
import com.djrapitops.plan.gathering.domain.FinishedSession;
import com.djrapitops.plan.storage.database.queries.LargeStoreQueries;
import com.djrapitops.plan.storage.database.transactions.Transaction;
@ -38,5 +39,6 @@ public class ServerShutdownTransaction extends Transaction {
@Override
protected void performOperations() {
execute(LargeStoreQueries.storeAllSessionsWithKillAndWorldData(unsavedSessions));
SessionCache.clear();
}
}

View File

@ -38,6 +38,10 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -101,16 +105,30 @@ public class PlanNukkit extends PluginBase implements PlanPlugin {
return PlanColorScheme.create(system.getConfigSystem().getConfig(), logger);
}
/**
* Disables the plugin.
*/
@Override
public void onDisable() {
if (serverShutdownSave != null) serverShutdownSave.performSave();
storeSessionsOnShutdown();
cancelAllTasks();
if (system != null) system.disable();
logger.info(locale != null ? locale.getString(PluginLang.DISABLED) : PluginLang.DISABLED.getDefault());
logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED));
}
private void storeSessionsOnShutdown() {
if (serverShutdownSave != null) {
Optional<Future<?>> complete = serverShutdownSave.performSave();
if (complete.isPresent()) {
try {
complete.get().get(4, TimeUnit.SECONDS); // wait for completion for 4s
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.error("Failed to save sessions to database on shutdown: " + e.getCause().getMessage());
} catch (TimeoutException e) {
logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED_UNSAVED_SESSIONS_TIMEOUT));
}
}
}
}
public void cancelAllTasks() {

View File

@ -20,6 +20,7 @@ import cn.nukkit.level.Level;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage;
import com.djrapitops.plan.extension.ExtensionServerDataUpdater;
import com.djrapitops.plan.gathering.ShutdownDataPreservation;
import com.djrapitops.plan.gathering.ShutdownHook;
import com.djrapitops.plan.gathering.timed.NukkitPingCounter;
import com.djrapitops.plan.gathering.timed.ServerTPSCounter;
@ -73,4 +74,8 @@ public interface NukkitTaskModule {
@Binds
@IntoSet
TaskSystem.Task bindJSONFileStorageCleanTask(JSONFileStorage.CleanTask cleanTask);
@Binds
@IntoSet
TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation);
}

View File

@ -47,6 +47,10 @@ import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
@Plugin(
@ -142,11 +146,28 @@ public class PlanSponge implements PlanPlugin {
}
public void onDisable() {
if (serverShutdownSave != null) serverShutdownSave.performSave();
storeSessionsOnShutdown();
cancelAllTasks();
if (system != null) system.disable();
logger.info(locale.getString(PluginLang.DISABLED));
logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED));
}
private void storeSessionsOnShutdown() {
if (serverShutdownSave != null) {
Optional<Future<?>> complete = serverShutdownSave.performSave();
if (complete.isPresent()) {
try {
complete.get().get(4, TimeUnit.SECONDS); // wait for completion for 4s
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.error("Failed to save sessions to database on shutdown: " + e.getCause().getMessage());
} catch (TimeoutException e) {
logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED_UNSAVED_SESSIONS_TIMEOUT));
}
}
}
}
public void cancelAllTasks() {

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.modules.sponge;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage;
import com.djrapitops.plan.extension.ExtensionServerDataUpdater;
import com.djrapitops.plan.gathering.ShutdownDataPreservation;
import com.djrapitops.plan.gathering.ShutdownHook;
import com.djrapitops.plan.gathering.timed.ServerTPSCounter;
import com.djrapitops.plan.gathering.timed.SpongePingCounter;
@ -73,4 +74,8 @@ public interface SpongeTaskModule {
@Binds
@IntoSet
TaskSystem.Task bindJSONFileStorageCleanTask(JSONFileStorage.CleanTask cleanTask);
@Binds
@IntoSet
TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation);
}