Skip to content

Commit

Permalink
cli commands for dagstore
Browse files Browse the repository at this point in the history
  • Loading branch information
nonsense committed Nov 17, 2021
1 parent 21e27b8 commit 80b8b0e
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 32 deletions.
6 changes: 6 additions & 0 deletions api/api_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ type StorageMiner interface {
// DagstoreGC runs garbage collection on the DAG store.
DagstoreGC(ctx context.Context) ([]DagstoreShardResult, error) //perm:admin

// DagstoreInvertedIndexSize returns the size of the inverted index.
DagstoreInvertedIndexSize(ctx context.Context) (int64, error) //perm:admin

// DagstoreLookupPieces returns information about shards that contain the given CID.
DagstoreLookupPieces(ctx context.Context, cid string) ([]DagstoreShardInfo, error) //perm:admin

// RuntimeSubsystems returns the subsystems that are enabled
// in this instance.
RuntimeSubsystems(ctx context.Context) (MinerSubsystems, error) //perm:read
Expand Down
26 changes: 26 additions & 0 deletions api/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified build/openrpc/full.json.gz
Binary file not shown.
Binary file modified build/openrpc/miner.json.gz
Binary file not shown.
Binary file modified build/openrpc/worker.json.gz
Binary file not shown.
140 changes: 108 additions & 32 deletions cmd/lotus-miner/dagstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var dagstoreCmd = &cli.Command{
dagstoreRecoverShardCmd,
dagstoreInitializeAllCmd,
dagstoreGcCmd,
dagstoreInvertedIndexSizeCmd,
dagstoreLookupPiecesCmd,
},
}

Expand Down Expand Up @@ -52,38 +54,7 @@ var dagstoreListShardsCmd = &cli.Command{
return err
}

if len(shards) == 0 {
return nil
}

tw := tablewriter.New(
tablewriter.Col("Key"),
tablewriter.Col("State"),
tablewriter.Col("Error"),
)

colors := map[string]color.Attribute{
"ShardStateAvailable": color.FgGreen,
"ShardStateServing": color.FgBlue,
"ShardStateErrored": color.FgRed,
"ShardStateNew": color.FgYellow,
}

for _, s := range shards {
m := map[string]interface{}{
"Key": s.Key,
"State": func() string {
if c, ok := colors[s.State]; ok {
return color.New(c).Sprint(s.State)
}
return s.State
}(),
"Error": s.Error,
}
tw.Write(m)
}

return tw.Flush(os.Stdout)
return printTableShards(shards)
},
}

Expand Down Expand Up @@ -265,3 +236,108 @@ var dagstoreGcCmd = &cli.Command{
return nil
},
}

func printTableShards(shards []api.DagstoreShardInfo) error {
if len(shards) == 0 {
return nil
}

tw := tablewriter.New(
tablewriter.Col("Key"),
tablewriter.Col("State"),
tablewriter.Col("Error"),
)

colors := map[string]color.Attribute{
"ShardStateAvailable": color.FgGreen,
"ShardStateServing": color.FgBlue,
"ShardStateErrored": color.FgRed,
"ShardStateNew": color.FgYellow,
}

for _, s := range shards {
m := map[string]interface{}{
"Key": s.Key,
"State": func() string {
if c, ok := colors[s.State]; ok {
return color.New(c).Sprint(s.State)
}
return s.State
}(),
"Error": s.Error,
}
tw.Write(m)
}
return tw.Flush(os.Stdout)
}

var dagstoreInvertedIndexSizeCmd = &cli.Command{
Name: "inverted-index-size",
Usage: "Inspect the dagstore inverted index size",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "color",
Usage: "use color in display output",
DefaultText: "depends on output being a TTY",
},
},
Action: func(cctx *cli.Context) error {
if cctx.IsSet("color") {
color.NoColor = !cctx.Bool("color")
}

marketsApi, closer, err := lcli.GetMarketsAPI(cctx)
if err != nil {
return err
}
defer closer()

ctx := lcli.ReqContext(cctx)

size, err := marketsApi.DagstoreInvertedIndexSize(ctx)
if err != nil {
return err
}

fmt.Println(size)

return nil
},
}

