Paper/patches/server/0618-Add-Channel-initialization-listeners.patch
Spottedleaf 01a13871de
Rewrite chunk system (#8177)
Patch documentation to come

Issues with the old system that are fixed now:
- World generation does not scale with cpu cores effectively.
- Relies on the main thread for scheduling and maintaining chunk state, dropping chunk load/generate rates at lower tps.
- Unreliable prioritisation of chunk gen/load calls that block the main thread.
- Shutdown logic is utterly unreliable, as it has to wait for all chunks to unload - is it guaranteed that the chunk system is in a state on shutdown that it can reliably do this? Watchdog shutdown also typically failed due to thread checks, which is now resolved.
- Saving of data is not unified (i.e can save chunk data without saving entity data, poses problems for desync if shutdown is really abnormal.
- Entities are not loaded with chunks. This caused quite a bit of headache for Chunk#getEntities API, but now the new chunk system loads entities with chunks so that they are ready whenever the chunk loads in. Effectively brings the behavior back to 1.16 era, but still storing entities in their own separate regionfiles.

The above list is not complete. The patch documentation will complete it.

New chunk system hard relies on starlight and dataconverter, and most importantly the new concurrent utilities in ConcurrentUtil.

Some of the old async chunk i/o interface (i.e the old file io thread reroutes _some_ calls to the new file io thread) is kept for plugin compat reasons. It will be removed in the next major version of minecraft.

The old legacy chunk system patches have been moved to the removed folder in case we need them again.
2022-09-26 01:02:51 -07:00

156 lines
6.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Nassim Jahnke <nassim@njahnke.dev>
Date: Thu, 29 Apr 2021 21:19:33 +0200
Subject: [PATCH] Add Channel initialization listeners
diff --git a/src/main/java/io/papermc/paper/network/ChannelInitializeListener.java b/src/main/java/io/papermc/paper/network/ChannelInitializeListener.java
new file mode 100644
index 0000000000000000000000000000000000000000..88099df34c2d74daba9645aadf65b446ca795a91
--- /dev/null
+++ b/src/main/java/io/papermc/paper/network/ChannelInitializeListener.java
@@ -0,0 +1,15 @@
+package io.papermc.paper.network;
+
+import io.netty.channel.Channel;
+import org.checkerframework.checker.nullness.qual.NonNull;
+
+/**
+ * Internal API to register channel initialization listeners.
+ * <p>
+ * This is not officially supported API and we make no guarantees to the existence or state of this interface.
+ */
+@FunctionalInterface
+public interface ChannelInitializeListener {
+
+ void afterInitChannel(@NonNull Channel channel);
+}
diff --git a/src/main/java/io/papermc/paper/network/ChannelInitializeListenerHolder.java b/src/main/java/io/papermc/paper/network/ChannelInitializeListenerHolder.java
new file mode 100644
index 0000000000000000000000000000000000000000..30e62719e0a83525daa33cf41cb61df360c0e046
--- /dev/null
+++ b/src/main/java/io/papermc/paper/network/ChannelInitializeListenerHolder.java
@@ -0,0 +1,74 @@
+package io.papermc.paper.network;
+
+import io.netty.channel.Channel;
+import net.kyori.adventure.key.Key;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Internal API to register channel initialization listeners.
+ * <p>
+ * This is not officially supported API and we make no guarantees to the existence or state of this class.
+ */
+public final class ChannelInitializeListenerHolder {
+
+ private static final Map<Key, ChannelInitializeListener> LISTENERS = new HashMap<>();
+ private static final Map<Key, ChannelInitializeListener> IMMUTABLE_VIEW = Collections.unmodifiableMap(LISTENERS);
+
+ private ChannelInitializeListenerHolder() {
+ }
+
+ /**
+ * Registers whether an initialization listener is registered under the given key.
+ *
+ * @param key key
+ * @return whether an initialization listener is registered under the given key
+ */
+ public static boolean hasListener(@NonNull Key key) {
+ return LISTENERS.containsKey(key);
+ }
+
+ /**
+ * Registers a channel initialization listener called after ServerConnection is initialized.
+ *
+ * @param key key
+ * @param listener initialization listeners
+ */
+ public static void addListener(@NonNull Key key, @NonNull ChannelInitializeListener listener) {
+ LISTENERS.put(key, listener);
+ }
+
+ /**
+ * Removes and returns an initialization listener registered by the given key if present.
+ *
+ * @param key key
+ * @return removed initialization listener if present
+ */
+ public static @Nullable ChannelInitializeListener removeListener(@NonNull Key key) {
+ return LISTENERS.remove(key);
+ }
+
+ /**
+ * Returns an immutable map of registered initialization listeners.
+ *
+ * @return immutable map of registered initialization listeners
+ */
+ public static @NonNull Map<Key, ChannelInitializeListener> getListeners() {
+ return IMMUTABLE_VIEW;
+ }
+
+ /**
+ * Calls the registered listeners with the given channel.
+ *
+ * @param channel channel
+ */
+ public static void callListeners(@NonNull Channel channel) {
+ for (ChannelInitializeListener listener : LISTENERS.values()) {
+ listener.afterInitChannel(channel);
+ }
+ }
+}
diff --git a/src/main/java/io/papermc/paper/network/ConnectionEvent.java b/src/main/java/io/papermc/paper/network/ConnectionEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..0d7e7db9e37ef0183c32b217bd944fb4f41ab83a
--- /dev/null
+++ b/src/main/java/io/papermc/paper/network/ConnectionEvent.java
@@ -0,0 +1,10 @@
+package io.papermc.paper.network;
+
+/**
+ * Internal connection pipeline events.
+ */
+public enum ConnectionEvent {
+
+ COMPRESSION_THRESHOLD_SET,
+ COMPRESSION_DISABLED
+}
diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java
index b8167dc1f37153deb237fbb77f89091d6a25094a..13213bbc9ca5ca20c3eb8a6744dc3204c8c1e423 100644
--- a/src/main/java/net/minecraft/network/Connection.java
+++ b/src/main/java/net/minecraft/network/Connection.java
@@ -596,6 +596,7 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
} else {
this.channel.pipeline().addBefore("encoder", "compress", new CompressionEncoder(compressionThreshold));
}
+ this.channel.pipeline().fireUserEventTriggered(io.papermc.paper.network.ConnectionEvent.COMPRESSION_THRESHOLD_SET); // Paper
} else {
if (this.channel.pipeline().get("decompress") instanceof CompressionDecoder) {
this.channel.pipeline().remove("decompress");
@@ -604,6 +605,7 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
if (this.channel.pipeline().get("compress") instanceof CompressionEncoder) {
this.channel.pipeline().remove("compress");
}
+ this.channel.pipeline().fireUserEventTriggered(io.papermc.paper.network.ConnectionEvent.COMPRESSION_DISABLED); // Paper
}
}
diff --git a/src/main/java/net/minecraft/server/network/ServerConnectionListener.java b/src/main/java/net/minecraft/server/network/ServerConnectionListener.java
index 1b38326c9a709536dc4cccf9af93aede98a1a782..83af90fb0dcb4b1a5a68f655cf66d101b472e8e7 100644
--- a/src/main/java/net/minecraft/server/network/ServerConnectionListener.java
+++ b/src/main/java/net/minecraft/server/network/ServerConnectionListener.java
@@ -114,6 +114,7 @@ public class ServerConnectionListener {
pending.add((Connection) object); // Paper
channel.pipeline().addLast("packet_handler", (ChannelHandler) object);
((Connection) object).setListener(new ServerHandshakePacketListenerImpl(ServerConnectionListener.this.server, (Connection) object));
+ io.papermc.paper.network.ChannelInitializeListenerHolder.callListeners(channel); // Paper
}
}).group((EventLoopGroup) lazyinitvar.get()).localAddress(address, port)).option(ChannelOption.AUTO_READ, false).bind().syncUninterruptibly()); // CraftBukkit
}