mirror of
https://gitee.com/milvus-io/milvus.git
synced 2025-12-06 17:18:35 +08:00
issue: #41435 After introducing the caching layer's lazy loading and eviction mechanisms, most parts of a segment won't be loaded into memory or disk immediately, even if the segment is marked as LOADED. This means physical resource usage may be very low. However, we still need to reserve enough resources for the segments marked as LOADED. Thus, the logic of resource usage estimation during segment loading, which based on physcial resource usage only for now, should be changed. To address this issue, we introduced the concept of logical resource usage in this patch. This can be thought of as the base reserved resource for each LOADED segment. A segment’s logical resource usage is derived from its final evictable and inevictable resource usage and calculated as follows: ``` SLR = SFPIER + evitable_cache_ratio * SFPER ``` it also equals to ``` SLR = (SFPIER + SFPER) - (1.0 - evitable_cache_ratio) * SFPER ``` `SLR`: The logical resource usage of a segment. `SFPIER`: The final physical inevictable resource usage of a segment. `SFPER`: The final physical evictable resource usage of a segment. `evitable_cache_ratio`: The ratio of a segment's evictable resources that can be cached locally. The higher the ratio, the more physical memory is reserved for evictable memory. When loading a segment, two types of resource usage are taken into account. First is the estimated maximum physical resource usage: ``` PPR = HPR + CPR + SMPR - SFPER ``` `PPR`: The predicted physical resource usage after the current segment is allowed to load. `HPR`: The physical resource usage obtained from hardware information. `CPR`: The total physical resource usage of segments that have been committed but not yet loaded. When one new segment is allow to load, `CPR' = CPR + (SMR - SER)`. When one of the committed segments is loaded, `CPR' = CPR - (SMR - SER)`. `SMPR`: The maximum physical resource usage of the current segment. `SFPER`: The final physical evictable resource usage of the current segment. Second is the estimated logical resource usage, this check is only valid when eviction is enabled: ``` PLR = LLR + CLR + SLR ``` `PLR`: The predicted logical resource usage after the current segment is allowed to load. `LLR`: The total logical resource usage of all loaded segments. When a new segment is loaded, `LLR` should be updated to `LLR' = LLR + SLR`. `CLR`: The total logical resource usage of segments that have been committed but not yet loaded. When one new segment is allow to load, `CLR' = CLR + SLR`. When one of the committed segments is loaded, `CLR' = CLR - SLR`. `SLR`: The logical resource usage of the current segment. Only when `PPR < PRL && PLR < PRL` (`PRL`: Physical resource limit of the querynode), the segment is allowed to be loaded. --------- Signed-off-by: Shawn Wang <shawn.wang@zilliz.com>
227 lines
6.5 KiB
Go
227 lines
6.5 KiB
Go
package segments
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
|
"github.com/milvus-io/milvus/internal/mocks/util/mock_segcore"
|
|
storage "github.com/milvus-io/milvus/internal/storage"
|
|
"github.com/milvus-io/milvus/internal/util/initcore"
|
|
"github.com/milvus-io/milvus/pkg/v2/proto/datapb"
|
|
"github.com/milvus-io/milvus/pkg/v2/proto/querypb"
|
|
"github.com/milvus-io/milvus/pkg/v2/util/paramtable"
|
|
"github.com/milvus-io/milvus/pkg/v2/util/typeutil"
|
|
)
|
|
|
|
type SegmentSuite struct {
|
|
suite.Suite
|
|
rootPath string
|
|
chunkManager storage.ChunkManager
|
|
|
|
// Data
|
|
manager *Manager
|
|
collectionID int64
|
|
partitionID int64
|
|
segmentID int64
|
|
collection *Collection
|
|
sealed Segment
|
|
growing Segment
|
|
}
|
|
|
|
func (suite *SegmentSuite) SetupSuite() {
|
|
paramtable.Init()
|
|
}
|
|
|
|
func (suite *SegmentSuite) SetupTest() {
|
|
var err error
|
|
ctx := context.Background()
|
|
msgLength := 100
|
|
|
|
suite.rootPath = suite.T().Name()
|
|
chunkManagerFactory := storage.NewTestChunkManagerFactory(paramtable.Get(), suite.rootPath)
|
|
suite.chunkManager, _ = chunkManagerFactory.NewPersistentStorageChunkManager(ctx)
|
|
initcore.InitRemoteChunkManager(paramtable.Get())
|
|
localDataRootPath := filepath.Join(paramtable.Get().LocalStorageCfg.Path.GetValue(), typeutil.QueryNodeRole)
|
|
initcore.InitLocalChunkManager(localDataRootPath)
|
|
initcore.InitMmapManager(paramtable.Get())
|
|
|
|
suite.collectionID = 100
|
|
suite.partitionID = 10
|
|
suite.segmentID = 1
|
|
|
|
suite.manager = NewManager()
|
|
schema := mock_segcore.GenTestCollectionSchema("test-reduce", schemapb.DataType_Int64, true)
|
|
indexMeta := mock_segcore.GenTestIndexMeta(suite.collectionID, schema)
|
|
suite.manager.Collection.PutOrRef(suite.collectionID,
|
|
schema,
|
|
indexMeta,
|
|
&querypb.LoadMetaInfo{
|
|
LoadType: querypb.LoadType_LoadCollection,
|
|
CollectionID: suite.collectionID,
|
|
PartitionIDs: []int64{suite.partitionID},
|
|
},
|
|
)
|
|
suite.collection = suite.manager.Collection.Get(suite.collectionID)
|
|
|
|
suite.sealed, err = NewSegment(ctx,
|
|
suite.collection,
|
|
suite.manager.Segment,
|
|
SegmentTypeSealed,
|
|
0,
|
|
&querypb.SegmentLoadInfo{
|
|
CollectionID: suite.collectionID,
|
|
SegmentID: suite.segmentID,
|
|
PartitionID: suite.partitionID,
|
|
InsertChannel: fmt.Sprintf("by-dev-rootcoord-dml_0_%dv0", suite.collectionID),
|
|
Level: datapb.SegmentLevel_Legacy,
|
|
NumOfRows: int64(msgLength),
|
|
BinlogPaths: []*datapb.FieldBinlog{
|
|
{
|
|
FieldID: 101,
|
|
Binlogs: []*datapb.Binlog{
|
|
{
|
|
LogSize: 10086,
|
|
MemorySize: 10086,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
binlogs, _, err := mock_segcore.SaveBinLog(ctx,
|
|
suite.collectionID,
|
|
suite.partitionID,
|
|
suite.segmentID,
|
|
msgLength,
|
|
schema,
|
|
suite.chunkManager,
|
|
)
|
|
suite.Require().NoError(err)
|
|
g, err := suite.sealed.(*LocalSegment).StartLoadData()
|
|
suite.Require().NoError(err)
|
|
for _, binlog := range binlogs {
|
|
err = suite.sealed.(*LocalSegment).LoadFieldData(ctx, binlog.FieldID, int64(msgLength), binlog)
|
|
suite.Require().NoError(err)
|
|
}
|
|
g.Done(nil)
|
|
|
|
suite.growing, err = NewSegment(ctx,
|
|
suite.collection,
|
|
suite.manager.Segment,
|
|
SegmentTypeGrowing,
|
|
0,
|
|
&querypb.SegmentLoadInfo{
|
|
SegmentID: suite.segmentID + 1,
|
|
CollectionID: suite.collectionID,
|
|
PartitionID: suite.partitionID,
|
|
InsertChannel: fmt.Sprintf("by-dev-rootcoord-dml_0_%dv0", suite.collectionID),
|
|
Level: datapb.SegmentLevel_Legacy,
|
|
},
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
insertMsg, err := mock_segcore.GenInsertMsg(suite.collection.GetCCollection(), suite.partitionID, suite.growing.ID(), msgLength)
|
|
suite.Require().NoError(err)
|
|
insertRecord, err := storage.TransferInsertMsgToInsertRecord(suite.collection.Schema(), insertMsg)
|
|
suite.Require().NoError(err)
|
|
err = suite.growing.Insert(ctx, insertMsg.RowIDs, insertMsg.Timestamps, insertRecord)
|
|
suite.Require().NoError(err)
|
|
|
|
suite.manager.Segment.Put(context.Background(), SegmentTypeSealed, suite.sealed)
|
|
suite.manager.Segment.Put(context.Background(), SegmentTypeGrowing, suite.growing)
|
|
}
|
|
|
|
func (suite *SegmentSuite) TearDownTest() {
|
|
ctx := context.Background()
|
|
suite.sealed.Release(context.Background())
|
|
suite.growing.Release(context.Background())
|
|
DeleteCollection(suite.collection)
|
|
suite.chunkManager.RemoveWithPrefix(ctx, suite.rootPath)
|
|
}
|
|
|
|
func (suite *SegmentSuite) TestLoadInfo() {
|
|
// sealed segment has load info
|
|
suite.NotNil(suite.sealed.LoadInfo())
|
|
// growing segment has no load info
|
|
suite.NotNil(suite.growing.LoadInfo())
|
|
}
|
|
|
|
func (suite *SegmentSuite) TestResourceUsageEstimate() {
|
|
// growing segment has resource usage
|
|
// growing segment can not estimate resource usage
|
|
usage := suite.growing.ResourceUsageEstimate()
|
|
suite.Zero(usage.MemorySize)
|
|
suite.Zero(usage.DiskSize)
|
|
// growing segment has no resource usage
|
|
usage = suite.sealed.ResourceUsageEstimate()
|
|
suite.NotZero(usage.MemorySize)
|
|
suite.Zero(usage.DiskSize)
|
|
suite.Zero(usage.MmapFieldCount)
|
|
}
|
|
|
|
func (suite *SegmentSuite) TestDelete() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
pks := storage.NewInt64PrimaryKeys(2)
|
|
pks.AppendRaw(0, 1)
|
|
|
|
// Test for sealed
|
|
rowNum := suite.sealed.RowNum()
|
|
err := suite.sealed.Delete(ctx, pks, []uint64{1000, 1000})
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(rowNum-int64(pks.Len()), suite.sealed.RowNum())
|
|
suite.Equal(rowNum, suite.sealed.InsertCount())
|
|
|
|
// Test for growing
|
|
rowNum = suite.growing.RowNum()
|
|
err = suite.growing.Delete(ctx, pks, []uint64{1000, 1000})
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(rowNum-int64(pks.Len()), suite.growing.RowNum())
|
|
suite.Equal(rowNum, suite.growing.InsertCount())
|
|
}
|
|
|
|
func (suite *SegmentSuite) TestHasRawData() {
|
|
has := suite.growing.HasRawData(mock_segcore.SimpleFloatVecField.ID)
|
|
suite.True(has)
|
|
has = suite.sealed.HasRawData(mock_segcore.SimpleFloatVecField.ID)
|
|
suite.True(has)
|
|
}
|
|
|
|
func (suite *SegmentSuite) TestCASVersion() {
|
|
segment := suite.sealed
|
|
|
|
curVersion := segment.Version()
|
|
suite.False(segment.CASVersion(curVersion-1, curVersion+1))
|
|
suite.NotEqual(curVersion+1, segment.Version())
|
|
|
|
suite.True(segment.CASVersion(curVersion, curVersion+1))
|
|
suite.Equal(curVersion+1, segment.Version())
|
|
}
|
|
|
|
func (suite *SegmentSuite) TestSegmentRemoveUnusedFieldFiles() {
|
|
}
|
|
|
|
func (suite *SegmentSuite) TestSegmentReleased() {
|
|
suite.sealed.Release(context.Background())
|
|
|
|
sealed := suite.sealed.(*LocalSegment)
|
|
|
|
suite.False(sealed.ptrLock.PinIfNotReleased())
|
|
suite.EqualValues(0, sealed.RowNum())
|
|
suite.EqualValues(0, sealed.MemSize())
|
|
suite.False(sealed.HasRawData(101))
|
|
}
|
|
|
|
func TestSegment(t *testing.T) {
|
|
suite.Run(t, new(SegmentSuite))
|
|
}
|