Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do t.storage.Flush() on torrent completion, and on storage.Close() #755

Merged
merged 22 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bad_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _ storage.ClientImpl = badStorage{}
func (bs badStorage) OpenTorrent(*metainfo.Info, metainfo.Hash) (storage.TorrentImpl, error) {
return storage.TorrentImpl{
Piece: bs.Piece,
Flush: func() error { return nil },
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's common enough not to have a Flush method, that teh caller can check for Flush != nil in the TorrentImpl struct.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}, nil
}

Expand Down
21 changes: 20 additions & 1 deletion mmap_span/mmap_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,30 @@ func (ms *MMapSpan) Append(mMap mmap.MMap) {
ms.mMaps = append(ms.mMaps, mMap)
}

func (ms *MMapSpan) Flush() (errs []error) {
ms.mu.Lock()
defer ms.mu.Unlock()
for _, mMap := range ms.mMaps {
err := mMap.Flush()
if err != nil {
errs = append(errs, err)
}
}
// This is for issue 211.
ms.mMaps = nil
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems strange, why woudl Flush do this, especially if htere is no error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, mistake. removed.

ms.InitIndex()
return
}

func (ms *MMapSpan) Close() (errs []error) {
ms.mu.Lock()
defer ms.mu.Unlock()
for _, mMap := range ms.mMaps {
err := mMap.Unmap()
err := mMap.Flush()
if err != nil {
errs = append(errs, err)
}
err = mMap.Unmap()
if err != nil {
errs = append(errs, err)
}
Expand Down
2 changes: 1 addition & 1 deletion peerconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func BenchmarkConnectionMainReadLoop(b *testing.B) {
Length: 1 << 20,
PieceLength: 1 << 20,
}))
t.storage = &storage.Torrent{TorrentImpl: storage.TorrentImpl{Piece: ts.Piece, Close: ts.Close}}
t.storage = &storage.Torrent{TorrentImpl: storage.TorrentImpl{Piece: ts.Piece, Close: ts.Close, Flush: func() error { return nil }}}
t.onSetInfo()
t._pendingPieces.Add(0)
r, w := net.Pipe()
Expand Down
2 changes: 2 additions & 0 deletions storage/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (me *boltClient) OpenTorrent(_ *metainfo.Info, infoHash metainfo.Hash) (Tor
return TorrentImpl{
Piece: t.Piece,
Close: t.Close,
Flush: t.Flush,
}, nil
}

Expand All @@ -62,3 +63,4 @@ func (me *boltTorrent) Piece(p metainfo.Piece) PieceImpl {
}

func (boltTorrent) Close() error { return nil }
func (boltTorrent) Flush() error { return nil }
3 changes: 3 additions & 0 deletions storage/disabled/disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func (c Client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storag
Close: func() error {
return nil
},
Flush: func() error {
return nil
},
Capacity: &capFunc,
}, nil
}
Expand Down
5 changes: 5 additions & 0 deletions storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (fs fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash
return TorrentImpl{
Piece: t.Piece,
Close: t.Close,
Flush: t.Flush,
}, nil
}

Expand Down Expand Up @@ -122,6 +123,10 @@ func (fs *fileTorrentImpl) Close() error {
return nil
}

func (fs *fileTorrentImpl) Flush() error {
return nil
}

// A helper to create zero-length files which won't appear for file-orientated storage since no
// writes will ever occur to them (no torrent data is associated with a zero-length file). The
// caller should make sure the file name provided is safe/sanitized.
Expand Down
1 change: 1 addition & 0 deletions storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type TorrentCapacity *func() (cap int64, capped bool)
type TorrentImpl struct {
Piece func(p metainfo.Piece) PieceImpl
Close func() error
Flush func() error
// Storages that share the same space, will provide equal pointers. The function is called once
// to determine the storage for torrents sharing the same function pointer, and mutated in
// place.
Expand Down
9 changes: 8 additions & 1 deletion storage/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *mmapClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash
span: span,
pc: s.pc,
}
return TorrentImpl{Piece: t.Piece, Close: t.Close}, err
return TorrentImpl{Piece: t.Piece, Close: t.Close, Flush: t.Flush}, err
}

func (s *mmapClientImpl) Close() error {
Expand Down Expand Up @@ -71,6 +71,13 @@ func (ts *mmapTorrentStorage) Close() error {
}
return nil
}
func (ts *mmapTorrentStorage) Flush() error {
errs := ts.span.Flush()
if len(errs) > 0 {
return errs[0]
}
return nil
}

type mmapStoragePiece struct {
pc PieceCompletionGetSetter
Expand Down
6 changes: 5 additions & 1 deletion storage/piece-resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ func (piecePerResourceTorrentImpl) Close() error {
return nil
}

func (piecePerResourceTorrentImpl) Flush() error {
return nil
}

func (s piecePerResource) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) {
t := piecePerResourceTorrentImpl{
s,
make([]sync.RWMutex, info.NumPieces()),
}
return TorrentImpl{Piece: t.Piece, Close: t.Close}, nil
return TorrentImpl{Piece: t.Piece, Close: t.Close, Flush: t.Flush}, nil
}

func (s piecePerResourceTorrentImpl) Piece(p metainfo.Piece) PieceImpl {
Expand Down
5 changes: 4 additions & 1 deletion storage/sqlite/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type client struct {

func (c *client) OpenTorrent(*metainfo.Info, metainfo.Hash) (storage.TorrentImpl, error) {
t := torrent{c.Cache}
return storage.TorrentImpl{Piece: t.Piece, Close: t.Close, Capacity: &c.capacity}, nil
return storage.TorrentImpl{Piece: t.Piece, Close: t.Close, Capacity: &c.capacity, Flush: t.Flush}, nil
}

type torrent struct {
Expand All @@ -59,6 +59,9 @@ func (t torrent) Piece(p metainfo.Piece) storage.PieceImpl {
func (t torrent) Close() error {
return nil
}
func (t torrent) Flush() error {
return nil
}

type piece struct {
sb squirrel.Blob
Expand Down
6 changes: 5 additions & 1 deletion test/issue377_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ func (me *diskFullStorage) Close() error {
return nil
}

func (me *diskFullStorage) Flush() error {
return nil
}

func (d *diskFullStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage.TorrentImpl, error) {
return storage.TorrentImpl{Piece: d.Piece, Close: d.Close}, nil
return storage.TorrentImpl{Piece: d.Piece, Close: d.Close, Flush: d.Flush}, nil
}

type pieceImpl struct {
Expand Down
4 changes: 4 additions & 0 deletions torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,11 @@ func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex {
}

func (t *Torrent) updateComplete() {
before := t.Complete.Bool()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there might be a more suitable place to put this, there should be a method that's only called when torrent completion transitions to complete from incomplete.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I don't see such method.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see the confusion. This flushes the entire torrent, when the entire torrent is complete. Do you want perhaps flush the individual memory-maps, when associated pieces become complete? That would be a more gradual approach.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not spotting this before. Similarly, if this was the approach, the Flush would move on to the piece implementation. Let me know your thoughts.

t.Complete.SetBool(t.haveAllPieces())
if !before && t.Complete.Bool() {
_ = t.storage.Flush()
}
}

func (t *Torrent) cancelRequest(r RequestIndex) *Peer {
Expand Down