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 4 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
18 changes: 17 additions & 1 deletion mmap_span/mmap_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,27 @@ 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)
}
}
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
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
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.storage.Flush != nil && t.Complete.Bool() {
_ = t.storage.Flush()
}
}

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