milvus/internal/querynodev2/server_test.go
chyezh 77e123762f
enhance: add graceful stop timeout to avoid node stop hang under extreme cases (#30320)
1. add coordinator and proxy graceful stop timeout to 5s.
3. add other work node graceful stop timeout to 900s, and we should
potentially change this to 600s when graceful stop is smooth
4. change the order of datacoord component while stop.
5. `LivenessCheck` do not perform graceful shutdown now. 

issue: https://github.com/milvus-io/milvus/issues/30310
pr: #30317
also see: https://github.com/milvus-io/milvus/pull/30306

---------

Signed-off-by: chyezh <chyezh@outlook.com>
2024-01-27 08:45:02 +08:00

250 lines
8.1 KiB
Go

// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package querynodev2
import (
"context"
"os"
"sync/atomic"
"testing"
"time"
"github.com/cockroachdb/errors"
"github.com/spf13/viper"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
"github.com/milvus-io/milvus/internal/proto/querypb"
"github.com/milvus-io/milvus/internal/querynodev2/optimizers"
"github.com/milvus-io/milvus/internal/querynodev2/segments"
"github.com/milvus-io/milvus/internal/storage"
"github.com/milvus-io/milvus/internal/util/dependency"
"github.com/milvus-io/milvus/internal/util/sessionutil"
"github.com/milvus-io/milvus/pkg/util/conc"
"github.com/milvus-io/milvus/pkg/util/etcd"
"github.com/milvus-io/milvus/pkg/util/paramtable"
)
type QueryNodeSuite struct {
suite.Suite
// data
address string
// dependency
params *paramtable.ComponentParam
node *QueryNode
etcd *clientv3.Client
chunkManagerFactory *storage.ChunkManagerFactory
// mock
factory *dependency.MockFactory
}
func (suite *QueryNodeSuite) SetupSuite() {
suite.address = "test-address"
}
func (suite *QueryNodeSuite) SetupTest() {
var err error
paramtable.Init()
suite.params = paramtable.Get()
suite.params.Save(suite.params.QueryNodeCfg.GCEnabled.Key, "false")
// mock factory
suite.factory = dependency.NewMockFactory(suite.T())
suite.chunkManagerFactory = storage.NewChunkManagerFactory("local", storage.RootPath("/tmp/milvus_test"))
// new node
suite.node = NewQueryNode(context.Background(), suite.factory)
// init etcd
suite.etcd, err = etcd.GetEtcdClient(
suite.params.EtcdCfg.UseEmbedEtcd.GetAsBool(),
suite.params.EtcdCfg.EtcdUseSSL.GetAsBool(),
suite.params.EtcdCfg.Endpoints.GetAsStrings(),
suite.params.EtcdCfg.EtcdTLSCert.GetValue(),
suite.params.EtcdCfg.EtcdTLSKey.GetValue(),
suite.params.EtcdCfg.EtcdTLSCACert.GetValue(),
suite.params.EtcdCfg.EtcdTLSMinVersion.GetValue())
suite.NoError(err)
}
func (suite *QueryNodeSuite) TearDownTest() {
suite.etcd.Close()
os.RemoveAll("/tmp/milvus-test")
}
func (suite *QueryNodeSuite) TestBasic() {
// mock expect
suite.factory.EXPECT().Init(mock.Anything).Return()
suite.factory.EXPECT().NewPersistentStorageChunkManager(mock.Anything).Return(suite.chunkManagerFactory.NewPersistentStorageChunkManager(context.Background()))
var err error
suite.node.SetEtcdClient(suite.etcd)
err = suite.node.Init()
suite.NoError(err)
// node should be unhealthy before node start
suite.False(suite.node.lifetime.GetState() == commonpb.StateCode_Healthy)
// start node
err = suite.node.Start()
suite.NoError(err)
// node should be healthy after node start
suite.True(suite.node.lifetime.GetState() == commonpb.StateCode_Healthy)
// register node to etcd
err = suite.node.Register()
suite.NoError(err)
// set and get address
suite.node.SetAddress(suite.address)
address := suite.node.GetAddress()
suite.Equal(suite.address, address)
// close node
err = suite.node.Stop()
suite.NoError(err)
// node should be unhealthy after node stop
suite.False(suite.node.lifetime.GetState() == commonpb.StateCode_Healthy)
}
func (suite *QueryNodeSuite) TestInit_RemoteChunkManagerFailed() {
var err error
suite.node.SetEtcdClient(suite.etcd)
// init remote chunk manager failed
suite.factory.EXPECT().Init(mock.Anything).Return()
suite.factory.EXPECT().NewPersistentStorageChunkManager(mock.Anything).Return(nil, errors.New("mock error"))
err = suite.node.Init()
suite.Error(err)
}
func (suite *QueryNodeSuite) TestInit_VactorChunkManagerFailed() {
var err error
suite.node.SetEtcdClient(suite.etcd)
// init vactor chunk manager failed
suite.factory.EXPECT().Init(mock.Anything).Return()
suite.factory.EXPECT().NewPersistentStorageChunkManager(mock.Anything).Return(suite.chunkManagerFactory.NewPersistentStorageChunkManager(context.Background())).Once()
suite.factory.EXPECT().NewPersistentStorageChunkManager(mock.Anything).Return(nil, errors.New("mock error")).Once()
err = suite.node.Init()
suite.Error(err)
}
func (suite *QueryNodeSuite) TestInit_QueryHook() {
// mock expect
suite.factory.EXPECT().Init(mock.Anything).Return()
suite.factory.EXPECT().NewPersistentStorageChunkManager(mock.Anything).Return(suite.chunkManagerFactory.NewPersistentStorageChunkManager(context.Background()))
var err error
suite.node.SetEtcdClient(suite.etcd)
err = suite.node.Init()
suite.NoError(err)
mockHook := optimizers.NewMockQueryHook(suite.T())
suite.node.queryHook = mockHook
suite.node.handleQueryHookEvent()
yamlWriter := viper.New()
yamlWriter.SetConfigFile("../../configs/milvus.yaml")
yamlWriter.ReadInConfig()
var x1, x2, x3 int32
suite.Equal(atomic.LoadInt32(&x1), int32(0))
suite.Equal(atomic.LoadInt32(&x2), int32(0))
suite.Equal(atomic.LoadInt32(&x3), int32(0))
mockHook.EXPECT().InitTuningConfig(mock.Anything).Run(func(params map[string]string) {
atomic.StoreInt32(&x1, 6)
}).Return(nil)
// create tuning conf
yamlWriter.Set("autoIndex.params.tuning.1238", "xxxx")
yamlWriter.WriteConfig()
suite.Eventually(func() bool {
return atomic.LoadInt32(&x1) == int32(6)
}, 20*time.Second, time.Second)
mockHook.EXPECT().Init(mock.Anything).Run(func(params string) {
atomic.StoreInt32(&x2, 5)
}).Return(nil)
yamlWriter.Set("autoIndex.params.search", "aaaa")
yamlWriter.WriteConfig()
suite.Eventually(func() bool {
return atomic.LoadInt32(&x2) == int32(5)
}, 20*time.Second, time.Second)
yamlWriter.Set("autoIndex.params.search", "")
yamlWriter.WriteConfig()
atomic.StoreInt32(&x1, 0)
suite.Equal(atomic.LoadInt32(&x1), int32(0))
// update tuning conf
yamlWriter.Set("autoIndex.params.tuning.1238", "yyyy")
yamlWriter.WriteConfig()
suite.Eventually(func() bool {
return atomic.LoadInt32(&x1) == int32(6)
}, 20*time.Second, time.Second)
mockHook.EXPECT().DeleteTuningConfig(mock.Anything).Run(func(params string) {
atomic.StoreInt32(&x3, 7)
}).Return(nil)
// delete tuning conf
yamlWriter.Set("autoIndex.params.tuning", "")
yamlWriter.WriteConfig()
suite.Eventually(func() bool {
return atomic.LoadInt32(&x3) == int32(7)
}, 20*time.Second, time.Second)
}
func (suite *QueryNodeSuite) TestStop() {
suite.node.manager = segments.NewManager()
mockSession := sessionutil.NewMockSession(suite.T())
mockSession.EXPECT().GoingStop().Return(nil)
mockSession.EXPECT().Stop()
suite.node.session = mockSession
schema := segments.GenTestCollectionSchema("test_stop", schemapb.DataType_Int64)
collection := segments.NewCollection(1, schema, nil, querypb.LoadType_LoadCollection)
segment, err := segments.NewSegment(context.Background(), collection, 100, 10, 1, "test_stop_channel", segments.SegmentTypeSealed, 1, nil, nil)
suite.NoError(err)
suite.node.manager.Segment.Put(segments.SegmentTypeSealed, segment)
future := conc.Go(func() (struct{}, error) {
return struct{}{}, suite.node.Stop()
})
// Graceful stop, should wait for all segments to be released
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
select {
case <-future.Inner():
suite.FailNow("stop should be blocked")
case <-ctx.Done():
}
suite.node.manager.Segment.Clear()
_, err = future.Await()
suite.NoError(err)
suite.True(suite.node.manager.Segment.Empty())
}
func TestQueryNode(t *testing.T) {
suite.Run(t, new(QueryNodeSuite))
}