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"
|
2021-11-08 13:21:48 +00:00
|
|
|
"fmt"
|
2020-10-29 15:53:45 +00:00
|
|
|
"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.
|
2020-10-30 11:26:57 +00:00
|
|
|
func (c *cleanupTable) update(snapshot *netmap.Netmap, now uint64) {
|
2020-10-29 15:53:45 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
// replacing map is less memory efficient but faster
|
2021-11-08 13:21:48 +00:00
|
|
|
newMap := make(map[string]epochStampWithNodeInfo, len(snapshot.Nodes))
|
2020-10-29 15:53:45 +00:00
|
|
|
|
2020-10-30 11:26:57 +00:00
|
|
|
for i := range snapshot.Nodes {
|
2021-11-08 13:21:48 +00:00
|
|
|
binNodeInfo, err := snapshot.Nodes[i].Marshal()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("could not marshal node info: %w", err)) // seems better than ignore
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:26:57 +00:00
|
|
|
keyString := hex.EncodeToString(snapshot.Nodes[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:
|
|
|
|
// * node hasn't been accessed yet;
|
|
|
|
// * remove flag is set;
|
|
|
|
// * binary node info has changed.
|
|
|
|
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
|
|
|
|
}
|