-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdoctor.go
263 lines (224 loc) · 7.82 KB
/
doctor.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package piecedirectory
import (
"context"
"errors"
"fmt"
"math/rand"
"strings"
"time"
"github.com/filecoin-project/boost/db"
bdclient "github.com/filecoin-project/boost/extern/boostd-data/client"
"github.com/filecoin-project/boost/extern/boostd-data/model"
"github.com/filecoin-project/boost/sectorstatemgr"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
verifregtypes "github.com/filecoin-project/go-state-types/builtin/v9/verifreg"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
)
var doclog = logging.Logger("piecedoc")
// The Doctor periodically queries the local index directory for piece cids, and runs
// checks against those pieces. If there is a problem with a piece, it is
// flagged, so that it can be surfaced to the user.
// Note that multiple Doctor processes can run in parallel. The logic for which
// pieces to give to the Doctor to check is in the local index directory.
type Doctor struct {
maddr address.Address
store *bdclient.Store
ssm *sectorstatemgr.SectorStateMgr
fullnodeApi api.FullNode
}
func NewDoctor(maddr address.Address, store *bdclient.Store, ssm *sectorstatemgr.SectorStateMgr, fullnodeApi api.FullNode) *Doctor {
return &Doctor{maddr: maddr, store: store, ssm: ssm, fullnodeApi: fullnodeApi}
}
// The average interval between calls to NextPiecesToCheck
const avgCheckInterval = 30 * time.Second
func (d *Doctor) Run(ctx context.Context) {
doclog.Info("piece doctor: running")
timer := time.NewTimer(0)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return
case <-timer.C:
}
err := func() error {
var lu *sectorstatemgr.SectorStateUpdates
d.ssm.LatestUpdateMu.Lock()
lu = d.ssm.LatestUpdate
d.ssm.LatestUpdateMu.Unlock()
if lu == nil {
doclog.Warn("sector state manager not yet updated")
return nil
}
head, err := d.fullnodeApi.ChainHead(ctx)
if err != nil {
return err
}
// Get the next pieces to check (eg pieces that haven't been checked
// for a while) from the local index directory
pcids, err := d.store.NextPiecesToCheck(ctx, d.maddr)
if err != nil {
return err
}
// Check each piece for problems
doclog.Debugw("piece doctor: checking pieces", "count", len(pcids))
// Prefetch the state claims
doclog.Debugw("prefetching state claims", "address", d.maddr)
claims, err := d.fullnodeApi.StateGetClaims(ctx, d.maddr, types.EmptyTSK)
if err != nil {
return fmt.Errorf("getting claims for the miner %s: %w", d.maddr, err)
}
for _, pcid := range pcids {
err := d.checkPiece(ctx, pcid, lu, head, claims)
if err != nil {
if errors.Is(err, context.Canceled) {
return err
}
doclog.Errorw("checking piece", "piece", pcid, "err", err)
}
}
doclog.Debugw("piece doctor: completed checking pieces", "count", len(pcids))
return nil
}()
if err != nil {
if errors.Is(err, context.Canceled) {
doclog.Errorw("piece doctor: context canceled, stopping doctor", "error", err)
return
}
doclog.Errorw("piece doctor: iteration got error", "error", err)
}
// Sleep for a few seconds between ticks.
// The time to sleep is randomized, so that if there are multiple doctor
// processes they will each process some pieces some of the time.
sleepTime := avgCheckInterval/2 + time.Duration(rand.Intn(int(avgCheckInterval)))
timer.Reset(sleepTime)
}
}
func (d *Doctor) checkPiece(ctx context.Context, pieceCid cid.Cid, lu *sectorstatemgr.SectorStateUpdates, head *types.TipSet, claims map[verifregtypes.ClaimId]verifregtypes.Claim) error {
defer func(start time.Time) { log.Debugw("checkPiece processing", "took", time.Since(start)) }(time.Now())
// Check if piece belongs to an active sector
md, err := d.store.GetPieceMetadata(ctx, pieceCid)
if err != nil {
// If piece is not found then it should be unflagged and removed from future tracking
if strings.Contains(err.Error(), "not found") {
serr := d.store.UnflagPiece(ctx, pieceCid, d.maddr)
if serr != nil {
return fmt.Errorf("failed to unflag the missing piece %s: %w", pieceCid.String(), serr)
}
serr = d.store.UntrackPiece(ctx, pieceCid, d.maddr)
if serr != nil {
return fmt.Errorf("failed to delete piece from tracker table %s: %w", pieceCid.String(), serr)
}
return nil
}
return fmt.Errorf("failed to get piece %s from local index directory: %w", pieceCid, err)
}
lacksActiveSector := true // check whether the piece is present in active sector
hasDealsOnThisMiner := false
var chainDeals []model.DealInfo
for _, dl := range md.Deals {
// Ignore deals that were not made on this node's miner
if d.maddr != dl.MinerAddr {
continue
}
hasDealsOnThisMiner = true
mid, err := address.IDFromAddress(dl.MinerAddr)
if err != nil {
return err
}
sectorID := abi.SectorID{
Miner: abi.ActorID(mid),
Number: dl.SectorID,
}
// check if we have an active sector
if _, ok := lu.ActiveSectors[sectorID]; ok {
lacksActiveSector = false
chainDeals = append(chainDeals, dl)
}
}
if !hasDealsOnThisMiner {
doclog.Warnw("ignoring piece as it is not present in any deals on this miner", "piece", pieceCid.String(), "miner", d.maddr.String())
return nil
}
if lacksActiveSector {
doclog.Debugw("ignoring and unflagging piece as it is not present in an active sector", "piece", pieceCid.String())
err = d.store.UnflagPiece(ctx, pieceCid, d.maddr)
if err != nil {
return fmt.Errorf("failed to unflag piece %s: %w", pieceCid, err)
}
return nil
}
// Check that Deal is actually on-chain for the active sectors
if d.fullnodeApi != nil { // nil in tests
found := false
for _, dealId := range chainDeals {
if dealId.IsDirectDeal {
doclog.Debugw("checking state for direct deal", "piece", pieceCid, "allocation", dealId.ChainDealID)
for _, v := range claims {
if v.Sector == dealId.SectorID {
found = true
}
}
} else {
doclog.Debugw("checking state for market deal", "piece", pieceCid, "deal", dealId.ChainDealID)
_, err := d.fullnodeApi.StateMarketStorageDeal(ctx, dealId.ChainDealID, head.Key())
if err == nil {
found = true
break
}
}
}
if !found {
doclog.Debugw("ignoring and unflagging piece as no deal id found on chain", "piece", pieceCid)
err = d.store.UnflagPiece(ctx, pieceCid, d.maddr)
if err != nil {
return fmt.Errorf("failed to unflag piece %s: %w", pieceCid, err)
}
return nil
}
}
var hasUnsealedCopy bool
for _, dl := range md.Deals {
// Ignore deals that were not made on this node's miner
if d.maddr != dl.MinerAddr {
continue
}
mid, err := address.IDFromAddress(dl.MinerAddr)
if err != nil {
return err
}
sectorID := abi.SectorID{
Miner: abi.ActorID(mid),
Number: dl.SectorID,
}
if lu.SectorStates[sectorID] == db.SealStateUnsealed {
hasUnsealedCopy = true
break
}
}
// Check if piece has been indexed
isIndexed, err := d.store.IsIndexed(ctx, pieceCid)
if err != nil {
return fmt.Errorf("failed to check index status of piece %s: %w", pieceCid, err)
}
// If piece is not indexed or has no unsealed copy, flag it
if !isIndexed || !hasUnsealedCopy {
err = d.store.FlagPiece(ctx, pieceCid, hasUnsealedCopy, d.maddr)
if err != nil {
return fmt.Errorf("failed to flag piece %s: %w", pieceCid, err)
}
doclog.Debugw("flagging piece", "piece", pieceCid, "isIndexed", isIndexed, "hasUnsealedCopy", hasUnsealedCopy, "len(activeSectors)", len(lu.ActiveSectors), "len(sectorStates)", len(lu.SectorStates))
return nil
}
// There are no known issues with the piece, so unflag it
doclog.Debugw("unflagging piece", "piece", pieceCid)
err = d.store.UnflagPiece(ctx, pieceCid, d.maddr)
if err != nil {
return fmt.Errorf("failed to unflag piece %s: %w", pieceCid, err)
}
return nil
}