-
Notifications
You must be signed in to change notification settings - Fork 39
Avoid blocking on completion signals when the waiter has left #681
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
kevindharmawan
wants to merge
4
commits into
hyperledger:main
Choose a base branch
from
SeeMoReLab:fix-completion-channel-blocking
base: main
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
4 commits
Select commit
Hold shift + click to select a range
d1533be
Avoid blocking controller delivery completion
kevindharmawan 6bd2924
Avoid blocking in-flight view-change completion
kevindharmawan 72dd0a2
Remove unused in-flight attempt state and dead callbacks
kevindharmawan a167920
Skip view bookkeeping for decisions of a stopped view
kevindharmawan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // Copyright IBM Corp. All Rights Reserved. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| package bft | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/hyperledger-labs/SmartBFT/pkg/types" | ||
| protos "github.com/hyperledger-labs/SmartBFT/smartbftprotos" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/zap" | ||
| "google.golang.org/protobuf/proto" | ||
| ) | ||
|
|
||
| func TestControllerDecideDoesNotBlockIfDeliveryWaiterLeft(t *testing.T) { | ||
| view := &View{abortChan: make(chan struct{})} | ||
| controller := newDecidingController(t, view) | ||
|
|
||
| requireReturns(t, func() { | ||
| controller.decide(newDecision(t, view)) | ||
| }) | ||
| assert.Equal(t, uint64(1), controller.getCurrentDecisionsInView()) | ||
| } | ||
|
|
||
| func TestControllerDecideSkipsViewBookkeepingIfDecidingViewIsGone(t *testing.T) { | ||
| t.Run("view aborted", func(t *testing.T) { | ||
| view := &View{abortChan: make(chan struct{})} | ||
| controller := newDecidingController(t, view) | ||
| view.stop() | ||
|
|
||
| requireReturns(t, func() { | ||
| controller.decide(newDecision(t, view)) | ||
| }) | ||
| assert.Equal(t, uint64(0), controller.getCurrentDecisionsInView()) | ||
| assert.Empty(t, controller.leaderToken) | ||
| }) | ||
|
|
||
| t.Run("view replaced", func(t *testing.T) { | ||
| view := &View{abortChan: make(chan struct{})} | ||
| view.stop() | ||
| controller := newDecidingController(t, &View{abortChan: make(chan struct{})}) | ||
|
|
||
| requireReturns(t, func() { | ||
| controller.decide(newDecision(t, view)) | ||
| }) | ||
| assert.Equal(t, uint64(0), controller.getCurrentDecisionsInView()) | ||
| assert.Empty(t, controller.leaderToken) | ||
| }) | ||
| } | ||
|
|
||
| func TestViewChangerDecideDoesNotBlockIfInFlightWaiterLeft(t *testing.T) { | ||
| inFlightView := &View{abortChan: make(chan struct{})} | ||
| viewChanger := &ViewChanger{ | ||
| Logger: zap.NewNop().Sugar(), | ||
| Application: applicationFunc(func(types.Proposal, []types.Signature) types.Reconfig { return types.Reconfig{} }), | ||
| RequestsTimer: noopRequestsTimer{}, | ||
| Pruner: noopPruner{}, | ||
| } | ||
| // Unbuffered and unread: the attempt's waiter has already left. | ||
| attempt := &inFlightAttempt{ | ||
| decideCh: make(chan struct{}), | ||
| syncCh: make(chan struct{}), | ||
| viewRef: inFlightView, | ||
| } | ||
|
|
||
| requireReturns(t, func() { | ||
| viewChanger.decideInFlight(attempt, types.Proposal{}, nil, nil) | ||
| }) | ||
| assert.True(t, inFlightView.Stopped()) | ||
| } | ||
|
|
||
| func TestViewChangerSyncDoesNotBlockIfInFlightWaiterLeft(t *testing.T) { | ||
| viewChanger := &ViewChanger{ | ||
| Logger: zap.NewNop().Sugar(), | ||
| Synchronizer: synchronizerFunc(func() {}), | ||
| } | ||
| // Unbuffered and unread: the attempt's waiter has already left. | ||
| attempt := &inFlightAttempt{ | ||
| decideCh: make(chan struct{}), | ||
| syncCh: make(chan struct{}), | ||
| } | ||
|
|
||
| requireReturns(t, func() { | ||
| viewChanger.syncInFlight(attempt) | ||
| }) | ||
| } | ||
|
|
||
| // newDecidingController returns a controller whose current view is the given view, | ||
| // and whose own ID is the leader of the current view, so that a delivered decision | ||
| // would normally acquire the leader token. | ||
| func newDecidingController(t *testing.T, currView Proposer) *Controller { | ||
| metadata, err := proto.Marshal(&protos.ViewMetadata{}) | ||
| assert.NoError(t, err) | ||
|
|
||
| checkpoint := &types.Checkpoint{} | ||
| checkpoint.Set(types.Proposal{Metadata: metadata}, nil) | ||
|
|
||
| return &Controller{ | ||
| ID: 1, | ||
| N: 4, | ||
| NodesList: []uint64{1, 2, 3, 4}, | ||
| Logger: zap.NewNop().Sugar(), | ||
| Deliver: applicationFunc(func(types.Proposal, []types.Signature) types.Reconfig { return types.Reconfig{} }), | ||
| Verifier: noopVerifier{}, | ||
| Checkpoint: checkpoint, | ||
| currView: currView, | ||
| stopChan: make(chan struct{}), | ||
| leaderToken: make(chan struct{}, 1), | ||
| } | ||
| } | ||
|
|
||
| func newDecision(t *testing.T, view Proposer) decision { | ||
| metadata, err := proto.Marshal(&protos.ViewMetadata{}) | ||
| assert.NoError(t, err) | ||
|
|
||
| return decision{ | ||
| proposal: types.Proposal{Metadata: metadata}, | ||
| view: view, | ||
| delivered: make(chan struct{}), | ||
| } | ||
| } | ||
|
|
||
| func requireReturns(t *testing.T, f func()) { | ||
| t.Helper() | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| defer close(done) | ||
| f() | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("function blocked") | ||
| } | ||
| } | ||
|
|
||
| // These tests live in package bft to reach unexported completion paths, so they | ||
| // cannot use internal/bft/mocks without creating an import cycle. | ||
|
|
||
| type applicationFunc func(types.Proposal, []types.Signature) types.Reconfig | ||
|
|
||
| func (f applicationFunc) Deliver(proposal types.Proposal, signatures []types.Signature) types.Reconfig { | ||
| return f(proposal, signatures) | ||
| } | ||
|
|
||
| type noopVerifier struct{} | ||
|
|
||
| func (noopVerifier) VerificationSequence() uint64 { | ||
| return 0 | ||
| } | ||
|
|
||
| func (noopVerifier) VerifyProposal(types.Proposal) ([]types.RequestInfo, error) { | ||
| panic("unexpected VerifyProposal call") | ||
| } | ||
|
|
||
| func (noopVerifier) VerifyRequest([]byte) (types.RequestInfo, error) { | ||
| panic("unexpected VerifyRequest call") | ||
| } | ||
|
|
||
| func (noopVerifier) VerifyConsenterSig(types.Signature, types.Proposal) ([]byte, error) { | ||
| panic("unexpected VerifyConsenterSig call") | ||
| } | ||
|
|
||
| func (noopVerifier) VerifySignature(types.Signature) error { | ||
| panic("unexpected VerifySignature call") | ||
| } | ||
|
|
||
| func (noopVerifier) RequestsFromProposal(types.Proposal) []types.RequestInfo { | ||
| panic("unexpected RequestsFromProposal call") | ||
| } | ||
|
|
||
| func (noopVerifier) AuxiliaryData([]byte) []byte { | ||
| panic("unexpected AuxiliaryData call") | ||
| } | ||
|
|
||
| type noopRequestsTimer struct{} | ||
|
|
||
| func (noopRequestsTimer) StopTimers() {} | ||
|
|
||
| func (noopRequestsTimer) RestartTimers() {} | ||
|
|
||
| func (noopRequestsTimer) RemoveRequest(types.RequestInfo) error { | ||
| return nil | ||
| } | ||
|
|
||
| type noopPruner struct{} | ||
|
|
||
| func (noopPruner) MaybePruneRevokedRequests() {} | ||
|
|
||
| type synchronizerFunc func() | ||
|
|
||
| func (f synchronizerFunc) Sync() { | ||
| f() | ||
| } |
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.
Behavior change worth confirming: previously
decideblocked on the unbuffereddeliverChansend when the waiter had left (view aborted), so the post-delivery bookkeeping below (incrementCurrentDecisionsInView,checkIfRotate/changeView,MaybePruneRevokedRequests,acquireLeaderToken) never ran in the aborted case. It now always runs once delivery happened. This unblocks the run loop (the real fix) and is defensible because the decision was in fact delivered, but it means a rotationchangeViewand leader-token acquisition can now fire for a view that is being aborted, racing the in-progress view change.changeViewguards against going backwards (latestView > newViewNumber), which mitigates it, but please confirm the extra increment/rotate cannot momentarily double-count or start a spurious rotation view before the pending abort/view-change is processed.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.
Addressed in a167920.
decidenow records the view that produced the decision, anddecidechecks after delivery that this view is still current and not stopped. Otherwise it only prunes revoked requests and returns, skipping the increment,checkIfRotate/changeView, andacquireLeaderToken. The check runs on the controller run loop, the same goroutine that handlesabortViewandchangeView, so a view change already processed is always observed first. For a dead view the result matches the pre-PR behavior, where the blocked send prevented the bookkeeping, minus the hang.TestControllerDecideSkipsViewBookkeepingIfDecidingViewIsGonecovers the aborted and replaced cases.