The non-game type implementation will not guard against double calls,
which means that any invocation of disconnect() would immediately
call onDisconnect and then later the connection handler would
also call onDisconnect.
Fixes https://github.com/PaperMC/Folia/pull/181
The region shift is configurable under `grid-exponent`, which
allows setting the region shift to any value in [0, 31]. Note
that values above 6 affect the lock shift, as the lock shift
currently is computed as max(ticket shift = 6, region shift).
The shift is left configurable for now as the lower default
shift of 2 may have negative performance impacts.
The default region shift has been adjusted to 2 from 4, and
the empty chunk buffer has been reduced to 8 from 16. These
changes reduce, but do not eliminate, player spread
requirements. The previous block range was around ~1500 blocks
at VD = 10, but is now closer to ~900 blocks at VD = 10. This
roughly reduces the area that each player uses in the regioniser
by 2.5x.
Currently, /tick is disabled but is planned to be re-added.
It's unlikely that support for sprinting can be implemented
precisely due to the fact that Folia ticks many regions
independently. However, a best-effort approach will be used.
Profiling for a region starts with the /profiler command.
The usage for /profiler:
/profiler <world> <block x> <block z> <time in s> [radius, default 100 blocks]
Any region within the radius of the specified block coordinates
will be profiled. The profiling will stop after the specified
time has passed.
Once the profiler finishes, it will place a report in
the directory ./profiler/<id>.
Since regions can split into smaller regions, or merge into
other regions, the profiler will track this information. If
a profiled region splits, then all of the regions it splits
into are attached to the same profiler instance. If a profiled
region merges into another region, then the merged region is
profiled. This information is tracked and logged into the
"journal.txt" file contained in the report directory. The
journal tracks the region ids for the merge/split operations.
Region profiling is placed into the "region-X.txt" file where
X is the region id inside the profile directory. The header
of the file describes some stats about the region, namely
total profiling duration, ticks, utilisation, TPS, and MSPT.
Then, the timing tree is follows. The format is as specified:
There are two types of data recorded: Timers and Counters.
Timers are specified as follows:
<indent><name> X% total, Y% parent, self A% total, self B% children, avg D sum E, Fms raw sum
The above specifies the format for a named timer.
The <indent> specifies the number of parent timers.
"X" represents the percentage of time the timer took relative
to the entire profiling instance.
"Y" represents the percentage of time the timer took relative
to its _parents_ timer. For example:
```
Full Tick 100.000% total, 100.000% parent, self 0.889% total, self 0.889% children, avg 200.000 sum 200, 401.300ms raw sum
|+++Tick World: minecraft:overworld 81.409% total, 81.409% parent, self 1.873% total, self 2.301% children, avg 1.000 sum 200, 326.694ms raw sum
|---|---Entity Tick 56.784% total, 69.751% parent, self 6.049% total, self 10.652% children, avg 1.000 sum 200, 227.874ms raw sum
```
"Entity Tick" measured 69.751% of the time for the "Tick World: minecraft:overworld" timer.
"A" represents the self time relative to the entire profiling instance.
The self time is the amount of time for a timer that is _not_ measured
by a child timer.
"B" represents the self time relative to its _parents_ timer.
"D" represents the average number of times the timer is invoked relative to
its parent.
For example:
```
|---|---|---Entity Tick: bat 2.642% total, 7.343% parent, self 2.642% total, self 100.000% children, avg 14.975 sum 2,995, 23.127ms raw sum
```
In this case, an average of 14.975 bats were ticked for every
time the "Entity Tick" timer was invoked.
"E" represents the total number of times the timer is invoked.
"F" represents the total raw time accumulated by the timer.
Counters are specified as follows:
<indent>#<name> avg X sum Y
The X is the average number of times the counter is invoked
relative to the parent, exactly similar to the D field of Timers,
where Y is the total number of times the counter is invoked.
Very early build, network configuration switching is supported
but not tested (note: changes need to be backported to Paper)
Changes:
- Supports per player mob caps
- Adds entity tracker optimisations which are not in Paper
(and will not be ported to Paper due to plugin conflicts)
- No longer reverts paper distance map optimisations, as
those are replaced by the NearbyPlayers class
These changes should bring Folia in-line with Paper's optimisations
at least (probably more given the entity tracker optimisations),
still missing features like world loading / some commands
Merge the container is empty and move checks when attempting
to pull from other containers, as in both cases the container
needs to be searched entirely anyways.
Use getEntitiesOfClass when searching for entity containers,
so that non-containers are not searched.
For tryMoveItems, merge both the empty and full checks into one
as they basically perform the same logic. Note that it
relies on ejectItems returning true if _any_ item is moved, as
moving items out of the hopper would affect whether the hopper
is full or not.
If the target is removed, then it is possible that
using the CraftEntity could retrieve a teleported
entity. This would cause the Mob to be removed
when getHandle() is invoked.
Fixes https://github.com/PaperMC/Folia/issues/117
This should prevent attempting to load chunks in areas outside
of the current region, and is what Vanilla logic would do eventually
Fixes https://github.com/PaperMC/Folia/issues/117
The CHUNKY_FIXERS field is modified during the constructors
of the BlockFixers, but the code that uses CHUNKY_FIXERS does
not properly ensure that BlockFixers has been initialised before
using it, leading to a possible race condition where instances of
BlockFixers are accessed before they have initialised correctly.
We can force the class to initialise fully before accessing the
field by calling any method on the class, and for convenience
we use values().
Fixes https://github.com/PaperMC/Folia/issues/141
As part of Folia's place logic, the player's health is sent
after the respawn packet.
Since the health is <= 0.0, this would cause the client to
die again. This would cause the respawn screen to appear again,
and would additionally cause other players to see the player as
dead as well.
There is a small window where this would not have occurred, and
that is where the server would send the correct health before
the client ticks again. This is why the issue was not reproducable
locally, as there was is almost zero delay between those events
on an idle server and on perfect 0ms ping.
Fixes https://github.com/PaperMC/Folia/issues/112
The new spawn selection algorithm attempts to search the area
around a selected point, in an effort to reduce the total number
of chunk loads required to select a spawn point.
Additionally, the new spawn selection algorithm does not perform
recursion when the selected area is already loaded and owned by
the current region. This fixes https://github.com/PaperMC/Folia/issues/138
The end gateway is supposed to teleport the person who threw
the ender pearl.
The changes more closely mirror Vanilla behavior. The current
exceptions to Vanilla behavior are:
1. The first teleportation attempt for the end gateway always fails
2. If the ender pearl thrower is riding a vehicle, the thrower is
dismounted from their vehicle.
I don't see any solutions for #1 right now. The root issue is that
since the end gateway does not have a target location, it has to
search for one. However, it can _fail_ to find a target location,
in which case the teleportation should not occur. Since the search
must take place asynchronously, it requires the entity to be
removed from the world.
For #2, this is because Vanilla's behavior is broken and does not
correctly teleport players riding boats. We can fix this by simply
dismounting the player and teleporting them separately of their boat,
which seems to be what Vanilla is trying to do given it does _not_
try to teleport the root vehicle of the player.
This is a partial fix to https://github.com/PaperMC/Folia/issues/51
Since Folia moves the connection tick to the beginning of the tick,
the player's position would be incorrectly updated by the move
packet and be used during the tick.
This would cause the player's bounding box to be incorrect, which
would cause incorrect movement collision calculations, such as
colliding with fire.
Fixes https://github.com/PaperMC/Folia/issues/119
The intention behind erasing the memory was to match the case
where the villager would lose. However, there may not be
any competitors in which case the villager would never lose.
Instead, the new behavior is to behave as if the villager was not
loaded.
Fixes https://github.com/PaperMC/Folia/issues/64
Some plugins are bad and update the `from` position to something
completely garbage. To avoid a crash from this cross-region
teleportation, the teleportAsync function is now used.
The reason the teleport isn't simply ignored is since there may
be legitimate reasons to update the `from` position to something
off-region. This also handles the case where the plugin _uses_
an asynchronous teleport while cancelling the event.
This mirrors the behavior for changing the target destination
but not cancelling the event.
Fixes https://github.com/PaperMC/Folia/issues/115
When there are many chunkholders and regions, the cost of collecting
and checking tick thread for each one for every region save
becomes the biggest cost for the save call. To avoid this from
happening, collect the chunk holders from the current region's
owned sections.
This showed significant speedup locally when running the
"walk test" found in RegionizedServer locally (>90% of time
was spent on the holder iteration/checking).
While for merging the synchronisation occured, it did not synchronise
for splitting. This resolves a possible CME that may occur while
splitting regions.
Player spawn position changing caused any player to log in while
on a boat to trip a thread check. To resolve this, simply reposition
any mounted entities if they are more than 5 blocks away from the player.
In general, this may happen to other entities that are loaded from
chunks as well. In these cases, we can delete the entity if it itself
is not saved in the correct chunk, and we can reposition the mounted
entities if they are not in the correct chunk. For tile entities,
we can simply remove them if they are not in the chunk.
These changes broadly should make loading player/chunk data more
resiliant to bad logic run by plugins.
Also, the Folia test revealed that the chunk generate rate limiter
also affected chunk loading, which was not intended and has been
resolved by exempting already FULL status chunks from the limit.
It becomes invalid switching dimensions or by moving far. If
it used after teleporting, then it may also trip thread checks.
Fixes https://github.com/PaperMC/Folia/issues/94
Starlight does not use the sky light sources, and there
appear to be issues with TE access on Folia when initializing
them. So, we can just delete it entirely.
Fix two regionizer issues:
In ThreadedRegionizer#addChunk, fix the incorrect handling
of merging two regions where one of the regions had
pending merges. If the first region had pending merges,
and the second was marked as "ready" then the merge would
cause a "ready" region to have pending merges. The fix is
to simply downgrade the "ready" region to "transient,"
as was previously done if the merge was delayed in the
case where the first region was "ticking."
Additionally, prevent the creation of empty regions
by checking if any new sections were created. This would
happen when a section existed, but had no marked chunks
in it AND all of the sections neighbours existed. In these
cases, no region needs to be created as no sections were
created.
The last save was based on region tick, but it was not adjusted
on player region change or region merge. To resolve this,
I have adjusted the last save to be based on time so that it does
not need adjustments on region change or region merge.
Additionally, fix the max per tick handling.
Mobs would use the evenness of server tick count plus id
to determine whether they eoilf tick only their running
goals or to tick the goal selector to find additional
goals. If the server had an even number of regions,
then every 50ms the server tick field would be incremented
by an even number and as a result would not change
the evenness of the mob goal check. This could put
some mobs in a state where they only ticked their
running goals, which would result in them
freezing.
Fixes https://github.com/PaperMC/Folia/issues/42
First, when a section update is stolen, the thread that acquires
the stolen update should remove the update from the update queue
before returning to mark it as completed and allow other threads
waiting on the update to continue. This fixes a deadlock issue
with section updates.
Fix incorrect decrease queue resize. Previously, it attempted
to resize the _increase_ queue, which is the wrong queue.
Use ALL_DIRECTIONS_BITSET for every decrease queue direction bitset
as decrease propagation cancellation due to neighbour values exceeding
the target decrease value cause some neighbour directions to not
be checked, which causes the final update grid to be incorrect.
We must attempt to synchronise when the returned queue is null
so that we can get a correct queue result or return false due to
the reference counter being released, or even to throw an exception
when the queue is null but the reference counter is not released.
I noticed during my stress testing that the total size of the
light list was far too large, which indicates many duplicates.
For me, this caused many GC problems which made stress testing
harder.
It turns out, it was possible for the light list recalculation
logic to occur _and_ the addition of the light list data from
the NBT data. Since there is no logic to de-duplicate this list,
every chunk load would re-add all light sources into the light
list and the light list would grow uncontrollably.
Since the recalculation logic would often run, I have
decided to solve this by discarding the data on disk and always
just calculating the list from the chunk data alone. Additionally,
I have applied an optimization from Vanilla 1.20 to avoid
searching sections without light sources by first checking the
palette for possible block sources.
Now my stress tests do not have issues with GC at all.
In 1.18, every chunk section is initialised to a non-null value
and recalcBlockCounts() is invoked for each section.
However, in a standard world, most sections are empty. In such cases,
recalcBlockCounts() would iterate over ever position - even though
the block data would all be air. To avoid this, we skip
searching the section unless the palette indicates there _could_ be
a non-air block state or non-empty fluid state.
Chunk loading initially showed that recalcBlockCounts() over
sections with a ZeroBitStorage data to to take ~20% of the process,
now it takes <1%.
Since the chunk load task was not scheduled, the entity/poi load
task fields will not be set, but the task complete counter
will not be adjusted. Thus, the chunk load task will not complete.
To resolve this, detect when the entity/poi tasks were not scheduled
and decrement the task complete counter in such cases.
It must be marked as completed during that lock hold since the
waiters field is set to null. Thus, any other thread attempting
a cancellation will fail to remove from waiters. Also, any
other thread attempting to cancel may set the completed field
to true which would cause accept() to fail as well.
Completion was always designed to happen while holding the
scheduling lock to prevent these race conditions. The code
was originally set up to complete while not holding the
scheduling lock to avoid invoking callbacks while holding the
lock, however the access to the completion field was not
considered.
Resolve this by marking the callback as completed during the
lock, but invoking the accept() function after releasing
the lock. This will prevent any cancellation attempts to be
blocked, and allow the current thread to complete the callback
without any issues.
Since multiple regions can exist, there are concurrent accesses
in this class. To prevent deadlock, the monitor is not held
when recalculating permissions, as Permissable holds its own
lock.
This fixes CMEs originating from this class.
The concurrent access occurs on the Netty IO threads when
serializing packets. Thus, it seems it was an oversight of
the implementator of this function as there are typically
more than one Netty IO thread.
Fixes https://github.com/PaperMC/Folia/issues/11
A significant overhead in Folia comes from the chunk system's
locks, the ticket lock and the scheduling lock. The public
test server, which had ~330 players, had signficant performance
problems with these locks: ~80% of the time spent ticking
was _waiting_ for the locks to free. Given that it used
around 15 cores total at peak, this is a complete and utter loss
of potential.
To address this issue, I have replaced the ticket lock and scheduling
lock with two ReentrantAreaLocks. The ReentrantAreaLock takes a
shift, which is used internally to group positions into sections.
This grouping is neccessary, as the possible radius of area that
needs to be acquired for any given lock usage is up to 64. As such,
the shift is critical to reduce the number of areas required to lock
for any lock operation. Currently, it is set to a shift of 6, which
is identical to the ticket level propagation shift (and, it must be
at least the ticket level propagation shift AND the region shift).
The chunk system locking changes required a complete rewrite of the
chunk system tick, chunk system unload, and chunk system ticket level
propagation - as all of the previous logic only works with a single
global lock.
This does introduce two other section shifts: the lock shift, and the
ticket shift. The lock shift is simply what shift the area locks use,
and the ticket shift represents the size of the ticket sections.
Currently, these values are just set to the region shift for simplicity.
However, they are not arbitrary: the lock shift must be at least the size
of the ticket shift and must be at least the size of the region shift.
The ticket shift must also be >= the ceil(log2(max ticket level source)).
The chunk system's ticket propagator is now global state, instead of
region state. This cleans up the logic for ticket levels significantly,
and removes usage of the region lock in this area, but it also means
that the addition of a ticket no longer creates a region. To alleviate
the side effects of this change, the global tick thread now processes
ticket level updates for each world every tick to guarantee eventual
ticket level processing. The chunk system also provides a hook to
process ticket level changes in a given _section_, so that the
region queue can guarantee that after adding its reference counter
that the region section is created/exists/wont be destroyed.
The ticket propagator operates by updating the sources in a single ticket
section, and propagating the updates to its 1 radius neighbours. This
allows the ticket updates to occur in parallel or selectively (see above).
Currently, the process ticket level update function operates by
polling from a concurrent queue of sections to update and simply
invoking the single section update logic. This allows the function
to operate completely in parallel, provided the queue is ordered right.
Additionally, this limits the area used in the ticket/scheduling lock
when processing updates, which should massively increase parallelism compared
to before.
The chunk system ticket addition for expirable ticket types has been modified
to no longer track exact tick deadlines, as this relies on what region the
ticket is in. Instead, the chunk system tracks a map of
lock section -> (chunk coordinate -> expire ticket count) and every ticket
has been changed to have a removeDelay count that is decremented each tick.
Each region searches its own sections to find tickets to try to expire.
Chunk system unloading has been modified to track unloads by lock section.
The ordering is determined by which section a chunk resides in.
The unload process now removes from unload sections and processes
the full unload stages (1, 2, 3) before moving to the next section, if possible.
This allows the unload logic to only hold one lock section at a time for
each lock, which is a massive parallelism increase.
In stress testing, these changes lowered the locking overhead to only 5%
from ~70%, which completely fix the original problem as described.
Instead, we can just check the loaded chunk's block position for
the lodestone block, as that is at least safe enough for the light
engine compared to the POI access. This should make it safe for
off-region access.
Fixes https://github.com/PaperMC/Folia/issues/60
In general, worldstate read/write is unacceptable during
data deserialization and is racey even in Vanilla. But in Folia,
some accesses may throw and as such we need to fix this directly.
Fixes https://github.com/PaperMC/Folia/issues/57
The returned TE may be in the world, in which case it is unsafe
for the current thread to modify or access its contents.
Fixes https://github.com/PaperMC/Folia/issues/52
This is to prevent block physics from tripping thread checks by
far exceeding the bounds of the current region. While this does
add explicit block update suppression techniques, it's better
than the server crashing.
Perform thread checks on the chunk send and warn when the
world is mismatched. I suspect that the world mismatches for
an unknown reason, but need to confirm it to chase it down.
Change overview:
- Rework limiting
- Remove mid tick updates
- Introduce consistency checks
The old limiting logic used an intervalled counter, but
did not account for possible slight changes in mid tick
invoke rate as it relied heavily on mid-tick logic. Due to
the removal of mid tick updates, it is now important that
the logic functions correctly no matter what rate it is invoked
at. The new logic directly tracks the last update time and
allocates an amount based proportional on the rate targetted,
which makes the logic call rate independent.
The removal of mid tick updates is done to eliminate recursive
call risk, and to additionally reduce the lock pressure on the
chunk system by grouping chunk loads onto one part of the tick
rather than spreading it out. The limiting rework should ensure
that this does not negatively affect rates, but it will decrease
the perceived smoothness of chunk generation/loading at low rates.
Introduce more consistency checks such as correct tick thread
and ticking-after-removal checks. Also, perform checks during the player
chunk loader tick to avoid updating potentially removed
players during the tick.
The checks are primarily made to try to hunt down a bug that
is causing the player chunk loader to double send a chunk
to a player.
Due to TPS catchup being removed, a lost tick will always
affect block breaking.
Additionally, constant low TPS would affect block breaking
in an intrusive manner to the user when there is no need
for that to occur.
The expected behavior is that the entity is only dismounted
_if_ the teleport takes place, not regardless of whether
the teleport takes place.
To adhere to the expected behavior, we need to create a new teleport
flag so that the NMS teleportAsync can perform the dismount.
Required to add some basic hacks to expose a regionstats object
in the TickRegions data class, but this ensures that the retrieval
is thread-safe and could possibly support other data exposure
for async reads.
Additionally, since DecimalFormat is not thread-safe we need
to use ThreadLocals to instantiate them. Change the format
as well to use commas to separate groups of digits when
formatting large numbers so they are easier to read.
For example, 1000 becomes 1,000.
Vanilla inserted the alive check so that dead players
could see the chunks around them, but in Folia we do not
remove dead players so chunks will still load for them.
This prevents the client from having chunks in memory it
should not.
The inventory was being cleared by removing the entity
from the world, but Folia changed the remove to be
before the entity copy which caused the new entity
to have a cleared inventory. Fix this by creating
a post-dimension copy callback that will clear
the inventory of the old entity.
Fixes https://github.com/PaperMC/Folia/issues/29
If the player moves out of range by the time the block is destroyed,
then the exception would throw and remove the player from the world
Additionally, when players fail to tick instead of removing
the player from the world, kick them to prevent a limbo state
The MinecraftServer executor will throw, and thanks
to CompletableFuture's awful exception handling
the exception was not logged or handled. Add
exception handling as well.
Fixes https://github.com/PaperMC/Folia/issues/27
The code to stop all brain tasks is required to pass the current
game time to the tasks it stops. But, when a villager is being
portalled, the copied entity does not have any running tasks. So,
we can simply return early before invoking getGameTime if there
are no running tasks.
Fixes https://github.com/PaperMC/Folia/issues/23