Skip to content
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

enhance(cmd/verify): add goroutine count to improve verify speed #5710

Merged
merged 1 commit into from
Nov 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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