forked from filecoin-project/boost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoctor_test.go
443 lines (369 loc) · 12 KB
/
doctor_test.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//go:build test_lid
// +build test_lid
package piecedirectory
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/filecoin-project/boost/db"
"github.com/filecoin-project/boost/extern/boostd-data/client"
"github.com/filecoin-project/boost/extern/boostd-data/ldb"
"github.com/filecoin-project/boost/extern/boostd-data/model"
"github.com/filecoin-project/boost/extern/boostd-data/svc"
"github.com/filecoin-project/boost/extern/boostd-data/yugabyte"
"github.com/filecoin-project/boost/sectorstatemgr"
"github.com/filecoin-project/boost/testutil"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/google/uuid"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/v2"
"github.com/stretchr/testify/require"
)
func TestPieceDoctor(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
defer cancel()
t.Run("leveldb", func(t *testing.T) {
prev := ldb.MinPieceCheckPeriod
ldb.MinPieceCheckPeriod = 1 * time.Second
bdsvc, err := svc.NewLevelDB("")
require.NoError(t, err)
ln, err := bdsvc.Start(ctx, "localhost:0")
require.NoError(t, err)
cl := client.NewStore()
err = cl.Dial(ctx, fmt.Sprintf("ws://%s", ln))
require.NoError(t, err)
defer cl.Close(ctx)
t.Run("next pieces pagination", func(t *testing.T) {
prevp := ldb.PiecesToTrackerBatchSize
testNextPiecesPagination(ctx, t, cl, func(pageSize int) {
ldb.PiecesToTrackerBatchSize = pageSize
})
ldb.PiecesToTrackerBatchSize = prevp
})
t.Run("check pieces", func(t *testing.T) {
testCheckPieces(ctx, t, cl)
})
t.Run("pieces count", func(t *testing.T) {
testPiecesCount(ctx, t, cl)
})
ldb.MinPieceCheckPeriod = prev
})
t.Run("yugabyte", func(t *testing.T) {
prev := yugabyte.MinPieceCheckPeriod
yugabyte.MinPieceCheckPeriod = 1 * time.Second
bdsvc := svc.SetupYugabyte(t)
ln, err := bdsvc.Start(ctx, "localhost:0")
require.NoError(t, err)
cl := client.NewStore()
err = cl.Dial(ctx, fmt.Sprintf("ws://%s", ln))
require.NoError(t, err)
defer cl.Close(ctx)
ybstore := bdsvc.Impl.(*yugabyte.Store)
t.Run("next pieces", func(t *testing.T) {
svc.RecreateTables(ctx, t, ybstore)
testNextPieces(ctx, t, cl, yugabyte.MinPieceCheckPeriod)
})
t.Run("next pieces pagination", func(t *testing.T) {
svc.RecreateTables(ctx, t, ybstore)
prevp := yugabyte.TrackerCheckBatchSize
testNextPiecesPagination(ctx, t, cl, func(pageSize int) {
yugabyte.TrackerCheckBatchSize = pageSize
})
yugabyte.TrackerCheckBatchSize = prevp
})
t.Run("check pieces", func(t *testing.T) {
svc.RecreateTables(ctx, t, ybstore)
testCheckPieces(ctx, t, cl)
})
t.Run("pieces count", func(t *testing.T) {
svc.RecreateTables(ctx, t, ybstore)
testPiecesCount(ctx, t, cl)
})
yugabyte.MinPieceCheckPeriod = prev
})
}
// Verify that after a new piece is added
// - NextPiecesToCheck immediately returns the piece
// - NextPiecesToCheck returns the piece every pieceCheckPeriod
func testNextPieces(ctx context.Context, t *testing.T, cl *client.Store, pieceCheckPeriod time.Duration) {
// Add a new piece
pieceCid := blocks.NewBlock([]byte(fmt.Sprintf("%d", time.Now().UnixMilli()))).Cid()
minerAddr := address.TestAddress
di := model.DealInfo{
DealUuid: uuid.New().String(),
ChainDealID: 1,
MinerAddr: minerAddr,
SectorID: 1,
PieceOffset: 0,
PieceLength: 2048,
}
err := cl.AddDealForPiece(ctx, pieceCid, di)
require.NoError(t, err)
// Add another piece with a different miner address - it should be ignored
di2 := model.DealInfo{
DealUuid: uuid.New().String(),
ChainDealID: 2,
MinerAddr: address.TestAddress2,
SectorID: 2,
PieceOffset: 0,
PieceLength: 2048,
}
err = cl.AddDealForPiece(ctx, pieceCid, di2)
require.NoError(t, err)
// Sleep for half the piece check period
time.Sleep(pieceCheckPeriod / 2)
// NextPiecesToCheck should return the piece (because it hasn't been checked yet)
pcids, err := cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Contains(t, pcids, pieceCid)
// Calling NextPiecesToCheck again should return nothing, because the piece
// was just checked
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.NotContains(t, pcids, pieceCid)
// Sleep for at least the piece check period
time.Sleep(2 * pieceCheckPeriod)
// Calling NextPiecesToCheck should return the piece, because it has not
// been checked for at least one piece check period
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Contains(t, pcids, pieceCid)
}
func testNextPiecesPagination(ctx context.Context, t *testing.T, cl *client.Store, setPageSize func(int)) {
setPageSize(4)
// Add 9 pieces
seen := make(map[cid.Cid]int)
minerAddr := address.TestAddress
for i := 1; i <= 9; i++ {
pieceCid := testutil.GenerateCid()
di := model.DealInfo{
DealUuid: uuid.New().String(),
ChainDealID: abi.DealID(i),
MinerAddr: minerAddr,
SectorID: abi.SectorNumber(i),
PieceOffset: 0,
PieceLength: 2048,
}
err := cl.AddDealForPiece(ctx, pieceCid, di)
require.NoError(t, err)
}
// expect to get 4 pieces
pcids, err := cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 4)
for _, cid := range pcids {
seen[cid] = 1
}
// expect to get 4 pieces
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 4)
for _, cid := range pcids {
seen[cid] = 1
}
// expect to get 1 pieces (end of table)
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 1)
for _, cid := range pcids {
seen[cid] = 1
}
require.Len(t, seen, 9)
// expect to get 0 pieces (first four)
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 0)
// expect to get 0 pieces (second four)
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 0)
// expect to get 0 pieces (end of table)
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 0)
// Add 1 more piece
for i := 1; i <= 1; i++ {
pieceCid := testutil.GenerateCid()
di := model.DealInfo{
DealUuid: uuid.New().String(),
ChainDealID: abi.DealID(100),
MinerAddr: minerAddr,
SectorID: abi.SectorNumber(100),
PieceOffset: 0,
PieceLength: 2048,
}
err := cl.AddDealForPiece(ctx, pieceCid, di)
require.NoError(t, err)
}
// wait to reset the interval and start from scratch
time.Sleep(2 * time.Second)
seen = make(map[cid.Cid]int)
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 4)
for _, cid := range pcids {
seen[cid] = 1
}
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 4)
for _, cid := range pcids {
seen[cid] = 1
}
pcids, err = cl.NextPiecesToCheck(ctx, minerAddr)
require.NoError(t, err)
require.Len(t, pcids, 2)
for _, cid := range pcids {
seen[cid] = 1
}
require.Len(t, seen, 10)
}
func testCheckPieces(ctx context.Context, t *testing.T, cl *client.Store) {
// Create a random CAR file
_, carFilePath := CreateCarFile(t)
carFile, err := os.Open(carFilePath)
require.NoError(t, err)
defer carFile.Close()
carReader, err := car.OpenReader(carFilePath)
require.NoError(t, err)
defer carReader.Close()
carv1Reader, err := carReader.DataReader()
require.NoError(t, err)
commpCalc := CalculateCommp(t, carv1Reader)
// Add deal info for the piece
minerActorID := abi.ActorID(1011)
minerAddr, err := address.NewIDAddress(uint64(minerActorID))
require.NoError(t, err)
di := model.DealInfo{
DealUuid: uuid.New().String(),
ChainDealID: 1,
MinerAddr: minerAddr,
SectorID: 1,
PieceOffset: 0,
PieceLength: commpCalc.PieceSize,
}
dlSectorID := abi.SectorID{
Miner: minerActorID,
Number: di.SectorID,
}
err = cl.AddDealForPiece(ctx, commpCalc.PieceCID, di)
require.NoError(t, err)
// Initialize the sector state such that the deal's sector is active and
// there is an unsealed copy
ssu := §orstatemgr.SectorStateUpdates{
ActiveSectors: map[abi.SectorID]struct{}{
dlSectorID: struct{}{},
},
SectorStates: map[abi.SectorID]db.SealState{
dlSectorID: db.SealStateUnsealed,
},
}
// Create a doctor
doc := NewDoctor(minerAddr, cl, nil, nil)
// Check the piece
err = doc.checkPiece(ctx, commpCalc.PieceCID, ssu, nil)
require.NoError(t, err)
// The piece should be flagged because there is no index for it
count, err := cl.FlaggedPiecesCount(ctx, nil)
require.NoError(t, err)
require.Equal(t, 1, count)
pcids, err := cl.FlaggedPiecesList(ctx, nil, nil, 0, 10)
require.NoError(t, err)
require.Equal(t, 1, len(pcids))
// Create an index for the piece
recs := GetRecords(t, carv1Reader)
err = cl.AddIndex(ctx, commpCalc.PieceCID, recs, true)
require.NoError(t, err)
// Check the piece
err = doc.checkPiece(ctx, commpCalc.PieceCID, ssu, nil)
require.NoError(t, err)
// The piece should no longer be flagged
count, err = cl.FlaggedPiecesCount(ctx, nil)
require.NoError(t, err)
require.Equal(t, 0, count)
pcids, err = cl.FlaggedPiecesList(ctx, nil, nil, 0, 10)
require.NoError(t, err)
require.Equal(t, 0, len(pcids))
// Simulate deleting the unsealed copy
ssu.SectorStates[dlSectorID] = db.SealStateSealed
// Check the piece
err = doc.checkPiece(ctx, commpCalc.PieceCID, ssu, nil)
require.NoError(t, err)
// The piece should be flagged because there is no unsealed copy
count, err = cl.FlaggedPiecesCount(ctx, nil)
require.NoError(t, err)
require.Equal(t, 1, count)
pcids, err = cl.FlaggedPiecesList(ctx, nil, nil, 0, 10)
require.NoError(t, err)
require.Equal(t, 1, len(pcids))
// Simulate a sector unseal
ssu.SectorStates[dlSectorID] = db.SealStateUnsealed
// Check the piece
err = doc.checkPiece(ctx, commpCalc.PieceCID, ssu, nil)
require.NoError(t, err)
// The piece should no longer be flagged
count, err = cl.FlaggedPiecesCount(ctx, nil)
require.NoError(t, err)
require.Equal(t, 0, count)
pcids, err = cl.FlaggedPiecesList(ctx, nil, nil, 0, 10)
require.NoError(t, err)
require.Equal(t, 0, len(pcids))
}
func testPiecesCount(ctx context.Context, t *testing.T, cl *client.Store) {
// Create a random CAR file
_, carFilePath := CreateCarFile(t)
carFile, err := os.Open(carFilePath)
require.NoError(t, err)
defer carFile.Close()
carReader, err := car.OpenReader(carFilePath)
require.NoError(t, err)
defer carReader.Close()
carv1Reader, err := carReader.DataReader()
require.NoError(t, err)
commpCalc := CalculateCommp(t, carv1Reader)
// Add deal info for the piece on miner 1001
minerAddr, err := address.NewIDAddress(1001)
require.NoError(t, err)
di := model.DealInfo{
DealUuid: uuid.New().String(),
ChainDealID: 1,
MinerAddr: minerAddr,
SectorID: 1,
PieceOffset: 0,
PieceLength: commpCalc.PieceSize,
}
err = cl.AddDealForPiece(ctx, commpCalc.PieceCID, di)
require.NoError(t, err)
// Add deal info for the piece on miner 1002
minerAddr2, err := address.NewIDAddress(1002)
require.NoError(t, err)
di2 := model.DealInfo{
DealUuid: uuid.New().String(),
ChainDealID: 2,
MinerAddr: minerAddr2,
SectorID: 2,
PieceOffset: 0,
PieceLength: commpCalc.PieceSize,
}
err = cl.AddDealForPiece(ctx, commpCalc.PieceCID, di2)
require.NoError(t, err)
// In the yugabyte implementation PiecesCount queries the postgres
// PieceTracker table. We need to first call NextPiecesToCheck so that the
// piece information is copied from the cassandra database over to the
// postgres database PieceTracker table.
_, err = cl.NextPiecesToCheck(ctx, minerAddr)
// There should be one deal for the piece on the first miner address
count, err := cl.PiecesCount(ctx, minerAddr)
require.NoError(t, err)
require.Equal(t, 1, count)
// There should be zero pieces for another random miner address
otherMinerAddr, err := address.NewIDAddress(1234)
require.NoError(t, err)
count, err = cl.PiecesCount(ctx, otherMinerAddr)
require.NoError(t, err)
require.Equal(t, 0, count)
}