-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: add experimental on-demand pinning #11252
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
Open
ihlec
wants to merge
2
commits into
ipfs:master
Choose a base branch
from
ihlec:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package config | ||
|
|
||
| import "time" | ||
|
|
||
| const ( | ||
| DefaultOnDemandPinReplicationTarget = 5 | ||
| DefaultOnDemandPinCheckInterval = 10 * time.Minute | ||
| DefaultOnDemandPinUnpinGracePeriod = 24 * time.Hour | ||
| ) | ||
|
|
||
| type OnDemandPinning struct { | ||
| // Minimum providers desired in the DHT (excluding self). | ||
| ReplicationTarget OptionalInteger | ||
|
|
||
| // How often the checker evaluates all registered CIDs. | ||
| CheckInterval OptionalDuration | ||
|
|
||
| // How long replication must stay above target before unpinning. | ||
| UnpinGracePeriod OptionalDuration | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,280 @@ | ||
| package pin | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "time" | ||
|
|
||
| cmds "github.com/ipfs/go-ipfs-cmds" | ||
| "github.com/ipfs/kubo/config" | ||
| cmdenv "github.com/ipfs/kubo/core/commands/cmdenv" | ||
| "github.com/ipfs/kubo/core/commands/cmdutils" | ||
| "github.com/ipfs/kubo/ondemandpin" | ||
| ) | ||
|
|
||
| const onDemandLiveOptionName = "live" | ||
|
|
||
| var onDemandPinCmd = &cmds.Command{ | ||
| Helptext: cmds.HelpText{ | ||
| Tagline: "Manage on-demand pins.", | ||
| ShortDescription: ` | ||
| On-demand pins when few DHT providers exist in the routing table; unpins after | ||
| replication stays above target for a grace period. Requires config | ||
| Experimental.OnDemandPinningEnabled. | ||
| `, | ||
| }, | ||
| Subcommands: map[string]*cmds.Command{ | ||
| "add": addOnDemandPinCmd, | ||
| "rm": rmOnDemandPinCmd, | ||
| "ls": listOnDemandPinCmd, | ||
| }, | ||
| } | ||
|
|
||
| type OnDemandPinOutput struct { | ||
| Cid string `json:"Cid"` | ||
| } | ||
|
|
||
| var addOnDemandPinCmd = &cmds.Command{ | ||
| Helptext: cmds.HelpText{ | ||
| Tagline: "Register CIDs for on-demand pinning.", | ||
| ShortDescription: `Registers CID(s) for on-demand pinning; checker pins when needed.`, | ||
| }, | ||
| Arguments: []cmds.Argument{ | ||
| cmds.StringArg("cid", true, true, "CID(s) to register."), | ||
| }, | ||
| Type: OnDemandPinOutput{}, | ||
| Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
| n, err := cmdenv.GetNode(env) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cfg, err := n.Repo.Config() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !cfg.Experimental.OnDemandPinningEnabled { | ||
| return fmt.Errorf("on-demand pinning is not enabled; set Experimental.OnDemandPinningEnabled = true in config") | ||
| } | ||
|
|
||
| store := n.OnDemandPinStore | ||
|
|
||
| api, err := cmdenv.GetApi(env, req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, arg := range req.Arguments { | ||
| p, err := cmdutils.PathOrCidPath(arg) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid CID or path %q: %w", arg, err) | ||
| } | ||
|
|
||
| rp, _, err := api.ResolvePath(req.Context, p) | ||
| if err != nil { | ||
| return fmt.Errorf("resolving %q: %w", arg, err) | ||
| } | ||
| c := rp.RootCid() | ||
|
|
||
| if err := store.Add(req.Context, c); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if checker := n.OnDemandPinChecker; checker != nil { | ||
| checker.Enqueue(c) | ||
| } | ||
|
|
||
| if err := res.Emit(&OnDemandPinOutput{ | ||
| Cid: c.String(), | ||
| }); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| Encoders: cmds.EncoderMap{ | ||
| cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *OnDemandPinOutput) error { | ||
| fmt.Fprintf(w, "registered %s for on-demand pinning\n", out.Cid) | ||
| return nil | ||
| }), | ||
| }, | ||
| } | ||
|
|
||
| var rmOnDemandPinCmd = &cmds.Command{ | ||
| Helptext: cmds.HelpText{ | ||
| Tagline: "Remove CIDs from on-demand pinning.", | ||
| ShortDescription: ` | ||
| Removes CID(s) from the registry. Checker-pinned content is unpinned. | ||
|
|
||
| Works when on-demand pinning is disabled, to clear old registrations. | ||
| `, | ||
| }, | ||
| Arguments: []cmds.Argument{ | ||
| cmds.StringArg("cid", true, true, "CID(s) to remove."), | ||
| }, | ||
| Type: OnDemandPinOutput{}, | ||
| Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
| n, err := cmdenv.GetNode(env) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| store := n.OnDemandPinStore | ||
|
|
||
| api, err := cmdenv.GetApi(env, req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, arg := range req.Arguments { | ||
| p, err := cmdutils.PathOrCidPath(arg) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid CID or path %q: %w", arg, err) | ||
| } | ||
|
|
||
| rp, _, err := api.ResolvePath(req.Context, p) | ||
| if err != nil { | ||
| return fmt.Errorf("resolving %q: %w", arg, err) | ||
| } | ||
| c := rp.RootCid() | ||
|
|
||
| isOurs, err := ondemandpin.PinHasName(req.Context, n.Pinning, c, ondemandpin.OnDemandPinName) | ||
| if err != nil { | ||
| return fmt.Errorf("checking pin state for %s: %w", c, err) | ||
| } | ||
| if isOurs { | ||
| if err := api.Pin().Rm(req.Context, rp); err != nil { | ||
| return fmt.Errorf("unpinning %s: %w", c, err) | ||
| } | ||
| } | ||
|
|
||
| if err := store.Remove(req.Context, c); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := res.Emit(&OnDemandPinOutput{ | ||
| Cid: c.String(), | ||
| }); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| Encoders: cmds.EncoderMap{ | ||
| cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *OnDemandPinOutput) error { | ||
| fmt.Fprintf(w, "removed %s from on-demand pinning\n", out.Cid) | ||
| return nil | ||
| }), | ||
| }, | ||
| } | ||
|
|
||
| type OnDemandLsOutput struct { | ||
| Cid string `json:"Cid"` | ||
| PinnedByUs bool `json:"PinnedByUs"` | ||
| Providers *int `json:"Providers,omitempty"` | ||
| LastAboveTarget string `json:"LastAboveTarget,omitempty"` | ||
| CreatedAt string `json:"CreatedAt"` | ||
| } | ||
|
|
||
| var listOnDemandPinCmd = &cmds.Command{ | ||
| Helptext: cmds.HelpText{ | ||
| Tagline: "List on-demand pins.", | ||
| ShortDescription: ` | ||
| Lists CIDs registered for on-demand pinning with their current state. | ||
| Use --live to include real-time provider counts from the DHT. | ||
| `, | ||
| }, | ||
| Arguments: []cmds.Argument{ | ||
| cmds.StringArg("cid", false, true, "Optional CID(s) to filter."), | ||
| }, | ||
| Options: []cmds.Option{ | ||
| cmds.BoolOption(onDemandLiveOptionName, "l", "Perform live provider lookup."), | ||
| }, | ||
| Type: OnDemandLsOutput{}, | ||
| Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
| n, err := cmdenv.GetNode(env) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| store := n.OnDemandPinStore | ||
|
|
||
| live, _ := req.Options[onDemandLiveOptionName].(bool) | ||
|
|
||
| var globalTarget int | ||
| if live { | ||
| cfg, err := n.Repo.Config() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| globalTarget = int(cfg.OnDemandPinning.ReplicationTarget.WithDefault(config.DefaultOnDemandPinReplicationTarget)) | ||
| } | ||
|
|
||
| var records []ondemandpin.Record | ||
| if len(req.Arguments) > 0 { | ||
| api, err := cmdenv.GetApi(env, req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, arg := range req.Arguments { | ||
| p, err := cmdutils.PathOrCidPath(arg) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid CID or path %q: %w", arg, err) | ||
| } | ||
| rp, _, err := api.ResolvePath(req.Context, p) | ||
| if err != nil { | ||
| return fmt.Errorf("resolving %q: %w", arg, err) | ||
| } | ||
| rec, err := store.Get(req.Context, rp.RootCid()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| records = append(records, *rec) | ||
| } | ||
| } else { | ||
| records, err = store.List(req.Context) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| for _, rec := range records { | ||
| out := OnDemandLsOutput{ | ||
| Cid: rec.Cid.String(), | ||
| PinnedByUs: rec.PinnedByUs, | ||
| CreatedAt: rec.CreatedAt.Format(time.RFC3339), | ||
| } | ||
| if !rec.LastAboveTarget.IsZero() { | ||
| out.LastAboveTarget = rec.LastAboveTarget.Format(time.RFC3339) | ||
| } | ||
|
|
||
| if live && n.Routing != nil { | ||
| count := ondemandpin.CountProviders(req.Context, n.Routing, n.Identity, rec.Cid, globalTarget) | ||
| out.Providers = &count | ||
| } | ||
|
|
||
| if err := res.Emit(&out); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| Encoders: cmds.EncoderMap{ | ||
| cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *OnDemandLsOutput) error { | ||
| pinState := "not-pinned" | ||
| if out.PinnedByUs { | ||
| pinState = "pinned" | ||
| } | ||
| fmt.Fprintf(w, "%s", out.Cid) | ||
| if out.Providers != nil { | ||
| fmt.Fprintf(w, " providers=%d", *out.Providers) | ||
| } | ||
| fmt.Fprintf(w, " %s created=%s", pinState, out.CreatedAt) | ||
| if out.LastAboveTarget != "" { | ||
| fmt.Fprintf(w, " above-target-since=%s", out.LastAboveTarget) | ||
| } | ||
| fmt.Fprintln(w) | ||
| return nil | ||
| }), | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the ondemand ls command shows the locally stored pin records (CID, pinned status, timestamps). With ---live, it additionally queries the DHT in real-time to report how many providers currently exist
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DHT queries are sequential. Doing
ipfs pin ondemand ls --liveon hundreds of CIDs will take a long time. Still handy for debugging and development.