/
sync.go
161 lines (137 loc) · 4.39 KB
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Package cmd implements the command to print the blockchain.
package cmd
import (
"bytes"
"strconv"
"github.com/filecoin-project/venus/venus-shared/types"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/filecoin-project/venus/app/node"
)
var syncCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Inspect the sync",
},
Subcommands: map[string]*cmds.Command{
"status": storeStatusCmd,
"history": historyCmd,
"concurrent": getConcurrent,
"set-concurrent": setConcurrent,
},
}
var getConcurrent = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "get concurrent of sync thread",
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
concurrent := env.(*node.Env).SyncerAPI.Concurrent(req.Context)
return printOneString(re, strconv.Itoa(int(concurrent)))
},
}
var setConcurrent = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "set concurrent of sync thread",
},
Options: []cmds.Option{
cmds.Int64Option("concurrent", "coucurrent of sync thread"),
},
Arguments: []cmds.Argument{
cmds.StringArg("concurrent", true, false, "coucurrent of sync thread"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
concurrent, err := strconv.Atoi(req.Arguments[0])
if err != nil {
return cmds.ClientError("invalid number")
}
env.(*node.Env).SyncerAPI.SetConcurrent(req.Context, int64(concurrent)) //nolint
return nil
},
}
var storeStatusCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show status of chain sync operation.",
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
tracker := env.(*node.Env).SyncerAPI.SyncerTracker(req.Context)
targets := tracker.Buckets
var inSyncing []*types.Target
var waitTarget []*types.Target
for _, t := range targets {
if t.State == types.StageMessages {
inSyncing = append(inSyncing, t)
} else {
waitTarget = append(waitTarget, t)
}
}
w := bytes.NewBufferString("")
writer := NewSilentWriter(w)
if len(inSyncing) == 0 && len(waitTarget) == 0 {
lenTH := len(tracker.History)
if lenTH > 0 {
writer.Println(tracker.History[lenTH-1].String())
}
writer.Println("Done!")
return re.Emit(w)
}
count := 1
if len(inSyncing) > 0 {
writer.Println("Syncing:")
for _, t := range inSyncing {
writer.Println("SyncTarget:", strconv.Itoa(count))
writer.Println("\tBase:", t.Base.Height(), t.Base.Key().String())
writer.Println("\tTarget:", t.Head.Height(), t.Head.Key().String())
writer.Println("\tCurrent:", t.Current.Height(), t.Current.Key().String())
HeightDiff := t.Head.Height() - t.Current.Height()
writer.Println("\tHeightDiff:", HeightDiff)
writer.Println("\tStatus:", t.State.String())
writer.Println("\tErr:", t.Err)
writer.Println()
count++
}
}
if len(waitTarget) > 0 {
writer.Println("Waiting:")
for _, t := range waitTarget {
writer.Println("SyncTarget:", strconv.Itoa(count))
writer.Println("\tBase:", t.Base.Height(), t.Base.Key().String())
writer.Println("\tTarget:", t.Head.Height(), t.Head.Key().String())
writer.Println("\tCurrent:", t.Current.Height(), t.Current.Key().String())
HeightDiff := t.Head.Height() - t.Current.Height()
writer.Println("\tHeightDiff:", HeightDiff)
writer.Println("\tStatus:", t.State.String())
writer.Println("\tErr:", t.Err)
writer.Println()
count++
}
}
return re.Emit(w)
},
}
var historyCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show history of chain sync.",
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
tracker := env.(*node.Env).SyncerAPI.SyncerTracker(req.Context)
w := bytes.NewBufferString("")
writer := NewSilentWriter(w)
writer.Println("History:")
history := tracker.History
count := 1
for _, t := range history {
writer.Println("SyncTarget:", strconv.Itoa(count))
writer.Println("\tBase:", t.Base.Height(), t.Base.Key().String())
writer.Println("\tTarget:", t.Head.Height(), t.Head.Key().String())
if t.Current != nil {
writer.Println("\tCurrent:", t.Current.Height(), t.Current.Key().String())
} else {
writer.Println("\tCurrent:")
}
writer.Println("\tTime:", t.End.Sub(t.Start).Milliseconds())
writer.Println("\tStatus:", t.State.String())
writer.Println("\tErr:", t.Err)
writer.Println()
count++
}
return re.Emit(w)
},
}