mock: Fix concurrent map writes in AddPiece

This commit is contained in:
Łukasz Magiera 2020-06-05 18:43:47 +02:00
parent c06dc750da
commit 958f74340b

View File

@ -70,17 +70,6 @@ func (mgr *SectorMgr) NewSector(ctx context.Context, sector abi.SectorID) error
func (mgr *SectorMgr) AddPiece(ctx context.Context, sectorId abi.SectorID, existingPieces []abi.UnpaddedPieceSize, size abi.UnpaddedPieceSize, r io.Reader) (abi.PieceInfo, error) { func (mgr *SectorMgr) AddPiece(ctx context.Context, sectorId abi.SectorID, existingPieces []abi.UnpaddedPieceSize, size abi.UnpaddedPieceSize, r io.Reader) (abi.PieceInfo, error) {
log.Warn("Add piece: ", sectorId, size, mgr.proofType) log.Warn("Add piece: ", sectorId, size, mgr.proofType)
mgr.lk.Lock()
ss, ok := mgr.sectors[sectorId]
if !ok {
ss = &sectorState{
state: statePacking,
}
mgr.sectors[sectorId] = ss
}
mgr.lk.Unlock()
ss.lk.Lock()
defer ss.lk.Unlock()
var b bytes.Buffer var b bytes.Buffer
tr := io.TeeReader(r, &b) tr := io.TeeReader(r, &b)
@ -92,9 +81,24 @@ func (mgr *SectorMgr) AddPiece(ctx context.Context, sectorId abi.SectorID, exist
log.Warn("Generated Piece CID: ", c) log.Warn("Generated Piece CID: ", c)
mgr.lk.Lock()
mgr.pieces[c] = b.Bytes() mgr.pieces[c] = b.Bytes()
ss, ok := mgr.sectors[sectorId]
if !ok {
ss = &sectorState{
state: statePacking,
}
mgr.sectors[sectorId] = ss
}
mgr.lk.Unlock()
ss.lk.Lock()
ss.pieces = append(ss.pieces, c) ss.pieces = append(ss.pieces, c)
ss.lk.Unlock()
return abi.PieceInfo{ return abi.PieceInfo{
Size: size.Padded(), Size: size.Padded(),
PieceCID: c, PieceCID: c,
}, nil }, nil