Skip to content

Commit

Permalink
ir/container: Check quantitative policy limits of created containers
Browse files Browse the repository at this point in the history
Follow nspcc-dev/neofs-api#307.

Refs #3075.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Feb 25, 2025
1 parent d2a319a commit 0e4c200
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/innerring/processors/container/process_container.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package container

import (
"errors"
"fmt"
"slices"
"strings"

"github.com/nspcc-dev/neo-go/pkg/network/payload"
Expand All @@ -10,6 +12,7 @@ import (
containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
containerSDK "github.com/nspcc-dev/neofs-sdk-go/container"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
"github.com/nspcc-dev/neofs-sdk-go/session"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -106,6 +109,10 @@ func (cp *Processor) checkPutContainer(ctx *putContainerContext) error {
return fmt.Errorf("auth container creation: %w", err)
}

if err = verifyStoragePolicy(ctx.cnr.PlacementPolicy()); err != nil {
return fmt.Errorf("invalid storage policy: %w", err)
}

// check homomorphic hashing setting
err = checkHomomorphicHashing(cp.netState, ctx.cnr)
if err != nil {
Expand Down Expand Up @@ -265,3 +272,51 @@ func checkHomomorphicHashing(ns NetworkState, cnr containerSDK.Container) error

return nil
}

const (
maxObjectReplicasPerSet = 8
maxContainerNodesInSet = 64
maxContainerNodeSets = 256
maxContainerNodes = 512
)

const defaultBackupFactor = 3

var errInvalidNodeSetDesc = errors.New("invalid node set descriptor")

func verifyStoragePolicy(policy netmap.PlacementPolicy) error {
rs := policy.Replicas()
if len(rs) > maxContainerNodeSets {
return fmt.Errorf("more than %d node sets", maxContainerNodeSets)
}
ss := policy.Selectors()
bf := policy.ContainerBackupFactor()
if bf == 0 {
bf = defaultBackupFactor
}
var cnrNodeCount uint32
for i := range rs {
rNum := rs[i].NumberOfObjects()
if rNum > maxObjectReplicasPerSet {
return fmt.Errorf("%w #%d: more than %d object replicas", errInvalidNodeSetDesc, i, maxObjectReplicasPerSet)
}
var sNum uint32
if sName := rs[i].SelectorName(); sName != "" {
si := slices.IndexFunc(ss, func(s netmap.Selector) bool { return s.Name() == sName })
if si < 0 {
return fmt.Errorf("%w #%d: missing selector %q", errInvalidNodeSetDesc, i, sName)
}
sNum = ss[si].NumberOfNodes()
} else {
sNum = rNum
}
nodesInSet := bf * sNum
if nodesInSet > maxContainerNodesInSet {
return fmt.Errorf("%w #%d: more than %d nodes", errInvalidNodeSetDesc, i, maxContainerNodesInSet)
}
if cnrNodeCount += nodesInSet; cnrNodeCount > maxContainerNodes {
return fmt.Errorf("more than %d nodes in total", maxContainerNodes)
}
}
return nil
}

0 comments on commit 0e4c200

Please sign in to comment.