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

Prevent allocator underflow #248

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 12 additions & 3 deletions allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ func (a *Allocator) ReleaseBlockMemory(p peer.ID, amount uint64) error {
if !ok {
return errors.New("cannot deallocate from peer with no allocations")
}
if status.totalAllocated > amount {
if status.totalAllocated >= amount {
status.totalAllocated -= amount
} else {
log.Infof("deallocation greater than peer memory", "amount", amount, "peer", p, "peer total", status.totalAllocated)
// change the amount deallocated so that the global total continues to match the sum of all peers
amount = status.totalAllocated
status.totalAllocated = 0
}
if a.totalAllocatedAllPeers > amount {
if a.totalAllocatedAllPeers >= amount {
a.totalAllocatedAllPeers -= amount
} else {
log.Warnf("deallocation greater than total allocated", "amount", amount, "peer", p, "global total", a.totalAllocatedAllPeers)
a.totalAllocatedAllPeers = 0
}
log.Debugw("memory released", "amount", amount, "peer", p, "peer total", status.totalAllocated, "global total", a.totalAllocatedAllPeers, "max per peer", a.maxAllowedAllocatedPerPeer, "global max", a.maxAllowedAllocatedTotal)
Expand All @@ -111,7 +115,12 @@ func (a *Allocator) ReleasePeerMemory(p peer.ID) error {
for _, pendingAllocation := range status.pendingAllocations {
pendingAllocation.response <- errors.New("peer has been deallocated")
}
a.totalAllocatedAllPeers -= status.totalAllocated
if a.totalAllocatedAllPeers >= status.totalAllocated {
a.totalAllocatedAllPeers -= status.totalAllocated
} else {
log.Warnf("peer dellocation greater than global total", "peer memory", status.totalAllocated, "peer", p, "global total", a.totalAllocatedAllPeers)
a.totalAllocatedAllPeers = 0
}
log.Debugw("memory released", "amount", status.totalAllocated, "peer", p, "peer total", 0, "global total", a.totalAllocatedAllPeers, "max per peer", a.maxAllowedAllocatedPerPeer, "global max", a.maxAllowedAllocatedTotal)
a.processPendingAllocations()
return nil
Expand Down
31 changes: 31 additions & 0 deletions allocator/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,37 @@ func TestAllocator(t *testing.T) {
},
},
},
"release block then peer more than allocated": {
total: 2000,
maxPerPeer: 1000,
steps: []allocStep{
{
op: alloc{peers[0], 1000},
totals: map[peer.ID]uint64{peers[0]: 1000},
expectedPending: []pendingResult{},
},
{
op: alloc{peers[1], 500},
totals: map[peer.ID]uint64{peers[0]: 1000, peers[1]: 500},
expectedPending: []pendingResult{},
},
{
op: releaseBlock{peers[0], 500},
totals: map[peer.ID]uint64{peers[0]: 500, peers[1]: 500},
expectedPending: []pendingResult{},
},
{
op: releaseBlock{peers[0], 1000},
totals: map[peer.ID]uint64{peers[0]: 0, peers[1]: 500},
expectedPending: []pendingResult{},
},
{
op: releasePeer{peers[1]},
totals: map[peer.ID]uint64{peers[0]: 0, peers[1]: 0},
expectedPending: []pendingResult{},
},
},
},
}
for testCase, data := range testCases {
t.Run(testCase, func(t *testing.T) {
Expand Down