-
Notifications
You must be signed in to change notification settings - Fork 387
fix(pushsync): prevent silent chunk loss on shallow receipts #5390
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
Open
gacevicljubisa
wants to merge
10
commits into
master
Choose a base branch
from
fix/pushsync-out-of-depth-chunk-loss
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
850dfcb
fix(pushsync,pusher): prevent silent chunk loss on shallow receipts
gacevicljubisa 0fab07c
fix(pushsync): modify TestShallowReceipt
gacevicljubisa 9089e44
fix(pushsync): add OutOfDepthStoring counter metric
gacevicljubisa 68669c3
chore: gofmt
gacevicljubisa 68b9c47
fix: revert unrelated changes
gacevicljubisa acae9c8
fix(pushsync): move shallow receipt check below unwrap block
gacevicljubisa 03057b1
test(pushsync): assert specific ErrWantSelf in TestOutOfDepthStoring
gacevicljubisa 1fe15dd
Merge remote-tracking branch 'origin' into fix/pushsync-out-of-depth-…
gacevicljubisa 6865805
fix(pushsync,pusher): complete CouldNotSync wiring and review fixes
gacevicljubisa 1e13f0a
fix(pushsync): strict shallow receipt check, no tolerance slop
gacevicljubisa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,27 +79,25 @@ type Storer interface { | |
| } | ||
|
|
||
| type PushSync struct { | ||
| address swarm.Address | ||
| networkID uint64 | ||
| radius func() (uint8, error) | ||
| nonce []byte | ||
| streamer p2p.StreamerDisconnecter | ||
| store Storer | ||
| topologyDriver topology.Driver | ||
| unwrap func(swarm.Chunk) | ||
| gsocHandler func(*soc.SOC) | ||
| logger log.Logger | ||
| accounting accounting.Interface | ||
| pricer pricer.Interface | ||
| metrics metrics | ||
| tracer *tracing.Tracer | ||
| validStamp postage.ValidStampFn | ||
| signer crypto.Signer | ||
| fullNode bool | ||
| errSkip *skippeers.List | ||
| stabilizer stabilization.Subscriber | ||
|
|
||
| shallowReceiptTolerance uint8 | ||
| address swarm.Address | ||
| networkID uint64 | ||
| radius func() (uint8, error) | ||
| nonce []byte | ||
| streamer p2p.StreamerDisconnecter | ||
| store Storer | ||
| topologyDriver topology.Driver | ||
| unwrap func(swarm.Chunk) | ||
| gsocHandler func(*soc.SOC) | ||
| logger log.Logger | ||
| accounting accounting.Interface | ||
| pricer pricer.Interface | ||
| metrics metrics | ||
| tracer *tracing.Tracer | ||
| validStamp postage.ValidStampFn | ||
| signer crypto.Signer | ||
| fullNode bool | ||
| errSkip *skippeers.List | ||
| stabilizer stabilization.Subscriber | ||
| overDraftRefreshLimiter *rate.Limiter | ||
| } | ||
|
|
||
|
|
@@ -128,7 +126,6 @@ func New( | |
| signer crypto.Signer, | ||
| tracer *tracing.Tracer, | ||
| stabilizer stabilization.Subscriber, | ||
| shallowReceiptTolerance uint8, | ||
| ) *PushSync { | ||
| ps := &PushSync{ | ||
| address: address, | ||
|
|
@@ -149,7 +146,6 @@ func New( | |
| signer: signer, | ||
| errSkip: skippeers.NewList(time.Minute), | ||
| stabilizer: stabilizer, | ||
| shallowReceiptTolerance: shallowReceiptTolerance, | ||
| overDraftRefreshLimiter: rate.NewLimiter(rate.Every(time.Second), 1), | ||
| } | ||
|
|
||
|
|
@@ -299,6 +295,12 @@ func (ps *PushSync) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) | |
|
|
||
| switch receipt, err := ps.pushToClosest(ctx, chunk, false); { | ||
| case errors.Is(err, topology.ErrWantSelf): | ||
| // Out-of-AOR chunks are unreachable via retrieval even when not | ||
| // evicted; let the origin try the next peer instead. | ||
| if swarm.Proximity(ps.address.Bytes(), chunkAddress.Bytes()) < rad { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if you have doubling set on, and the chunk is saved into a sister neighborhood? Should we have here a check based on the committed depth? |
||
| ps.metrics.OutOfDepthStoring.Inc() | ||
| return ErrOutOfDepthStoring | ||
| } | ||
| stored, reason = true, "want self" | ||
| return store(ctx) | ||
| case err == nil: | ||
|
|
@@ -361,10 +363,12 @@ func (ps *PushSync) pushToClosest(ctx context.Context, ch swarm.Chunk, origin bo | |
| ps.metrics.TotalRequests.Inc() | ||
|
|
||
| var ( | ||
| sentErrorsLeft = 1 | ||
| preemptiveTicker <-chan time.Time | ||
| inflight int | ||
| parallelForwards = maxMultiplexForwards | ||
| sentErrorsLeft = 1 | ||
| preemptiveTicker <-chan time.Time | ||
| inflight int | ||
| parallelForwards = maxMultiplexForwards | ||
| shallowReceiptResult *pb.Receipt | ||
| shallowReceiptPO uint8 | ||
| ) | ||
|
|
||
| if origin { | ||
|
|
@@ -415,33 +419,45 @@ func (ps *PushSync) pushToClosest(ctx context.Context, ch swarm.Chunk, origin bo | |
| // For non-origin peers, if the chunk is not within depth, they may store the chunk if they are the closest peer to the chunk. | ||
| fullSkip := append(skip.ChunkPeers(idAddress), ps.errSkip.ChunkPeers(idAddress)...) | ||
| peer, err := ps.closestPeer(ch.Address(), origin, fullSkip) | ||
| if errors.Is(err, topology.ErrNotFound) { | ||
| if skip.PruneExpiresAfter(idAddress, overDraftRefresh) == 0 { // no overdraft peers, we have depleted ALL peers | ||
| if inflight == 0 { | ||
| if ps.fullNode { | ||
| if cac.Valid(ch) { | ||
| go ps.unwrap(ch) | ||
| } | ||
| return nil, topology.ErrWantSelf | ||
| } | ||
| ps.logger.Debug("no peers left", "chunk_address", ch.Address(), "error", err) | ||
| return nil, err | ||
|
|
||
| // ErrWantSelf on a forwarder can mean closer peers exist but are | ||
| // overdraft-skipped; wait for refresh before falling back to self. | ||
| if errors.Is(err, topology.ErrNotFound) || errors.Is(err, topology.ErrWantSelf) { | ||
| if skip.PruneExpiresAfter(idAddress, overDraftRefresh) > 0 { | ||
| ps.metrics.OverdraftRefresh.Inc() | ||
| if ps.overDraftRefreshLimiter.Allow() { | ||
| ps.logger.Debug("sleeping to refresh overdraft balance") | ||
| } | ||
| continue // there is still an inflight request, wait for it's result | ||
| } | ||
|
|
||
| ps.metrics.OverdraftRefresh.Inc() | ||
| if ps.overDraftRefreshLimiter.Allow() { | ||
| ps.logger.Debug("sleeping to refresh overdraft balance") | ||
| select { | ||
| case <-time.After(overDraftRefresh): | ||
| retry() | ||
| continue | ||
| case <-ctx.Done(): | ||
| return nil, ctx.Err() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| select { | ||
| case <-time.After(overDraftRefresh): | ||
| retry() | ||
| continue | ||
| case <-ctx.Done(): | ||
| return nil, ctx.Err() | ||
| if errors.Is(err, topology.ErrNotFound) { | ||
| if inflight == 0 { | ||
| if ps.fullNode { | ||
| if cac.Valid(ch) { | ||
| go ps.unwrap(ch) | ||
| } | ||
| // prefer a shallow peer over self-store when one exists | ||
| if shallowReceiptResult != nil { | ||
| return shallowReceiptResult, ErrShallowReceipt | ||
| } | ||
| return nil, topology.ErrWantSelf | ||
| } | ||
| if shallowReceiptResult != nil { | ||
| return shallowReceiptResult, ErrShallowReceipt | ||
| } | ||
| ps.logger.Debug("no peers left", "chunk_address", ch.Address(), "error", err) | ||
| return nil, err | ||
| } | ||
| continue // there is still an inflight request, wait for it's result | ||
| } | ||
|
|
||
| if err != nil { | ||
|
|
@@ -486,12 +502,19 @@ func (ps *PushSync) pushToClosest(ctx context.Context, ch swarm.Chunk, origin bo | |
| return result.receipt, nil | ||
| } | ||
|
|
||
| switch err := ps.checkReceipt(result.receipt); { | ||
| // Cache the best (highest-PO) shallow receipt and exhaust the | ||
| // budget; surface ErrShallowReceipt only when nothing better lands. | ||
| switch po, err := ps.checkReceipt(result.receipt); { | ||
| case err == nil: | ||
| return result.receipt, nil | ||
| case errors.Is(err, ErrShallowReceipt): | ||
| ps.errSkip.Add(idAddress, result.peer, skiplistDur) | ||
| return result.receipt, err | ||
| if shallowReceiptResult == nil || po > shallowReceiptPO { | ||
| shallowReceiptResult = result.receipt | ||
| shallowReceiptPO = po | ||
| } | ||
| fallthrough | ||
| default: | ||
| result.err = err | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -505,6 +528,9 @@ func (ps *PushSync) pushToClosest(ctx context.Context, ch swarm.Chunk, origin bo | |
| } | ||
| } | ||
|
|
||
| if shallowReceiptResult != nil { | ||
| return shallowReceiptResult, ErrShallowReceipt | ||
| } | ||
| return nil, ErrNoPush | ||
| } | ||
|
|
||
|
|
@@ -564,42 +590,40 @@ func (ps *PushSync) push(parentCtx context.Context, resultChan chan<- receiptRes | |
| err = action.Apply() | ||
| } | ||
|
|
||
| func (ps *PushSync) checkReceipt(receipt *pb.Receipt) error { | ||
| // checkReceipt validates the receipt and returns the storer-to-chunk PO so | ||
| // callers can rank shallow receipts; PO is zero on signature errors. Strict | ||
| // po >= rad: a chunk is not synced until it lands within the AOR. | ||
| func (ps *PushSync) checkReceipt(receipt *pb.Receipt) (uint8, error) { | ||
| addr := swarm.NewAddress(receipt.Address) | ||
|
|
||
| publicKey, err := crypto.Recover(receipt.Signature, addr.Bytes()) | ||
| if err != nil { | ||
| return fmt.Errorf("pushsync: receipt recover: %w", err) | ||
| return 0, fmt.Errorf("pushsync: receipt recover: %w", err) | ||
| } | ||
|
|
||
| peer, err := crypto.NewOverlayAddress(*publicKey, ps.networkID, receipt.Nonce) | ||
| if err != nil { | ||
| return fmt.Errorf("pushsync: receipt storer address: %w", err) | ||
| return 0, fmt.Errorf("pushsync: receipt storer address: %w", err) | ||
| } | ||
|
|
||
| po := swarm.Proximity(addr.Bytes(), peer.Bytes()) | ||
|
|
||
| r, err := ps.radius() | ||
| if err != nil { | ||
| return fmt.Errorf("pushsync: storage radius: %w", err) | ||
| } | ||
|
|
||
| var tolerance uint8 | ||
| if r >= ps.shallowReceiptTolerance { // check for underflow of uint8 | ||
| tolerance = r - ps.shallowReceiptTolerance | ||
| return po, fmt.Errorf("pushsync: storage radius: %w", err) | ||
| } | ||
|
|
||
| if po < tolerance || uint32(po) < receipt.StorageRadius { | ||
| if po < r || uint32(po) < receipt.StorageRadius { | ||
| ps.metrics.ShallowReceiptDepth.WithLabelValues(strconv.Itoa(int(po))).Inc() | ||
| ps.metrics.ShallowReceipt.Inc() | ||
| ps.logger.Debug("shallow receipt", "chunk_address", addr, "peer_address", peer, "proximity_order", po, "peer_radius", receipt.StorageRadius, "self_radius", r) | ||
| return ErrShallowReceipt | ||
| return po, ErrShallowReceipt | ||
| } | ||
|
|
||
| ps.metrics.ReceiptDepth.WithLabelValues(strconv.Itoa(int(po))).Inc() | ||
| ps.logger.Debug("chunk pushed", "chunk_address", addr, "peer_address", peer, "proximity_order", po) | ||
|
|
||
| return nil | ||
| return po, nil | ||
| } | ||
|
|
||
| func (ps *PushSync) pushChunkToPeer(ctx context.Context, peer swarm.Address, ch swarm.Chunk) (receipt *pb.Receipt, err error) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing to ChunkCouldNotSync, will cause some false reporting because the ChunkCouldNotSync state is not checked and updated in uploadstore.go ->Report function. Maybe you would need to alsi include in that switch statement also ChunkCouldNotSync.