2020-10-29 15:53:45 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2021-11-08 13:21:48 +00:00
|
|
|
"bytes"
|
2020-10-29 15:53:45 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"sync"
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2020-10-29 15:53:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
cleanupTable struct {
|
|
|
|
*sync.RWMutex
|
|
|
|
enabled bool
|
|
|
|
threshold uint64
|
2021-11-08 13:21:48 +00:00
|
|
|
lastAccess map[string]epochStampWithNodeInfo
|
2020-10-29 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
epochStamp struct {
|
|
|
|
epoch uint64
|
|
|
|
removeFlag bool
|
|
|
|
}
|
2021-11-08 13:21:48 +00:00
|
|
|
|
|
|
|
epochStampWithNodeInfo struct {
|
|
|
|
epochStamp
|
|
|
|
|
|
|
|
binNodeInfo []byte
|
|
|
|
}
|
2020-10-29 15:53:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newCleanupTable(enabled bool, threshold uint64) cleanupTable {
|
|
|
|
return cleanupTable{
|
|
|
|
RWMutex: new(sync.RWMutex),
|
|
|
|
enabled: enabled,
|
|
|
|
threshold: threshold,
|
2021-11-08 13:21:48 +00:00
|
|
|
lastAccess: make(map[string]epochStampWithNodeInfo),
|
2020-10-29 15:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update cleanup table based on on-chain information about netmap.
|
2022-06-08 23:18:26 +00:00
|
|
|
func (c *cleanupTable) update(snapshot netmap.NetMap, now uint64) {
|
2020-10-29 15:53:45 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
nmNodes := snapshot.Nodes()
|
|
|
|
|
2020-10-29 15:53:45 +00:00
|
|
|
// replacing map is less memory efficient but faster
|
2022-06-08 23:18:26 +00:00
|
|
|
newMap := make(map[string]epochStampWithNodeInfo, len(nmNodes))
|
2020-10-29 15:53:45 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
for i := range nmNodes {
|
|
|
|
binNodeInfo := nmNodes[i].Marshal()
|
2021-11-08 13:21:48 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
keyString := hex.EncodeToString(nmNodes[i].PublicKey())
|
2021-11-08 13:21:48 +00:00
|
|
|
|
|
|
|
access, ok := c.lastAccess[keyString]
|
|
|
|
if ok {
|
2020-10-29 15:53:45 +00:00
|
|
|
access.removeFlag = false // reset remove Flag on each Update
|
|
|
|
} else {
|
2021-11-08 13:21:48 +00:00
|
|
|
access.epoch = now
|
2020-10-29 15:53:45 +00:00
|
|
|
}
|
2021-11-08 13:21:48 +00:00
|
|
|
|
|
|
|
access.binNodeInfo = binNodeInfo
|
|
|
|
|
|
|
|
newMap[keyString] = access
|
2020-10-29 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.lastAccess = newMap
|
|
|
|
}
|
|
|
|
|
2021-11-08 13:21:48 +00:00
|
|
|
// updates last access time of the netmap node by string public key.
|
|
|
|
//
|
|
|
|
// Returns true if at least one condition is met:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - node hasn't been accessed yet;
|
|
|
|
// - remove flag is set;
|
|
|
|
// - binary node info has changed.
|
2021-11-08 13:21:48 +00:00
|
|
|
func (c *cleanupTable) touch(keyString string, now uint64, binNodeInfo []byte) bool {
|
2020-10-29 15:53:45 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
access, ok := c.lastAccess[keyString]
|
2021-11-08 13:21:48 +00:00
|
|
|
result := !ok || access.removeFlag || !bytes.Equal(access.binNodeInfo, binNodeInfo)
|
2020-10-29 15:53:45 +00:00
|
|
|
|
|
|
|
access.removeFlag = false // reset remove flag on each touch
|
|
|
|
if now > access.epoch {
|
|
|
|
access.epoch = now
|
|
|
|
}
|
|
|
|
|
2021-11-08 13:21:48 +00:00
|
|
|
access.binNodeInfo = binNodeInfo // update binary node info
|
|
|
|
|
2020-10-29 15:53:45 +00:00
|
|
|
c.lastAccess[keyString] = access
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cleanupTable) flag(keyString string) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if access, ok := c.lastAccess[keyString]; ok {
|
|
|
|
access.removeFlag = true
|
|
|
|
c.lastAccess[keyString] = access
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cleanupTable) forEachRemoveCandidate(epoch uint64, f func(string) error) error {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
for keyString, access := range c.lastAccess {
|
|
|
|
if epoch-access.epoch > c.threshold {
|
|
|
|
access.removeFlag = true // set remove flag
|
|
|
|
c.lastAccess[keyString] = access
|
|
|
|
|
|
|
|
if err := f(keyString); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|