var dagstoreLookupPiecesCmd = &cli.Command{
Name: "lookup-pieces",
Usage: "Lookup pieces that a given CID belongs to",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "color",
Usage: "use color in display output",
DefaultText: "depends on output being a TTY",
},
&cli.BoolFlag{
Name: "cid",
Usage: "cid to lookup",
DefaultText: "",
},
},
Action: func(cctx *cli.Context) error {
if cctx.IsSet("color") {
color.NoColor = !cctx.Bool("color")
}

marketsApi, closer, err := lcli.GetMarketsAPI(cctx)
if err != nil {
return err
}
defer closer()

ctx := lcli.ReqContext(cctx)

shards, err := marketsApi.DagstoreLookupPieces(ctx, cctx.String("cid"))
if err != nil {
return err
}

return printTableShards(shards)
},
}
27 changes: 27 additions & 0 deletions documentation/en/api-v0-methods-miner.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* [DagstoreGC](#DagstoreGC)
* [DagstoreInitializeAll](#DagstoreInitializeAll)
* [DagstoreInitializeShard](#DagstoreInitializeShard)
* [DagstoreInvertedIndexSize](#DagstoreInvertedIndexSize)
* [DagstoreListShards](#DagstoreListShards)
* [DagstoreLookupPieces](#DagstoreLookupPieces)
* [DagstoreRecoverShard](#DagstoreRecoverShard)
* [Deals](#Deals)
* [DealsConsiderOfflineRetrievalDeals](#DealsConsiderOfflineRetrievalDeals)
Expand Down Expand Up @@ -431,6 +433,16 @@ Inputs:

Response: `{}`

### DagstoreInvertedIndexSize
DagstoreInvertedIndexSize returns the size of the inverted index.


Perms: admin

Inputs: `null`

Response: `9`

### DagstoreListShards
DagstoreListShards returns information about all shards known to the
DAG store. Only available on nodes running the markets subsystem.
Expand All @@ -442,6 +454,21 @@ Inputs: `null`

Response: `null`

### DagstoreLookupPieces
DagstoreLookupPieces returns information about shards that contain the given CID.


Perms: admin

Inputs:
```json
[
"string value"
]
```

Response: `null`

### DagstoreRecoverShard
DagstoreRecoverShard attempts to recover a failed shard.

Expand Down
56 changes: 56 additions & 0 deletions node/impl/storminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,62 @@ func (sm *StorageMinerAPI) DagstoreGC(ctx context.Context) ([]api.DagstoreShardR
return ret, nil
}

func (sm *StorageMinerAPI) DagstoreInvertedIndexSize(ctx context.Context) (int64, error) {
if sm.DAGStore == nil {
return 0, fmt.Errorf("dagstore not available on this node")
}

res, err := sm.DAGStore.TopLevelIndex.Size()
if err != nil {
return 0, fmt.Errorf("failed to get dagstore inverted index size: %w", err)
}

return res, nil
}

func (sm *StorageMinerAPI) DagstoreLookupPieces(ctx context.Context, cidStr string) ([]api.DagstoreShardInfo, error) {
if sm.DAGStore == nil {
return nil, fmt.Errorf("dagstore not available on this node")
}

cid, err := cid.Parse(cidStr)
if err != nil {
return nil, err
}

keys, err := sm.DAGStore.TopLevelIndex.GetShardsForMultihash(cid.Hash())
if err != nil {
return nil, err
}

var ret []api.DagstoreShardInfo

for _, k := range keys {
shard, err := sm.DAGStore.GetShardInfo(k)
if err != nil {
return nil, err
}

ret = append(ret, api.DagstoreShardInfo{
Key: k.String(),
State: shard.ShardState.String(),
Error: func() string {
if shard.Error == nil {
return ""
}
return shard.Error.Error()
}(),
})
}

// order by key.
sort.SliceStable(ret, func(i, j int) bool {
return ret[i].Key < ret[j].Key
})

return ret, nil
}

func (sm *StorageMinerAPI) DealsList(ctx context.Context) ([]api.MarketDeal, error) {
return sm.listDeals(ctx)
}
Expand Down

0 comments on commit 80b8b0e

Please sign in to comment.