Skip to content

Commit

Permalink
Merge pull request #5710 from chenminjian/enhance/cmd/verify
Browse files Browse the repository at this point in the history
enhance(cmd/verify): add goroutine count to improve verify speed
  • Loading branch information
Stebalien authored Nov 2, 2018
2 parents 9bec503 + 17e7892 commit 11978f5
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package commands

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"text/tabwriter"

oldcmds "github.com/ipfs/go-ipfs/commands"
Expand Down Expand Up @@ -276,6 +279,48 @@ type VerifyProgress struct {
Progress int
}

func verifyWorkerRun(ctx context.Context, wg *sync.WaitGroup, keys <-chan cid.Cid, results chan<- string, bs bstore.Blockstore) {
defer wg.Done()

for k := range keys {
_, err := bs.Get(k)
if err != nil {
select {
case results <- fmt.Sprintf("block %s was corrupt (%s)", k, err):
case <-ctx.Done():
return
}

continue
}

select {
case results <- "":
case <-ctx.Done():
return
}
}
}

func verifyResultChan(ctx context.Context, keys <-chan cid.Cid, bs bstore.Blockstore) <-chan string {
results := make(chan string)

go func() {
defer close(results)

var wg sync.WaitGroup

for i := 0; i < runtime.NumCPU()*2; i++ {
wg.Add(1)
go verifyWorkerRun(ctx, &wg, keys, results, bs)
}

wg.Wait()
}()

return results
}

var repoVerifyCmd = &oldcmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Verify all blocks in repo are not corrupted.",
Expand All @@ -300,15 +345,14 @@ var repoVerifyCmd = &oldcmds.Command{
return
}

results := verifyResultChan(req.Context(), keys, bs)

var fails int
var i int
for k := range keys {
_, err := bs.Get(k)
if err != nil {
for msg := range results {
if msg != "" {
select {
case out <- &VerifyProgress{
Msg: fmt.Sprintf("block %s was corrupt (%s)", k, err),
}:
case out <- &VerifyProgress{Msg: msg}:
case <-req.Context().Done():
return
}
Expand Down

0 comments on commit 11978f5

Please sign in to comment.