mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Restore the Spigot and Paper timeouts Iris raised when auto-configuration is off
Iris rewrote spigot.yml timeout-time and paper-global.yml early-warning-delay without keeping the old values, so turning the setting off never undid it. The originals are now recorded in server-timeouts.json before the raise and restored on later starts unless the operator has changed the value since. Writes are atomic and go through symbolic links to the real file.
Upstream: b357eee18
This commit is contained in:
1 parent
3558a09a1f
commit
b18cc310d7
3 files changed
+394
-30
No files matched your search
@@ -46,7 +46,6 @@ import lombok.NonNull;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.NamespacedKey;
|
import org.bukkit.NamespacedKey;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -92,6 +91,9 @@ public class ServerConfigurator {
|
|||||||
private static final String COMPILER_INPUT_FINGERPRINT_CACHE = "datapack-compiler-input-fingerprint";
|
private static final String COMPILER_INPUT_FINGERPRINT_CACHE = "datapack-compiler-input-fingerprint";
|
||||||
static final String POST_COMPILE_RESTART_WARNING = "Iris installed updated datapack registry entries; "
|
static final String POST_COMPILE_RESTART_WARNING = "Iris installed updated datapack registry entries; "
|
||||||
+ "restart the server before creating worlds or opening Studios.";
|
+ "restart the server before creating worlds or opening Studios.";
|
||||||
|
private static final Path SPIGOT_CONFIGURATION = Path.of("spigot.yml");
|
||||||
|
private static final Path PAPER_GLOBAL_CONFIGURATION = Path.of("config", "paper-global.yml");
|
||||||
|
private static final String TIMEOUT_RECOVERY_FILE = "server-timeouts.json";
|
||||||
private static final int FINGERPRINT_BUFFER_BYTES = 64 * 1024;
|
private static final int FINGERPRINT_BUFFER_BYTES = 64 * 1024;
|
||||||
private static volatile boolean loadedDatapackRuntimeReady;
|
private static volatile boolean loadedDatapackRuntimeReady;
|
||||||
private static volatile String loadedDatapackCompilerInputFingerprint = "";
|
private static volatile String loadedDatapackCompilerInputFingerprint = "";
|
||||||
@@ -108,11 +110,27 @@ public class ServerConfigurator {
|
|||||||
}
|
}
|
||||||
IrisSettings.IrisSettingsAutoconfiguration s = IrisSettings.get().getAutoConfiguration();
|
IrisSettings.IrisSettingsAutoconfiguration s = IrisSettings.get().getAutoConfiguration();
|
||||||
if (s.isConfigureSpigotTimeoutTime()) {
|
if (s.isConfigureSpigotTimeoutTime()) {
|
||||||
J.attempt(ServerConfigurator::increaseKeepAliveSpigot);
|
Throwable failure = J.attemptCatch(ServerConfigurator::increaseKeepAliveSpigot);
|
||||||
|
if (failure != null) {
|
||||||
|
IrisLogging.reportError("Unable to persist the Spigot timeout setting.", failure);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Throwable failure = J.attemptCatch(() -> timeoutConfiguration().restore(ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY));
|
||||||
|
if (failure != null) {
|
||||||
|
IrisLogging.reportError("Unable to restore the Spigot timeout setting.", failure);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s.isConfigurePaperWatchdogDelay()) {
|
if (s.isConfigurePaperWatchdogDelay()) {
|
||||||
J.attempt(ServerConfigurator::increasePaperWatchdog);
|
Throwable failure = J.attemptCatch(ServerConfigurator::increasePaperWatchdog);
|
||||||
|
if (failure != null) {
|
||||||
|
IrisLogging.reportError("Unable to persist the Paper watchdog setting.", failure);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Throwable failure = J.attemptCatch(() -> timeoutConfiguration().restore(ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY));
|
||||||
|
if (failure != null) {
|
||||||
|
IrisLogging.reportError("Unable to restore the Paper watchdog setting.", failure);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DefaultPackBootstrapProvisioner.wasProvisionedThisStartup()) {
|
if (DefaultPackBootstrapProvisioner.wasProvisionedThisStartup()) {
|
||||||
@@ -230,34 +248,45 @@ public class ServerConfigurator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void increaseKeepAliveSpigot() throws IOException, InvalidConfigurationException {
|
private static void increaseKeepAliveSpigot() throws IOException, InvalidConfigurationException {
|
||||||
File spigotConfig = new File("spigot.yml");
|
increaseKeepAliveSpigot(SPIGOT_CONFIGURATION, timeoutConfiguration());
|
||||||
FileConfiguration f = new YamlConfiguration();
|
}
|
||||||
f.load(spigotConfig);
|
|
||||||
long tt = f.getLong("settings.timeout-time");
|
|
||||||
|
|
||||||
long spigotTimeout = TimeUnit.MINUTES.toSeconds(5);
|
static void increaseKeepAliveSpigot(Path file, ServerTimeoutConfiguration timeouts) throws IOException, InvalidConfigurationException {
|
||||||
|
long previous = loadConfiguration(file).getLong(ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY);
|
||||||
if (tt < spigotTimeout) {
|
long minimum = TimeUnit.MINUTES.toSeconds(5);
|
||||||
IrisLogging.warn("Updating spigot.yml timeout-time: " + tt + " -> " + spigotTimeout + " (5 minutes)");
|
if (previous >= minimum) {
|
||||||
IrisLogging.warn("You can disable this change (autoconfigureServer) in Iris settings, then change back the value.");
|
return;
|
||||||
f.set("settings.timeout-time", spigotTimeout);
|
|
||||||
f.save(spigotConfig);
|
|
||||||
}
|
}
|
||||||
|
timeouts.raise(file, ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY, minimum);
|
||||||
|
IrisLogging.warn("Updated " + file + " settings.timeout-time: " + previous + " -> " + minimum
|
||||||
|
+ " seconds. Takes effect after a server restart.");
|
||||||
|
IrisLogging.warn("Disable autoConfiguration.configureSpigotTimeoutTime in iris.json to restore the original value.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void increasePaperWatchdog() throws IOException, InvalidConfigurationException {
|
private static void increasePaperWatchdog() throws IOException, InvalidConfigurationException {
|
||||||
File spigotConfig = new File("config/paper-global.yml");
|
increasePaperWatchdog(PAPER_GLOBAL_CONFIGURATION, timeoutConfiguration());
|
||||||
FileConfiguration f = new YamlConfiguration();
|
}
|
||||||
f.load(spigotConfig);
|
|
||||||
long tt = f.getLong("watchdog.early-warning-delay");
|
|
||||||
|
|
||||||
long watchdog = TimeUnit.MINUTES.toMillis(3);
|
static void increasePaperWatchdog(Path file, ServerTimeoutConfiguration timeouts) throws IOException, InvalidConfigurationException {
|
||||||
if (tt < watchdog) {
|
long previous = loadConfiguration(file).getLong(ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY);
|
||||||
IrisLogging.warn("Updating paper.yml watchdog early-warning-delay: " + tt + " -> " + watchdog + " (3 minutes)");
|
long minimum = TimeUnit.MINUTES.toMillis(3);
|
||||||
IrisLogging.warn("You can disable this change (autoconfigureServer) in Iris settings, then change back the value.");
|
if (previous >= minimum) {
|
||||||
f.set("watchdog.early-warning-delay", watchdog);
|
return;
|
||||||
f.save(spigotConfig);
|
|
||||||
}
|
}
|
||||||
|
timeouts.raise(file, ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY, minimum);
|
||||||
|
IrisLogging.warn("Updated " + file + " watchdog.early-warning-delay: " + previous + " -> " + minimum
|
||||||
|
+ " milliseconds. Takes effect after a server restart.");
|
||||||
|
IrisLogging.warn("Disable autoConfiguration.configurePaperWatchdogDelay in iris.json to restore the original value.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static YamlConfiguration loadConfiguration(Path file) throws IOException, InvalidConfigurationException {
|
||||||
|
YamlConfiguration configuration = new YamlConfiguration();
|
||||||
|
configuration.load(file.toFile());
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ServerTimeoutConfiguration timeoutConfiguration() {
|
||||||
|
return new ServerTimeoutConfiguration(IrisPlatforms.get().dataFolder().toPath().resolve(TIMEOUT_RECOVERY_FILE));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static KList<File> getDatapacksFolder() {
|
public static KList<File> getDatapacksFolder() {
|
||||||
@@ -842,7 +871,7 @@ public class ServerConfigurator {
|
|||||||
|
|
||||||
private static void writeFingerprintCache(Path cacheFile, PackFingerprint fingerprint) {
|
private static void writeFingerprintCache(Path cacheFile, PackFingerprint fingerprint) {
|
||||||
try {
|
try {
|
||||||
writeFingerprintAtomic(cacheFile, fingerprint.content() + "\n" + fingerprint.metadata());
|
writeStringAtomic(cacheFile, fingerprint.content() + "\n" + fingerprint.metadata());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
IrisLogging.warn("Failed to write datapack fingerprint cache: " + e.getMessage());
|
IrisLogging.warn("Failed to write datapack fingerprint cache: " + e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -850,23 +879,23 @@ public class ServerConfigurator {
|
|||||||
|
|
||||||
private static void writeCompilerInputFingerprintCache(Path cacheFile, String fingerprint) {
|
private static void writeCompilerInputFingerprintCache(Path cacheFile, String fingerprint) {
|
||||||
try {
|
try {
|
||||||
writeFingerprintAtomic(cacheFile, fingerprint);
|
writeStringAtomic(cacheFile, fingerprint);
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
IrisLogging.warn("Failed to write datapack compiler-input fingerprint cache: "
|
IrisLogging.warn("Failed to write datapack compiler-input fingerprint cache: "
|
||||||
+ exception.getMessage());
|
+ exception.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeFingerprintAtomic(Path target, String fingerprint) throws IOException {
|
static void writeStringAtomic(Path target, String content) throws IOException {
|
||||||
Path absoluteTarget = target.toAbsolutePath().normalize();
|
Path absoluteTarget = target.toAbsolutePath().normalize();
|
||||||
Path parent = absoluteTarget.getParent();
|
Path parent = absoluteTarget.getParent();
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
throw new IOException("Datapack fingerprint target has no parent: " + absoluteTarget);
|
throw new IOException("Atomic write target has no parent: " + absoluteTarget);
|
||||||
}
|
}
|
||||||
Files.createDirectories(parent);
|
Files.createDirectories(parent);
|
||||||
Path staged = Files.createTempFile(parent, ".datapack-fingerprint-", ".tmp");
|
Path staged = Files.createTempFile(parent, ".iris-write-", ".tmp");
|
||||||
try {
|
try {
|
||||||
Files.writeString(staged, fingerprint, StandardCharsets.UTF_8);
|
Files.writeString(staged, content, StandardCharsets.UTF_8);
|
||||||
try (FileChannel channel = FileChannel.open(staged, StandardOpenOption.WRITE)) {
|
try (FileChannel channel = FileChannel.open(staged, StandardOpenOption.WRITE)) {
|
||||||
channel.force(true);
|
channel.force(true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
package art.arcane.iris.core;
|
||||||
|
|
||||||
|
import art.arcane.iris.spi.IrisLogging;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
final class ServerTimeoutConfiguration {
|
||||||
|
static final String SPIGOT_TIMEOUT_KEY = "settings.timeout-time";
|
||||||
|
static final String PAPER_WATCHDOG_KEY = "watchdog.early-warning-delay";
|
||||||
|
|
||||||
|
private static final Object MUTATION_LOCK = new Object();
|
||||||
|
private static final Gson JSON = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
|
||||||
|
|
||||||
|
private final Path recoveryFile;
|
||||||
|
|
||||||
|
ServerTimeoutConfiguration(Path recoveryFile) {
|
||||||
|
this.recoveryFile = Objects.requireNonNull(recoveryFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
void raise(Path file, String key, long minimum) throws IOException {
|
||||||
|
synchronized (MUTATION_LOCK) {
|
||||||
|
Path target = file.toRealPath();
|
||||||
|
YamlConfiguration configuration = load(target);
|
||||||
|
Long original = value(configuration, key);
|
||||||
|
List<SavedTimeout> changes = readChanges();
|
||||||
|
SavedTimeout saved = find(changes, target, key);
|
||||||
|
if (saved == null || !Objects.equals(original, saved.original()) && !Objects.equals(original, saved.applied())) {
|
||||||
|
changes.remove(saved);
|
||||||
|
changes.add(new SavedTimeout(target.toString(), key, original, minimum));
|
||||||
|
writeChanges(changes);
|
||||||
|
}
|
||||||
|
configuration.set(key, minimum);
|
||||||
|
ServerConfigurator.writeStringAtomic(target, configuration.saveToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void restore(String key) throws IOException {
|
||||||
|
synchronized (MUTATION_LOCK) {
|
||||||
|
List<SavedTimeout> changes = readChanges();
|
||||||
|
for (SavedTimeout saved : List.copyOf(changes)) {
|
||||||
|
if (!saved.key().equals(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
restore(saved);
|
||||||
|
changes.remove(saved);
|
||||||
|
writeChanges(changes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restore(SavedTimeout saved) throws IOException {
|
||||||
|
Path target = Path.of(saved.file());
|
||||||
|
if (!Files.exists(target)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
YamlConfiguration configuration = load(target);
|
||||||
|
if (!Objects.equals(value(configuration, saved.key()), saved.applied())) {
|
||||||
|
IrisLogging.info("Kept the current " + target + " " + saved.key()
|
||||||
|
+ " because it differs from the value saved by Iris.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
configuration.set(saved.key(), saved.original());
|
||||||
|
ServerConfigurator.writeStringAtomic(target, configuration.saveToString());
|
||||||
|
IrisLogging.info("Restored " + target + " " + saved.key() + " to "
|
||||||
|
+ (saved.original() == null ? "its server default" : saved.original())
|
||||||
|
+ ". Takes effect after a server restart.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<SavedTimeout> readChanges() throws IOException {
|
||||||
|
if (!Files.exists(recoveryFile)) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
SavedTimeouts saved = JSON.fromJson(Files.readString(recoveryFile), SavedTimeouts.class);
|
||||||
|
if (saved == null) {
|
||||||
|
throw new IOException("Empty server timeout recovery data: " + recoveryFile);
|
||||||
|
}
|
||||||
|
return new ArrayList<>(saved.changes());
|
||||||
|
} catch (JsonParseException | IllegalArgumentException exception) {
|
||||||
|
throw new IOException("Invalid server timeout recovery data: " + recoveryFile, exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeChanges(List<SavedTimeout> changes) throws IOException {
|
||||||
|
ServerConfigurator.writeStringAtomic(recoveryFile, JSON.toJson(new SavedTimeouts(changes)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SavedTimeout find(List<SavedTimeout> changes, Path target, String key) {
|
||||||
|
for (SavedTimeout change : changes) {
|
||||||
|
if (change.file().equals(target.toString()) && change.key().equals(key)) {
|
||||||
|
return change;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static YamlConfiguration load(Path file) throws IOException {
|
||||||
|
YamlConfiguration configuration = new YamlConfiguration();
|
||||||
|
try {
|
||||||
|
configuration.load(file.toFile());
|
||||||
|
} catch (InvalidConfigurationException exception) {
|
||||||
|
throw new IOException("Invalid server configuration: " + file, exception);
|
||||||
|
}
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Long value(YamlConfiguration configuration, String key) throws IOException {
|
||||||
|
if (!configuration.isSet(key)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!configuration.isInt(key) && !configuration.isLong(key)) {
|
||||||
|
throw new IOException("Server timeout " + key + " must be an integer.");
|
||||||
|
}
|
||||||
|
return configuration.getLong(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private record SavedTimeout(String file, String key, Long original, long applied) {
|
||||||
|
private SavedTimeout {
|
||||||
|
Objects.requireNonNull(file, "file");
|
||||||
|
Objects.requireNonNull(key, "key");
|
||||||
|
if (!Path.of(file).isAbsolute() || applied <= 0
|
||||||
|
|| !key.equals(SPIGOT_TIMEOUT_KEY) && !key.equals(PAPER_WATCHDOG_KEY)) {
|
||||||
|
throw new IllegalArgumentException("Invalid saved server timeout");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private record SavedTimeouts(List<SavedTimeout> changes) {
|
||||||
|
private SavedTimeouts {
|
||||||
|
changes = List.copyOf(changes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,192 @@
|
|||||||
|
package art.arcane.iris.core;
|
||||||
|
|
||||||
|
import art.arcane.iris.spi.IrisLogging;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||||
|
import static org.mockito.Mockito.mockStatic;
|
||||||
|
|
||||||
|
public class ServerTimeoutConfigurationTest {
|
||||||
|
private static final String SPIGOT_KEY = ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY;
|
||||||
|
private static final String PAPER_KEY = ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void originalValuesSurviveRestartsAndRestoreIndependently() throws Exception {
|
||||||
|
Path spigot = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
Path paper = configuration("paper-global.yml", "watchdog:\n early-warning-delay: 10000\n early-warning-every: 5000\n");
|
||||||
|
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(spigot, timeouts());
|
||||||
|
ServerConfigurator.increasePaperWatchdog(paper, timeouts());
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(spigot, timeouts());
|
||||||
|
timeouts().restore(SPIGOT_KEY);
|
||||||
|
assertEquals(60L, load(spigot).getLong(SPIGOT_KEY));
|
||||||
|
assertEquals(180000L, load(paper).getLong(PAPER_KEY));
|
||||||
|
timeouts().restore(PAPER_KEY);
|
||||||
|
assertEquals(10000L, load(paper).getLong(PAPER_KEY));
|
||||||
|
assertEquals(5000L, load(paper).getLong("watchdog.early-warning-every"));
|
||||||
|
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
|
||||||
|
assertFalse(Files.readString(recoveryFile()).contains("paper-global.yml"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void valueAtOrAboveTheMinimumIsLeftAlone() throws Exception {
|
||||||
|
String original = "settings:\n timeout-time: 600\n";
|
||||||
|
Path file = configuration("spigot.yml", original);
|
||||||
|
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||||
|
assertEquals(original, Files.readString(file));
|
||||||
|
assertFalse(Files.exists(recoveryFile()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void restoringAbsentSettingRemovesTheOverride() throws Exception {
|
||||||
|
Path file = configuration("spigot.yml", "settings:\n unrelated: retained\n");
|
||||||
|
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||||
|
assertEquals(300L, load(file).getLong(SPIGOT_KEY));
|
||||||
|
timeouts().restore(SPIGOT_KEY);
|
||||||
|
assertFalse(load(file).isSet(SPIGOT_KEY));
|
||||||
|
assertEquals("retained", load(file).getString("settings.unrelated"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void restorePreservesOperatorChangesAndOtherSettings() throws Exception {
|
||||||
|
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||||
|
Files.writeString(file, "# Operator settings\nsettings:\n timeout-time: 90\n unrelated: retained\n");
|
||||||
|
String changed = Files.readString(file);
|
||||||
|
timeouts().restore(SPIGOT_KEY);
|
||||||
|
assertEquals(changed, Files.readString(file));
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||||
|
timeouts().restore(SPIGOT_KEY);
|
||||||
|
assertEquals(90L, load(file).getLong(SPIGOT_KEY));
|
||||||
|
assertEquals("retained", load(file).getString("settings.unrelated"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void symlinkSurvivesRaiseAndRestore() throws Exception {
|
||||||
|
Path target = configuration("shared-spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
Path link = temporaryFolder.getRoot().toPath().resolve("spigot.yml");
|
||||||
|
Files.createSymbolicLink(link, target.getFileName());
|
||||||
|
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(link, timeouts());
|
||||||
|
assertEquals(300L, load(target).getLong(SPIGOT_KEY));
|
||||||
|
timeouts().restore(SPIGOT_KEY);
|
||||||
|
assertTrue(Files.isSymbolicLink(link));
|
||||||
|
assertEquals(60L, load(target).getLong(SPIGOT_KEY));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void aLaterAdjustmentRecordsTheOperatorsNewOriginal() throws Exception {
|
||||||
|
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||||
|
Files.writeString(file, "settings:\n timeout-time: 90\n");
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||||
|
timeouts().restore(SPIGOT_KEY);
|
||||||
|
assertEquals(90L, load(file).getLong(SPIGOT_KEY));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void failedRecoveryWriteLeavesServerSettingsUntouched() throws Exception {
|
||||||
|
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
Path blocked = temporaryFolder.newFile("blocked").toPath();
|
||||||
|
ServerTimeoutConfiguration timeouts = new ServerTimeoutConfiguration(blocked.resolve("server-timeouts.json"));
|
||||||
|
assertThrows(IOException.class, () -> timeouts.raise(file, SPIGOT_KEY, 300L));
|
||||||
|
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void failedServerWriteKeepsTheOriginalRecorded() throws Exception {
|
||||||
|
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
try (MockedStatic<ServerConfigurator> writes = mockStatic(ServerConfigurator.class, CALLS_REAL_METHODS)) {
|
||||||
|
writes.when(() -> ServerConfigurator.writeStringAtomic(eq(file.toRealPath()), anyString()))
|
||||||
|
.thenThrow(new IOException("blocked server write"));
|
||||||
|
assertThrows(IOException.class, () -> timeouts().raise(file, SPIGOT_KEY, 300L));
|
||||||
|
}
|
||||||
|
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
|
||||||
|
assertTrue(Files.readString(recoveryFile()).contains("\"original\": 60"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void interruptedRestoreRetriesWithoutOverwritingLaterOperatorEdits() throws Exception {
|
||||||
|
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||||
|
try (MockedStatic<ServerConfigurator> writes = mockStatic(ServerConfigurator.class, CALLS_REAL_METHODS)) {
|
||||||
|
writes.when(() -> ServerConfigurator.writeStringAtomic(eq(recoveryFile()), anyString()))
|
||||||
|
.thenThrow(new IOException("interrupted recovery write"));
|
||||||
|
assertThrows(IOException.class, () -> timeouts().restore(SPIGOT_KEY));
|
||||||
|
}
|
||||||
|
assertTrue(Files.readString(recoveryFile()).contains("spigot.yml"));
|
||||||
|
Files.writeString(file, "settings:\n timeout-time: 120\n");
|
||||||
|
timeouts().restore(SPIGOT_KEY);
|
||||||
|
assertEquals(120L, load(file).getLong(SPIGOT_KEY));
|
||||||
|
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void recordForADeletedFileIsDropped() throws Exception {
|
||||||
|
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||||
|
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||||
|
Files.delete(file);
|
||||||
|
timeouts().restore(SPIGOT_KEY);
|
||||||
|
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void malformedRecoveryDataPreventsNewConfigurationWrites() throws Exception {
|
||||||
|
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||||
|
Files.writeString(recoveryFile(), "{invalid");
|
||||||
|
assertThrows(IOException.class, () -> timeouts().raise(file, SPIGOT_KEY, 300L));
|
||||||
|
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
|
||||||
|
assertEquals("{invalid", Files.readString(recoveryFile()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path configuration(String name, String content) throws IOException {
|
||||||
|
Path file = temporaryFolder.newFile(name).toPath();
|
||||||
|
Files.writeString(file, content);
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServerTimeoutConfiguration timeouts() {
|
||||||
|
return new ServerTimeoutConfiguration(recoveryFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path recoveryFile() {
|
||||||
|
return temporaryFolder.getRoot().toPath().resolve("server-timeouts.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static YamlConfiguration load(Path file) throws Exception {
|
||||||
|
YamlConfiguration configuration = new YamlConfiguration();
|
||||||
|
configuration.load(file.toFile());
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user