2021-05-18 19:07:27 +08:00
|
|
|
package sessionutil
|
2021-05-18 11:39:21 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-05-19 18:36:05 +08:00
|
|
|
"math/rand"
|
2021-05-18 11:39:21 +08:00
|
|
|
"sync"
|
|
|
|
"testing"
|
2021-05-19 18:36:05 +08:00
|
|
|
"time"
|
2021-05-18 11:39:21 +08:00
|
|
|
|
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Params paramtable.BaseTable
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
func TestGetServerIDConcurrently(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randVal := rand.Int()
|
2021-05-18 11:39:21 +08:00
|
|
|
Params.Init()
|
|
|
|
|
|
|
|
etcdAddr, err := Params.Load("_EtcdAddress")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := clientv3.New(clientv3.Config{Endpoints: []string{etcdAddr}})
|
|
|
|
assert.Nil(t, err)
|
2021-05-19 18:36:05 +08:00
|
|
|
rootPath := fmt.Sprintf("/%d/test/meta", randVal)
|
2021-05-18 11:39:21 +08:00
|
|
|
etcdKV := etcdkv.NewEtcdKV(cli, rootPath)
|
|
|
|
|
|
|
|
defer etcdKV.Close()
|
|
|
|
defer etcdKV.RemoveWithPrefix("")
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var muList sync.Mutex = sync.Mutex{}
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
self := NewSession("test", "testAddr", false)
|
|
|
|
sm := NewSessionManager(ctx, etcdAddr, rootPath, self)
|
2021-05-18 11:39:21 +08:00
|
|
|
res := make([]int64, 0)
|
|
|
|
|
|
|
|
getIDFunc := func() {
|
2021-05-19 18:36:05 +08:00
|
|
|
sm.checkIDExist()
|
|
|
|
id, err := sm.getServerID()
|
2021-05-18 11:39:21 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
muList.Lock()
|
|
|
|
res = append(res, id)
|
|
|
|
muList.Unlock()
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go getIDFunc()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2021-05-19 18:36:05 +08:00
|
|
|
for i := 1; i <= 10; i++ {
|
2021-05-18 11:39:21 +08:00
|
|
|
assert.Contains(t, res, int64(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
func TestInit(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randVal := rand.Int()
|
2021-05-18 11:39:21 +08:00
|
|
|
Params.Init()
|
|
|
|
|
|
|
|
etcdAddr, err := Params.Load("_EtcdAddress")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := clientv3.New(clientv3.Config{Endpoints: []string{etcdAddr}})
|
|
|
|
assert.Nil(t, err)
|
2021-05-19 18:36:05 +08:00
|
|
|
rootPath := fmt.Sprintf("/%d/test/meta", randVal)
|
2021-05-18 11:39:21 +08:00
|
|
|
etcdKV := etcdkv.NewEtcdKV(cli, rootPath)
|
|
|
|
|
|
|
|
defer etcdKV.Close()
|
|
|
|
defer etcdKV.RemoveWithPrefix("")
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
self := NewSession("test", "testAddr", false)
|
|
|
|
sm := NewSessionManager(ctx, etcdAddr, rootPath, self)
|
|
|
|
sm.Init()
|
|
|
|
assert.NotEqual(t, 0, sm.Self.LeaseID)
|
|
|
|
assert.NotEqual(t, 0, sm.Self.ServerID)
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
func TestUpdateSessions(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randVal := rand.Int()
|
2021-05-18 19:07:27 +08:00
|
|
|
Params.Init()
|
|
|
|
|
|
|
|
etcdAddr, err := Params.Load("_EtcdAddress")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := clientv3.New(clientv3.Config{Endpoints: []string{etcdAddr}})
|
|
|
|
assert.Nil(t, err)
|
2021-05-19 18:36:05 +08:00
|
|
|
rootPath := fmt.Sprintf("/%d/test/meta", randVal)
|
2021-05-18 19:07:27 +08:00
|
|
|
etcdKV := etcdkv.NewEtcdKV(cli, rootPath)
|
|
|
|
|
|
|
|
defer etcdKV.Close()
|
|
|
|
defer etcdKV.RemoveWithPrefix("")
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var muList sync.Mutex = sync.Mutex{}
|
|
|
|
|
|
|
|
self := NewSession("test", "testAddr", false)
|
|
|
|
sm := NewSessionManager(ctx, etcdAddr, rootPath, self)
|
|
|
|
sm.WatchServices(ctx, "test")
|
2021-05-18 19:07:27 +08:00
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
err = sm.UpdateSessions("test")
|
2021-05-18 19:07:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
sessionManagers := make([]*SessionManager, 0)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
getIDFunc := func() {
|
|
|
|
service := NewSession("test", "testAddr", false)
|
|
|
|
singleManager := NewSessionManager(ctx, etcdAddr, rootPath, service)
|
|
|
|
singleManager.Init()
|
|
|
|
muList.Lock()
|
|
|
|
sessionManagers = append(sessionManagers, singleManager)
|
|
|
|
muList.Unlock()
|
|
|
|
wg.Done()
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go getIDFunc()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
assert.Equal(t, len(sm.GetSessions()), 10)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
sessions := sm.GetSessions()
|
2021-05-18 11:39:21 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-19 18:36:05 +08:00
|
|
|
assert.Equal(t, len(sessions), 10)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
etcdKV.RemoveWithPrefix("")
|
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
return len(sm.GetSessions()) == 0
|
|
|
|
}, 10*time.Second, 100*time.Millisecond)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
|
|
|
}
|