policy: Check for redundant selectors and filters #154

Merged
fyrchik merged 1 commit from acid-ant/frostfs-sdk-go:bugfix/150-check-redundant into master 2023-09-04 14:12:55 +00:00
Member

Close #150
Policy REP 1 SELECT 4 FROM * AS X will fail with error policy: found redundant selector.
REP 1 IN X SELECT 4 FROM * AS X FILTER 'UN' EQ 'RU' AS F - with error policy: found redundant filter

Signed-off-by: Anton Nikiforov an.nikiforov@yadro.com

Close #150 Policy `REP 1 SELECT 4 FROM * AS X` will fail with error `policy: found redundant selector`. `REP 1 IN X SELECT 4 FROM * AS X FILTER 'UN' EQ 'RU' AS F` - with error `policy: found redundant filter` Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
acid-ant requested review from storage-core-committers 2023-08-29 05:44:20 +00:00
acid-ant requested review from storage-core-developers 2023-08-29 05:44:34 +00:00
dstepanov-yadro approved these changes 2023-08-29 07:29:26 +00:00
fyrchik reviewed 2023-08-29 07:32:58 +00:00
netmap/policy.go Outdated
@ -553,2 +553,3 @@
if err := validatePolicy(*p); err != nil {
if err := validatePolicy(*parsed); err != nil {
//if err := validatePolicy(*p); err != nil {
Owner

What's this comment for?

What's this comment for?
Author
Member

Ooops, removed.

Ooops, removed.
fyrchik marked this conversation as resolved
netmap/policy.go Outdated
@ -864,0 +864,4 @@
seenSelectors := map[string]*netmap.Selector{}
for i := range p.selectors {
if p.selectors[i].GetName() == "" {
return fmt.Errorf("%w: use keyword AS to provide name for selector", errRedundantSelector)
Owner

It's is errRedundantSelector, but maybe errUnnamedSelector would be better? Like unnamed selectors are useless, make sure to pair REP and SELECT clauses: "REP .. IN X" + "SELECT ... AS X"

It's is `errRedundantSelector`, but maybe `errUnnamedSelector` would be better? Like `unnamed selectors are useless, make sure to pair REP and SELECT clauses: "REP .. IN X" + "SELECT ... AS X"`
Author
Member

Agree, sounds better. Updated.

Agree, sounds better. Updated.
fyrchik marked this conversation as resolved
netmap/policy.go Outdated
@ -864,0 +866,4 @@
if p.selectors[i].GetName() == "" {
return fmt.Errorf("%w: use keyword AS to provide name for selector", errRedundantSelector)
}
if flt := p.selectors[i].GetFilter(); flt != mainFilterName {
Owner

What happens if flt be is an empty string here?

What happens if `flt` be is an empty string here?
Author
Member

Thought about this too earlier. In this case, we will have the different error: policy: syntax error: line 1:68 mismatched input '<EOF>' expecting 'AS

Thought about this too earlier. In this case, we will have the different error: `policy: syntax error: line 1:68 mismatched input '<EOF>' expecting 'AS`
Owner

Hm, this makes me think -- what if we just change the grammar for selectors too? I.e. make this obligatory https://git.frostfs.info/TrueCloudLab/frostfs-sdk-go/src/branch/master/netmap/parser/Query.g4#L21

For validation we could just check used/unused then.

Hm, this makes me think -- what if we just change the grammar for selectors too? I.e. make this obligatory https://git.frostfs.info/TrueCloudLab/frostfs-sdk-go/src/branch/master/netmap/parser/Query.g4#L21 For validation we could just check used/unused then.
netmap/policy.go Outdated
@ -864,3 +885,2 @@
for i := range p.replicas {
if sel := p.replicas[i].GetSelector(); sel != "" && !seenSelectors[sel] {
return fmt.Errorf("%w: '%s'", errUnknownSelector, sel)
var sel *netmap.Selector
Owner

This variable is used once inside an if, maybe move it there sel :=?

This variable is used once inside an `if`, maybe move it there `sel :=`?
Author
Member

Right, garbage from the previous implementation, updated.

Right, garbage from the previous implementation, updated.
fyrchik marked this conversation as resolved
@ -0,0 +42,4 @@
var p PlacementPolicy
for _, testCase := range testCases {
require.NoError(t, p.DecodeString(testCase), fmt.Sprintf("unable parse %s", testCase))
Owner

I believe require naturally supports format strings: require.NoError(..., "unable to parse %s", testCase)

I believe `require` naturally supports format strings: `require.NoError(..., "unable to parse %s", testCase)`
Author
Member

Fixed.

Fixed.
fyrchik marked this conversation as resolved
acid-ant force-pushed bugfix/150-check-redundant from 0886d18d49 to 9c0b33c6fa 2023-08-29 11:14:57 +00:00 Compare
acid-ant force-pushed bugfix/150-check-redundant from 9c0b33c6fa to b5fe52d6bd 2023-08-29 11:17:03 +00:00 Compare
aarifullin approved these changes 2023-08-30 07:47:05 +00:00
fyrchik merged commit b5fe52d6bd into master 2023-09-04 14:12:55 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: TrueCloudLab/frostfs-sdk-go#154
No description provided.