Adjust bStats data sending delay (#4914)

Many servers tend to restart at a fixed time at xx:00 which causes an
uneven distribution of requests on the bStats backend. To circumvent
this problem, this commit introduces some randomness to the initial
and second delay.
This commit is contained in:
Bastian Oppermann 2020-12-20 16:41:00 +01:00 committed by GitHub
parent de31e8c709
commit d5f3458c8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 24 deletions

View file

@ -15,16 +15,15 @@ decisions on behalf of the project.
diff --git a/src/main/java/com/destroystokyo/paper/Metrics.java b/src/main/java/com/destroystokyo/paper/Metrics.java diff --git a/src/main/java/com/destroystokyo/paper/Metrics.java b/src/main/java/com/destroystokyo/paper/Metrics.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..e4923ca371012188793b284cb0ed4280c31f50b6 index 0000000000000000000000000000000000000000..0b9e689d57705965721b5c55bc45d36657f360e4
--- /dev/null --- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/Metrics.java +++ b/src/main/java/com/destroystokyo/paper/Metrics.java
@@ -0,0 +1,664 @@ @@ -0,0 +1,670 @@
+package com.destroystokyo.paper; +package com.destroystokyo.paper;
+ +
+import net.minecraft.server.MinecraftServer; +import net.minecraft.server.MinecraftServer;
+import org.bukkit.Bukkit; +import org.bukkit.Bukkit;
+import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.craftbukkit.util.CraftLegacy;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers; +import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.Plugin;
+ +
@ -39,6 +38,9 @@ index 0000000000000000000000000000000000000000..e4923ca371012188793b284cb0ed4280
+import java.net.URL; +import java.net.URL;
+import java.util.*; +import java.util.*;
+import java.util.concurrent.Callable; +import java.util.concurrent.Callable;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level; +import java.util.logging.Level;
+import java.util.logging.Logger; +import java.util.logging.Logger;
+import java.util.regex.Matcher; +import java.util.regex.Matcher;
@ -52,6 +54,10 @@ index 0000000000000000000000000000000000000000..e4923ca371012188793b284cb0ed4280
+ */ + */
+public class Metrics { +public class Metrics {
+ +
+ // Executor service for requests
+ // We use an executor service because the Bukkit scheduler is affected by server lags
+ private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
+
+ // The version of this bStats class + // The version of this bStats class
+ public static final int B_STATS_VERSION = 1; + public static final int B_STATS_VERSION = 1;
+ +
@ -107,16 +113,16 @@ index 0000000000000000000000000000000000000000..e4923ca371012188793b284cb0ed4280
+ * Starts the Scheduler which submits our data every 30 minutes. + * Starts the Scheduler which submits our data every 30 minutes.
+ */ + */
+ private void startSubmitting() { + private void startSubmitting() {
+ final Timer timer = new Timer(true); + final Runnable submitTask = this::submitData;
+ timer.scheduleAtFixedRate(new TimerTask() { +
+ @Override + // Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution of requests on the
+ public void run() { + // bStats backend. To circumvent this problem, we introduce some randomness into the initial and second delay.
+ submitData(); + // WARNING: You must not modify any part of this Metrics class, including the submit delay or frequency!
+ } + // WARNING: Modifying this code will get your plugin banned on bStats. Just don't do it!
+ }, 1000 * 60 * 5, 1000 * 60 * 30); + long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3));
+ // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start + long secondDelay = (long) (1000 * 60 * (Math.random() * 30));
+ // WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted! + scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS);
+ // WARNING: Just don't do it! + scheduler.scheduleAtFixedRate(submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS);
+ } + }
+ +
+ /** + /**

View file

@ -41,19 +41,23 @@ This also moves all plugins who register "delayed init" tasks to occur just befo
are properly accounted for and wont trip watchdog on init. are properly accounted for and wont trip watchdog on init.
diff --git a/src/main/java/com/destroystokyo/paper/Metrics.java b/src/main/java/com/destroystokyo/paper/Metrics.java diff --git a/src/main/java/com/destroystokyo/paper/Metrics.java b/src/main/java/com/destroystokyo/paper/Metrics.java
index e4923ca371012188793b284cb0ed4280c31f50b6..03c95b30d8a0557482d39468c058e7966114f824 100644 index 081f8d86503b4ab1a704c9fc5ec8a38fd2795427..cef9b0fa633c8b16bb5b99ff94b7b77526034b06 100644
--- a/src/main/java/com/destroystokyo/paper/Metrics.java --- a/src/main/java/com/destroystokyo/paper/Metrics.java
+++ b/src/main/java/com/destroystokyo/paper/Metrics.java +++ b/src/main/java/com/destroystokyo/paper/Metrics.java
@@ -90,6 +90,9 @@ public class Metrics { @@ -92,7 +92,12 @@ public class Metrics {
timer.scheduleAtFixedRate(new TimerTask() { * Starts the Scheduler which submits our data every 30 minutes.
@Override */
public void run() { private void startSubmitting() {
+ if (MinecraftServer.getServer().hasStopped()) { - final Runnable submitTask = this::submitData;
+ return; + final Runnable submitTask = () -> {
+ } + if (MinecraftServer.getServer().hasStopped()) {
submitData(); + return;
} + }
}, 1000 * 60 * 5, 1000 * 60 * 30); + submitData();
+ };
// Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution of requests on the
// bStats backend. To circumvent this problem, we introduce some randomness into the initial and second delay.
diff --git a/src/main/java/net/minecraft/server/CrashReport.java b/src/main/java/net/minecraft/server/CrashReport.java diff --git a/src/main/java/net/minecraft/server/CrashReport.java b/src/main/java/net/minecraft/server/CrashReport.java
index 95e6a6de7ccfc4445d0ac19c5f874c0d533b1712..cc6e6f245ee5e73bd570cf42381bf55ee0b364d3 100644 index 95e6a6de7ccfc4445d0ac19c5f874c0d533b1712..cc6e6f245ee5e73bd570cf42381bf55ee0b364d3 100644
--- a/src/main/java/net/minecraft/server/CrashReport.java --- a/src/main/java/net/minecraft/server/CrashReport.java