2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2022-06-07 08:25:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap/parser"
|
2023-06-01 07:26:07 +00:00
|
|
|
"github.com/antlr4-go/antlr/v4"
|
2021-10-27 10:00:35 +00:00
|
|
|
)
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// PlacementPolicy declares policy to store objects in the FrostFS container.
|
2022-06-07 08:25:34 +00:00
|
|
|
// Within itself, PlacementPolicy represents a set of rules to select a subset
|
2022-12-29 10:46:18 +00:00
|
|
|
// of nodes from FrostFS network map - node-candidates for object storage.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// PlacementPolicy is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap.PlacementPolicy
|
2022-06-07 08:25:34 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
|
|
|
type PlacementPolicy struct {
|
|
|
|
backupFactor uint32
|
|
|
|
|
|
|
|
filters []netmap.Filter
|
|
|
|
|
|
|
|
selectors []netmap.Selector
|
|
|
|
|
|
|
|
replicas []netmap.Replica
|
2023-06-05 10:38:15 +00:00
|
|
|
|
|
|
|
unique bool
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 13:40:50 +00:00
|
|
|
func (p *PlacementPolicy) readFromV2(m netmap.PlacementPolicy, checkFieldPresence bool) error {
|
2022-06-07 08:25:34 +00:00
|
|
|
p.replicas = m.GetReplicas()
|
2024-02-22 19:29:25 +00:00
|
|
|
if checkFieldPresence {
|
|
|
|
if len(p.replicas) == 0 {
|
|
|
|
return errors.New("missing replicas")
|
|
|
|
}
|
|
|
|
if len(p.replicas) != 1 {
|
|
|
|
for i := range p.replicas {
|
|
|
|
if p.replicas[i].GetECDataCount() != 0 || p.replicas[i].GetECParityCount() != 0 {
|
|
|
|
return errors.New("erasure code group must be used exclusively")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 13:40:50 +00:00
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
|
2022-06-15 13:40:50 +00:00
|
|
|
p.backupFactor = m.GetContainerBackupFactor()
|
|
|
|
p.selectors = m.GetSelectors()
|
|
|
|
p.filters = m.GetFilters()
|
2023-06-05 10:38:15 +00:00
|
|
|
p.unique = m.GetUnique()
|
2022-06-15 13:40:50 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Marshal encodes PlacementPolicy into a binary format of the FrostFS API
|
2022-06-15 19:08:19 +00:00
|
|
|
// protocol (Protocol Buffers with direct field order).
|
|
|
|
//
|
|
|
|
// See also Unmarshal.
|
|
|
|
func (p PlacementPolicy) Marshal() []byte {
|
|
|
|
var m netmap.PlacementPolicy
|
|
|
|
p.WriteToV2(&m)
|
|
|
|
|
|
|
|
return m.StableMarshal(nil)
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Unmarshal decodes FrostFS API protocol binary format into the PlacementPolicy
|
2022-06-15 19:08:19 +00:00
|
|
|
// (Protocol Buffers with direct field order). Returns an error describing
|
|
|
|
// a format violation.
|
|
|
|
//
|
|
|
|
// See also Marshal.
|
|
|
|
func (p *PlacementPolicy) Unmarshal(data []byte) error {
|
|
|
|
var m netmap.PlacementPolicy
|
|
|
|
|
|
|
|
err := m.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.readFromV2(m, false)
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// MarshalJSON encodes PlacementPolicy into a JSON format of the FrostFS API
|
2022-06-15 19:08:19 +00:00
|
|
|
// protocol (Protocol Buffers JSON).
|
|
|
|
//
|
|
|
|
// See also UnmarshalJSON.
|
|
|
|
func (p PlacementPolicy) MarshalJSON() ([]byte, error) {
|
|
|
|
var m netmap.PlacementPolicy
|
|
|
|
p.WriteToV2(&m)
|
|
|
|
|
|
|
|
return m.MarshalJSON()
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// UnmarshalJSON decodes FrostFS API protocol JSON format into the PlacementPolicy
|
2022-06-15 19:08:19 +00:00
|
|
|
// (Protocol Buffers JSON). Returns an error describing a format violation.
|
|
|
|
//
|
|
|
|
// See also MarshalJSON.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (p *PlacementPolicy) UnmarshalJSON(data []byte) error {
|
|
|
|
var m netmap.PlacementPolicy
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
err := m.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-15 13:40:50 +00:00
|
|
|
return p.readFromV2(m, false)
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFromV2 reads PlacementPolicy from the netmap.PlacementPolicy message.
|
2022-12-29 10:46:18 +00:00
|
|
|
// Checks if the message conforms to FrostFS API V2 protocol.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also WriteToV2.
|
|
|
|
func (p *PlacementPolicy) ReadFromV2(m netmap.PlacementPolicy) error {
|
2022-06-15 13:40:50 +00:00
|
|
|
return p.readFromV2(m, true)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// WriteToV2 writes PlacementPolicy to the session.Token message.
|
|
|
|
// The message must not be nil.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also ReadFromV2.
|
|
|
|
func (p PlacementPolicy) WriteToV2(m *netmap.PlacementPolicy) {
|
|
|
|
m.SetContainerBackupFactor(p.backupFactor)
|
|
|
|
m.SetFilters(p.filters)
|
|
|
|
m.SetSelectors(p.selectors)
|
|
|
|
m.SetReplicas(p.replicas)
|
2023-06-05 10:38:15 +00:00
|
|
|
m.SetUnique(p.unique)
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReplicaDescriptor replica descriptor characterizes replicas of objects from
|
|
|
|
// the subset selected by a particular Selector.
|
|
|
|
type ReplicaDescriptor struct {
|
|
|
|
m netmap.Replica
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
// SetNumberOfObjects sets number of object replicas.
|
|
|
|
func (r *ReplicaDescriptor) SetNumberOfObjects(c uint32) {
|
2022-06-07 08:25:34 +00:00
|
|
|
r.m.SetCount(c)
|
|
|
|
}
|
|
|
|
|
2024-05-07 06:30:31 +00:00
|
|
|
func (r *ReplicaDescriptor) SetECDataCount(v uint32) {
|
2024-03-27 11:43:23 +00:00
|
|
|
r.m.SetECDataCount(v)
|
|
|
|
}
|
|
|
|
|
2024-05-07 06:30:31 +00:00
|
|
|
func (r *ReplicaDescriptor) SetECParityCount(v uint32) {
|
2024-03-27 11:43:23 +00:00
|
|
|
r.m.SetECParityCount(v)
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
// NumberOfObjects returns number set using SetNumberOfObjects.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
2022-06-14 14:27:51 +00:00
|
|
|
// Zero ReplicaDescriptor has zero number of objects.
|
|
|
|
func (r ReplicaDescriptor) NumberOfObjects() uint32 {
|
2022-06-07 08:25:34 +00:00
|
|
|
return r.m.GetCount()
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:43:23 +00:00
|
|
|
func (r ReplicaDescriptor) GetECDataCount() uint32 {
|
|
|
|
return r.m.GetECDataCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ReplicaDescriptor) GetECParityCount() uint32 {
|
|
|
|
return r.m.GetECParityCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalECPartCount returns total sum of ECDataCount and ECParityCount.
|
|
|
|
func (r ReplicaDescriptor) TotalECPartCount() uint32 {
|
|
|
|
return r.m.GetECDataCount() + r.m.GetECParityCount()
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetSelectorName sets name of the related Selector.
|
|
|
|
//
|
|
|
|
// Zero ReplicaDescriptor references to the root bucket's selector: it contains
|
|
|
|
// all possible nodes to store the object.
|
|
|
|
func (r *ReplicaDescriptor) SetSelectorName(s string) {
|
|
|
|
r.m.SetSelector(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddReplicas adds a bunch object replica's characteristics.
|
|
|
|
//
|
|
|
|
// See also IterateReplicas.
|
|
|
|
func (p *PlacementPolicy) AddReplicas(rs ...ReplicaDescriptor) {
|
|
|
|
off := len(p.replicas)
|
|
|
|
|
|
|
|
p.replicas = append(p.replicas, make([]netmap.Replica, len(rs))...)
|
|
|
|
|
|
|
|
for i := range rs {
|
|
|
|
p.replicas[off+i] = rs[i].m
|
2022-06-15 07:12:35 +00:00
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
2022-06-15 07:12:35 +00:00
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
// NumberOfReplicas returns number of replica descriptors set using AddReplicas.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
2022-06-15 13:40:50 +00:00
|
|
|
// Zero PlacementPolicy has no replicas which is incorrect according to the
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS API protocol.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (p PlacementPolicy) NumberOfReplicas() int {
|
|
|
|
return len(p.replicas)
|
|
|
|
}
|
2022-06-15 07:12:35 +00:00
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
// ReplicaNumberByIndex returns number of object replicas from the i-th replica
|
2022-06-07 08:25:34 +00:00
|
|
|
// descriptor. Index MUST be in range [0; NumberOfReplicas()).
|
|
|
|
//
|
|
|
|
// Zero PlacementPolicy has no replicas.
|
2024-03-27 11:43:23 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PlacementPolicy.ReplicaDescriptor(int).NumberOfObjects() instead.
|
2022-06-14 14:27:51 +00:00
|
|
|
func (p PlacementPolicy) ReplicaNumberByIndex(i int) uint32 {
|
2022-06-07 08:25:34 +00:00
|
|
|
return p.replicas[i].GetCount()
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:43:23 +00:00
|
|
|
// ReplicaDescriptor returns i-th replica descriptor. Index MUST be in range [0; NumberOfReplicas()).
|
|
|
|
func (p PlacementPolicy) ReplicaDescriptor(i int) ReplicaDescriptor {
|
|
|
|
return ReplicaDescriptor{
|
|
|
|
m: p.replicas[i],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetContainerBackupFactor sets container backup factor: it controls how deep
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS will search for nodes alternatives to include into container's nodes subset.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
|
|
|
// Zero PlacementPolicy has zero container backup factor.
|
|
|
|
func (p *PlacementPolicy) SetContainerBackupFactor(f uint32) {
|
|
|
|
p.backupFactor = f
|
|
|
|
}
|
|
|
|
|
2023-06-05 10:38:15 +00:00
|
|
|
// SetUnique sets the unique flag: it controls whether the selected replica buckets
|
|
|
|
// are disjoint or not.
|
|
|
|
//
|
|
|
|
// Zero PlacementPolicy has false unique flag.
|
|
|
|
func (p *PlacementPolicy) SetUnique(b bool) {
|
|
|
|
p.unique = b
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Selector describes the bucket selection operator: choose a number of nodes
|
|
|
|
// from the bucket taking the nearest nodes to the related container by hash distance.
|
|
|
|
type Selector struct {
|
|
|
|
m netmap.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetName sets name with which the Selector can be referenced.
|
|
|
|
//
|
|
|
|
// Zero Selector is unnamed.
|
|
|
|
func (s *Selector) SetName(name string) {
|
|
|
|
s.m.SetName(name)
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
// SetNumberOfNodes sets number of nodes to select from the bucket.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
|
|
|
// Zero Selector selects nothing.
|
2022-06-14 14:27:51 +00:00
|
|
|
func (s *Selector) SetNumberOfNodes(num uint32) {
|
|
|
|
s.m.SetCount(num)
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SelectByBucketAttribute sets attribute of the bucket to select nodes from.
|
|
|
|
//
|
2022-06-15 13:40:50 +00:00
|
|
|
// Zero Selector has empty attribute.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (s *Selector) SelectByBucketAttribute(bucket string) {
|
|
|
|
s.m.SetAttribute(bucket)
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:16:48 +00:00
|
|
|
// SetClause sets the clause for the Selector.
|
|
|
|
func (s *Selector) SetClause(clause netmap.Clause) {
|
|
|
|
s.m.SetClause(clause)
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SelectSame makes selection algorithm to select only nodes having the same values
|
|
|
|
// of the bucket attribute.
|
|
|
|
//
|
|
|
|
// Zero Selector doesn't specify selection modifier so nodes are selected randomly.
|
|
|
|
//
|
|
|
|
// See also SelectByBucketAttribute.
|
|
|
|
func (s *Selector) SelectSame() {
|
|
|
|
s.m.SetClause(netmap.Same)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectDistinct makes selection algorithm to select only nodes having the different values
|
|
|
|
// of the bucket attribute.
|
|
|
|
//
|
|
|
|
// Zero Selector doesn't specify selection modifier so nodes are selected randomly.
|
|
|
|
//
|
|
|
|
// See also SelectByBucketAttribute.
|
|
|
|
func (s *Selector) SelectDistinct() {
|
|
|
|
s.m.SetClause(netmap.Distinct)
|
|
|
|
}
|
|
|
|
|
2022-06-15 13:40:50 +00:00
|
|
|
// SetFilterName sets reference to pre-filtering nodes for selection.
|
|
|
|
//
|
|
|
|
// Zero Selector has no filtering reference.
|
2022-06-07 08:25:34 +00:00
|
|
|
//
|
2022-06-15 13:40:50 +00:00
|
|
|
// See also Filter.SetName.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (s *Selector) SetFilterName(f string) {
|
|
|
|
s.m.SetFilter(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSelectors adds a Selector bunch to form the subset of the nodes
|
|
|
|
// to store container objects.
|
|
|
|
//
|
2022-06-15 13:40:50 +00:00
|
|
|
// Zero PlacementPolicy does not declare selectors.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (p *PlacementPolicy) AddSelectors(ss ...Selector) {
|
|
|
|
off := len(p.selectors)
|
|
|
|
|
|
|
|
p.selectors = append(p.selectors, make([]netmap.Selector, len(ss))...)
|
|
|
|
|
|
|
|
for i := range ss {
|
|
|
|
p.selectors[off+i] = ss[i].m
|
2022-06-15 07:12:35 +00:00
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
2022-06-15 07:12:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Filter contains rules for filtering the node sets.
|
|
|
|
type Filter struct {
|
|
|
|
m netmap.Filter
|
2021-11-26 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetName sets name with which the Filter can be referenced or, for inner filters,
|
2022-06-14 14:27:51 +00:00
|
|
|
// to which the Filter references. Top-level filters MUST be named. The name
|
2022-06-07 08:25:34 +00:00
|
|
|
// MUST NOT be '*'.
|
|
|
|
//
|
|
|
|
// Zero Filter is unnamed.
|
|
|
|
func (x *Filter) SetName(name string) {
|
|
|
|
x.m.SetName(name)
|
|
|
|
}
|
2022-06-15 07:12:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x *Filter) setAttribute(key string, op netmap.Operation, val string) {
|
|
|
|
x.m.SetKey(key)
|
|
|
|
x.m.SetOp(op)
|
|
|
|
x.m.SetValue(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal applies the rule to accept only nodes with the same attribute value.
|
|
|
|
//
|
|
|
|
// Method SHOULD NOT be called along with other similar methods.
|
|
|
|
func (x *Filter) Equal(key, value string) {
|
|
|
|
x.setAttribute(key, netmap.EQ, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotEqual applies the rule to accept only nodes with the distinct attribute value.
|
|
|
|
//
|
|
|
|
// Method SHOULD NOT be called along with other similar methods.
|
|
|
|
func (x *Filter) NotEqual(key, value string) {
|
|
|
|
x.setAttribute(key, netmap.NE, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumericGT applies the rule to accept only nodes with the numeric attribute
|
|
|
|
// greater than given number.
|
|
|
|
//
|
|
|
|
// Method SHOULD NOT be called along with other similar methods.
|
|
|
|
func (x *Filter) NumericGT(key string, num int64) {
|
|
|
|
x.setAttribute(key, netmap.GT, strconv.FormatInt(num, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumericGE applies the rule to accept only nodes with the numeric attribute
|
|
|
|
// greater than or equal to given number.
|
|
|
|
//
|
|
|
|
// Method SHOULD NOT be called along with other similar methods.
|
|
|
|
func (x *Filter) NumericGE(key string, num int64) {
|
|
|
|
x.setAttribute(key, netmap.GE, strconv.FormatInt(num, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumericLT applies the rule to accept only nodes with the numeric attribute
|
|
|
|
// less than given number.
|
|
|
|
//
|
|
|
|
// Method SHOULD NOT be called along with other similar methods.
|
|
|
|
func (x *Filter) NumericLT(key string, num int64) {
|
|
|
|
x.setAttribute(key, netmap.LT, strconv.FormatInt(num, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumericLE applies the rule to accept only nodes with the numeric attribute
|
|
|
|
// less than or equal to given number.
|
|
|
|
//
|
|
|
|
// Method SHOULD NOT be called along with other similar methods.
|
|
|
|
func (x *Filter) NumericLE(key string, num int64) {
|
|
|
|
x.setAttribute(key, netmap.LE, strconv.FormatInt(num, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Filter) setInnerFilters(op netmap.Operation, filters []Filter) {
|
|
|
|
x.setAttribute("", op, "")
|
|
|
|
|
|
|
|
inner := x.m.GetFilters()
|
|
|
|
if rem := len(filters) - len(inner); rem > 0 {
|
|
|
|
inner = append(inner, make([]netmap.Filter, rem)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range filters {
|
|
|
|
inner[i] = filters[i].m
|
2022-06-15 07:12:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
x.m.SetFilters(inner)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogicalOR applies the rule to accept only nodes which satisfy at least one
|
|
|
|
// of the given filters.
|
|
|
|
//
|
|
|
|
// Method SHOULD NOT be called along with other similar methods.
|
|
|
|
func (x *Filter) LogicalOR(filters ...Filter) {
|
|
|
|
x.setInnerFilters(netmap.OR, filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogicalAND applies the rule to accept only nodes which satisfy all the given
|
|
|
|
// filters.
|
|
|
|
//
|
|
|
|
// Method SHOULD NOT be called along with other similar methods.
|
|
|
|
func (x *Filter) LogicalAND(filters ...Filter) {
|
|
|
|
x.setInnerFilters(netmap.AND, filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddFilters adds a Filter bunch that will be applied when selecting nodes.
|
|
|
|
//
|
2022-06-15 13:40:50 +00:00
|
|
|
// Zero PlacementPolicy has no filters.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (p *PlacementPolicy) AddFilters(fs ...Filter) {
|
|
|
|
off := len(p.filters)
|
|
|
|
|
|
|
|
p.filters = append(p.filters, make([]netmap.Filter, len(fs))...)
|
|
|
|
|
|
|
|
for i := range fs {
|
|
|
|
p.filters[off+i] = fs[i].m
|
|
|
|
}
|
2021-11-26 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// WriteStringTo encodes PlacementPolicy into human-readably query and writes
|
|
|
|
// the result into w. Returns w's errors directly.
|
|
|
|
//
|
|
|
|
// See also DecodeString.
|
2023-06-27 14:47:07 +00:00
|
|
|
// nolint: funlen
|
2022-06-07 08:25:34 +00:00
|
|
|
func (p PlacementPolicy) WriteStringTo(w io.StringWriter) (err error) {
|
2023-06-05 10:38:15 +00:00
|
|
|
if p.unique {
|
|
|
|
if _, err := w.WriteString("UNIQUE\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 05:38:08 +00:00
|
|
|
delim := ""
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range p.replicas {
|
|
|
|
c := p.replicas[i].GetCount()
|
|
|
|
s := p.replicas[i].GetSelector()
|
|
|
|
|
2024-02-22 19:29:25 +00:00
|
|
|
if c != 0 {
|
2023-04-07 05:38:08 +00:00
|
|
|
_, err = w.WriteString(fmt.Sprintf("%sREP %d", delim, c))
|
2024-02-22 19:29:25 +00:00
|
|
|
} else {
|
|
|
|
ecx, ecy := p.replicas[i].GetECDataCount(), p.replicas[i].GetECParityCount()
|
|
|
|
_, err = w.WriteString(fmt.Sprintf("%sEC %d.%d", delim, ecx, ecy))
|
|
|
|
}
|
|
|
|
if s != "" {
|
|
|
|
_, err = w.WriteString(fmt.Sprintf(" IN %s", s))
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-07 05:38:08 +00:00
|
|
|
|
|
|
|
delim = "\n"
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if p.backupFactor > 0 {
|
2023-04-07 05:38:08 +00:00
|
|
|
_, err = w.WriteString(fmt.Sprintf("\nCBF %d", p.backupFactor))
|
2022-06-07 08:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var s string
|
|
|
|
|
|
|
|
for i := range p.selectors {
|
2023-04-07 05:38:08 +00:00
|
|
|
_, err = w.WriteString(fmt.Sprintf("\nSELECT %d", p.selectors[i].GetCount()))
|
2022-06-07 08:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s = p.selectors[i].GetAttribute(); s != "" {
|
|
|
|
var clause string
|
|
|
|
|
|
|
|
switch p.selectors[i].GetClause() {
|
|
|
|
case netmap.Same:
|
|
|
|
clause = "SAME "
|
|
|
|
case netmap.Distinct:
|
|
|
|
clause = "DISTINCT "
|
|
|
|
default:
|
|
|
|
clause = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.WriteString(fmt.Sprintf(" IN %s%s", clause, s))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s = p.selectors[i].GetFilter(); s != "" {
|
|
|
|
_, err = w.WriteString(" FROM " + s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s = p.selectors[i].GetName(); s != "" {
|
|
|
|
_, err = w.WriteString(" AS " + s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range p.filters {
|
2023-04-07 05:38:08 +00:00
|
|
|
_, err = w.WriteString("\nFILTER ")
|
2022-06-07 08:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:15:40 +00:00
|
|
|
err = writeFilterStringTo(w, p.filters[i], false)
|
2022-06-07 08:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 10:15:40 +00:00
|
|
|
func writeFilterStringTo(w io.StringWriter, f netmap.Filter, mayNeedOuterBrackets bool) error {
|
2022-06-07 08:25:34 +00:00
|
|
|
var err error
|
|
|
|
var s string
|
|
|
|
op := f.GetOp()
|
|
|
|
unspecified := op == 0
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if s = f.GetKey(); s != "" {
|
2023-07-05 11:25:18 +00:00
|
|
|
_, err = w.WriteString(fmt.Sprintf("%s %s %s", escapeString(s), op, escapeString(f.GetValue())))
|
2022-06-07 08:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if s = f.GetName(); unspecified && s != "" {
|
|
|
|
_, err = w.WriteString(fmt.Sprintf("@%s", s))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner := f.GetFilters()
|
|
|
|
|
2023-05-24 09:50:13 +00:00
|
|
|
if op == netmap.NOT {
|
|
|
|
_, err = w.WriteString(op.String() + " (")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-26 10:15:40 +00:00
|
|
|
err = writeFilterStringTo(w, inner[0], false)
|
2023-05-24 09:50:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.WriteString(")")
|
2022-06-07 08:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 09:50:13 +00:00
|
|
|
} else {
|
2023-10-26 10:15:40 +00:00
|
|
|
useBrackets := mayNeedOuterBrackets && op == netmap.OR && len(inner) > 1
|
|
|
|
if useBrackets {
|
|
|
|
_, err = w.WriteString("(")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 09:50:13 +00:00
|
|
|
for i := range inner {
|
|
|
|
if i != 0 {
|
|
|
|
_, err = w.WriteString(" " + op.String() + " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 10:15:40 +00:00
|
|
|
err = writeFilterStringTo(w, inner[i], true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if useBrackets {
|
|
|
|
_, err = w.WriteString(")")
|
2023-05-24 09:50:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if s = f.GetName(); s != "" && !unspecified {
|
|
|
|
_, err = w.WriteString(" AS " + s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// DecodeString decodes PlacementPolicy from the string composed using
|
|
|
|
// WriteStringTo. Returns error if s is malformed.
|
|
|
|
func (p *PlacementPolicy) DecodeString(s string) error {
|
2022-06-28 10:42:35 +00:00
|
|
|
var v policyVisitor
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
input := antlr.NewInputStream(s)
|
|
|
|
lexer := parser.NewQueryLexer(input)
|
2022-06-28 06:27:46 +00:00
|
|
|
lexer.RemoveErrorListeners()
|
2022-06-28 10:42:35 +00:00
|
|
|
lexer.AddErrorListener(&v)
|
2022-06-07 08:25:34 +00:00
|
|
|
stream := antlr.NewCommonTokenStream(lexer, 0)
|
|
|
|
|
|
|
|
pp := parser.NewQuery(stream)
|
|
|
|
pp.BuildParseTrees = true
|
|
|
|
|
|
|
|
pp.RemoveErrorListeners()
|
|
|
|
pp.AddErrorListener(&v)
|
|
|
|
pl := pp.Policy().Accept(&v)
|
|
|
|
|
|
|
|
if len(v.errors) != 0 {
|
|
|
|
return v.errors[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed, ok := pl.(*PlacementPolicy)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected parsed instance type %T", pl)
|
|
|
|
} else if parsed == nil {
|
|
|
|
return errors.New("parsed nil value")
|
|
|
|
}
|
|
|
|
|
2023-08-29 05:41:59 +00:00
|
|
|
if err := validatePolicy(*parsed); err != nil {
|
2022-06-07 08:25:34 +00:00
|
|
|
return fmt.Errorf("invalid policy: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = *parsed
|
|
|
|
|
|
|
|
return nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 13:12:47 +00:00
|
|
|
// SelectFilterExpr is an expression containing only selectors and filters.
|
|
|
|
// It's useful to evaluate their effect before being used in a policy.
|
|
|
|
type SelectFilterExpr struct {
|
|
|
|
cbf uint32
|
|
|
|
selector *netmap.Selector
|
|
|
|
filters []netmap.Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeString decodes a string into a SelectFilterExpr.
|
|
|
|
// Returns an error if s is malformed.
|
|
|
|
func DecodeSelectFilterString(s string) (*SelectFilterExpr, error) {
|
|
|
|
var v policyVisitor
|
|
|
|
|
|
|
|
input := antlr.NewInputStream(s)
|
|
|
|
lexer := parser.NewQueryLexer(input)
|
|
|
|
lexer.RemoveErrorListeners()
|
|
|
|
lexer.AddErrorListener(&v)
|
|
|
|
stream := antlr.NewCommonTokenStream(lexer, 0)
|
|
|
|
|
|
|
|
pp := parser.NewQuery(stream)
|
|
|
|
pp.BuildParseTrees = true
|
|
|
|
|
|
|
|
pp.RemoveErrorListeners()
|
|
|
|
pp.AddErrorListener(&v)
|
|
|
|
sfExpr := pp.SelectFilterExpr().Accept(&v)
|
|
|
|
|
|
|
|
if len(v.errors) != 0 {
|
|
|
|
return nil, v.errors[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed, ok := sfExpr.(*SelectFilterExpr)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected parsed instance type %T", sfExpr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed, nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var (
|
|
|
|
// errUnknownFilter is returned when a value of FROM in a query is unknown.
|
|
|
|
errUnknownFilter = errors.New("filter not found")
|
|
|
|
// errUnknownSelector is returned when a value of IN is unknown.
|
|
|
|
errUnknownSelector = errors.New("policy: selector not found")
|
|
|
|
// errSyntaxError is returned for errors found by ANTLR parser.
|
|
|
|
errSyntaxError = errors.New("policy: syntax error")
|
2023-08-29 05:41:59 +00:00
|
|
|
// errRedundantSelector is returned for errors found by selectors policy validator.
|
|
|
|
errRedundantSelector = errors.New("policy: found redundant selector")
|
|
|
|
// errUnnamedSelector is returned for errors found by selectors policy validator.
|
|
|
|
errUnnamedSelector = errors.New("policy: unnamed selectors are useless, " +
|
|
|
|
"make sure to pair REP and SELECT clauses: \"REP .. IN X\" + \"SELECT ... AS X\"")
|
|
|
|
// errRedundantSelector is returned for errors found by filters policy validator.
|
|
|
|
errRedundantFilter = errors.New("policy: found redundant filter")
|
2024-02-22 19:29:25 +00:00
|
|
|
// errECFewSelectors is returned when EC keyword is used without UNIQUE keyword.
|
|
|
|
errECFewSelectors = errors.New("policy: too few nodes to select")
|
2022-06-07 08:25:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type policyVisitor struct {
|
|
|
|
errors []error
|
|
|
|
parser.BaseQueryVisitor
|
|
|
|
antlr.DefaultErrorListener
|
|
|
|
}
|
|
|
|
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) SyntaxError(_ antlr.Recognizer, _ any, line, column int, msg string, _ antlr.RecognitionException) {
|
2022-06-07 08:25:34 +00:00
|
|
|
p.reportError(fmt.Errorf("%w: line %d:%d %s", errSyntaxError, line, column, msg))
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) reportError(err error) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
p.errors = append(p.errors, err)
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// VisitPolicy implements parser.QueryVisitor interface.
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitPolicy(ctx *parser.PolicyContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
if len(p.errors) != 0 {
|
2021-10-27 10:00:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
pl := new(PlacementPolicy)
|
2023-06-05 10:38:15 +00:00
|
|
|
|
|
|
|
pl.unique = ctx.UNIQUE() != nil
|
|
|
|
|
2024-02-22 19:29:25 +00:00
|
|
|
stmts := ctx.GetChildren()
|
|
|
|
for _, r := range stmts {
|
|
|
|
var res *netmap.Replica
|
|
|
|
var ok bool
|
|
|
|
switch r := r.(type) {
|
|
|
|
case parser.IRepStmtContext:
|
|
|
|
res, ok = r.Accept(p).(*netmap.Replica)
|
|
|
|
case parser.IEcStmtContext:
|
|
|
|
res, ok = r.Accept(p).(*netmap.Replica)
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pl.replicas = append(pl.replicas, *res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cbfStmt := ctx.CbfStmt(); cbfStmt != nil {
|
|
|
|
cbf, ok := cbfStmt.(*parser.CbfStmtContext).Accept(p).(uint32)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pl.SetContainerBackupFactor(cbf)
|
|
|
|
}
|
|
|
|
|
|
|
|
selStmts := ctx.AllSelectStmt()
|
|
|
|
pl.selectors = make([]netmap.Selector, 0, len(selStmts))
|
|
|
|
|
|
|
|
for _, s := range selStmts {
|
|
|
|
res, ok := s.Accept(p).(*netmap.Selector)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pl.selectors = append(pl.selectors, *res)
|
|
|
|
}
|
|
|
|
|
|
|
|
filtStmts := ctx.AllFilterStmt()
|
|
|
|
pl.filters = make([]netmap.Filter, 0, len(filtStmts))
|
|
|
|
|
|
|
|
for _, f := range filtStmts {
|
|
|
|
pl.filters = append(pl.filters, *f.Accept(p).(*netmap.Filter))
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return pl
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 13:12:47 +00:00
|
|
|
func (p *policyVisitor) VisitSelectFilterExpr(ctx *parser.SelectFilterExprContext) any {
|
|
|
|
if len(p.errors) != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sfExpr := new(SelectFilterExpr)
|
|
|
|
|
|
|
|
if cbfStmt := ctx.CbfStmt(); cbfStmt != nil {
|
|
|
|
cbf, ok := cbfStmt.(*parser.CbfStmtContext).Accept(p).(uint32)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sfExpr.cbf = cbf
|
|
|
|
}
|
|
|
|
|
|
|
|
if selStmt := ctx.SelectStmt(); selStmt != nil {
|
|
|
|
sel, ok := selStmt.Accept(p).(*netmap.Selector)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sfExpr.selector = sel
|
|
|
|
}
|
|
|
|
|
|
|
|
filtStmts := ctx.AllFilterStmt()
|
|
|
|
sfExpr.filters = make([]netmap.Filter, 0, len(filtStmts))
|
|
|
|
|
|
|
|
for _, f := range filtStmts {
|
|
|
|
sfExpr.filters = append(sfExpr.filters, *f.Accept(p).(*netmap.Filter))
|
|
|
|
}
|
|
|
|
|
|
|
|
return sfExpr
|
|
|
|
}
|
|
|
|
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitCbfStmt(ctx *parser.CbfStmtContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
cbf, err := strconv.ParseUint(ctx.GetBackupFactor().GetText(), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return p.reportError(errInvalidNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint32(cbf)
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// VisitRepStmt implements parser.QueryVisitor interface.
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitRepStmt(ctx *parser.RepStmtContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
num, err := strconv.ParseUint(ctx.GetCount().GetText(), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return p.reportError(errInvalidNumber)
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
rs := new(netmap.Replica)
|
|
|
|
rs.SetCount(uint32(num))
|
|
|
|
|
|
|
|
if sel := ctx.GetSelector(); sel != nil {
|
|
|
|
rs.SetSelector(sel.GetText())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return rs
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 19:29:25 +00:00
|
|
|
// VisitRepStmt implements parser.QueryVisitor interface.
|
|
|
|
func (p *policyVisitor) VisitEcStmt(ctx *parser.EcStmtContext) any {
|
|
|
|
dataCount, err := strconv.ParseUint(ctx.GetData().GetText(), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return p.reportError(errInvalidNumber)
|
|
|
|
}
|
|
|
|
parityCount, err := strconv.ParseUint(ctx.GetParity().GetText(), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return p.reportError(errInvalidNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := new(netmap.Replica)
|
|
|
|
rs.SetECDataCount(uint32(dataCount))
|
|
|
|
rs.SetECParityCount(uint32(parityCount))
|
|
|
|
|
|
|
|
if sel := ctx.GetSelector(); sel != nil {
|
|
|
|
rs.SetSelector(sel.GetText())
|
|
|
|
}
|
|
|
|
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// VisitSelectStmt implements parser.QueryVisitor interface.
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitSelectStmt(ctx *parser.SelectStmtContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
res, err := strconv.ParseUint(ctx.GetCount().GetText(), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return p.reportError(errInvalidNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := new(netmap.Selector)
|
|
|
|
s.SetCount(uint32(res))
|
|
|
|
|
|
|
|
if clStmt := ctx.Clause(); clStmt != nil {
|
|
|
|
s.SetClause(clauseFromString(clStmt.GetText()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if bStmt := ctx.GetBucket(); bStmt != nil {
|
|
|
|
s.SetAttribute(ctx.GetBucket().GetText())
|
|
|
|
}
|
|
|
|
|
|
|
|
s.SetFilter(ctx.GetFilter().GetText()) // either ident or wildcard
|
|
|
|
|
|
|
|
if ctx.AS() != nil {
|
|
|
|
s.SetName(ctx.GetName().GetText())
|
|
|
|
}
|
|
|
|
return s
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// VisitFilterStmt implements parser.QueryVisitor interface.
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitFilterStmt(ctx *parser.FilterStmtContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
f := p.VisitFilterExpr(ctx.GetExpr().(*parser.FilterExprContext)).(*netmap.Filter)
|
|
|
|
f.SetName(ctx.GetName().GetText())
|
|
|
|
return f
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitFilterExpr(ctx *parser.FilterExprContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
if eCtx := ctx.Expr(); eCtx != nil {
|
|
|
|
return eCtx.Accept(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inner := ctx.GetInner(); inner != nil {
|
|
|
|
return inner.Accept(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := new(netmap.Filter)
|
|
|
|
op := operationFromString(ctx.GetOp().GetText())
|
|
|
|
f.SetOp(op)
|
|
|
|
|
2023-05-24 09:50:13 +00:00
|
|
|
if op == netmap.NOT {
|
|
|
|
f1 := *ctx.GetF1().Accept(p).(*netmap.Filter)
|
|
|
|
f.SetFilters([]netmap.Filter{f1})
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
f1 := *ctx.GetF1().Accept(p).(*netmap.Filter)
|
|
|
|
f2 := *ctx.GetF2().Accept(p).(*netmap.Filter)
|
|
|
|
|
|
|
|
// Consider f1=(.. AND ..) AND f2. This can be merged because our AND operation
|
|
|
|
// is of arbitrary arity. ANTLR generates left-associative parse-tree by default.
|
|
|
|
if f1.GetOp() == op {
|
|
|
|
f.SetFilters(append(f1.GetFilters(), f2))
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
f.SetFilters([]netmap.Filter{f1, f2})
|
|
|
|
|
|
|
|
return f
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// VisitFilterKey implements parser.QueryVisitor interface.
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitFilterKey(ctx *parser.FilterKeyContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
if id := ctx.Ident(); id != nil {
|
|
|
|
return id.GetText()
|
|
|
|
}
|
|
|
|
|
|
|
|
str := ctx.STRING().GetText()
|
|
|
|
return str[1 : len(str)-1]
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitFilterValue(ctx *parser.FilterValueContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
if id := ctx.Ident(); id != nil {
|
|
|
|
return id.GetText()
|
|
|
|
}
|
|
|
|
|
|
|
|
if num := ctx.Number(); num != nil {
|
|
|
|
return num.GetText()
|
|
|
|
}
|
|
|
|
|
|
|
|
str := ctx.STRING().GetText()
|
|
|
|
return str[1 : len(str)-1]
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// VisitExpr implements parser.QueryVisitor interface.
|
2023-05-15 14:21:49 +00:00
|
|
|
func (p *policyVisitor) VisitExpr(ctx *parser.ExprContext) any {
|
2022-06-07 08:25:34 +00:00
|
|
|
f := new(netmap.Filter)
|
|
|
|
if flt := ctx.GetFilter(); flt != nil {
|
|
|
|
f.SetName(flt.GetText())
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
key := ctx.GetKey().Accept(p)
|
|
|
|
opStr := ctx.SIMPLE_OP().GetText()
|
|
|
|
value := ctx.GetValue().Accept(p)
|
|
|
|
|
|
|
|
f.SetKey(key.(string))
|
|
|
|
f.SetOp(operationFromString(opStr))
|
|
|
|
f.SetValue(value.(string))
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// validatePolicy checks high-level constraints such as filter link in SELECT
|
|
|
|
// being actually defined in FILTER section.
|
|
|
|
func validatePolicy(p PlacementPolicy) error {
|
2023-09-08 13:57:41 +00:00
|
|
|
canOmitNames := len(p.selectors) == 1 && len(p.replicas) == 1
|
2022-06-07 08:25:34 +00:00
|
|
|
seenFilters := map[string]bool{}
|
2023-08-29 05:41:59 +00:00
|
|
|
expectedFilters := map[string]struct{}{}
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range p.filters {
|
|
|
|
seenFilters[p.filters[i].GetName()] = true
|
2023-08-29 05:41:59 +00:00
|
|
|
for _, f := range p.filters[i].GetFilters() {
|
|
|
|
if f.GetName() != "" {
|
|
|
|
expectedFilters[f.GetName()] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 05:41:59 +00:00
|
|
|
seenSelectors := map[string]*netmap.Selector{}
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range p.selectors {
|
2023-09-08 13:57:41 +00:00
|
|
|
if p.selectors[i].GetName() == "" && !canOmitNames {
|
2023-08-29 05:41:59 +00:00
|
|
|
return errUnnamedSelector
|
|
|
|
}
|
|
|
|
if flt := p.selectors[i].GetFilter(); flt != mainFilterName {
|
|
|
|
expectedFilters[flt] = struct{}{}
|
|
|
|
if !seenFilters[flt] {
|
|
|
|
return fmt.Errorf("%w: '%s'", errUnknownFilter, flt)
|
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
2023-08-29 05:41:59 +00:00
|
|
|
seenSelectors[p.selectors[i].GetName()] = &p.selectors[i]
|
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
|
2023-08-29 05:41:59 +00:00
|
|
|
for _, f := range p.filters {
|
|
|
|
if _, ok := expectedFilters[f.GetName()]; !ok {
|
|
|
|
return fmt.Errorf("%w: '%s'", errRedundantFilter, f.GetName())
|
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 05:41:59 +00:00
|
|
|
expectedSelectors := map[string]struct{}{}
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range p.replicas {
|
2023-08-29 05:41:59 +00:00
|
|
|
selName := p.replicas[i].GetSelector()
|
2023-09-08 13:57:41 +00:00
|
|
|
if selName != "" || canOmitNames {
|
2023-08-29 05:41:59 +00:00
|
|
|
expectedSelectors[selName] = struct{}{}
|
|
|
|
if seenSelectors[selName] == nil {
|
|
|
|
return fmt.Errorf("%w: '%s'", errUnknownSelector, selName)
|
|
|
|
}
|
2024-02-22 19:29:25 +00:00
|
|
|
|
|
|
|
dataCount := p.replicas[i].GetECDataCount()
|
|
|
|
parityCount := p.replicas[i].GetECParityCount()
|
|
|
|
if dataCount != 0 || parityCount != 0 {
|
|
|
|
if c := seenSelectors[selName].GetCount(); c < dataCount+parityCount {
|
|
|
|
return fmt.Errorf("%w: %d < %d + %d", errECFewSelectors, c, dataCount, parityCount)
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 05:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range p.selectors {
|
|
|
|
if _, ok := expectedSelectors[s.GetName()]; !ok {
|
|
|
|
return fmt.Errorf("%w: to use selector '%s' use keyword IN", errRedundantSelector, s.GetName())
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func clauseFromString(s string) (c netmap.Clause) {
|
|
|
|
if !c.FromString(strings.ToUpper(s)) {
|
|
|
|
// Such errors should be handled by ANTLR code thus this panic.
|
|
|
|
panic(fmt.Errorf("BUG: invalid clause: %s", c))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func operationFromString(s string) (op netmap.Operation) {
|
|
|
|
if !op.FromString(strings.ToUpper(s)) {
|
|
|
|
// Such errors should be handled by ANTLR code thus this panic.
|
|
|
|
panic(fmt.Errorf("BUG: invalid operation: %s", op))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
2023-07-05 11:25:18 +00:00
|
|
|
|
2024-03-01 14:30:10 +00:00
|
|
|
// escapeString returns single quote wrapped string.
|
|
|
|
// Wrapping rules must be kept in sync with QueryLexer.g4.
|
|
|
|
// Currently only ASCII letters, digits and underscore can be parsed without quotes.
|
2023-07-05 11:25:18 +00:00
|
|
|
func escapeString(s string) string {
|
2024-03-01 14:30:10 +00:00
|
|
|
for _, r := range s {
|
|
|
|
if 'a' <= r && r <= 'z' || 'A' <= r && r <= 'Z' || '0' <= r && r <= '9' || r == '_' {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-05 11:25:18 +00:00
|
|
|
return "'" + s + "'"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|