2023-06-06 06:29:40 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
rpcClient "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
2024-05-30 11:58:14 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2023-06-06 06:29:40 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type treeClientMock struct {
|
|
|
|
address string
|
|
|
|
err bool
|
|
|
|
}
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
func (t *treeClientMock) serviceClient() (*rpcClient.Client, error) {
|
2023-06-06 06:29:40 +00:00
|
|
|
if t.err {
|
|
|
|
return nil, errors.New("serviceClient() mock error")
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *treeClientMock) endpoint() string {
|
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *treeClientMock) isHealthy() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *treeClientMock) setHealthy(bool) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *treeClientMock) dial(context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *treeClientMock) redialIfNecessary(context.Context) (bool, error) {
|
|
|
|
if t.err {
|
|
|
|
return false, errors.New("redialIfNecessary() mock error")
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *treeClientMock) close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleError(t *testing.T) {
|
|
|
|
defaultError := errors.New("default error")
|
|
|
|
for _, tc := range []struct {
|
|
|
|
err error
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
err: defaultError,
|
|
|
|
expectedError: defaultError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
err: errors.New("something not found"),
|
|
|
|
expectedError: ErrNodeNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
err: errors.New("something is denied by some acl rule"),
|
|
|
|
expectedError: ErrNodeAccessDenied,
|
|
|
|
},
|
2024-05-30 11:58:14 +00:00
|
|
|
{
|
|
|
|
err: &apistatus.APEManagerAccessDenied{},
|
|
|
|
expectedError: ErrNodeAccessDenied,
|
|
|
|
},
|
2023-06-06 06:29:40 +00:00
|
|
|
} {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
err := handleError("err message", tc.err)
|
|
|
|
require.True(t, errors.Is(err, tc.expectedError))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetry(t *testing.T) {
|
2023-10-10 13:59:27 +00:00
|
|
|
ctx := context.Background()
|
2023-06-06 06:29:40 +00:00
|
|
|
nodes := [][]string{
|
|
|
|
{"node00", "node01", "node02", "node03"},
|
|
|
|
{"node10", "node11", "node12", "node13"},
|
|
|
|
}
|
|
|
|
|
2023-10-23 13:51:32 +00:00
|
|
|
var lenNodes int
|
|
|
|
for i := range nodes {
|
|
|
|
lenNodes += len(nodes[i])
|
|
|
|
}
|
|
|
|
|
2023-06-06 06:29:40 +00:00
|
|
|
p := &Pool{
|
2023-10-23 13:51:32 +00:00
|
|
|
logger: zaptest.NewLogger(t),
|
|
|
|
innerPools: makeInnerPool(nodes),
|
|
|
|
maxRequestAttempts: lenNodes,
|
2023-06-06 06:29:40 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
makeFn := func(client *rpcClient.Client) error {
|
2023-06-06 06:29:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("first ok", func(t *testing.T) {
|
2023-10-10 13:59:27 +00:00
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
2023-06-06 06:29:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("first failed", func(t *testing.T) {
|
|
|
|
setErrors(p, "node00")
|
2023-10-10 13:59:27 +00:00
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
2023-06-06 06:29:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("all failed", func(t *testing.T) {
|
|
|
|
setErrors(p, nodes[0]...)
|
|
|
|
setErrors(p, nodes[1]...)
|
2023-10-10 13:59:27 +00:00
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
2023-06-06 06:29:40 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("round", func(t *testing.T) {
|
|
|
|
setErrors(p, nodes[0][0], nodes[0][1])
|
|
|
|
setErrors(p, nodes[1]...)
|
2023-10-10 13:59:27 +00:00
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
2023-06-06 06:29:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndices(t, p, 0, 2)
|
|
|
|
resetClientsErrors(p)
|
|
|
|
|
|
|
|
setErrors(p, nodes[0][2], nodes[0][3])
|
2023-10-10 13:59:27 +00:00
|
|
|
err = p.requestWithRetry(ctx, makeFn)
|
2023-06-06 06:29:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("group switch", func(t *testing.T) {
|
|
|
|
setErrors(p, nodes[0]...)
|
|
|
|
setErrors(p, nodes[1][0])
|
2023-10-10 13:59:27 +00:00
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
2023-06-06 06:29:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 1, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("group round", func(t *testing.T) {
|
|
|
|
setErrors(p, nodes[0][1:]...)
|
2023-10-10 13:59:27 +00:00
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
2023-06-06 06:29:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("group round switch", func(t *testing.T) {
|
|
|
|
setErrors(p, nodes[0]...)
|
|
|
|
p.setStartIndices(0, 1)
|
2023-10-10 13:59:27 +00:00
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
2023-06-06 06:29:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 1, 0)
|
|
|
|
})
|
2023-06-28 10:06:23 +00:00
|
|
|
|
|
|
|
t.Run("no panic group switch", func(t *testing.T) {
|
|
|
|
setErrors(p, nodes[1]...)
|
|
|
|
p.setStartIndices(1, 0)
|
2023-10-10 13:59:27 +00:00
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
2023-06-28 10:06:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, 0)
|
|
|
|
})
|
2023-09-28 10:22:30 +00:00
|
|
|
|
|
|
|
t.Run("error empty result", func(t *testing.T) {
|
|
|
|
errNodes, index := 2, 0
|
2024-11-07 11:29:55 +00:00
|
|
|
err := p.requestWithRetry(ctx, func(client *rpcClient.Client) error {
|
2023-09-28 10:22:30 +00:00
|
|
|
if index < errNodes {
|
|
|
|
index++
|
|
|
|
return errNodeEmptyResult
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, errNodes)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error not found", func(t *testing.T) {
|
|
|
|
errNodes, index := 2, 0
|
2024-11-07 11:29:55 +00:00
|
|
|
err := p.requestWithRetry(ctx, func(client *rpcClient.Client) error {
|
2023-09-28 10:22:30 +00:00
|
|
|
if index < errNodes {
|
|
|
|
index++
|
|
|
|
return ErrNodeNotFound
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, errNodes)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error access denied", func(t *testing.T) {
|
|
|
|
var index int
|
2024-11-07 11:29:55 +00:00
|
|
|
err := p.requestWithRetry(ctx, func(client *rpcClient.Client) error {
|
2023-09-28 10:22:30 +00:00
|
|
|
index++
|
|
|
|
return ErrNodeAccessDenied
|
|
|
|
})
|
|
|
|
require.ErrorIs(t, err, ErrNodeAccessDenied)
|
|
|
|
require.Equal(t, 1, index)
|
|
|
|
checkIndicesAndReset(t, p, 0, 0)
|
|
|
|
})
|
2023-10-23 13:51:32 +00:00
|
|
|
|
|
|
|
t.Run("limit attempts", func(t *testing.T) {
|
|
|
|
oldVal := p.maxRequestAttempts
|
|
|
|
p.maxRequestAttempts = 2
|
|
|
|
setErrors(p, nodes[0]...)
|
|
|
|
setErrors(p, nodes[1]...)
|
|
|
|
err := p.requestWithRetry(ctx, makeFn)
|
|
|
|
require.Error(t, err)
|
|
|
|
checkIndicesAndReset(t, p, 0, 2)
|
|
|
|
p.maxRequestAttempts = oldVal
|
|
|
|
})
|
2023-06-06 06:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRebalance(t *testing.T) {
|
|
|
|
nodes := [][]string{
|
|
|
|
{"node00", "node01"},
|
|
|
|
{"node10", "node11"},
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &Pool{
|
|
|
|
logger: zaptest.NewLogger(t),
|
|
|
|
innerPools: makeInnerPool(nodes),
|
|
|
|
rebalanceParams: rebalanceParameters{
|
|
|
|
nodesGroup: makeNodesGroup(nodes),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
buffers := makeBuffer(p)
|
|
|
|
|
|
|
|
t.Run("check dirty buffers", func(t *testing.T) {
|
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
checkIndices(t, p, 0, 0)
|
|
|
|
setErrors(p, nodes[0][0])
|
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
checkIndices(t, p, 0, 1)
|
|
|
|
resetClients(p)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("don't change healthy status", func(t *testing.T) {
|
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
checkIndices(t, p, 0, 0)
|
|
|
|
resetClients(p)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("switch to second group", func(t *testing.T) {
|
|
|
|
setErrors(p, nodes[0][0], nodes[0][1])
|
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
checkIndices(t, p, 1, 0)
|
|
|
|
resetClients(p)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("switch back and forth", func(t *testing.T) {
|
|
|
|
setErrors(p, nodes[0][0], nodes[0][1])
|
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
checkIndices(t, p, 1, 0)
|
|
|
|
|
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
checkIndices(t, p, 1, 0)
|
|
|
|
|
|
|
|
setNoErrors(p, nodes[0][0])
|
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
checkIndices(t, p, 0, 0)
|
|
|
|
|
|
|
|
resetClients(p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeInnerPool(nodes [][]string) []*innerPool {
|
|
|
|
res := make([]*innerPool, len(nodes))
|
|
|
|
|
|
|
|
for i, group := range nodes {
|
|
|
|
res[i] = &innerPool{clients: make([]client, len(group))}
|
|
|
|
for j, node := range group {
|
|
|
|
res[i].clients[j] = &treeClientMock{address: node}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeNodesGroup(nodes [][]string) [][]pool.NodeParam {
|
|
|
|
res := make([][]pool.NodeParam, len(nodes))
|
|
|
|
|
|
|
|
for i, group := range nodes {
|
|
|
|
res[i] = make([]pool.NodeParam, len(group))
|
|
|
|
for j, node := range group {
|
|
|
|
res[i][j] = pool.NewNodeParam(1, node, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeBuffer(p *Pool) [][]bool {
|
|
|
|
buffers := make([][]bool, len(p.rebalanceParams.nodesGroup))
|
|
|
|
for i, nodes := range p.rebalanceParams.nodesGroup {
|
|
|
|
buffers[i] = make([]bool, len(nodes))
|
|
|
|
}
|
|
|
|
return buffers
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkIndicesAndReset(t *testing.T, p *Pool, iExp, jExp int) {
|
|
|
|
checkIndices(t, p, iExp, jExp)
|
|
|
|
resetClients(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkIndices(t *testing.T, p *Pool, iExp, jExp int) {
|
|
|
|
i, j := p.getStartIndices()
|
2023-06-28 10:06:23 +00:00
|
|
|
require.Equal(t, [2]int{iExp, jExp}, [2]int{i, j})
|
2023-06-06 06:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resetClients(p *Pool) {
|
|
|
|
resetClientsErrors(p)
|
|
|
|
p.setStartIndices(0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetClientsErrors(p *Pool) {
|
|
|
|
for _, group := range p.innerPools {
|
|
|
|
for _, cl := range group.clients {
|
|
|
|
node := cl.(*treeClientMock)
|
|
|
|
node.err = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setErrors(p *Pool, nodes ...string) {
|
|
|
|
setErrorsBase(p, true, nodes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setNoErrors(p *Pool, nodes ...string) {
|
|
|
|
setErrorsBase(p, false, nodes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setErrorsBase(p *Pool, err bool, nodes ...string) {
|
|
|
|
for _, group := range p.innerPools {
|
|
|
|
for _, cl := range group.clients {
|
|
|
|
node := cl.(*treeClientMock)
|
|
|
|
if containsStr(nodes, node.address) {
|
|
|
|
node.err = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsStr(list []string, item string) bool {
|
|
|
|
for i := range list {
|
|
|
|
if list[i] == item {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|