Andrew Gillis
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • Sharing URL Link copied
        • /edit
        • View mode
          • Edit mode
          • View mode
          • Book mode
          • Slide mode
          Edit mode View mode Book mode Slide mode
        • Customize slides
        • Note Permission
        • Read
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invite by email
        Invitee

        This note has no invitees

      • Publish Note

        Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

        Your note will be visible on your profile and discoverable by anyone.
        Your note is now live.
        This note is visible on your profile and discoverable online.
        Everyone on the web can find and read all notes of this public team.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        See published notes
        Unpublish note
        Please check the box to agree to the Community Guidelines.
        View profile
      • Commenting
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Suggest edit
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
      • Emoji Reply
      • Enable
      • Versions and GitHub Sync
      • Note settings
      • Note Insights New
      • Engagement control
      • Make a copy
      • Transfer ownership
      • Delete this note
      • Save as template
      • Insert from template
      • Import from
        • Dropbox
        • Google Drive
        • Gist
        • Clipboard
      • Export to
        • Dropbox
        • Google Drive
        • Gist
      • Download
        • Markdown
        • HTML
        • Raw HTML
    Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Engagement control Make a copy Transfer ownership Delete this note
    Import from
    Dropbox Google Drive Gist Clipboard
    Export to
    Dropbox Google Drive Gist
    Download
    Markdown HTML Raw HTML
    Back
    Sharing URL Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Customize slides
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # New IPFS GC rev2 ## Problem with Current Garbage Collector The current garbage collector implementation causes significant delays during a GC cycle, as it locks the entire blockstore during the GC cycle preventing other activity. The lock is held for a potentially long time while GC filters out blocks that are pinned or within the MFS tree, and deletes the remaining blocks. For large pin sets, this is expensive in both memory and time. ## Solution Overview ### Blocks are Reference-Counted IPFS will maintain reference counts for blocks. When a block is part of content that is pinned or added to MFS, the block's reference count is increased. Reference counts are decreased when content is unpinned or removed from MFS. Reference counts are only stored for counts > 0. Garbage collection performance issues arise because the previous GC attempts to remove blocks by loading a set of all pinned CIDs into memory and searching for each block’s CID in this set. In this new design, GC will instead examine a block's reference count to determine if a block can be deleted. This removes the need to load all pinned CIDs into memory. :::warning TODO: Are reference counts changed by anything other than pinning or MFS? ::: ### Bulk GC A GC cycle that removes all unreferenced blocks will only have to look at a block's reference count before deciding whether or not to remove the block. This is faster than searching the entire set of pinned blocks. This also saves time and memory since the a set of pinned CIDs does not need to be constructed in memory. Bulk GC is performed the same way as the previous GC implementation. ### Targeted GC Targeted GC means that the IPLD DAG starting at a specific CID is walked down to the leaves, and all associated blocks that have a 0 reference count (not pinned or part of MFS), and are not part of any other DAG, are removed. This operation would typically be performed after removing pins or after removing items from MFS, to delete that old content. This is faster than bulk GC since only a specific set of blocks needs to be needs to be examined collected, whereas with bulk GC all blocks need to be checked to see if they are collectible. For pinning services, this may be the only form of GC necessary since blocks become unreferenced only when content is unpinned. Targeted GC is performed by passing a CID when invoking garbage collection. As a convenience, it can also be performed as part of removing pins or items from MFS. ### Incremental/Concurrent GC :::info Incremental and concurrent GC are not required for the initial implementation. ::: GC can run relatively concurrently with other portions of IPFS that access the block store. Each GC cycle can some set of candidate blocks to remove without exclusively locking the blockstore. At the end of the cycle, GC will check that the candidate blocks exist, are not references or in an exclusion set, and then delete the blocks. The set of blocks is kept small to minimize the lock time for the check and delete. By removing small groups of unreferenced blocks at a time, GC can run without disrupting other operations, incrementally removing unreferenced blocks Logically deleting blocks may allow GC to quickly mark blocks as deleted, and make those blocks unavailable to the rest of the system. The a background GC process could cleanup to actual storage. Note: The added complexity of logical deletion needs to be evaluates against any blocking time saved. Note, It may be useful to better choose which blocks are better candidates by looking at: - Blocks in same DAG - Last use time - Fequency of use - Size ### Excluding New Content from GC Newly arrived blocks need to be protected from concurrent GC. When new blocks are being fetched, the session responsible for fetching a DAG will keep a set of CIDs fetched. GC will exclude any blocks from collection that exist in an active fetch set. Maintaining a GC exclusion set can be done when pinning, adding files, etc. by starting a new session. Note: From the perspective of every session/transaction, GC will always have happend either before the transaction started, or after it ended, because everything the transaction touches will be excluded from GC. :::warning Is this really necessary? - warpfork suspects: yes. Think I've heard anecdatal reports from partners that attempt to run IPFS-a-a-S that they effectively don't run GC in prod because of the lack of safety for concurrent puts. ::: ### Metrics When GC runs it will keep metrics for each run: - Total blocks searched - Blocks with 0 external reference count - Blocks with 0 reference, but in exclusion set - Blocks with 0 reference, but with >1 parent - Blocks collected (marked for removal) - Blocks removed from blockstore - Total time to complete GC cycle The metrics track GC progress over time and can be used to tune GC settings, such as frequency of execution and maximum storage settings. ### Diagnostics The metrics can be checked against the blocks in the block store for accuracy. If there are no changes to the local IPFS content, then the blocks in the block store should be consistent with the last GC metrics. Functionality to verify all block references counts should also exist. If a block has a nonzero reference count, then there must be pinned or part of content in MFS. The value of the nonzero reference count should be verified by determining that it matches the number of pins and number items of MFS content that refer to the block. ## Implementation ### Reference Count Storage Reference counts will be stored as key-value pairs using the `Datastore` interface from `go-datastore`. The key will be a multibase-base64 encoded CID, and the value will be an int32. The datasore used will be the same used to store pins. Reference counts will be stored in a `/refcounts/` namespace. An extra datastore action per ref-count change should not be too expensive. The datastore should keep the changes in memory and will only write these when flushed, which will happen at the end of pinning or adding content to MFS is complete. Even if this does incur some expense, it is only incurred at these specific times, when the DAG is already being traversed. #### External vs Internal References These reference counts are "external" reference counts and are changed by pinning and MFS. These are different from "internal" reference counts which are references from other blocks in the blockstore. Internal reference counts are kept by the blockstore itself. Currently, internal reference counts are not available. This GC implementation does not strictly depend on having internal reference counts. If the blockstore implementation does not make internal reference counts available then these are not part of the decision about whether to delete a block. If a unreferenced block is part of multiple DAGs, and the block gets deleted when one of the dags is deleted, this is OK. If/when the remaining DAG is traversed, the missing block will be re-retched. Remember, the deleted block was unreferenced, so it is not like it was pinned or in MFS. ### Pinner Changes #### Recursive Pins When adding a recursive pin, the pinner walks the IPFS DAG starting with the CID being pinned. This is done by calling `merkledag.FetchGraph` which fetches all the content and stores it on the local node. A new version of FetchGraph should either: 1. Increment block reference counts for all blocks referenced by the DAG. Or... 2. Return a list of block CIDs so that the reference counts for those blocks can be incremented. This could be implemented without any changes to `go-merkeldag` but would require a separate walk of the DAG. Since the pinner already walks the DAG to fetch all nodes when adding a pin, it would be most efficient to be able to increment reference counts during this walk, or return a set of all blocks referenced by the walked DAG When removing a recursive pin, the DAG referenced by the CID being unpinned will need to be walked to leaves, and all blocks referenced will have their reference decremented. :::warning It may be safer to update block reference counts in a single transaction to prevent having invalid reference counts in the case of an error during the pinning or reference update. Invalid high counts would result in unremovable blocks without some other recovery mechanism. Invalid low counts would cause GC to remove content that is pinned or in MFS. ::: #### Direct Pins If a block is identified by a CID being directly pinned, then that block's reference count is incremented. When removing a direct pin, the block identified by the CID has it reference count decremented. Both adding and removing a direct pin will require loading the IPLD node for the CID. ### DAG Service Changes The DAG service needs to provide functionality for walking a DAG and returning a set of all blocks associated with the DAG. This will be used to increment or decrement reverence counts for the blocks. ### MFS Changes When adding files to MFS, all blocks referenced by the DAG rooted at the CID being copied into MFS, get their reference count incremented. Modifying a file in MFS decrements ref counts for blocks associated with files old DAG and increments ref counts for blocks associated with file's new DAG. Changing CID: same as modifying file #### Directories When new directorys are created and files copied into those directories, the reference counts of blocks associated with files are not incremented as a result of putting those files in one or more directories. The reference counts are only chnaged when content is added to or removed from MFS. ### Block command changes The `ipfs block rm` command will need to change to remove the reference count for a block that is removed. The `ipfs block stat` command should probably show reference count. ### GC command changes The `ipfs repo gc` command will all have a new `--cid` option: ``` ipfs repo gc --cid=<cid> ``` A `-verify` flag will run a check to verify that all referenced blocks have the correct reference count, and that all unreferenced blocks are excluded from deletion or are elligible for deletion. ``` ipfs repo gc -verify ``` ## Related - [New GC Proposal Pitch](https://github.com/protocol/web3-dev-team/pull/8) - [Previous GC design, obsoleted by this one](https://docs.google.com/document/d/1wWbGFZhv4MDLAcasoBFi8VmyEcpUFoWip6na5YmjQDc/edit#heading=h.kyt8xl5gwuc2) - [GC Design Notes](https://hackmd.io/0T8z0ex8RWmI8BmFNPSafQ?view) from @warpfork ## Proposed Optimizations **Index blocks with reference count > 1** Faster to determine which blocks in a set can be deleted. **Store reference counts for blocks with reference count > 1** :::info Storing only reference counts > 1 may offer a significant savings in storage, but means that GC could only be done if given a specific CID to target. Also, when reference counts are incremented, determining if a block gets a reference count > 1 requires checking against all pinned and MFS blocks. Having to create that set in memory is something the new GC is trying to avoid. :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully