2020-09-14 23:44:20 +03:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
|
|
|
"github.com/ethereum/go-ethereum/les/utils"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/nodestate"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errBalanceOverflow = errors.New("balance overflow")
|
|
|
|
|
|
|
|
const maxBalance = math.MaxInt64 // maximum allowed balance value
|
|
|
|
|
|
|
|
const (
|
|
|
|
balanceCallbackUpdate = iota // called when priority drops below the last minimum estimate
|
|
|
|
balanceCallbackZero // called when priority drops to zero (positive balance exhausted)
|
|
|
|
balanceCallbackCount // total number of balance callbacks
|
|
|
|
)
|
|
|
|
|
|
|
|
// PriceFactors determine the pricing policy (may apply either to positive or
|
|
|
|
// negative balances which may have different factors).
|
|
|
|
// - TimeFactor is cost unit per nanosecond of connection time
|
|
|
|
// - CapacityFactor is cost unit per nanosecond of connection time per 1000000 capacity
|
|
|
|
// - RequestFactor is cost unit per request "realCost" unit
|
|
|
|
type PriceFactors struct {
|
|
|
|
TimeFactor, CapacityFactor, RequestFactor float64
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
// connectionPrice returns the price of connection per nanosecond at the given capacity
|
|
|
|
// and the estimated average request cost.
|
|
|
|
func (p PriceFactors) connectionPrice(cap uint64, avgReqCost float64) float64 {
|
|
|
|
return p.TimeFactor + float64(cap)*p.CapacityFactor/1000000 + p.RequestFactor*avgReqCost
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
type (
|
|
|
|
// nodePriority interface provides current and estimated future priorities on demand
|
|
|
|
nodePriority interface {
|
|
|
|
// priority should return the current priority of the node (higher is better)
|
|
|
|
priority(cap uint64) int64
|
|
|
|
// estimatePriority should return a lower estimate for the minimum of the node priority
|
|
|
|
// value starting from the current moment until the given time. If the priority goes
|
|
|
|
// under the returned estimate before the specified moment then it is the caller's
|
|
|
|
// responsibility to signal with updateFlag.
|
|
|
|
estimatePriority(cap uint64, addBalance int64, future, bias time.Duration, update bool) int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadOnlyBalance provides read-only operations on the node balance
|
|
|
|
ReadOnlyBalance interface {
|
|
|
|
nodePriority
|
|
|
|
GetBalance() (uint64, uint64)
|
|
|
|
GetRawBalance() (utils.ExpiredValue, utils.ExpiredValue)
|
|
|
|
GetPriceFactors() (posFactor, negFactor PriceFactors)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectedBalance provides operations permitted on connected nodes (non-read-only
|
|
|
|
// operations are not permitted inside a BalanceOperation)
|
|
|
|
ConnectedBalance interface {
|
|
|
|
ReadOnlyBalance
|
|
|
|
SetPriceFactors(posFactor, negFactor PriceFactors)
|
|
|
|
RequestServed(cost uint64) uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtomicBalanceOperator provides operations permitted in an atomic BalanceOperation
|
|
|
|
AtomicBalanceOperator interface {
|
|
|
|
ReadOnlyBalance
|
|
|
|
AddBalance(amount int64) (uint64, uint64, error)
|
|
|
|
SetBalance(pos, neg uint64) error
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// nodeBalance keeps track of the positive and negative balances of a connected
|
2020-09-14 23:44:20 +03:00
|
|
|
// client and calculates actual and projected future priority values.
|
|
|
|
// Implements nodePriority interface.
|
2021-04-06 21:42:50 +03:00
|
|
|
type nodeBalance struct {
|
|
|
|
bt *balanceTracker
|
2020-09-14 23:44:20 +03:00
|
|
|
lock sync.RWMutex
|
|
|
|
node *enode.Node
|
|
|
|
connAddress string
|
2021-04-06 21:42:50 +03:00
|
|
|
active, hasPriority, setFlags bool
|
2020-09-14 23:44:20 +03:00
|
|
|
capacity uint64
|
|
|
|
balance balance
|
|
|
|
posFactor, negFactor PriceFactors
|
|
|
|
sumReqCost uint64
|
|
|
|
lastUpdate, nextUpdate, initTime mclock.AbsTime
|
|
|
|
updateEvent mclock.Timer
|
|
|
|
// since only a limited and fixed number of callbacks are needed, they are
|
|
|
|
// stored in a fixed size array ordered by priority threshold.
|
|
|
|
callbacks [balanceCallbackCount]balanceCallback
|
|
|
|
// callbackIndex maps balanceCallback constants to callbacks array indexes (-1 if not active)
|
|
|
|
callbackIndex [balanceCallbackCount]int
|
|
|
|
callbackCount int // number of active callbacks
|
|
|
|
}
|
|
|
|
|
|
|
|
// balance represents a pair of positive and negative balances
|
|
|
|
type balance struct {
|
2021-04-06 21:42:50 +03:00
|
|
|
pos, neg utils.ExpiredValue
|
|
|
|
posExp, negExp utils.ValueExpirer
|
|
|
|
}
|
|
|
|
|
|
|
|
// posValue returns the value of positive balance at a given timestamp.
|
|
|
|
func (b balance) posValue(now mclock.AbsTime) uint64 {
|
|
|
|
return b.pos.Value(b.posExp.LogOffset(now))
|
|
|
|
}
|
|
|
|
|
|
|
|
// negValue returns the value of negative balance at a given timestamp.
|
|
|
|
func (b balance) negValue(now mclock.AbsTime) uint64 {
|
|
|
|
return b.neg.Value(b.negExp.LogOffset(now))
|
|
|
|
}
|
|
|
|
|
|
|
|
// addValue adds the value of a given amount to the balance. The original value and
|
|
|
|
// updated value will also be returned if the addition is successful.
|
|
|
|
// Returns the error if the given value is too large and the value overflows.
|
|
|
|
func (b *balance) addValue(now mclock.AbsTime, amount int64, pos bool, force bool) (uint64, uint64, int64, error) {
|
|
|
|
var (
|
|
|
|
val utils.ExpiredValue
|
|
|
|
offset utils.Fixed64
|
|
|
|
)
|
|
|
|
if pos {
|
|
|
|
offset, val = b.posExp.LogOffset(now), b.pos
|
|
|
|
} else {
|
|
|
|
offset, val = b.negExp.LogOffset(now), b.neg
|
|
|
|
}
|
|
|
|
old := val.Value(offset)
|
|
|
|
if amount > 0 && (amount > maxBalance || old > maxBalance-uint64(amount)) {
|
|
|
|
if !force {
|
|
|
|
return old, 0, 0, errBalanceOverflow
|
|
|
|
}
|
|
|
|
val = utils.ExpiredValue{}
|
|
|
|
amount = maxBalance
|
|
|
|
}
|
|
|
|
net := val.Add(amount, offset)
|
|
|
|
if pos {
|
|
|
|
b.pos = val
|
|
|
|
} else {
|
|
|
|
b.neg = val
|
|
|
|
}
|
|
|
|
return old, val.Value(offset), net, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setValue sets the internal balance amount to the given values. Returns the
|
|
|
|
// error if the given value is too large.
|
|
|
|
func (b *balance) setValue(now mclock.AbsTime, pos uint64, neg uint64) error {
|
|
|
|
if pos > maxBalance || neg > maxBalance {
|
|
|
|
return errBalanceOverflow
|
|
|
|
}
|
|
|
|
var pb, nb utils.ExpiredValue
|
|
|
|
pb.Add(int64(pos), b.posExp.LogOffset(now))
|
|
|
|
nb.Add(int64(neg), b.negExp.LogOffset(now))
|
|
|
|
b.pos = pb
|
|
|
|
b.neg = nb
|
|
|
|
return nil
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// balanceCallback represents a single callback that is activated when client priority
|
|
|
|
// reaches the given threshold
|
|
|
|
type balanceCallback struct {
|
|
|
|
id int
|
|
|
|
threshold int64
|
|
|
|
callback func()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBalance returns the current positive and negative balance.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) GetBalance() (uint64, uint64) {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
|
|
|
|
now := n.bt.clock.Now()
|
|
|
|
n.updateBalance(now)
|
2021-04-06 21:42:50 +03:00
|
|
|
return n.balance.posValue(now), n.balance.negValue(now)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawBalance returns the current positive and negative balance
|
|
|
|
// but in the raw(expired value) format.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) GetRawBalance() (utils.ExpiredValue, utils.ExpiredValue) {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
|
|
|
|
now := n.bt.clock.Now()
|
|
|
|
n.updateBalance(now)
|
|
|
|
return n.balance.pos, n.balance.neg
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBalance adds the given amount to the positive balance and returns the balance
|
|
|
|
// before and after the operation. Exceeding maxBalance results in an error (balance is
|
|
|
|
// unchanged) while adding a negative amount higher than the current balance results in
|
|
|
|
// zero balance.
|
2021-04-06 21:42:50 +03:00
|
|
|
// Note: this function should run inside a NodeStateMachine operation
|
|
|
|
func (n *nodeBalance) AddBalance(amount int64) (uint64, uint64, error) {
|
2020-09-14 23:44:20 +03:00
|
|
|
var (
|
2021-04-06 21:42:50 +03:00
|
|
|
err error
|
|
|
|
old, new uint64
|
|
|
|
now = n.bt.clock.Now()
|
|
|
|
callbacks []func()
|
|
|
|
setPriority bool
|
2020-09-14 23:44:20 +03:00
|
|
|
)
|
2021-04-06 21:42:50 +03:00
|
|
|
// Operation with holding the lock
|
|
|
|
n.bt.updateTotalBalance(n, func() bool {
|
|
|
|
n.updateBalance(now)
|
|
|
|
if old, new, _, err = n.balance.addValue(now, amount, true, false); err != nil {
|
|
|
|
return false
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
callbacks, setPriority = n.checkCallbacks(now), n.checkPriorityStatus()
|
|
|
|
n.storeBalance(true, false)
|
|
|
|
return true
|
2020-09-14 23:44:20 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return old, old, err
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
// Operation without holding the lock
|
|
|
|
for _, cb := range callbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
if n.setFlags {
|
|
|
|
if setPriority {
|
|
|
|
n.bt.ns.SetStateSub(n.node, n.bt.setup.priorityFlag, nodestate.Flags{}, 0)
|
|
|
|
}
|
|
|
|
// Note: priority flag is automatically removed by the zero priority callback if necessary
|
|
|
|
n.signalPriorityUpdate()
|
|
|
|
}
|
2020-09-14 23:44:20 +03:00
|
|
|
return old, new, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBalance sets the positive and negative balance to the given values
|
2021-04-06 21:42:50 +03:00
|
|
|
// Note: this function should run inside a NodeStateMachine operation
|
|
|
|
func (n *nodeBalance) SetBalance(pos, neg uint64) error {
|
|
|
|
var (
|
|
|
|
now = n.bt.clock.Now()
|
|
|
|
callbacks []func()
|
|
|
|
setPriority bool
|
|
|
|
)
|
|
|
|
// Operation with holding the lock
|
|
|
|
n.bt.updateTotalBalance(n, func() bool {
|
|
|
|
n.updateBalance(now)
|
|
|
|
if err := n.balance.setValue(now, pos, neg); err != nil {
|
|
|
|
return false
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
callbacks, setPriority = n.checkCallbacks(now), n.checkPriorityStatus()
|
|
|
|
n.storeBalance(true, true)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
// Operation without holding the lock
|
|
|
|
for _, cb := range callbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
if n.setFlags {
|
2020-09-14 23:44:20 +03:00
|
|
|
if setPriority {
|
2021-04-06 21:42:50 +03:00
|
|
|
n.bt.ns.SetStateSub(n.node, n.bt.setup.priorityFlag, nodestate.Flags{}, 0)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
// Note: priority flag is automatically removed by the zero priority callback if necessary
|
2020-09-14 23:44:20 +03:00
|
|
|
n.signalPriorityUpdate()
|
2021-04-06 21:42:50 +03:00
|
|
|
}
|
2020-09-14 23:44:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestServed should be called after serving a request for the given peer
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) RequestServed(cost uint64) (newBalance uint64) {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
var (
|
|
|
|
check bool
|
|
|
|
fcost = float64(cost)
|
|
|
|
now = n.bt.clock.Now()
|
|
|
|
)
|
2020-09-14 23:44:20 +03:00
|
|
|
n.updateBalance(now)
|
|
|
|
if !n.balance.pos.IsZero() {
|
2021-04-06 21:42:50 +03:00
|
|
|
posCost := -int64(fcost * n.posFactor.RequestFactor)
|
|
|
|
if posCost == 0 {
|
|
|
|
fcost = 0
|
|
|
|
newBalance = n.balance.posValue(now)
|
|
|
|
} else {
|
|
|
|
var net int64
|
|
|
|
_, newBalance, net, _ = n.balance.addValue(now, posCost, true, false)
|
|
|
|
if posCost == net {
|
2020-09-14 23:44:20 +03:00
|
|
|
fcost = 0
|
|
|
|
} else {
|
2021-04-06 21:42:50 +03:00
|
|
|
fcost *= 1 - float64(net)/float64(posCost)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
check = true
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
if fcost > 0 && n.negFactor.RequestFactor != 0 {
|
|
|
|
n.balance.addValue(now, int64(fcost*n.negFactor.RequestFactor), false, false)
|
|
|
|
check = true
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
n.sumReqCost += cost
|
|
|
|
|
|
|
|
var callbacks []func()
|
2020-09-14 23:44:20 +03:00
|
|
|
if check {
|
|
|
|
callbacks = n.checkCallbacks(now)
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
n.lock.Unlock()
|
|
|
|
|
|
|
|
if callbacks != nil {
|
|
|
|
n.bt.ns.Operation(func() {
|
|
|
|
for _, cb := range callbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
// priority returns the actual priority based on the current balance
|
|
|
|
func (n *nodeBalance) priority(capacity uint64) int64 {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
now := n.bt.clock.Now()
|
|
|
|
n.updateBalance(now)
|
|
|
|
return n.balanceToPriority(now, n.balance, capacity)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// EstMinPriority gives a lower estimate for the priority at a given time in the future.
|
|
|
|
// An average request cost per time is assumed that is twice the average cost per time
|
|
|
|
// in the current session.
|
2021-04-06 21:42:50 +03:00
|
|
|
// If update is true then a priority callback is added that turns updateFlag on and off
|
2020-09-14 23:44:20 +03:00
|
|
|
// in case the priority goes below the estimated minimum.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) estimatePriority(capacity uint64, addBalance int64, future, bias time.Duration, update bool) int64 {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
|
2021-03-01 12:24:20 +03:00
|
|
|
now := n.bt.clock.Now()
|
|
|
|
n.updateBalance(now)
|
2021-04-06 21:42:50 +03:00
|
|
|
|
|
|
|
b := n.balance // copy the balance
|
2021-03-01 12:24:20 +03:00
|
|
|
if addBalance != 0 {
|
2021-04-06 21:42:50 +03:00
|
|
|
b.addValue(now, addBalance, true, true)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-03-01 12:24:20 +03:00
|
|
|
if future > 0 {
|
|
|
|
var avgReqCost float64
|
|
|
|
dt := time.Duration(n.lastUpdate - n.initTime)
|
|
|
|
if dt > time.Second {
|
|
|
|
avgReqCost = float64(n.sumReqCost) * 2 / float64(dt)
|
|
|
|
}
|
|
|
|
b = n.reducedBalance(b, now, future, capacity, avgReqCost)
|
|
|
|
}
|
|
|
|
if bias > 0 {
|
|
|
|
b = n.reducedBalance(b, now+mclock.AbsTime(future), bias, capacity, 0)
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
// Note: we subtract one from the estimated priority in order to ensure that biased
|
|
|
|
// estimates are always lower than actual priorities, even if the bias is very small.
|
|
|
|
// This ensures that two nodes will not ping-pong update signals forever if both of
|
|
|
|
// them have zero estimated priority drop in the projected future.
|
|
|
|
pri := n.balanceToPriority(now, b, capacity) - 1
|
2020-09-14 23:44:20 +03:00
|
|
|
if update {
|
|
|
|
n.addCallback(balanceCallbackUpdate, pri, n.signalPriorityUpdate)
|
|
|
|
}
|
|
|
|
return pri
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPriceFactors sets the price factors. TimeFactor is the price of a nanosecond of
|
|
|
|
// connection while RequestFactor is the price of a request cost unit.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) SetPriceFactors(posFactor, negFactor PriceFactors) {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
now := n.bt.clock.Now()
|
|
|
|
n.updateBalance(now)
|
|
|
|
n.posFactor, n.negFactor = posFactor, negFactor
|
|
|
|
callbacks := n.checkCallbacks(now)
|
|
|
|
n.lock.Unlock()
|
|
|
|
if callbacks != nil {
|
|
|
|
n.bt.ns.Operation(func() {
|
|
|
|
for _, cb := range callbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPriceFactors returns the price factors
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) GetPriceFactors() (posFactor, negFactor PriceFactors) {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
|
|
|
|
return n.posFactor, n.negFactor
|
|
|
|
}
|
|
|
|
|
|
|
|
// activate starts time/capacity cost deduction.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) activate() {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.bt.updateTotalBalance(n, func() bool {
|
|
|
|
if n.active {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n.active = true
|
|
|
|
n.lastUpdate = n.bt.clock.Now()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// deactivate stops time/capacity cost deduction and saves the balances in the database
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) deactivate() {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.bt.updateTotalBalance(n, func() bool {
|
|
|
|
if !n.active {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n.updateBalance(n.bt.clock.Now())
|
|
|
|
if n.updateEvent != nil {
|
|
|
|
n.updateEvent.Stop()
|
|
|
|
n.updateEvent = nil
|
|
|
|
}
|
|
|
|
n.storeBalance(true, true)
|
|
|
|
n.active = false
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateBalance updates balance based on the time factor
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) updateBalance(now mclock.AbsTime) {
|
2020-09-14 23:44:20 +03:00
|
|
|
if n.active && now > n.lastUpdate {
|
2021-03-01 12:24:20 +03:00
|
|
|
n.balance = n.reducedBalance(n.balance, n.lastUpdate, time.Duration(now-n.lastUpdate), n.capacity, 0)
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lastUpdate = now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// storeBalance stores the positive and/or negative balance of the node in the database
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) storeBalance(pos, neg bool) {
|
2020-09-14 23:44:20 +03:00
|
|
|
if pos {
|
|
|
|
n.bt.storeBalance(n.node.ID().Bytes(), false, n.balance.pos)
|
|
|
|
}
|
|
|
|
if neg {
|
|
|
|
n.bt.storeBalance([]byte(n.connAddress), true, n.balance.neg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addCallback sets up a one-time callback to be called when priority reaches
|
|
|
|
// the threshold. If it has already reached the threshold the callback is called
|
|
|
|
// immediately.
|
|
|
|
// Note: should be called while n.lock is held
|
|
|
|
// Note 2: the callback function runs inside a NodeStateMachine operation
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) addCallback(id int, threshold int64, callback func()) {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.removeCallback(id)
|
|
|
|
idx := 0
|
|
|
|
for idx < n.callbackCount && threshold > n.callbacks[idx].threshold {
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
for i := n.callbackCount - 1; i >= idx; i-- {
|
|
|
|
n.callbackIndex[n.callbacks[i].id]++
|
|
|
|
n.callbacks[i+1] = n.callbacks[i]
|
|
|
|
}
|
|
|
|
n.callbackCount++
|
|
|
|
n.callbackIndex[id] = idx
|
|
|
|
n.callbacks[idx] = balanceCallback{id, threshold, callback}
|
|
|
|
now := n.bt.clock.Now()
|
|
|
|
n.updateBalance(now)
|
|
|
|
n.scheduleCheck(now)
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeCallback removes the given callback and returns true if it was active
|
|
|
|
// Note: should be called while n.lock is held
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) removeCallback(id int) bool {
|
2020-09-14 23:44:20 +03:00
|
|
|
idx := n.callbackIndex[id]
|
|
|
|
if idx == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n.callbackIndex[id] = -1
|
|
|
|
for i := idx; i < n.callbackCount-1; i++ {
|
|
|
|
n.callbackIndex[n.callbacks[i+1].id]--
|
|
|
|
n.callbacks[i] = n.callbacks[i+1]
|
|
|
|
}
|
|
|
|
n.callbackCount--
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkCallbacks checks whether the threshold of any of the active callbacks
|
|
|
|
// have been reached and returns triggered callbacks.
|
|
|
|
// Note: checkCallbacks assumes that the balance has been recently updated.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) checkCallbacks(now mclock.AbsTime) (callbacks []func()) {
|
2020-09-14 23:44:20 +03:00
|
|
|
if n.callbackCount == 0 || n.capacity == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
pri := n.balanceToPriority(now, n.balance, n.capacity)
|
2020-09-14 23:44:20 +03:00
|
|
|
for n.callbackCount != 0 && n.callbacks[n.callbackCount-1].threshold >= pri {
|
|
|
|
n.callbackCount--
|
|
|
|
n.callbackIndex[n.callbacks[n.callbackCount].id] = -1
|
|
|
|
callbacks = append(callbacks, n.callbacks[n.callbackCount].callback)
|
|
|
|
}
|
|
|
|
n.scheduleCheck(now)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// scheduleCheck sets up or updates a scheduled event to ensure that it will be called
|
|
|
|
// again just after the next threshold has been reached.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) scheduleCheck(now mclock.AbsTime) {
|
2020-09-14 23:44:20 +03:00
|
|
|
if n.callbackCount != 0 {
|
|
|
|
d, ok := n.timeUntil(n.callbacks[n.callbackCount-1].threshold)
|
|
|
|
if !ok {
|
|
|
|
n.nextUpdate = 0
|
|
|
|
n.updateAfter(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.nextUpdate == 0 || n.nextUpdate > now+mclock.AbsTime(d) {
|
|
|
|
if d > time.Second {
|
|
|
|
// Note: if the scheduled update is not in the very near future then we
|
|
|
|
// schedule the update a bit earlier. This way we do need to update a few
|
|
|
|
// extra times but don't need to reschedule every time a processed request
|
|
|
|
// brings the expected firing time a little bit closer.
|
|
|
|
d = ((d - time.Second) * 7 / 8) + time.Second
|
|
|
|
}
|
|
|
|
n.nextUpdate = now + mclock.AbsTime(d)
|
|
|
|
n.updateAfter(d)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n.nextUpdate = 0
|
|
|
|
n.updateAfter(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateAfter schedules a balance update and callback check in the future
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) updateAfter(dt time.Duration) {
|
2020-09-14 23:44:20 +03:00
|
|
|
if n.updateEvent == nil || n.updateEvent.Stop() {
|
|
|
|
if dt == 0 {
|
|
|
|
n.updateEvent = nil
|
|
|
|
} else {
|
|
|
|
n.updateEvent = n.bt.clock.AfterFunc(dt, func() {
|
|
|
|
var callbacks []func()
|
|
|
|
n.lock.Lock()
|
|
|
|
if n.callbackCount != 0 {
|
|
|
|
now := n.bt.clock.Now()
|
|
|
|
n.updateBalance(now)
|
|
|
|
callbacks = n.checkCallbacks(now)
|
|
|
|
}
|
|
|
|
n.lock.Unlock()
|
|
|
|
if callbacks != nil {
|
|
|
|
n.bt.ns.Operation(func() {
|
|
|
|
for _, cb := range callbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// balanceExhausted should be called when the positive balance is exhausted (priority goes to zero/negative)
|
|
|
|
// Note: this function should run inside a NodeStateMachine operation
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) balanceExhausted() {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
n.storeBalance(true, false)
|
2021-04-06 21:42:50 +03:00
|
|
|
n.hasPriority = false
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Unlock()
|
2021-04-06 21:42:50 +03:00
|
|
|
if n.setFlags {
|
|
|
|
n.bt.ns.SetStateSub(n.node, nodestate.Flags{}, n.bt.setup.priorityFlag, 0)
|
|
|
|
}
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkPriorityStatus checks whether the node has gained priority status and sets the priority
|
|
|
|
// callback and flag if necessary. It assumes that the balance has been recently updated.
|
|
|
|
// Note that the priority flag has to be set by the caller after the mutex has been released.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) checkPriorityStatus() bool {
|
|
|
|
if !n.hasPriority && !n.balance.pos.IsZero() {
|
|
|
|
n.hasPriority = true
|
2020-09-14 23:44:20 +03:00
|
|
|
n.addCallback(balanceCallbackZero, 0, func() { n.balanceExhausted() })
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// signalPriorityUpdate signals that the priority fell below the previous minimum estimate
|
|
|
|
// Note: this function should run inside a NodeStateMachine operation
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) signalPriorityUpdate() {
|
|
|
|
n.bt.ns.SetStateSub(n.node, n.bt.setup.updateFlag, nodestate.Flags{}, 0)
|
|
|
|
n.bt.ns.SetStateSub(n.node, nodestate.Flags{}, n.bt.setup.updateFlag, 0)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// setCapacity updates the capacity value used for priority calculation
|
|
|
|
// Note: capacity should never be zero
|
|
|
|
// Note 2: this function should run inside a NodeStateMachine operation
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) setCapacity(capacity uint64) {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
now := n.bt.clock.Now()
|
|
|
|
n.updateBalance(now)
|
|
|
|
n.capacity = capacity
|
|
|
|
callbacks := n.checkCallbacks(now)
|
|
|
|
n.lock.Unlock()
|
|
|
|
for _, cb := range callbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// balanceToPriority converts a balance to a priority value. Lower priority means
|
|
|
|
// first to disconnect. Positive balance translates to positive priority. If positive
|
|
|
|
// balance is zero then negative balance translates to a negative priority.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) balanceToPriority(now mclock.AbsTime, b balance, capacity uint64) int64 {
|
|
|
|
pos := b.posValue(now)
|
|
|
|
if pos > 0 {
|
|
|
|
return int64(pos / capacity)
|
|
|
|
}
|
|
|
|
return -int64(b.negValue(now))
|
|
|
|
}
|
|
|
|
|
|
|
|
// priorityToBalance converts a target priority to a requested balance value.
|
|
|
|
// If the priority is negative, then minimal negative balance is returned;
|
|
|
|
// otherwise the minimal positive balance is returned.
|
|
|
|
func (n *nodeBalance) priorityToBalance(priority int64, capacity uint64) (uint64, uint64) {
|
|
|
|
if priority > 0 {
|
|
|
|
return uint64(priority) * n.capacity, 0
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
return 0, uint64(-priority)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// reducedBalance estimates the reduced balance at a given time in the fututre based
|
2021-03-01 12:24:20 +03:00
|
|
|
// on the given balance, the time factor and an estimated average request cost per time ratio
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) reducedBalance(b balance, start mclock.AbsTime, dt time.Duration, capacity uint64, avgReqCost float64) balance {
|
2021-03-01 12:24:20 +03:00
|
|
|
// since the costs are applied continuously during the dt time period we calculate
|
|
|
|
// the expiration offset at the middle of the period
|
2021-04-06 21:42:50 +03:00
|
|
|
var (
|
|
|
|
at = start + mclock.AbsTime(dt/2)
|
|
|
|
dtf = float64(dt)
|
|
|
|
)
|
2020-09-14 23:44:20 +03:00
|
|
|
if !b.pos.IsZero() {
|
2021-04-06 21:42:50 +03:00
|
|
|
factor := n.posFactor.connectionPrice(capacity, avgReqCost)
|
2021-03-01 12:24:20 +03:00
|
|
|
diff := -int64(dtf * factor)
|
2021-04-06 21:42:50 +03:00
|
|
|
_, _, net, _ := b.addValue(at, diff, true, false)
|
|
|
|
if net == diff {
|
2021-03-01 12:24:20 +03:00
|
|
|
dtf = 0
|
2020-09-14 23:44:20 +03:00
|
|
|
} else {
|
2021-04-06 21:42:50 +03:00
|
|
|
dtf += float64(net) / factor
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
if dtf > 0 {
|
|
|
|
factor := n.negFactor.connectionPrice(capacity, avgReqCost)
|
|
|
|
b.addValue(at, int64(dtf*factor), false, false)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// timeUntil calculates the remaining time needed to reach a given priority level
|
|
|
|
// assuming that no requests are processed until then. If the given level is never
|
2021-04-06 21:42:50 +03:00
|
|
|
// reached then (0, false) is returned. If it has already been reached then (0, true)
|
|
|
|
// is returned.
|
2020-09-14 23:44:20 +03:00
|
|
|
// Note: the function assumes that the balance has been recently updated and
|
|
|
|
// calculates the time starting from the last update.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (n *nodeBalance) timeUntil(priority int64) (time.Duration, bool) {
|
|
|
|
var (
|
|
|
|
now = n.bt.clock.Now()
|
|
|
|
pos = n.balance.posValue(now)
|
|
|
|
targetPos, targetNeg = n.priorityToBalance(priority, n.capacity)
|
|
|
|
diffTime float64
|
|
|
|
)
|
|
|
|
if pos > 0 {
|
|
|
|
timePrice := n.posFactor.connectionPrice(n.capacity, 0)
|
2020-09-14 23:44:20 +03:00
|
|
|
if timePrice < 1e-100 {
|
|
|
|
return 0, false
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
if targetPos > 0 {
|
|
|
|
if targetPos > pos {
|
|
|
|
return 0, true
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
diffTime = float64(pos-targetPos) / timePrice
|
|
|
|
return time.Duration(diffTime), true
|
2021-03-01 12:24:20 +03:00
|
|
|
} else {
|
2021-04-06 21:42:50 +03:00
|
|
|
diffTime = float64(pos) / timePrice
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-06 21:42:50 +03:00
|
|
|
if targetPos > 0 {
|
|
|
|
return 0, true
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
neg := n.balance.negValue(now)
|
|
|
|
if targetNeg > neg {
|
|
|
|
timePrice := n.negFactor.connectionPrice(n.capacity, 0)
|
2020-09-14 23:44:20 +03:00
|
|
|
if timePrice < 1e-100 {
|
|
|
|
return 0, false
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
diffTime += float64(targetNeg-neg) / timePrice
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
return time.Duration(diffTime), true
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|