sync: a page is stored in one transaction with parallel verification; a sync is cut off by idleness, not a fixed 2 minutes
20,000 listings to a newcomer: 120 s (unfinished) -> 24 s. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A sync used to be cut off after a fixed two minutes, however well it was
|
||||
// going: a newcomer catching up from a large region stopped half way and
|
||||
// waited for the next anti-entropy round. Now it is cut off when it makes no
|
||||
// progress for syncIdle, and in any case after syncMax.
|
||||
const (
|
||||
syncIdle = 2 * time.Minute
|
||||
syncMax = 30 * time.Minute
|
||||
)
|
||||
|
||||
type idleKey struct{}
|
||||
|
||||
// idleTimer ends a context after a period without progress.
|
||||
type idleTimer struct {
|
||||
mu sync.Mutex
|
||||
timer *time.Timer
|
||||
idle time.Duration
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (t *idleTimer) touch() {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if !t.closed {
|
||||
t.timer.Reset(t.idle)
|
||||
}
|
||||
}
|
||||
|
||||
// withIdleTimeout returns a context that ends after idle without a call to
|
||||
// progress(ctx), or after max at the latest.
|
||||
func withIdleTimeout(parent context.Context, idle, max time.Duration) (context.Context, context.CancelFunc) {
|
||||
ctx, cancel := context.WithTimeout(parent, max)
|
||||
t := &idleTimer{idle: idle}
|
||||
t.timer = time.AfterFunc(idle, cancel)
|
||||
ctx = context.WithValue(ctx, idleKey{}, t)
|
||||
return ctx, func() {
|
||||
t.mu.Lock()
|
||||
t.closed = true
|
||||
t.timer.Stop()
|
||||
t.mu.Unlock()
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// progress tells ctx's idle timer that work is going on; for a context without
|
||||
// one it does nothing.
|
||||
func progress(ctx context.Context) {
|
||||
if t, ok := ctx.Value(idleKey{}).(*idleTimer); ok {
|
||||
t.touch()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestASyncThatKeepsMakingProgressIsNotCutOff(t *testing.T) {
|
||||
ctx, cancel := withIdleTimeout(context.Background(), 150*time.Millisecond, time.Minute)
|
||||
defer cancel()
|
||||
for i := 0; i < 10; i++ { // 500 ms in all, well past the idle limit
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
progress(ctx)
|
||||
if ctx.Err() != nil {
|
||||
t.Fatalf("cut off after %d steps although it made progress", i)
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("not cut off after it stopped making progress")
|
||||
}
|
||||
}
|
||||
|
||||
func TestASyncWithoutProgressIsCutOff(t *testing.T) {
|
||||
ctx, cancel := withIdleTimeout(context.Background(), 100*time.Millisecond, time.Minute)
|
||||
defer cancel()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("an idle sync was not cut off")
|
||||
}
|
||||
}
|
||||
|
||||
func TestASyncIsCutOffAtTheLatestAfterTheMaximum(t *testing.T) {
|
||||
ctx, cancel := withIdleTimeout(context.Background(), time.Minute, 200*time.Millisecond)
|
||||
defer cancel()
|
||||
for end := time.Now().Add(2 * time.Second); time.Now().Before(end); {
|
||||
progress(ctx)
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("progress kept a sync alive past the maximum")
|
||||
}
|
||||
|
||||
func TestProgressOnAPlainContextDoesNothing(t *testing.T) {
|
||||
progress(context.Background()) // must not panic
|
||||
}
|
||||
@@ -45,6 +45,7 @@ func (n *Node) recoverOwn(ctx context.Context, p peer.ID) error {
|
||||
for page := 0; page < maxSyncPages; page++ {
|
||||
var count int
|
||||
var last string
|
||||
var got []*listing.Listing
|
||||
err := n.request(ctx, p, syncRequest{
|
||||
Kind: "author", Author: author, Limit: n.cfg.SyncPageSize, AfterID: after,
|
||||
}, func(line []byte) {
|
||||
@@ -57,12 +58,11 @@ func (n *Node) recoverOwn(ctx context.Context, p peer.ID) error {
|
||||
if l.Author != author {
|
||||
return // peer sent something we did not ask for
|
||||
}
|
||||
if out, err := n.cfg.Store.Put(ctx, &l); err != nil {
|
||||
n.log.Debug("recover: listing rejected", "peer", p, "err", err)
|
||||
} else {
|
||||
n.noteListing(ctx, &l, out, "recover", p)
|
||||
}
|
||||
got = append(got, &l)
|
||||
})
|
||||
if serr := n.storeSynced(ctx, p, "recover", got); serr != nil {
|
||||
return serr
|
||||
}
|
||||
if err != nil {
|
||||
n.recoverAsked.Add(-1) // this peer could not answer; another may
|
||||
return err
|
||||
|
||||
+34
-11
@@ -320,7 +320,7 @@ func (n *Node) syncSoon(p peer.ID) {
|
||||
delete(n.syncing, p)
|
||||
n.mu.Unlock()
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(n.ctx, syncTimeout)
|
||||
ctx, cancel := withIdleTimeout(n.ctx, syncIdle, syncMax)
|
||||
defer cancel()
|
||||
if err := n.SyncFrom(ctx, p); err != nil {
|
||||
n.log.Debug("sync", "peer", p, "err", err)
|
||||
@@ -442,17 +442,17 @@ func (n *Node) pullListings(ctx context.Context, p peer.ID, region string) error
|
||||
for i, it := range batch {
|
||||
keys[i] = it.Key()
|
||||
}
|
||||
var got []*listing.Listing
|
||||
err := n.request(ctx, p, syncRequest{Kind: "fetch", Of: "listings", Region: region, Keys: keys}, func(line []byte) {
|
||||
var l listing.Listing
|
||||
if json.Unmarshal(line, &l) != nil || listing.Region(l.Geohash) != region {
|
||||
return
|
||||
}
|
||||
if out, err := n.cfg.Store.Put(ctx, &l); err != nil {
|
||||
n.log.Debug("sync: listing rejected", "peer", p, "err", err)
|
||||
} else {
|
||||
n.noteListing(ctx, &l, out, "sync", p)
|
||||
}
|
||||
got = append(got, &l)
|
||||
})
|
||||
if serr := n.storeSynced(ctx, p, "sync", got); serr != nil {
|
||||
return serr
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -598,6 +598,7 @@ func (n *Node) syncRegion(ctx context.Context, p peer.ID, region string) error {
|
||||
for page := 0; page < maxSyncPages; page++ {
|
||||
var count int
|
||||
var last *listing.Listing
|
||||
var got []*listing.Listing
|
||||
err := n.request(ctx, p, syncRequest{
|
||||
Kind: "listings", Region: region, Limit: n.cfg.SyncPageSize,
|
||||
AfterCreated: cur.CreatedAt, AfterAuthor: cur.Author, AfterID: cur.ID,
|
||||
@@ -611,12 +612,11 @@ func (n *Node) syncRegion(ctx context.Context, p peer.ID, region string) error {
|
||||
if listing.Region(l.Geohash) != region {
|
||||
return // peer sent something we did not ask for
|
||||
}
|
||||
if out, err := n.cfg.Store.Put(ctx, &l); err != nil {
|
||||
n.log.Debug("sync: listing rejected", "peer", p, "err", err)
|
||||
} else {
|
||||
n.noteListing(ctx, &l, out, "sync", p)
|
||||
}
|
||||
got = append(got, &l)
|
||||
})
|
||||
if serr := n.storeSynced(ctx, p, "sync", got); serr != nil {
|
||||
return serr
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -628,6 +628,27 @@ func (n *Node) syncRegion(ctx context.Context, p peer.ID, region string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// storeSynced stores listings that came from p in one transaction and counts
|
||||
// them. Refused ones are only logged.
|
||||
func (n *Node) storeSynced(ctx context.Context, p peer.ID, via string, ls []*listing.Listing) error {
|
||||
if len(ls) == 0 {
|
||||
return nil
|
||||
}
|
||||
res, err := n.cfg.Store.PutMany(ctx, ls)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
progress(ctx)
|
||||
for i, r := range res {
|
||||
if r.Err != nil {
|
||||
n.log.Debug(via+": listing rejected", "peer", p, "err", r.Err)
|
||||
continue
|
||||
}
|
||||
n.noteListing(ctx, ls[i], r.Outcome, via, p)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// request opens a sync stream, sends req and calls handle for each line.
|
||||
func (n *Node) request(ctx context.Context, p peer.ID, req syncRequest, handle func([]byte)) error {
|
||||
s, err := n.host.NewStream(ctx, p, SyncProtocol)
|
||||
@@ -648,11 +669,13 @@ func (n *Node) request(ctx context.Context, p peer.ID, req syncRequest, handle f
|
||||
sc := bufio.NewScanner(s)
|
||||
sc.Buffer(make([]byte, 0, 64<<10), listing.MaxSize+4096)
|
||||
for sc.Scan() {
|
||||
progress(ctx)
|
||||
handle(sc.Bytes())
|
||||
}
|
||||
if err := sc.Err(); err != nil {
|
||||
_ = s.Reset()
|
||||
return err
|
||||
}
|
||||
progress(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user