2021-05-18 19:07:27 +08:00
|
|
|
package sessionutil
|
2021-05-18 11:39:21 +08:00
|
|
|
|
|
|
|
import (
|
2021-08-27 15:37:56 +08:00
|
|
|
"context"
|
2021-05-26 20:14:30 +08:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2021-05-25 20:15:46 +08:00
|
|
|
"strconv"
|
2021-06-11 22:04:41 +08:00
|
|
|
"strings"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Params paramtable.BaseTable
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
func TestGetServerIDConcurrently(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-05-18 11:39:21 +08:00
|
|
|
Params.Init()
|
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
endpoints, err := Params.Load("_EtcdEndpoints")
|
2021-05-26 20:14:30 +08:00
|
|
|
metaRoot := fmt.Sprintf("%d/%s", rand.Int(), DefaultServiceRoot)
|
2021-05-18 11:39:21 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
etcdEndpoints := strings.Split(endpoints, ",")
|
2021-08-13 11:04:09 +08:00
|
|
|
etcdKV, err := etcdkv.NewEtcdKV(etcdEndpoints, metaRoot)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = etcdKV.RemoveWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
|
|
|
defer etcdKV.Close()
|
|
|
|
defer etcdKV.RemoveWithPrefix("")
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var muList sync.Mutex = sync.Mutex{}
|
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
s := NewSession(ctx, metaRoot, etcdEndpoints)
|
2021-05-18 11:39:21 +08:00
|
|
|
res := make([]int64, 0)
|
|
|
|
|
|
|
|
getIDFunc := func() {
|
2021-05-21 19:28:52 +08:00
|
|
|
s.checkIDExist()
|
|
|
|
id, err := s.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()
|
2021-05-18 11:39:21 +08:00
|
|
|
Params.Init()
|
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
endpoints, err := Params.Load("_EtcdEndpoints")
|
2021-05-18 11:39:21 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-13 11:04:09 +08:00
|
|
|
metaRoot := fmt.Sprintf("%d/%s", rand.Int(), DefaultServiceRoot)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
etcdEndpoints := strings.Split(endpoints, ",")
|
2021-08-13 11:04:09 +08:00
|
|
|
etcdKV, err := etcdkv.NewEtcdKV(etcdEndpoints, metaRoot)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = etcdKV.RemoveWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
|
|
|
defer etcdKV.Close()
|
|
|
|
defer etcdKV.RemoveWithPrefix("")
|
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
s := NewSession(ctx, metaRoot, etcdEndpoints)
|
2021-05-25 20:15:46 +08:00
|
|
|
s.Init("inittest", "testAddr", false)
|
2021-05-24 09:44:49 +08:00
|
|
|
assert.NotEqual(t, int64(0), s.leaseID)
|
|
|
|
assert.NotEqual(t, int64(0), s.ServerID)
|
2021-05-25 20:15:46 +08:00
|
|
|
sessions, _, err := s.GetSessions("inittest")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Contains(t, sessions, "inittest-"+strconv.FormatInt(s.ServerID, 10))
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
func TestUpdateSessions(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-05-18 19:07:27 +08:00
|
|
|
Params.Init()
|
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
endpoints, err := Params.Load("_EtcdEndpoints")
|
2021-05-18 19:07:27 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
etcdEndpoints := strings.Split(endpoints, ",")
|
2021-05-26 20:14:30 +08:00
|
|
|
metaRoot := fmt.Sprintf("%d/%s", rand.Int(), DefaultServiceRoot)
|
2021-08-13 11:04:09 +08:00
|
|
|
etcdKV, err := etcdkv.NewEtcdKV(etcdEndpoints, "")
|
|
|
|
assert.NoError(t, err)
|
2021-05-18 19:07:27 +08:00
|
|
|
|
|
|
|
defer etcdKV.Close()
|
|
|
|
defer etcdKV.RemoveWithPrefix("")
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var muList sync.Mutex = sync.Mutex{}
|
|
|
|
|
2021-06-11 22:04:41 +08:00
|
|
|
s := NewSession(ctx, metaRoot, etcdEndpoints)
|
2021-05-18 19:07:27 +08:00
|
|
|
|
2021-05-25 20:15:46 +08:00
|
|
|
sessions, rev, err := s.GetSessions("test")
|
2021-05-18 19:07:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-21 19:28:52 +08:00
|
|
|
assert.Equal(t, len(sessions), 0)
|
2021-05-25 20:15:46 +08:00
|
|
|
eventCh := s.WatchServices("test", rev)
|
2021-05-18 19:07:27 +08:00
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
sList := []*Session{}
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
getIDFunc := func() {
|
2021-06-11 22:04:41 +08:00
|
|
|
singleS := NewSession(ctx, metaRoot, etcdEndpoints)
|
2021-05-24 09:44:49 +08:00
|
|
|
singleS.Init("test", "testAddr", false)
|
2021-05-19 18:36:05 +08:00
|
|
|
muList.Lock()
|
2021-05-21 19:28:52 +08:00
|
|
|
sList = append(sList, singleS)
|
2021-05-19 18:36:05 +08:00
|
|
|
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-20 15:07:25 +08:00
|
|
|
assert.Eventually(t, func() bool {
|
2021-05-25 20:15:46 +08:00
|
|
|
sessions, _, _ := s.GetSessions("test")
|
2021-05-21 19:28:52 +08:00
|
|
|
return len(sessions) == 10
|
2021-05-20 15:07:25 +08:00
|
|
|
}, 10*time.Second, 100*time.Millisecond)
|
2021-05-25 20:15:46 +08:00
|
|
|
notExistSessions, _, _ := s.GetSessions("testt")
|
2021-05-21 19:28:52 +08:00
|
|
|
assert.Equal(t, len(notExistSessions), 0)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-26 20:14:30 +08:00
|
|
|
etcdKV.RemoveWithPrefix(metaRoot)
|
2021-05-19 18:36:05 +08:00
|
|
|
assert.Eventually(t, func() bool {
|
2021-05-25 20:15:46 +08:00
|
|
|
sessions, _, _ := s.GetSessions("test")
|
2021-05-21 19:28:52 +08:00
|
|
|
return len(sessions) == 0
|
2021-05-19 18:36:05 +08:00
|
|
|
}, 10*time.Second, 100*time.Millisecond)
|
2021-05-18 11:39:21 +08:00
|
|
|
|
2021-05-25 20:15:46 +08:00
|
|
|
sessionEvents := []*SessionEvent{}
|
|
|
|
addEventLen := 0
|
|
|
|
delEventLen := 0
|
|
|
|
eventLength := len(eventCh)
|
|
|
|
for i := 0; i < eventLength; i++ {
|
|
|
|
sessionEvent := <-eventCh
|
|
|
|
if sessionEvent.EventType == SessionAddEvent {
|
|
|
|
addEventLen++
|
|
|
|
}
|
|
|
|
if sessionEvent.EventType == SessionDelEvent {
|
|
|
|
delEventLen++
|
|
|
|
}
|
|
|
|
sessionEvents = append(sessionEvents, sessionEvent)
|
2021-05-20 15:07:25 +08:00
|
|
|
}
|
2021-05-25 20:15:46 +08:00
|
|
|
assert.Equal(t, len(sessionEvents), 20)
|
|
|
|
assert.Equal(t, addEventLen, 10)
|
|
|
|
assert.Equal(t, delEventLen, 10)
|
2021-05-18 11:39:21 +08:00
|
|
|
}
|
2021-09-23 18:29:55 +08:00
|
|
|
|
|
|
|
func TestSessionLivenessCheck(t *testing.T) {
|
|
|
|
s := &Session{}
|
|
|
|
ctx := context.Background()
|
|
|
|
ch := make(chan bool)
|
2021-10-14 16:40:35 +08:00
|
|
|
s.liveCh = ch
|
2021-09-23 18:29:55 +08:00
|
|
|
signal := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
flag := false
|
|
|
|
|
2021-10-14 16:40:35 +08:00
|
|
|
go s.LivenessCheck(ctx, func() {
|
2021-09-23 18:29:55 +08:00
|
|
|
flag = true
|
|
|
|
signal <- struct{}{}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.False(t, flag)
|
|
|
|
ch <- true
|
|
|
|
|
|
|
|
assert.False(t, flag)
|
|
|
|
close(ch)
|
|
|
|
|
|
|
|
<-signal
|
|
|
|
assert.True(t, flag)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
cancel()
|
|
|
|
ch = make(chan bool)
|
2021-10-14 16:40:35 +08:00
|
|
|
s.liveCh = ch
|
2021-09-23 18:29:55 +08:00
|
|
|
flag = false
|
|
|
|
|
2021-10-14 16:40:35 +08:00
|
|
|
go s.LivenessCheck(ctx, func() {
|
2021-09-23 18:29:55 +08:00
|
|
|
flag = true
|
|
|
|
signal <- struct{}{}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.False(t, flag)
|
|
|
|
}
|