mirror of
https://github.com/cubefs/cubefs.git
synced 2026-08-02 02:00:56 +00:00
fix(lcnode): hybrid cloud optimize conflict rule prefix #22311091
Signed-off-by: zhaochenyang <zhaochenyang@oppo.com>
This commit is contained in:
parent
bc8ef471bd
commit
da6467830a
@ -241,3 +241,86 @@ func TestLifecycleConfigurationTransition2(t *testing.T) {
|
||||
err = proto.ValidRules(l1.Rules)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestLifecycleConfigurationTransition3(t *testing.T) {
|
||||
LifecycleXml := `
|
||||
<LifecycleConfiguration>
|
||||
<Rule>
|
||||
<ID>id1</ID>
|
||||
<Status>Enabled</Status>
|
||||
<Transition>
|
||||
<Days>365</Days>
|
||||
<StorageClass>HDD</StorageClass>
|
||||
</Transition>
|
||||
</Rule>
|
||||
<Rule>
|
||||
<ID>id2</ID>
|
||||
<Status>Enabled</Status>
|
||||
<Transition>
|
||||
<Days>365</Days>
|
||||
<StorageClass>HDD</StorageClass>
|
||||
</Transition>
|
||||
</Rule>
|
||||
</LifecycleConfiguration>
|
||||
`
|
||||
|
||||
l1 := NewLifecycleConfiguration()
|
||||
err := xml.Unmarshal([]byte(LifecycleXml), l1)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = proto.ValidRules(l1.Rules)
|
||||
require.Equal(t, proto.LifeCycleErrConflictRules, err)
|
||||
|
||||
LifecycleXml2 := `
|
||||
<LifecycleConfiguration>
|
||||
<Rule>
|
||||
<ID>id1</ID>
|
||||
<Status>Enabled</Status>
|
||||
<Filter>
|
||||
<Prefix></Prefix>
|
||||
</Filter>
|
||||
<Transition>
|
||||
<Days>365</Days>
|
||||
<StorageClass>HDD</StorageClass>
|
||||
</Transition>
|
||||
</Rule>
|
||||
<Rule>
|
||||
<ID>id2</ID>
|
||||
<Status>Enabled</Status>
|
||||
<Filter>
|
||||
<Prefix></Prefix>
|
||||
</Filter>
|
||||
<Transition>
|
||||
<Days>365</Days>
|
||||
<StorageClass>HDD</StorageClass>
|
||||
</Transition>
|
||||
</Rule>
|
||||
</LifecycleConfiguration>
|
||||
`
|
||||
l2 := NewLifecycleConfiguration()
|
||||
err = xml.Unmarshal([]byte(LifecycleXml2), l2)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = proto.ValidRules(l2.Rules)
|
||||
require.Equal(t, proto.LifeCycleErrConflictRules, err)
|
||||
|
||||
l2.Rules[0].Filter.Prefix = "log"
|
||||
l2.Rules[1].Filter.Prefix = "log1"
|
||||
err = proto.ValidRules(l2.Rules)
|
||||
require.Equal(t, proto.LifeCycleErrConflictRules, err)
|
||||
|
||||
l2.Rules[0].Filter.Prefix = "log1"
|
||||
l2.Rules[1].Filter.Prefix = "log"
|
||||
err = proto.ValidRules(l2.Rules)
|
||||
require.Equal(t, proto.LifeCycleErrConflictRules, err)
|
||||
|
||||
l2.Rules[0].Filter.Prefix = "log"
|
||||
l2.Rules[1].Filter.Prefix = "log"
|
||||
err = proto.ValidRules(l2.Rules)
|
||||
require.Equal(t, proto.LifeCycleErrConflictRules, err)
|
||||
|
||||
l2.Rules[0].Filter.Prefix = "log1"
|
||||
l2.Rules[1].Filter.Prefix = "log2"
|
||||
err = proto.ValidRules(l2.Rules)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@ package proto
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -83,6 +84,7 @@ var (
|
||||
LifeCycleErrDaysType = errors.New("'Days' for Expiration action must be a positive integer")
|
||||
LifeCycleErrStorageClass = errors.New("'StorageClass' must be different for 'Transition' actions in same 'Rule'")
|
||||
LifeCycleErrMalformedXML = errors.New("The XML you provided was not well-formed or did not validate against our published schema")
|
||||
LifeCycleErrConflictRules = errors.New("Conflicting rule prefix")
|
||||
)
|
||||
|
||||
func ValidRules(Rules []*Rule) error {
|
||||
@ -106,6 +108,45 @@ func ValidRules(Rules []*Rule) error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := ValidRulePrefix(Rules); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidRulePrefix(Rules []*Rule) error {
|
||||
if len(Rules) == 1 {
|
||||
return nil
|
||||
}
|
||||
var prefixes []string
|
||||
for _, rule := range Rules {
|
||||
if rule.Filter == nil {
|
||||
return LifeCycleErrConflictRules
|
||||
}
|
||||
if rule.Filter != nil {
|
||||
if rule.Filter.Prefix == "" {
|
||||
return LifeCycleErrConflictRules
|
||||
} else {
|
||||
prefixes = append(prefixes, rule.Filter.Prefix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, p1 := range prefixes {
|
||||
for j, p2 := range prefixes {
|
||||
if i == j {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(p1, p2) {
|
||||
return LifeCycleErrConflictRules
|
||||
}
|
||||
if strings.HasPrefix(p2, p1) {
|
||||
return LifeCycleErrConflictRules
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user