From 03714dfd5fbfd976f090a6edae6c80d20ad034c9 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Fri, 10 Jun 2022 10:04:18 +0700 Subject: [PATCH 01/19] do t.storage.Flush() when torrent completion happen. And when storage.Close() happening. --- mmap_span/mmap_span.go | 21 ++++++++++++++++++++- storage/bolt.go | 2 ++ storage/file.go | 5 +++++ storage/interface.go | 1 + storage/mmap.go | 9 ++++++++- storage/piece-resource.go | 6 +++++- torrent.go | 4 ++++ 7 files changed, 45 insertions(+), 3 deletions(-) diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index 6a6c8392ed..ebd3f0ac3e 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -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 + 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) } diff --git a/storage/bolt.go b/storage/bolt.go index 945b24995d..5aad4558b2 100644 --- a/storage/bolt.go +++ b/storage/bolt.go @@ -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 } @@ -62,3 +63,4 @@ func (me *boltTorrent) Piece(p metainfo.Piece) PieceImpl { } func (boltTorrent) Close() error { return nil } +func (boltTorrent) Flush() error { return nil } diff --git a/storage/file.go b/storage/file.go index d07dc7e915..ad339ed238 100644 --- a/storage/file.go +++ b/storage/file.go @@ -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 } @@ -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. diff --git a/storage/interface.go b/storage/interface.go index ee6e6336cd..3d1bfb3bfe 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -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. diff --git a/storage/mmap.go b/storage/mmap.go index 300d863510..a9d922ac20 100644 --- a/storage/mmap.go +++ b/storage/mmap.go @@ -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 { @@ -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 diff --git a/storage/piece-resource.go b/storage/piece-resource.go index ec3848df21..1d75b0e789 100644 --- a/storage/piece-resource.go +++ b/storage/piece-resource.go @@ -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 { diff --git a/torrent.go b/torrent.go index c8606ad82c..10d89a1a63 100644 --- a/torrent.go +++ b/torrent.go @@ -2461,7 +2461,11 @@ func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex { } func (t *Torrent) updateComplete() { + before := t.Complete.Bool() t.Complete.SetBool(t.haveAllPieces()) + if !before && t.Complete.Bool() { + _ = t.storage.Flush() + } } func (t *Torrent) cancelRequest(r RequestIndex) *Peer { From 82f028f41fcf55e912a523a8fe0c24509bd64bf9 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Fri, 10 Jun 2022 10:57:40 +0700 Subject: [PATCH 02/19] do t.storage.Flush() when torrent completion happen. And when storage.Close() happening. --- bad_storage.go | 1 + peerconn_test.go | 2 +- storage/disabled/disabled.go | 3 +++ storage/sqlite/direct.go | 5 ++++- test/issue377_test.go | 6 +++++- 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/bad_storage.go b/bad_storage.go index fc15beb907..268ed725f3 100644 --- a/bad_storage.go +++ b/bad_storage.go @@ -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 }, }, nil } diff --git a/peerconn_test.go b/peerconn_test.go index 1bd9712b86..b1533d70ab 100644 --- a/peerconn_test.go +++ b/peerconn_test.go @@ -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() diff --git a/storage/disabled/disabled.go b/storage/disabled/disabled.go index f511222ae2..936e6feae9 100644 --- a/storage/disabled/disabled.go +++ b/storage/disabled/disabled.go @@ -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 } diff --git a/storage/sqlite/direct.go b/storage/sqlite/direct.go index 3d51fd316b..3bcb782d07 100644 --- a/storage/sqlite/direct.go +++ b/storage/sqlite/direct.go @@ -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 { @@ -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 diff --git a/test/issue377_test.go b/test/issue377_test.go index 48e5abfbf3..5087d067c4 100644 --- a/test/issue377_test.go +++ b/test/issue377_test.go @@ -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 { From 78741df1850dd8d355b5c1de687959163c553c4b Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 18 Jun 2022 09:02:35 +0600 Subject: [PATCH 03/19] check nil on caller --- bad_storage.go | 1 - peerconn_test.go | 2 +- storage/bolt.go | 2 -- storage/disabled/disabled.go | 3 --- storage/file.go | 5 ----- storage/piece-resource.go | 6 +----- storage/sqlite/direct.go | 5 +---- test/issue377_test.go | 6 +----- torrent.go | 2 +- 9 files changed, 5 insertions(+), 27 deletions(-) diff --git a/bad_storage.go b/bad_storage.go index 268ed725f3..fc15beb907 100644 --- a/bad_storage.go +++ b/bad_storage.go @@ -17,7 +17,6 @@ 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 }, }, nil } diff --git a/peerconn_test.go b/peerconn_test.go index b1533d70ab..1bd9712b86 100644 --- a/peerconn_test.go +++ b/peerconn_test.go @@ -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, Flush: func() error { return nil }}} + t.storage = &storage.Torrent{TorrentImpl: storage.TorrentImpl{Piece: ts.Piece, Close: ts.Close}} t.onSetInfo() t._pendingPieces.Add(0) r, w := net.Pipe() diff --git a/storage/bolt.go b/storage/bolt.go index 5aad4558b2..945b24995d 100644 --- a/storage/bolt.go +++ b/storage/bolt.go @@ -47,7 +47,6 @@ func (me *boltClient) OpenTorrent(_ *metainfo.Info, infoHash metainfo.Hash) (Tor return TorrentImpl{ Piece: t.Piece, Close: t.Close, - Flush: t.Flush, }, nil } @@ -63,4 +62,3 @@ func (me *boltTorrent) Piece(p metainfo.Piece) PieceImpl { } func (boltTorrent) Close() error { return nil } -func (boltTorrent) Flush() error { return nil } diff --git a/storage/disabled/disabled.go b/storage/disabled/disabled.go index 936e6feae9..f511222ae2 100644 --- a/storage/disabled/disabled.go +++ b/storage/disabled/disabled.go @@ -20,9 +20,6 @@ func (c Client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storag Close: func() error { return nil }, - Flush: func() error { - return nil - }, Capacity: &capFunc, }, nil } diff --git a/storage/file.go b/storage/file.go index ad339ed238..d07dc7e915 100644 --- a/storage/file.go +++ b/storage/file.go @@ -90,7 +90,6 @@ func (fs fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash return TorrentImpl{ Piece: t.Piece, Close: t.Close, - Flush: t.Flush, }, nil } @@ -123,10 +122,6 @@ 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. diff --git a/storage/piece-resource.go b/storage/piece-resource.go index 1d75b0e789..ec3848df21 100644 --- a/storage/piece-resource.go +++ b/storage/piece-resource.go @@ -49,16 +49,12 @@ 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, Flush: t.Flush}, nil + return TorrentImpl{Piece: t.Piece, Close: t.Close}, nil } func (s piecePerResourceTorrentImpl) Piece(p metainfo.Piece) PieceImpl { diff --git a/storage/sqlite/direct.go b/storage/sqlite/direct.go index 3bcb782d07..3d51fd316b 100644 --- a/storage/sqlite/direct.go +++ b/storage/sqlite/direct.go @@ -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, Flush: t.Flush}, nil + return storage.TorrentImpl{Piece: t.Piece, Close: t.Close, Capacity: &c.capacity}, nil } type torrent struct { @@ -59,9 +59,6 @@ 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 diff --git a/test/issue377_test.go b/test/issue377_test.go index 5087d067c4..48e5abfbf3 100644 --- a/test/issue377_test.go +++ b/test/issue377_test.go @@ -126,12 +126,8 @@ 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, Flush: d.Flush}, nil + return storage.TorrentImpl{Piece: d.Piece, Close: d.Close}, nil } type pieceImpl struct { diff --git a/torrent.go b/torrent.go index 10d89a1a63..104dad50ac 100644 --- a/torrent.go +++ b/torrent.go @@ -2463,7 +2463,7 @@ func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex { func (t *Torrent) updateComplete() { before := t.Complete.Bool() t.Complete.SetBool(t.haveAllPieces()) - if !before && t.Complete.Bool() { + if !before && t.storage.Flush != nil && t.Complete.Bool() { _ = t.storage.Flush() } } From b22e5c0678fe18debcb88d14d0fa4dd7c704e155 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 18 Jun 2022 09:09:52 +0600 Subject: [PATCH 04/19] clean --- mmap_span/mmap_span.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index ebd3f0ac3e..ee9eca6e10 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -28,9 +28,6 @@ func (ms *MMapSpan) Flush() (errs []error) { errs = append(errs, err) } } - // This is for issue 211. - ms.mMaps = nil - ms.InitIndex() return } From 842f5612e96b06b9dbeb56400c147d873cf08a58 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Tue, 21 Jun 2022 08:26:19 +0600 Subject: [PATCH 05/19] FlushAsync method --- go.mod | 2 ++ go.sum | 4 ++-- mmap_span/mmap_span.go | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index a23fd1ab36..080b8926b9 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module github.com/anacrolix/torrent go 1.18 +replace github.com/edsrzf/mmap-go => github.com/AskAlexSharov/mmap-go v1.2.0 + require ( crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 github.com/RoaringBitmap/roaring v1.0.1-0.20220510143707-3f418c4f42a4 diff --git a/go.sum b/go.sum index 9b257b75a2..7ce14ab7c8 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 h1:fILCBBFnjnrQ0whVJlGhfv1E/QiaFDNtGFBObEVRnYg= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= +github.com/AskAlexSharov/mmap-go v1.2.0 h1:5Tyz/L4+o3ywXN+50pq7DNAiu/YSwBPKeB2h1vJka80= +github.com/AskAlexSharov/mmap-go v1.2.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Julusian/godocdown v0.0.0-20170816220326-6d19f8ff2df8/go.mod h1:INZr5t32rG59/5xeltqoCJoNY7e5x/3xoY9WSWVWg74= github.com/RoaringBitmap/roaring v0.4.7/go.mod h1:8khRDP4HmeXns4xIj9oGrKSz7XTQiJx2zgh7AcNke4w= @@ -118,8 +120,6 @@ github.com/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813/go.mod h1:11Gm+ccJ github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= -github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ= -github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q= github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= github.com/elliotchance/orderedmap v1.4.0 h1:wZtfeEONCbx6in1CZyE6bELEt/vFayMvsxqI5SgsR+A= github.com/elliotchance/orderedmap v1.4.0/go.mod h1:wsDwEaX5jEoyhbs7x93zk2H/qv0zwuhg4inXhDkYqys= diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index ee9eca6e10..e5538e593b 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -23,7 +23,7 @@ func (ms *MMapSpan) Flush() (errs []error) { ms.mu.Lock() defer ms.mu.Unlock() for _, mMap := range ms.mMaps { - err := mMap.Flush() + err := mMap.FlushAsync() // ms.Close does blocking flush if err != nil { errs = append(errs, err) } From 32d30866019509e2735ccf5d410ba85f534226dc Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Tue, 21 Jun 2022 08:47:16 +0600 Subject: [PATCH 06/19] FlushAsyncAt method --- mmap_span/mmap_span.go | 13 ++++++++++++- storage/mmap.go | 2 +- torrent.go | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index e5538e593b..9ae2162973 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -19,7 +19,7 @@ func (ms *MMapSpan) Append(mMap mmap.MMap) { ms.mMaps = append(ms.mMaps, mMap) } -func (ms *MMapSpan) Flush() (errs []error) { +func (ms *MMapSpan) FlushAsync() (errs []error) { ms.mu.Lock() defer ms.mu.Unlock() for _, mMap := range ms.mMaps { @@ -63,6 +63,17 @@ func (me *MMapSpan) InitIndex() { // log.Printf("made mmapspan index: %v", me.segmentLocater) } +func (ms *MMapSpan) FlushAt(p []byte, off int64) (err error) { + ms.mu.Lock() + defer ms.mu.Unlock() + ms.segmentLocater.Locate(segments.Extent{off, int64(len(p))}, func(i int, e segments.Extent) bool { + //TODO: OS knows which pages are dirty, + _ = ms.mMaps[i].FlushAsync() + return true + }) + return nil +} + func (ms *MMapSpan) ReadAt(p []byte, off int64) (n int, err error) { // log.Printf("reading %v bytes at %v", len(p), off) ms.mu.RLock() diff --git a/storage/mmap.go b/storage/mmap.go index a9d922ac20..281b7adbfb 100644 --- a/storage/mmap.go +++ b/storage/mmap.go @@ -72,7 +72,7 @@ func (ts *mmapTorrentStorage) Close() error { return nil } func (ts *mmapTorrentStorage) Flush() error { - errs := ts.span.Flush() + errs := ts.span.FlushAsync() if len(errs) > 0 { return errs[0] } diff --git a/torrent.go b/torrent.go index f8af697ca7..66f9197dce 100644 --- a/torrent.go +++ b/torrent.go @@ -2088,6 +2088,7 @@ func (t *Torrent) onPieceCompleted(piece pieceIndex) { conn.have(piece) t.maybeDropMutuallyCompletePeer(&conn.Peer) } + t.piece(piece).Storage().MarkComplete() } // Called when a piece is found to be not complete. From 259201f4b30197970ce6086a4a5ea75b1e6af6c5 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Tue, 21 Jun 2022 08:52:56 +0600 Subject: [PATCH 07/19] FlushAsyncAt method --- go.mod | 2 +- go.sum | 4 ++-- mmap_span/mmap_span.go | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 080b8926b9..05c719f18b 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/anacrolix/torrent go 1.18 -replace github.com/edsrzf/mmap-go => github.com/AskAlexSharov/mmap-go v1.2.0 +replace github.com/edsrzf/mmap-go => github.com/AskAlexSharov/mmap-go v1.4.0 require ( crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 diff --git a/go.sum b/go.sum index 7ce14ab7c8..ddab658054 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 h1:fILCBBFnjnrQ0whVJlGhfv1E/QiaFDNtGFBObEVRnYg= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= -github.com/AskAlexSharov/mmap-go v1.2.0 h1:5Tyz/L4+o3ywXN+50pq7DNAiu/YSwBPKeB2h1vJka80= -github.com/AskAlexSharov/mmap-go v1.2.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q= +github.com/AskAlexSharov/mmap-go v1.4.0 h1:+4YYliojbn45IDAxNb4CqnEvOJIkUKSlVsn5x+ED4FI= +github.com/AskAlexSharov/mmap-go v1.4.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Julusian/godocdown v0.0.0-20170816220326-6d19f8ff2df8/go.mod h1:INZr5t32rG59/5xeltqoCJoNY7e5x/3xoY9WSWVWg74= github.com/RoaringBitmap/roaring v0.4.7/go.mod h1:8khRDP4HmeXns4xIj9oGrKSz7XTQiJx2zgh7AcNke4w= diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index 9ae2162973..7400f6f161 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -68,7 +68,7 @@ func (ms *MMapSpan) FlushAt(p []byte, off int64) (err error) { defer ms.mu.Unlock() ms.segmentLocater.Locate(segments.Extent{off, int64(len(p))}, func(i int, e segments.Extent) bool { //TODO: OS knows which pages are dirty, - _ = ms.mMaps[i].FlushAsync() + _ = ms.mMaps[i].FlushAsyncAt(e.Start, e.Length) return true }) return nil @@ -96,6 +96,7 @@ func (ms *MMapSpan) locateCopy(copyArgs func(remainingArgument, mmapped []byte) _n := copyBytes(copyArgs(p, mMapBytes)) p = p[_n:] n += _n + if segments.Int(_n) != e.Length { panic(fmt.Sprintf("did %d bytes, expected to do %d", _n, e.Length)) } From 320081ca2c97c7a8e617811b13d20b721f0d2edb Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Tue, 21 Jun 2022 08:56:05 +0600 Subject: [PATCH 08/19] FlushAsyncAt method --- torrent.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/torrent.go b/torrent.go index 66f9197dce..e53b897264 100644 --- a/torrent.go +++ b/torrent.go @@ -2462,11 +2462,7 @@ func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex { } func (t *Torrent) updateComplete() { - before := t.Complete.Bool() t.Complete.SetBool(t.haveAllPieces()) - if !before && t.storage.Flush != nil && t.Complete.Bool() { - _ = t.storage.Flush() - } } func (t *Torrent) cancelRequest(r RequestIndex) *Peer { From 2acf8481d855f3a2ae5913aa267f7264b2490558 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Tue, 21 Jun 2022 09:00:17 +0600 Subject: [PATCH 09/19] FlushAsyncAt method --- go.mod | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 05c719f18b..68f61d3d91 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/anacrolix/torrent go 1.18 -replace github.com/edsrzf/mmap-go => github.com/AskAlexSharov/mmap-go v1.4.0 - require ( crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 github.com/RoaringBitmap/roaring v1.0.1-0.20220510143707-3f418c4f42a4 @@ -30,7 +28,7 @@ require ( github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 github.com/davecgh/go-spew v1.1.1 github.com/dustin/go-humanize v1.0.0 - github.com/edsrzf/mmap-go v1.1.0 + github.com/AskAlexSharov/mmap-go v1.4.0 github.com/elliotchance/orderedmap v1.4.0 github.com/frankban/quicktest v1.14.3 github.com/fsnotify/fsnotify v1.5.1 From 8af4a58c6876b0b5024a48b07054541df761d4f7 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Tue, 21 Jun 2022 09:03:21 +0600 Subject: [PATCH 10/19] FlushAsyncAt method --- cmd/torrent-verify/main.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- iplist/packed.go | 2 +- mmap_span/mmap_span.go | 2 +- storage/mmap.go | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/torrent-verify/main.go b/cmd/torrent-verify/main.go index e945f12f6d..4e3f069879 100644 --- a/cmd/torrent-verify/main.go +++ b/cmd/torrent-verify/main.go @@ -9,8 +9,8 @@ import ( "os" "path/filepath" + "github.com/AskAlexSharov/mmap-go" "github.com/anacrolix/tagflag" - "github.com/edsrzf/mmap-go" "github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/mmap_span" diff --git a/go.mod b/go.mod index 68f61d3d91..4129329980 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,6 @@ require ( github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 github.com/davecgh/go-spew v1.1.1 github.com/dustin/go-humanize v1.0.0 - github.com/AskAlexSharov/mmap-go v1.4.0 github.com/elliotchance/orderedmap v1.4.0 github.com/frankban/quicktest v1.14.3 github.com/fsnotify/fsnotify v1.5.1 @@ -49,6 +48,7 @@ require ( ) require ( + github.com/AskAlexSharov/mmap-go v1.5.0 // indirect github.com/alexflint/go-scalar v1.1.0 // indirect github.com/anacrolix/mmsg v1.0.0 // indirect github.com/anacrolix/stm v0.3.0 // indirect diff --git a/go.sum b/go.sum index ddab658054..6f8ebf9a25 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 h1:fILCBBFnjnrQ0whVJlGhfv1E/QiaFDNtGFBObEVRnYg= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= -github.com/AskAlexSharov/mmap-go v1.4.0 h1:+4YYliojbn45IDAxNb4CqnEvOJIkUKSlVsn5x+ED4FI= -github.com/AskAlexSharov/mmap-go v1.4.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q= +github.com/AskAlexSharov/mmap-go v1.5.0 h1:jSaqLONrUIJOSzQGyszDMbF6q9rHrVhxwcPxrw6Lyhk= +github.com/AskAlexSharov/mmap-go v1.5.0/go.mod h1:ph1xIqkS6JOQQoBg9LKEf/IXnO0zfOnU0FJyPbFf4rQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Julusian/godocdown v0.0.0-20170816220326-6d19f8ff2df8/go.mod h1:INZr5t32rG59/5xeltqoCJoNY7e5x/3xoY9WSWVWg74= github.com/RoaringBitmap/roaring v0.4.7/go.mod h1:8khRDP4HmeXns4xIj9oGrKSz7XTQiJx2zgh7AcNke4w= diff --git a/iplist/packed.go b/iplist/packed.go index 5ae1faea2b..8944af8466 100644 --- a/iplist/packed.go +++ b/iplist/packed.go @@ -10,7 +10,7 @@ import ( "net" "os" - "github.com/edsrzf/mmap-go" + "github.com/AskAlexSharov/mmap-go" ) // The packed format is an 8 byte integer of the number of ranges. Then 20 diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index 7400f6f161..60503d558c 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -5,8 +5,8 @@ import ( "io" "sync" + "github.com/AskAlexSharov/mmap-go" "github.com/anacrolix/torrent/segments" - "github.com/edsrzf/mmap-go" ) type MMapSpan struct { diff --git a/storage/mmap.go b/storage/mmap.go index 281b7adbfb..b545fe5387 100644 --- a/storage/mmap.go +++ b/storage/mmap.go @@ -10,8 +10,8 @@ import ( "os" "path/filepath" + "github.com/AskAlexSharov/mmap-go" "github.com/anacrolix/missinggo/v2" - "github.com/edsrzf/mmap-go" "github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/mmap_span" From 6e9f2d01888c4eaf4fa8cfd7615b69252f31d910 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Tue, 21 Jun 2022 09:03:34 +0600 Subject: [PATCH 11/19] FlushAsyncAt method --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 4129329980..948e4ee382 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 + github.com/AskAlexSharov/mmap-go v1.5.0 github.com/RoaringBitmap/roaring v1.0.1-0.20220510143707-3f418c4f42a4 github.com/ajwerner/btree v0.0.0-20211221152037-f427b3e689c0 github.com/alexflint/go-arg v1.4.2 @@ -48,7 +49,6 @@ require ( ) require ( - github.com/AskAlexSharov/mmap-go v1.5.0 // indirect github.com/alexflint/go-scalar v1.1.0 // indirect github.com/anacrolix/mmsg v1.0.0 // indirect github.com/anacrolix/stm v0.3.0 // indirect From df962eb34f0f1285a4e26bb16d758a88fc035682 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Fri, 1 Jul 2022 11:36:28 +0600 Subject: [PATCH 12/19] save --- cmd/torrent-verify/main.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- iplist/packed.go | 2 +- mmap_span/mmap_span.go | 17 +++-------------- piece.go | 4 ++++ storage/mmap.go | 4 ++-- torrent.go | 3 +++ 8 files changed, 17 insertions(+), 21 deletions(-) diff --git a/cmd/torrent-verify/main.go b/cmd/torrent-verify/main.go index 4e3f069879..e945f12f6d 100644 --- a/cmd/torrent-verify/main.go +++ b/cmd/torrent-verify/main.go @@ -9,8 +9,8 @@ import ( "os" "path/filepath" - "github.com/AskAlexSharov/mmap-go" "github.com/anacrolix/tagflag" + "github.com/edsrzf/mmap-go" "github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/mmap_span" diff --git a/go.mod b/go.mod index cc7cf1f20c..7d56c415fe 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.18 require ( crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 - github.com/AskAlexSharov/mmap-go v1.5.0 github.com/RoaringBitmap/roaring v1.0.1-0.20220510143707-3f418c4f42a4 github.com/ajwerner/btree v0.0.0-20211221152037-f427b3e689c0 github.com/alexflint/go-arg v1.4.2 @@ -29,6 +28,7 @@ require ( github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 github.com/davecgh/go-spew v1.1.1 github.com/dustin/go-humanize v1.0.0 + github.com/edsrzf/mmap-go v1.0.0 github.com/elliotchance/orderedmap v1.4.0 github.com/frankban/quicktest v1.14.3 github.com/fsnotify/fsnotify v1.5.1 diff --git a/go.sum b/go.sum index 3348e7f3d5..ef3277c886 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,6 @@ crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 h1:fILCBBFnjnrQ0whVJlGhfv1E/QiaFDNtGFBObEVRnYg= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= -github.com/AskAlexSharov/mmap-go v1.5.0 h1:jSaqLONrUIJOSzQGyszDMbF6q9rHrVhxwcPxrw6Lyhk= -github.com/AskAlexSharov/mmap-go v1.5.0/go.mod h1:ph1xIqkS6JOQQoBg9LKEf/IXnO0zfOnU0FJyPbFf4rQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Julusian/godocdown v0.0.0-20170816220326-6d19f8ff2df8/go.mod h1:INZr5t32rG59/5xeltqoCJoNY7e5x/3xoY9WSWVWg74= github.com/RoaringBitmap/roaring v0.4.7/go.mod h1:8khRDP4HmeXns4xIj9oGrKSz7XTQiJx2zgh7AcNke4w= @@ -120,6 +118,8 @@ github.com/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813/go.mod h1:11Gm+ccJ github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= +github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw= +github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= github.com/elliotchance/orderedmap v1.4.0 h1:wZtfeEONCbx6in1CZyE6bELEt/vFayMvsxqI5SgsR+A= github.com/elliotchance/orderedmap v1.4.0/go.mod h1:wsDwEaX5jEoyhbs7x93zk2H/qv0zwuhg4inXhDkYqys= diff --git a/iplist/packed.go b/iplist/packed.go index 8944af8466..5ae1faea2b 100644 --- a/iplist/packed.go +++ b/iplist/packed.go @@ -10,7 +10,7 @@ import ( "net" "os" - "github.com/AskAlexSharov/mmap-go" + "github.com/edsrzf/mmap-go" ) // The packed format is an 8 byte integer of the number of ranges. Then 20 diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index 60503d558c..438c54ba8d 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -5,8 +5,8 @@ import ( "io" "sync" - "github.com/AskAlexSharov/mmap-go" "github.com/anacrolix/torrent/segments" + "github.com/edsrzf/mmap-go" ) type MMapSpan struct { @@ -19,11 +19,11 @@ func (ms *MMapSpan) Append(mMap mmap.MMap) { ms.mMaps = append(ms.mMaps, mMap) } -func (ms *MMapSpan) FlushAsync() (errs []error) { +func (ms *MMapSpan) Flush() (errs []error) { ms.mu.Lock() defer ms.mu.Unlock() for _, mMap := range ms.mMaps { - err := mMap.FlushAsync() // ms.Close does blocking flush + err := mMap.Flush() // ms.Close does blocking flush if err != nil { errs = append(errs, err) } @@ -63,17 +63,6 @@ func (me *MMapSpan) InitIndex() { // log.Printf("made mmapspan index: %v", me.segmentLocater) } -func (ms *MMapSpan) FlushAt(p []byte, off int64) (err error) { - ms.mu.Lock() - defer ms.mu.Unlock() - ms.segmentLocater.Locate(segments.Extent{off, int64(len(p))}, func(i int, e segments.Extent) bool { - //TODO: OS knows which pages are dirty, - _ = ms.mMaps[i].FlushAsyncAt(e.Start, e.Length) - return true - }) - return nil -} - func (ms *MMapSpan) ReadAt(p []byte, off int64) (n int, err error) { // log.Printf("reading %v bytes at %v", len(p), off) ms.mu.RLock() diff --git a/piece.go b/piece.go index 680675bab3..ffb58d4732 100644 --- a/piece.go +++ b/piece.go @@ -54,6 +54,10 @@ func (p *Piece) Storage() storage.Piece { return p.t.storage.Piece(p.Info()) } +func (p *Piece) Flush() { + _ = p.t.storage.Flush() +} + func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool { return !p.chunkIndexDirty(chunkIndex) } diff --git a/storage/mmap.go b/storage/mmap.go index b545fe5387..a9d922ac20 100644 --- a/storage/mmap.go +++ b/storage/mmap.go @@ -10,8 +10,8 @@ import ( "os" "path/filepath" - "github.com/AskAlexSharov/mmap-go" "github.com/anacrolix/missinggo/v2" + "github.com/edsrzf/mmap-go" "github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/mmap_span" @@ -72,7 +72,7 @@ func (ts *mmapTorrentStorage) Close() error { return nil } func (ts *mmapTorrentStorage) Flush() error { - errs := ts.span.FlushAsync() + errs := ts.span.Flush() if len(errs) > 0 { return errs[0] } diff --git a/torrent.go b/torrent.go index e53b897264..02e4aa2c72 100644 --- a/torrent.go +++ b/torrent.go @@ -2008,6 +2008,9 @@ func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) { } t.clearPieceTouchers(piece) t.cl.unlock() + if p.hasDirtyChunks() { + p.Flush() // You can be synchronous here! + } err := p.Storage().MarkComplete() if err != nil { t.logger.Printf("%T: error marking piece complete %d: %s", t.storage, piece, err) From eaa4a6ea7156209b451ac3995eb36b12a3faf819 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Fri, 1 Jul 2022 11:36:56 +0600 Subject: [PATCH 13/19] save --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 7d56c415fe..d63462cb3d 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 github.com/davecgh/go-spew v1.1.1 github.com/dustin/go-humanize v1.0.0 - github.com/edsrzf/mmap-go v1.0.0 + github.com/edsrzf/mmap-go v1.1.0 github.com/elliotchance/orderedmap v1.4.0 github.com/frankban/quicktest v1.14.3 github.com/fsnotify/fsnotify v1.5.1 diff --git a/go.sum b/go.sum index ef3277c886..e589784c14 100644 --- a/go.sum +++ b/go.sum @@ -118,8 +118,8 @@ github.com/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813/go.mod h1:11Gm+ccJ github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= -github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw= -github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= +github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ= +github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q= github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= github.com/elliotchance/orderedmap v1.4.0 h1:wZtfeEONCbx6in1CZyE6bELEt/vFayMvsxqI5SgsR+A= github.com/elliotchance/orderedmap v1.4.0/go.mod h1:wsDwEaX5jEoyhbs7x93zk2H/qv0zwuhg4inXhDkYqys= From 0c797270613aaca436e3875b8b04df0150feac88 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Fri, 1 Jul 2022 11:37:54 +0600 Subject: [PATCH 14/19] save --- mmap_span/mmap_span.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index 438c54ba8d..dd2f628072 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -23,7 +23,7 @@ func (ms *MMapSpan) Flush() (errs []error) { ms.mu.Lock() defer ms.mu.Unlock() for _, mMap := range ms.mMaps { - err := mMap.Flush() // ms.Close does blocking flush + err := mMap.Flush() if err != nil { errs = append(errs, err) } @@ -35,11 +35,7 @@ func (ms *MMapSpan) Close() (errs []error) { ms.mu.Lock() defer ms.mu.Unlock() for _, mMap := range ms.mMaps { - err := mMap.Flush() - if err != nil { - errs = append(errs, err) - } - err = mMap.Unmap() + err := mMap.Unmap() if err != nil { errs = append(errs, err) } From 9e69d94ec258de70189c804b87bc4826cdf26cc2 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sun, 3 Jul 2022 10:47:09 +0600 Subject: [PATCH 15/19] save --- mmap_span/mmap_span.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index dd2f628072..d9970be4be 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -20,8 +20,8 @@ func (ms *MMapSpan) Append(mMap mmap.MMap) { } func (ms *MMapSpan) Flush() (errs []error) { - ms.mu.Lock() - defer ms.mu.Unlock() + ms.mu.RLock() + defer ms.mu.RUnlock() for _, mMap := range ms.mMaps { err := mMap.Flush() if err != nil { From cf9f013e0755f73a7a3e6627ee8332443bbf3dd4 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sun, 3 Jul 2022 10:58:51 +0600 Subject: [PATCH 16/19] save --- mmap_span/mmap_span.go | 2 ++ piece.go | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index d9970be4be..c6a48e0ca9 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "sync" + "time" "github.com/anacrolix/torrent/segments" "github.com/edsrzf/mmap-go" @@ -22,6 +23,7 @@ func (ms *MMapSpan) Append(mMap mmap.MMap) { func (ms *MMapSpan) Flush() (errs []error) { ms.mu.RLock() defer ms.mu.RUnlock() + defer func(t time.Time) { fmt.Printf("mmap_span.go:26: %s\n", time.Since(t)) }(time.Now()) for _, mMap := range ms.mMaps { err := mMap.Flush() if err != nil { diff --git a/piece.go b/piece.go index ffb58d4732..3ef3576fdd 100644 --- a/piece.go +++ b/piece.go @@ -55,7 +55,9 @@ func (p *Piece) Storage() storage.Piece { } func (p *Piece) Flush() { - _ = p.t.storage.Flush() + if p.t.storage.Flush != nil { + _ = p.t.storage.Flush() + } } func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool { From a79295e9dd67d48e140512f89c91a455d56a6be5 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Tue, 5 Jul 2022 09:41:59 +0600 Subject: [PATCH 17/19] save --- torrent.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/torrent.go b/torrent.go index 02e4aa2c72..5b4d3b4bfb 100644 --- a/torrent.go +++ b/torrent.go @@ -2007,8 +2007,9 @@ func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) { c._stats.incrementPiecesDirtiedGood() } t.clearPieceTouchers(piece) + hasDirty := p.hasDirtyChunks() t.cl.unlock() - if p.hasDirtyChunks() { + if hasDirty { p.Flush() // You can be synchronous here! } err := p.Storage().MarkComplete() From ce57a482f970bd7b7e858569c7acef3b6b5759b8 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Thu, 7 Jul 2022 10:33:21 +0600 Subject: [PATCH 18/19] save --- mmap_span/mmap_span.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/mmap_span/mmap_span.go b/mmap_span/mmap_span.go index c6a48e0ca9..d9970be4be 100644 --- a/mmap_span/mmap_span.go +++ b/mmap_span/mmap_span.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "sync" - "time" "github.com/anacrolix/torrent/segments" "github.com/edsrzf/mmap-go" @@ -23,7 +22,6 @@ func (ms *MMapSpan) Append(mMap mmap.MMap) { func (ms *MMapSpan) Flush() (errs []error) { ms.mu.RLock() defer ms.mu.RUnlock() - defer func(t time.Time) { fmt.Printf("mmap_span.go:26: %s\n", time.Since(t)) }(time.Now()) for _, mMap := range ms.mMaps { err := mMap.Flush() if err != nil { From a4a87d5a987220357bd6b68c6437a5440c072b77 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Thu, 7 Jul 2022 10:38:25 +0600 Subject: [PATCH 19/19] save --- torrent.go | 1 - 1 file changed, 1 deletion(-) diff --git a/torrent.go b/torrent.go index 5b4d3b4bfb..e53b6bef23 100644 --- a/torrent.go +++ b/torrent.go @@ -2092,7 +2092,6 @@ func (t *Torrent) onPieceCompleted(piece pieceIndex) { conn.have(piece) t.maybeDropMutuallyCompletePeer(&conn.Peer) } - t.piece(piece).Storage().MarkComplete() } // Called when a piece is found to be not complete.