[#665] node: Bind length of copies number to number of replicas
All checks were successful
DCO action / DCO (pull_request) Successful in 2m31s
Vulncheck / Vulncheck (pull_request) Successful in 2m40s
Build / Build Components (1.20) (pull_request) Successful in 4m10s
Tests and linters / Tests (1.21) (pull_request) Successful in 5m0s
Tests and linters / Staticcheck (pull_request) Successful in 5m42s
Tests and linters / Tests (1.20) (pull_request) Successful in 7m43s
Tests and linters / Tests with -race (pull_request) Successful in 9m14s
Build / Build Components (1.21) (pull_request) Successful in 12m25s
Tests and linters / Lint (pull_request) Successful in 19m37s

Allow to use one digit in copies number array for backward compatibility.

Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
Anton Nikiforov 2023-08-30 13:43:43 +03:00
parent a724debb19
commit 63942ae2be
2 changed files with 21 additions and 8 deletions

View file

@ -58,6 +58,9 @@ var errNilBuilder = errors.New("placement builder is nil")
var errNilPolicy = errors.New("placement policy is nil")
var errCopiesNumberLen = errors.New("copies number accepts only one number or array with length " +
"equal to length of replicas")
func defaultCfg() *cfg {
return &cfg{
trackCopies: true,
@ -74,6 +77,11 @@ func NewTraverser(opts ...Option) (*Traverser, error) {
}
}
cnLen := len(cfg.copyNumbers)
if cnLen > 0 && cnLen != 1 && cnLen != cfg.policy.NumberOfReplicas() {
return nil, errCopiesNumberLen
}
if cfg.builder == nil {
return nil, fmt.Errorf("%s: %w", invalidOptsMsg, errNilBuilder)
} else if !cfg.policySet {

View file

@ -220,6 +220,7 @@ func TestTraverserRemValues(t *testing.T) {
name string
copyNumbers []uint32
expectedRem []int
expectedErr error
}{
{
name: "zero copy numbers",
@ -231,11 +232,6 @@ func TestTraverserRemValues(t *testing.T) {
copyNumbers: []uint32{0},
expectedRem: replicas,
},
{
name: "compatible zero copy numbers, len 2",
copyNumbers: []uint32{0, 0},
expectedRem: replicas,
},
{
name: "compatible zero copy numbers, len 3",
copyNumbers: []uint32{0, 0, 0},
@ -253,9 +249,14 @@ func TestTraverserRemValues(t *testing.T) {
},
{
name: "multiple copy numbers for multiple replicas",
copyNumbers: []uint32{1, 1},
copyNumbers: []uint32{1, 1, 4},
expectedRem: []int{1, 1, 4},
},
{
name: "incompatible copies number vector",
copyNumbers: []uint32{1, 1},
expectedErr: errCopiesNumberLen,
},
}
for _, testCase := range testCases {
@ -265,8 +266,12 @@ func TestTraverserRemValues(t *testing.T) {
UseBuilder(&testBuilder{vectors: nodesCopy}),
WithCopyNumbers(testCase.copyNumbers),
)
require.NoError(t, err)
require.Equal(t, testCase.expectedRem, tr.rem)
if testCase.expectedErr == nil {
require.NoError(t, err, testCase.name)
require.Equal(t, testCase.expectedRem, tr.rem, testCase.name)
} else {
require.Error(t, err, testCase.expectedErr, testCase.name)
}
})
}
}