d7076778e2
This commit removes badger from the deal-making processes, and moves to a new architecture with the dagstore as the cental component on the miner-side, and CARv2s on the client-side. Every deal that has been handed off to the sealing subsystem becomes a shard in the dagstore. Shards are mounted via the LotusMount, which teaches the dagstore how to load the related piece when serving retrievals. When the miner starts the Lotus for the first time with this patch, we will perform a one-time migration of all active deals into the dagstore. This is a lightweight process, and it consists simply of registering the shards in the dagstore. Shards are backed by the unsealed copy of the piece. This is currently a CARv1. However, the dagstore keeps CARv2 indices for all pieces, so when it's time to acquire a shard to serve a retrieval, the unsealed CARv1 is joined with its index (safeguarded by the dagstore), to form a read-only blockstore, thus taking the place of the monolithic badger. Data transfers have been adjusted to interface directly with CARv2 files. On inbound transfers (client retrievals, miner storage deals), we stream the received data into a CARv2 ReadWrite blockstore. On outbound transfers (client storage deals, miner retrievals), we serve the data off a CARv2 ReadOnly blockstore. Client-side imports are managed by the refactored *imports.Manager component (when not using IPFS integration). Just like it before, we use the go-filestore library to avoid duplicating the data from the original file in the resulting UnixFS DAG (concretely the leaves). However, the target of those imports are what we call "ref-CARv2s": CARv2 files placed under the `$LOTUS_PATH/imports` directory, containing the intermediate nodes in full, and the leaves as positional references to the original file on disk. Client-side retrievals are placed into CARv2 files in the location: `$LOTUS_PATH/retrievals`. A new set of `Dagstore*` JSON-RPC operations and `lotus-miner dagstore` subcommands have been introduced on the miner-side to inspect and manage the dagstore. Despite moving to a CARv2-backed system, the IPFS integration has been respected, and it continues to be possible to make storage deals with data held in an IPFS node, and to perform retrievals directly into an IPFS node. NOTE: because the "staging" and "client" Badger blockstores are no longer used, existing imports on the client will be rendered useless. On startup, Lotus will enumerate all imports and print WARN statements on the log for each import that needs to be reimported. These log lines contain these messages: - import lacks carv2 path; import will not work; please reimport - import has missing/broken carv2; please reimport At the end, we will print a "sanity check completed" message indicating the count of imports found, and how many were deemed broken. Co-authored-by: Aarsh Shah <aarshkshah1992@gmail.com> Co-authored-by: Dirk McCormick <dirkmdev@gmail.com> Co-authored-by: Raúl Kripalani <raul@protocol.ai> Co-authored-by: Dirk McCormick <dirkmdev@gmail.com>
238 lines
6.1 KiB
Go
238 lines
6.1 KiB
Go
package dagstore
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
ds "github.com/ipfs/go-datastore"
|
|
ds_sync "github.com/ipfs/go-datastore/sync"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
|
|
|
|
"github.com/filecoin-project/go-fil-markets/piecestore"
|
|
piecestoreimpl "github.com/filecoin-project/go-fil-markets/piecestore/impl"
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
|
"github.com/filecoin-project/go-fil-markets/shared"
|
|
)
|
|
|
|
const unsealedSectorID = abi.SectorNumber(1)
|
|
const sealedSectorID = abi.SectorNumber(2)
|
|
|
|
func TestLotusAccessorFetchUnsealedPiece(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
cid1, err := cid.Parse("bafkqaaa")
|
|
require.NoError(t, err)
|
|
|
|
unsealedSectorData := "unsealed"
|
|
sealedSectorData := "sealed"
|
|
mockData := map[abi.SectorNumber]string{
|
|
unsealedSectorID: unsealedSectorData,
|
|
sealedSectorID: sealedSectorData,
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
deals []abi.SectorNumber
|
|
fetchedData string
|
|
isUnsealed bool
|
|
|
|
expectErr bool
|
|
}{{
|
|
// Expect error if there is no deal info for piece CID
|
|
name: "no deals",
|
|
expectErr: true,
|
|
}, {
|
|
// Expect the API to always fetch the unsealed deal (because it's
|
|
// cheaper than fetching the sealed deal)
|
|
name: "prefer unsealed deal",
|
|
deals: []abi.SectorNumber{unsealedSectorID, sealedSectorID},
|
|
fetchedData: unsealedSectorData,
|
|
isUnsealed: true,
|
|
}, {
|
|
// Expect the API to unseal the data if there are no unsealed deals
|
|
name: "unseal if necessary",
|
|
deals: []abi.SectorNumber{sealedSectorID},
|
|
fetchedData: sealedSectorData,
|
|
isUnsealed: false,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ps := getPieceStore(t)
|
|
rpn := &mockRPN{
|
|
sectors: mockData,
|
|
}
|
|
api := NewMinerAPI(ps, rpn, 100)
|
|
require.NoError(t, api.Start(ctx))
|
|
|
|
// Add deals to piece store
|
|
for _, sectorID := range tc.deals {
|
|
dealInfo := piecestore.DealInfo{
|
|
SectorID: sectorID,
|
|
}
|
|
err = ps.AddDealForPiece(cid1, dealInfo)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// Fetch the piece
|
|
r, err := api.FetchUnsealedPiece(ctx, cid1)
|
|
if tc.expectErr {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
|
|
// Check that the returned reader is for the correct piece
|
|
require.NoError(t, err)
|
|
bz, err := io.ReadAll(r)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.fetchedData, string(bz))
|
|
|
|
uns, err := api.IsUnsealed(ctx, cid1)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.isUnsealed, uns)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLotusAccessorGetUnpaddedCARSize(t *testing.T) {
|
|
ctx := context.Background()
|
|
cid1, err := cid.Parse("bafkqaaa")
|
|
require.NoError(t, err)
|
|
|
|
ps := getPieceStore(t)
|
|
rpn := &mockRPN{}
|
|
api := NewMinerAPI(ps, rpn, 100)
|
|
require.NoError(t, api.Start(ctx))
|
|
|
|
// Add a deal with data Length 10
|
|
dealInfo := piecestore.DealInfo{
|
|
Length: 10,
|
|
}
|
|
err = ps.AddDealForPiece(cid1, dealInfo)
|
|
require.NoError(t, err)
|
|
|
|
// Check that the data length is correct
|
|
len, err := api.GetUnpaddedCARSize(ctx, cid1)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, 10, len)
|
|
}
|
|
|
|
func TestThrottle(t *testing.T) {
|
|
ctx := context.Background()
|
|
cid1, err := cid.Parse("bafkqaaa")
|
|
require.NoError(t, err)
|
|
|
|
ps := getPieceStore(t)
|
|
rpn := &mockRPN{
|
|
sectors: map[abi.SectorNumber]string{
|
|
unsealedSectorID: "foo",
|
|
},
|
|
}
|
|
api := NewMinerAPI(ps, rpn, 3)
|
|
require.NoError(t, api.Start(ctx))
|
|
|
|
// Add a deal with data Length 10
|
|
dealInfo := piecestore.DealInfo{
|
|
SectorID: unsealedSectorID,
|
|
Length: 10,
|
|
}
|
|
err = ps.AddDealForPiece(cid1, dealInfo)
|
|
require.NoError(t, err)
|
|
|
|
// hold the lock to block.
|
|
rpn.lk.Lock()
|
|
|
|
// fetch the piece concurrently.
|
|
errgrp, ctx := errgroup.WithContext(context.Background())
|
|
for i := 0; i < 10; i++ {
|
|
errgrp.Go(func() error {
|
|
r, err := api.FetchUnsealedPiece(ctx, cid1)
|
|
if err == nil {
|
|
_ = r.Close()
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
require.EqualValues(t, 3, atomic.LoadInt32(&rpn.calls)) // throttled
|
|
|
|
// allow to proceed.
|
|
rpn.lk.Unlock()
|
|
|
|
// allow all to finish.
|
|
err = errgrp.Wait()
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 10, atomic.LoadInt32(&rpn.calls)) // throttled
|
|
|
|
}
|
|
|
|
func getPieceStore(t *testing.T) piecestore.PieceStore {
|
|
ps, err := piecestoreimpl.NewPieceStore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
|
require.NoError(t, err)
|
|
|
|
ch := make(chan struct{}, 1)
|
|
ps.OnReady(func(_ error) {
|
|
ch <- struct{}{}
|
|
})
|
|
|
|
err = ps.Start(context.Background())
|
|
require.NoError(t, err)
|
|
<-ch
|
|
return ps
|
|
}
|
|
|
|
type mockRPN struct {
|
|
calls int32 // guarded by atomic
|
|
lk sync.RWMutex // lock to simulate blocks.
|
|
sectors map[abi.SectorNumber]string
|
|
}
|
|
|
|
func (m *mockRPN) UnsealSector(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (io.ReadCloser, error) {
|
|
atomic.AddInt32(&m.calls, 1)
|
|
m.lk.RLock()
|
|
defer m.lk.RUnlock()
|
|
|
|
data, ok := m.sectors[sectorID]
|
|
if !ok {
|
|
panic("sector not found")
|
|
}
|
|
return io.NopCloser(bytes.NewBuffer([]byte(data))), nil
|
|
}
|
|
|
|
func (m *mockRPN) IsUnsealed(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (bool, error) {
|
|
return sectorID == unsealedSectorID, nil
|
|
}
|
|
|
|
func (m *mockRPN) GetChainHead(ctx context.Context) (shared.TipSetToken, abi.ChainEpoch, error) {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (m *mockRPN) GetMinerWorkerAddress(ctx context.Context, miner address.Address, tok shared.TipSetToken) (address.Address, error) {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (m *mockRPN) SavePaymentVoucher(ctx context.Context, paymentChannel address.Address, voucher *paych.SignedVoucher, proof []byte, expectedAmount abi.TokenAmount, tok shared.TipSetToken) (abi.TokenAmount, error) {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (m *mockRPN) GetRetrievalPricingInput(ctx context.Context, pieceCID cid.Cid, storageDeals []abi.DealID) (retrievalmarket.PricingInput, error) {
|
|
panic("implement me")
|
|
}
|
|
|
|
var _ retrievalmarket.RetrievalProviderNode = (*mockRPN)(nil)
|