-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Basic Filestore Utilties #3653
Merged
Merged
Basic Filestore Utilties #3653
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4bf4304
filestore util: basic filestore commands.
kevina 13f617d
filestore util: allow listing/verifying of individual blocks.
kevina 886a6fe
filestore: be more specific when there is a problem reading the backi…
kevina 064bb53
filestore util: add documentation for 'filestore ls' and 'verify' com…
kevina 033c442
filestore util: add tests for verifying and listing filestore blocks.
kevina 1f2e174
filestore: use the same codes in ListRes and CorruptReferenceError.
kevina f7efb34
filestore util: Add 'filestore dups' command. Enhance tests.
kevina 81af034
filestore util: change "???..." to "<corrupt key>"
kevina 96269e0
filestore: Refactor.
kevina 620b52b
filestore util: refactor and clean up tests
kevina 1eddb60
filestore util: doc improvement
kevina 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 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,251 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
cmds "github.com/ipfs/go-ipfs/commands" | ||
"github.com/ipfs/go-ipfs/core" | ||
"github.com/ipfs/go-ipfs/filestore" | ||
cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid" | ||
u "gx/ipfs/QmZuY8aV7zbNXVy6DyN9SmnuH3o9nG852F4aTiSBpts8d1/go-ipfs-util" | ||
) | ||
|
||
var FileStoreCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Interact with filestore objects.", | ||
}, | ||
Subcommands: map[string]*cmds.Command{ | ||
"ls": lsFileStore, | ||
"verify": verifyFileStore, | ||
"dups": dupsFileStore, | ||
}, | ||
} | ||
|
||
var lsFileStore = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "List objects in filestore.", | ||
LongDescription: ` | ||
List objects in the filestore. | ||
|
||
If one or more <obj> is specified only list those specific objects, | ||
otherwise list all objects. | ||
|
||
The output is: | ||
|
||
<hash> <size> <path> <offset> | ||
`, | ||
}, | ||
Arguments: []cmds.Argument{ | ||
cmds.StringArg("obj", false, true, "Cid of objects to list."), | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
_, fs, err := getFilestore(req) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
args := req.Arguments() | ||
if len(args) > 0 { | ||
out := perKeyActionToChan(args, func(c *cid.Cid) *filestore.ListRes { | ||
return filestore.List(fs, c) | ||
}, req.Context()) | ||
res.SetOutput(out) | ||
} else { | ||
next, err := filestore.ListAll(fs) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
out := listResToChan(next, req.Context()) | ||
res.SetOutput(out) | ||
} | ||
}, | ||
PostRun: func(req cmds.Request, res cmds.Response) { | ||
if res.Error() != nil { | ||
return | ||
} | ||
outChan, ok := res.Output().(<-chan interface{}) | ||
if !ok { | ||
res.SetError(u.ErrCast(), cmds.ErrNormal) | ||
return | ||
} | ||
res.SetOutput(nil) | ||
errors := false | ||
for r0 := range outChan { | ||
r := r0.(*filestore.ListRes) | ||
if r.ErrorMsg != "" { | ||
errors = true | ||
fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg) | ||
} else { | ||
fmt.Fprintf(res.Stdout(), "%s\n", r.FormatLong()) | ||
} | ||
} | ||
if errors { | ||
res.SetError(fmt.Errorf("errors while displaying some entries"), cmds.ErrNormal) | ||
} | ||
}, | ||
Type: filestore.ListRes{}, | ||
} | ||
|
||
var verifyFileStore = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Verify objects in filestore.", | ||
LongDescription: ` | ||
Verify objects in the filestore. | ||
|
||
If one or more <obj> is specified only verify those specific objects, | ||
otherwise verify all objects. | ||
|
||
The output is: | ||
|
||
<status> <hash> <size> <path> <offset> | ||
|
||
Where <status> is one of: | ||
ok: the block can be reconstructed | ||
changed: the contents of the backing file have changed | ||
no-file: the backing file could not be found | ||
error: there was some other problem reading the file | ||
missing: <obj> could not be found in the filestore | ||
ERROR: internal error, most likely due to a corrupt database | ||
|
||
For ERROR entries the error will also be printed to stderr. | ||
`, | ||
}, | ||
Arguments: []cmds.Argument{ | ||
cmds.StringArg("obj", false, true, "Cid of objects to verify."), | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
_, fs, err := getFilestore(req) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
args := req.Arguments() | ||
if len(args) > 0 { | ||
out := perKeyActionToChan(args, func(c *cid.Cid) *filestore.ListRes { | ||
return filestore.Verify(fs, c) | ||
}, req.Context()) | ||
res.SetOutput(out) | ||
} else { | ||
next, err := filestore.VerifyAll(fs) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
out := listResToChan(next, req.Context()) | ||
res.SetOutput(out) | ||
} | ||
}, | ||
PostRun: func(req cmds.Request, res cmds.Response) { | ||
if res.Error() != nil { | ||
return | ||
} | ||
outChan, ok := res.Output().(<-chan interface{}) | ||
if !ok { | ||
res.SetError(u.ErrCast(), cmds.ErrNormal) | ||
return | ||
} | ||
res.SetOutput(nil) | ||
for r0 := range outChan { | ||
r := r0.(*filestore.ListRes) | ||
if r.Status == filestore.StatusOtherError { | ||
fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg) | ||
} | ||
fmt.Fprintf(res.Stdout(), "%s %s\n", r.Status.Format(), r.FormatLong()) | ||
} | ||
}, | ||
Type: filestore.ListRes{}, | ||
} | ||
|
||
var dupsFileStore = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "List blocks that are both in the filestore and standard block storage.", | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
_, fs, err := getFilestore(req) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
ch, err := fs.FileManager().AllKeysChan(req.Context()) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
out := make(chan interface{}, 128) | ||
res.SetOutput((<-chan interface{})(out)) | ||
|
||
go func() { | ||
defer close(out) | ||
for cid := range ch { | ||
have, err := fs.MainBlockstore().Has(cid) | ||
if err != nil { | ||
out <- &RefWrapper{Err: err.Error()} | ||
return | ||
} | ||
if have { | ||
out <- &RefWrapper{Ref: cid.String()} | ||
} | ||
} | ||
}() | ||
}, | ||
Marshalers: refsMarshallerMap, | ||
Type: RefWrapper{}, | ||
} | ||
|
||
func getFilestore(req cmds.Request) (*core.IpfsNode, *filestore.Filestore, error) { | ||
n, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
fs := n.Filestore | ||
if fs == nil { | ||
return n, nil, fmt.Errorf("filestore not enabled") | ||
} | ||
return n, fs, err | ||
} | ||
|
||
func listResToChan(next func() *filestore.ListRes, ctx context.Context) <-chan interface{} { | ||
out := make(chan interface{}, 128) | ||
go func() { | ||
defer close(out) | ||
for { | ||
r := next() | ||
if r == nil { | ||
return | ||
} | ||
select { | ||
case out <- r: | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
return out | ||
} | ||
|
||
func perKeyActionToChan(args []string, action func(*cid.Cid) *filestore.ListRes, ctx context.Context) <-chan interface{} { | ||
out := make(chan interface{}, 128) | ||
go func() { | ||
defer close(out) | ||
for _, arg := range args { | ||
c, err := cid.Decode(arg) | ||
if err != nil { | ||
out <- &filestore.ListRes{ | ||
Status: filestore.StatusOtherError, | ||
ErrorMsg: fmt.Sprintf("%s: %v", arg, err), | ||
} | ||
continue | ||
} | ||
r := action(c) | ||
select { | ||
case out <- r: | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
return out | ||
} |
This file contains 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 |
---|---|---|
|
@@ -47,6 +47,7 @@ ADVANCED COMMANDS | |
pin Pin objects to local storage | ||
repo Manipulate the IPFS repository | ||
stats Various operational stats | ||
filestore Manage the filestore (experimental) | ||
|
||
NETWORK COMMANDS | ||
id Show info about IPFS peers | ||
|
@@ -124,6 +125,7 @@ var rootSubcommands = map[string]*cmds.Command{ | |
"update": ExternalBinary(), | ||
"version": VersionCmd, | ||
"bitswap": BitswapCmd, | ||
"filestore": FileStoreCmd, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be mentioned in helptext of main command. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oversight. Will fix. |
||
} | ||
|
||
// RootRO is the readonly version of Root | ||
|
This file contains 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 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.
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.
Why is this in the
PostRun
. Shouldnt this be a marshaler?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.
This is in PostRun becuase I output to both stdout and stderr something that a marshaler can't do, unless I am mistaken.
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.
Note, I did a very similar thing in
ipfs block rm
: https://github.com/ipfs/go-ipfs/blob/master/core/commands/block.go#L283.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.
Also
ipfs add
does not use a marshaler.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.
This should be able to go in the marshaler. You can write to stderr and stdout in the marshaler the same way you can in the PostRun. If we do weird things like this in the post run then it breaks the API expectations in a weird way.
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.
@whyrusleeping so should the Marshalers just return nil for the Reader?
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.
@whyrusleeping okay done, let me know if its okay now.