Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Bulk Loading/Writing for Chunks #535

Open
wants to merge 70 commits into
base: master
Choose a base branch
from

Conversation

Mili-ssm
Copy link
Contributor

@Mili-ssm Mili-ssm commented Feb 9, 2025

This PR implement the Bulk API for chunks loading/writing with some function changes that get leverage of the bulk operation (like reducing allocations, locks and more).

Also this includes some Anvil refactor and the creation of a Traits/API that can be re-used on entity storage system and to implements diferents Writers/Loaders (as DB cached, one that use in Cloud Storage or more optimizations).

Actual Improves

  • Don´t write a chunk on disk when loaded from disk.

  • Lock the Chunk Cache entry while generating a new chunk.
    ( if more threads wants that same chunks, they can wait without doing any work until the chunk is generated )

  • Every time a bunch of chunks (of the same player) are deallocated, the file is write only 1 time.

  • The Files that have ongoing writes/reads operations are stored on memory (to avoid disk readings and file lockings).

  • We have a DashMap for File Cache to avoid writing collisions and don`t corrupt files. (not an Anvil issue at least usually but it was a problem with the Linear format that enforce an FULL FILE WRITE)

  • Chunk writing operation now are made on a path.tmp so now the writing operation cant corrupt files ( when the writting finish, the file name is change, so the file is never swap before the full writting )

  • Now the file Lock/Cache implements a clean_cache fn that ensure no memory leak along the server run.

Actual Changes

  • The system behind ChunkReader and ChunkWriter is now a ChunkIO who use ChunkSerializer.
  • ChunkIO is a trait that use a Serializer.
  • ChunkSerializer is a trait that helps to serialize some type of chunk data (like Chunks terrain or Chunks entitis)
  • Now the ChunkIO instead of a Result<Data,Err> it returns a LoadedData<Data,Error> wich helps with:
    • LoadedData::Error((position,Err)) : Manage errors per chunk loaded on the bulk.
    • LoadedData::Missing(position) : Manage missing chunks (not previusly generated) becaouse the return chunks order is not enforced.
    • LoadedData::Loaded(Data) : Manage normal returned chunks.
  • The fetch made by clients threads use par_chunk instead of iter, this helps to group chunks by the asked sorted way but grouped in 64 chunks of Chunks, it take advantage of the file cache and bulk API.
  • Now when cleaning chunks, we check if the chunks is not being watched prev to remove it (to improve the undesirable instant loading of removed chunks) but if we cant remove it, we still write it in the file, this way we make the chunk writing worth and avoid unsaved changes.
  • The File Lock/Cache is refactored and embedded into the ChunkFileManager. (the actual ChunkIO implementation for Anvil and Linear)

Mili and others added 30 commits January 24, 2025 21:56
…ment and use exception to know if file exist
@Mili-ssm Mili-ssm marked this pull request as ready for review February 20, 2025 00:34
@kralverde
Copy link
Contributor

Are entries ever removed from the file lock when the file is no longer being used?

@Mili-ssm
Copy link
Contributor Author

@kralverde i refactor the "file lock" and yes i create a clean_cache fn that executes when the read lr the write finishes that check how much references of the lock are being holded. May be i should add a trace log for that but i checked that actually those entre are removed.

@Mili-ssm Mili-ssm requested a review from Snowiiii February 20, 2025 14:53
@Snowiiii
Copy link
Collaborator

What do you think about using the Minecraft chunk batch packets to may improve network bandwidth and probably also client side performance ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants