Skip to content

Commit 01a1387

Browse files
authored
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.
1 parent abe53a7 commit 01a1387

File tree

942 files changed

+20129
-2695
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

942 files changed

+20129
-2695
lines changed

patches/server/0004-Paper-config-files.patch

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -431,14 +431,13 @@ index 0000000000000000000000000000000000000000..c2dca89291361d60cbf160cab77749cb
431431
+}
432432
diff --git a/src/main/java/io/papermc/paper/configuration/GlobalConfiguration.java b/src/main/java/io/papermc/paper/configuration/GlobalConfiguration.java
433433
new file mode 100644
434-
index 0000000000000000000000000000000000000000..aaecd691922a28e971b859175574c80a330edb8e
434+
index 0000000000000000000000000000000000000000..84785fed0d85d78c4caf8fabe35c0e89a59240d5
435435
--- /dev/null
436436
+++ b/src/main/java/io/papermc/paper/configuration/GlobalConfiguration.java
437-
@@ -0,0 +1,274 @@
437+
@@ -0,0 +1,275 @@
438438
+package io.papermc.paper.configuration;
439439
+
440440
+import co.aikar.timings.MinecraftTimings;
441-
+import com.destroystokyo.paper.io.chunk.ChunkTaskManager;
442441
+import io.papermc.paper.configuration.constraint.Constraint;
443442
+import io.papermc.paper.configuration.constraint.Constraints;
444443
+import net.kyori.adventure.text.Component;
@@ -604,15 +603,17 @@ index 0000000000000000000000000000000000000000..aaecd691922a28e971b859175574c80a
604603
+ public boolean saveEmptyScoreboardTeams = false;
605604
+ }
606605
+
607-
+ public AsyncChunks asyncChunks;
606+
+ public ChunkSystem chunkSystem;
608607
+
609-
+ public class AsyncChunks extends ConfigurationPart.Post {
610-
+ public int threads = -1;
611-
+ public transient boolean asyncChunks = false;
608+
+ public class ChunkSystem extends ConfigurationPart.Post {
609+
+
610+
+ public int ioThreads = -1;
611+
+ public int workerThreads = -1;
612+
+ public String genParallelism = "default";
612613
+
613614
+ @Override
614615
+ public void postProcess() {
615-
+ ChunkTaskManager.processConfiguration(this);
616+
+ io.papermc.paper.chunk.system.scheduling.ChunkTaskScheduler.init(this);
616617
+ }
617618
+ }
618619
+

patches/server/0006-ConcurrentUtil.patch

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,160 @@ index 0000000000000000000000000000000000000000..f4415f782b32fed25da98e44b172f717
14121412
+ }
14131413
+ }
14141414
+}
1415+
diff --git a/src/main/java/ca/spottedleaf/concurrentutil/collection/SRSWLinkedQueue.java b/src/main/java/ca/spottedleaf/concurrentutil/collection/SRSWLinkedQueue.java
1416+
new file mode 100644
1417+
index 0000000000000000000000000000000000000000..597659f38aa816646dcda4ca39c002b6d9f9a792
1418+
--- /dev/null
1419+
+++ b/src/main/java/ca/spottedleaf/concurrentutil/collection/SRSWLinkedQueue.java
1420+
@@ -0,0 +1,148 @@
1421+
+package ca.spottedleaf.concurrentutil.collection;
1422+
+
1423+
+import ca.spottedleaf.concurrentutil.util.ConcurrentUtil;
1424+
+import ca.spottedleaf.concurrentutil.util.Validate;
1425+
+import java.lang.invoke.VarHandle;
1426+
+import java.util.ConcurrentModificationException;
1427+
+
1428+
+/**
1429+
+ * Single reader thread single writer thread queue. The reader side of the queue is ordered by acquire semantics,
1430+
+ * and the writer side of the queue is ordered by release semantics.
1431+
+ */
1432+
+// TODO test
1433+
+public class SRSWLinkedQueue<E> {
1434+
+
1435+
+ // always non-null
1436+
+ protected LinkedNode<E> head;
1437+
+
1438+
+ // always non-null
1439+
+ protected LinkedNode<E> tail;
1440+
+
1441+
+ /* IMPL NOTE: Leave hashCode and equals to their defaults */
1442+
+
1443+
+ public SRSWLinkedQueue() {
1444+
+ final LinkedNode<E> dummy = new LinkedNode<>(null, null);
1445+
+ this.head = this.tail = dummy;
1446+
+ }
1447+
+
1448+
+ /**
1449+
+ * Must be the reader thread.
1450+
+ *
1451+
+ * <p>
1452+
+ * Returns, without removing, the first element of this queue.
1453+
+ * </p>
1454+
+ * @return Returns, without removing, the first element of this queue.
1455+
+ */
1456+
+ public E peekFirst() {
1457+
+ LinkedNode<E> head = this.head;
1458+
+ E ret = head.getElementPlain();
1459+
+ if (ret == null) {
1460+
+ head = head.getNextAcquire();
1461+
+ if (head == null) {
1462+
+ // empty
1463+
+ return null;
1464+
+ }
1465+
+ // update head reference for next poll() call
1466+
+ this.head = head;
1467+
+ // guaranteed to be non-null
1468+
+ ret = head.getElementPlain();
1469+
+ if (ret == null) {
1470+
+ throw new ConcurrentModificationException("Multiple reader threads");
1471+
+ }
1472+
+ }
1473+
+
1474+
+ return ret;
1475+
+ }
1476+
+
1477+
+ /**
1478+
+ * Must be the reader thread.
1479+
+ *
1480+
+ * <p>
1481+
+ * Returns and removes the first element of this queue.
1482+
+ * </p>
1483+
+ * @return Returns and removes the first element of this queue.
1484+
+ */
1485+
+ public E poll() {
1486+
+ LinkedNode<E> head = this.head;
1487+
+ E ret = head.getElementPlain();
1488+
+ if (ret == null) {
1489+
+ head = head.getNextAcquire();
1490+
+ if (head == null) {
1491+
+ // empty
1492+
+ return null;
1493+
+ }
1494+
+ // guaranteed to be non-null
1495+
+ ret = head.getElementPlain();
1496+
+ if (ret == null) {
1497+
+ throw new ConcurrentModificationException("Multiple reader threads");
1498+
+ }
1499+
+ }
1500+
+
1501+
+ head.setElementPlain(null);
1502+
+ LinkedNode<E> next = head.getNextAcquire();
1503+
+ this.head = next == null ? head : next;
1504+
+
1505+
+ return ret;
1506+
+ }
1507+
+
1508+
+ /**
1509+
+ * Must be the writer thread.
1510+
+ *
1511+
+ * <p>
1512+
+ * Adds the element to the end of the queue.
1513+
+ * </p>
1514+
+ *
1515+
+ * @throws NullPointerException If the provided element is null
1516+
+ */
1517+
+ public void addLast(final E element) {
1518+
+ Validate.notNull(element, "Provided element cannot be null");
1519+
+ final LinkedNode<E> append = new LinkedNode<>(element, null);
1520+
+
1521+
+ this.tail.setNextRelease(append);
1522+
+ this.tail = append;
1523+
+ }
1524+
+
1525+
+ protected static final class LinkedNode<E> {
1526+
+
1527+
+ protected volatile Object element;
1528+
+ protected volatile LinkedNode<E> next;
1529+
+
1530+
+ protected static final VarHandle ELEMENT_HANDLE = ConcurrentUtil.getVarHandle(LinkedNode.class, "element", Object.class);
1531+
+ protected static final VarHandle NEXT_HANDLE = ConcurrentUtil.getVarHandle(LinkedNode.class, "next", LinkedNode.class);
1532+
+
1533+
+ protected LinkedNode(final Object element, final LinkedNode<E> next) {
1534+
+ ELEMENT_HANDLE.set(this, element);
1535+
+ NEXT_HANDLE.set(this, next);
1536+
+ }
1537+
+
1538+
+ /* element */
1539+
+
1540+
+ @SuppressWarnings("unchecked")
1541+
+ protected final E getElementPlain() {
1542+
+ return (E)ELEMENT_HANDLE.get(this);
1543+
+ }
1544+
+
1545+
+ protected final void setElementPlain(final E update) {
1546+
+ ELEMENT_HANDLE.set(this, (Object)update);
1547+
+ }
1548+
+ /* next */
1549+
+
1550+
+ @SuppressWarnings("unchecked")
1551+
+ protected final LinkedNode<E> getNextPlain() {
1552+
+ return (LinkedNode<E>)NEXT_HANDLE.get(this);
1553+
+ }
1554+
+
1555+
+ @SuppressWarnings("unchecked")
1556+
+ protected final LinkedNode<E> getNextAcquire() {
1557+
+ return (LinkedNode<E>)NEXT_HANDLE.getAcquire(this);
1558+
+ }
1559+
+
1560+
+ protected final void setNextPlain(final LinkedNode<E> next) {
1561+
+ NEXT_HANDLE.set(this, next);
1562+
+ }
1563+
+
1564+
+ protected final void setNextRelease(final LinkedNode<E> next) {
1565+
+ NEXT_HANDLE.setRelease(this, next);
1566+
+ }
1567+
+ }
1568+
+}
14151569
diff --git a/src/main/java/ca/spottedleaf/concurrentutil/completable/Completable.java b/src/main/java/ca/spottedleaf/concurrentutil/completable/Completable.java
14161570
new file mode 100644
14171571
index 0000000000000000000000000000000000000000..a1ad3308f9c3545a604b635896259a1cd3382b2a
@@ -1518,7 +1672,7 @@ index 0000000000000000000000000000000000000000..a1ad3308f9c3545a604b635896259a1c
15181672
+}
15191673
diff --git a/src/main/java/ca/spottedleaf/concurrentutil/executor/BaseExecutor.java b/src/main/java/ca/spottedleaf/concurrentutil/executor/BaseExecutor.java
15201674
new file mode 100644
1521-
index 0000000000000000000000000000000000000000..716a0fd3f558df748e355069746272facb91de22
1675+
index 0000000000000000000000000000000000000000..8c452b0988da4725762d543f6bee09915c328ae6
15221676
--- /dev/null
15231677
+++ b/src/main/java/ca/spottedleaf/concurrentutil/executor/BaseExecutor.java
15241678
@@ -0,0 +1,198 @@
@@ -1575,11 +1729,11 @@ index 0000000000000000000000000000000000000000..716a0fd3f558df748e355069746272fa
15751729
+ * @throws IllegalStateException If the current thread is not allowed to wait
15761730
+ */
15771731
+ public default void waitUntilAllExecuted() throws IllegalStateException {
1578-
+ long failures = 9L; // start out at 1ms
1732+
+ long failures = 1L; // start at 0.25ms
15791733
+
15801734
+ while (!this.haveAllTasksExecuted()) {
15811735
+ Thread.yield();
1582-
+ failures = ConcurrentUtil.linearLongBackoff(failures, 500_000L, 5_000_000L); // 500us, 5ms
1736+
+ failures = ConcurrentUtil.linearLongBackoff(failures, 250_000L, 5_000_000L); // 500us, 5ms
15831737
+ }
15841738
+ }
15851739
+

0 commit comments

Comments
 (0)