2020-09-17 15:37:44 +00:00
|
|
|
package placement
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-17 15:37:44 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-09-17 15:37:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Builder is an interface of the
|
|
|
|
// object placement vector builder.
|
|
|
|
type Builder interface {
|
|
|
|
// BuildPlacement returns the list of placement vectors
|
|
|
|
// for object according to the placement policy.
|
|
|
|
//
|
|
|
|
// Must return all container nodes if object identifier
|
|
|
|
// is nil.
|
2022-01-26 12:11:13 +00:00
|
|
|
BuildPlacement(*addressSDK.Address, *netmap.PlacementPolicy) ([]netmap.Nodes, error)
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option represents placement traverser option.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
// Traverser represents utility for controlling
|
|
|
|
// traversal of object placement vectors.
|
|
|
|
type Traverser struct {
|
|
|
|
mtx *sync.RWMutex
|
|
|
|
|
|
|
|
vectors []netmap.Nodes
|
2020-09-18 15:14:26 +00:00
|
|
|
|
|
|
|
rem []int
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cfg struct {
|
2020-12-01 14:10:12 +00:00
|
|
|
trackCopies bool
|
|
|
|
|
|
|
|
flatSuccess *uint32
|
2020-09-17 15:37:44 +00:00
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-09-17 15:37:44 +00:00
|
|
|
|
|
|
|
policy *netmap.PlacementPolicy
|
|
|
|
|
|
|
|
builder Builder
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
const invalidOptsMsg = "invalid traverser options"
|
|
|
|
|
2020-09-17 15:37:44 +00:00
|
|
|
var errNilBuilder = errors.New("placement builder is nil")
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
var errNilPolicy = errors.New("placement policy is nil")
|
|
|
|
|
2020-09-17 15:37:44 +00:00
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return &cfg{
|
2020-12-01 14:10:12 +00:00
|
|
|
trackCopies: true,
|
2022-01-26 12:11:13 +00:00
|
|
|
addr: addressSDK.NewAddress(),
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTraverser creates, initializes with options and returns Traverser instance.
|
|
|
|
func NewTraverser(opts ...Option) (*Traverser, error) {
|
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if opts[i] != nil {
|
|
|
|
opts[i](cfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.builder == nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("%s: %w", invalidOptsMsg, errNilBuilder)
|
2020-09-18 15:14:26 +00:00
|
|
|
} else if cfg.policy == nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("%s: %w", invalidOptsMsg, errNilPolicy)
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ns, err := cfg.builder.BuildPlacement(cfg.addr, cfg.policy)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not build placement: %w", err)
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 14:10:12 +00:00
|
|
|
var rem []int
|
|
|
|
if cfg.flatSuccess != nil {
|
|
|
|
ns = flatNodes(ns)
|
|
|
|
rem = []int{int(*cfg.flatSuccess)}
|
|
|
|
} else {
|
|
|
|
rs := cfg.policy.Replicas()
|
|
|
|
rem = make([]int, 0, len(rs))
|
|
|
|
|
|
|
|
for i := range rs {
|
|
|
|
if cfg.trackCopies {
|
|
|
|
rem = append(rem, int(rs[i].Count()))
|
|
|
|
} else {
|
|
|
|
rem = append(rem, -1)
|
|
|
|
}
|
2020-09-18 15:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 15:37:44 +00:00
|
|
|
return &Traverser{
|
|
|
|
mtx: new(sync.RWMutex),
|
2020-09-18 15:14:26 +00:00
|
|
|
rem: rem,
|
2020-09-17 15:37:44 +00:00
|
|
|
vectors: ns,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:10:12 +00:00
|
|
|
func flatNodes(ns []netmap.Nodes) []netmap.Nodes {
|
|
|
|
sz := 0
|
|
|
|
for i := range ns {
|
|
|
|
sz += len(ns[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
flat := make(netmap.Nodes, 0, sz)
|
|
|
|
for i := range ns {
|
|
|
|
flat = append(flat, ns[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []netmap.Nodes{flat}
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:35:06 +00:00
|
|
|
// Node is a descriptor of storage node with information required for intra-container communication.
|
|
|
|
type Node struct {
|
|
|
|
addresses network.AddressGroup
|
2021-09-06 12:05:04 +00:00
|
|
|
|
|
|
|
key []byte
|
2021-09-06 11:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Addresses returns group of network addresses.
|
|
|
|
func (x Node) Addresses() network.AddressGroup {
|
|
|
|
return x.addresses
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:17:11 +00:00
|
|
|
// PublicKey returns public key in a binary format. Should not be mutated.
|
|
|
|
func (x Node) PublicKey() []byte {
|
2021-09-06 12:05:04 +00:00
|
|
|
return x.key
|
|
|
|
}
|
|
|
|
|
2020-09-17 15:37:44 +00:00
|
|
|
// Next returns next unprocessed address of the object placement.
|
|
|
|
//
|
|
|
|
// Returns nil if no nodes left or traversal operation succeeded.
|
2021-09-06 11:35:06 +00:00
|
|
|
func (t *Traverser) Next() []Node {
|
2020-09-17 15:37:44 +00:00
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
t.skipEmptyVectors()
|
|
|
|
|
|
|
|
if len(t.vectors) == 0 {
|
|
|
|
return nil
|
|
|
|
} else if len(t.vectors[0]) < t.rem[0] {
|
2020-09-17 15:37:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
count := t.rem[0]
|
|
|
|
if count < 0 {
|
|
|
|
count = len(t.vectors[0])
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:35:06 +00:00
|
|
|
nodes := make([]Node, count)
|
2020-09-18 15:14:26 +00:00
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2021-09-06 11:35:06 +00:00
|
|
|
err := nodes[i].addresses.FromIterator(t.vectors[0][i])
|
2020-09-18 15:14:26 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO: log error
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-06 12:05:04 +00:00
|
|
|
|
|
|
|
nodes[i].key = t.vectors[0][i].PublicKey()
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
t.vectors[0] = t.vectors[0][count:]
|
|
|
|
|
2021-09-06 11:35:06 +00:00
|
|
|
return nodes
|
2020-09-18 15:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Traverser) skipEmptyVectors() {
|
|
|
|
for i := 0; i < len(t.vectors); i++ { // don't use range, slice changes in body
|
|
|
|
if len(t.vectors[i]) == 0 && t.rem[i] <= 0 || t.rem[0] == 0 {
|
|
|
|
t.vectors = append(t.vectors[:i], t.vectors[i+1:]...)
|
|
|
|
t.rem = append(t.rem[:i], t.rem[i+1:]...)
|
|
|
|
i--
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitSuccess writes single succeeded node operation.
|
|
|
|
func (t *Traverser) SubmitSuccess() {
|
|
|
|
t.mtx.Lock()
|
2020-09-18 15:14:26 +00:00
|
|
|
if len(t.rem) > 0 {
|
|
|
|
t.rem[0]--
|
|
|
|
}
|
2020-09-17 15:37:44 +00:00
|
|
|
t.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success returns true if traversal operation succeeded.
|
|
|
|
func (t *Traverser) Success() bool {
|
|
|
|
t.mtx.RLock()
|
2020-09-18 15:14:26 +00:00
|
|
|
defer t.mtx.RUnlock()
|
|
|
|
|
|
|
|
for i := range t.rem {
|
|
|
|
if t.rem[i] > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 15:37:44 +00:00
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
return true
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseBuilder is a placement builder setting option.
|
|
|
|
//
|
|
|
|
// Overlaps UseNetworkMap option.
|
|
|
|
func UseBuilder(b Builder) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.builder = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForContainer is a traversal container setting option.
|
|
|
|
func ForContainer(cnr *container.Container) Option {
|
|
|
|
return func(c *cfg) {
|
2020-11-16 09:43:52 +00:00
|
|
|
c.policy = cnr.PlacementPolicy()
|
2020-09-17 15:37:44 +00:00
|
|
|
c.addr.SetContainerID(container.CalculateID(cnr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForObject is a processing object setting option.
|
2022-01-26 12:11:13 +00:00
|
|
|
func ForObject(id *oidSDK.ID) Option {
|
2020-09-17 15:37:44 +00:00
|
|
|
return func(c *cfg) {
|
|
|
|
c.addr.SetObjectID(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:10:12 +00:00
|
|
|
// SuccessAfter is a flat success number setting option.
|
2020-09-17 15:37:44 +00:00
|
|
|
//
|
|
|
|
// Option has no effect if the number is not positive.
|
2020-12-01 14:10:12 +00:00
|
|
|
func SuccessAfter(v uint32) Option {
|
2020-09-17 15:37:44 +00:00
|
|
|
return func(c *cfg) {
|
|
|
|
if v > 0 {
|
2020-12-01 14:10:12 +00:00
|
|
|
c.flatSuccess = &v
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithoutSuccessTracking disables success tracking in traversal.
|
|
|
|
func WithoutSuccessTracking() Option {
|
|
|
|
return func(c *cfg) {
|
2020-12-01 14:10:12 +00:00
|
|
|
c.trackCopies = false
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
}
|