2021-10-11 14:10:48 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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 storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2021-10-12 17:00:34 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-10-11 14:10:48 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-11-02 18:16:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/common"
|
2021-10-11 14:10:48 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/uniquegenerator"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
memkv "github.com/milvus-io/milvus/internal/kv/mem"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockLessHeaderDataKV struct {
|
2021-10-12 19:47:08 +08:00
|
|
|
kv.BaseKV
|
2021-10-11 14:10:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (kv *mockLessHeaderDataKV) LoadPartial(key string, start, end int64) ([]byte, error) {
|
|
|
|
header := &baseEventHeader{}
|
|
|
|
|
|
|
|
headerSize := binary.Size(header)
|
|
|
|
mockSize := headerSize - 1
|
|
|
|
|
|
|
|
ret := make([]byte, mockSize)
|
|
|
|
_, _ = rand.Read(ret)
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:34:21 +08:00
|
|
|
func (kv *mockLessHeaderDataKV) GetSize(key string) (int64, error) {
|
|
|
|
return 0, errors.New("less header")
|
|
|
|
}
|
|
|
|
|
2021-10-11 14:10:48 +08:00
|
|
|
func newMockLessHeaderDataKV() *mockLessHeaderDataKV {
|
|
|
|
return &mockLessHeaderDataKV{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockWrongHeaderDataKV struct {
|
2021-10-12 19:47:08 +08:00
|
|
|
kv.BaseKV
|
2021-10-11 14:10:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (kv *mockWrongHeaderDataKV) LoadPartial(key string, start, end int64) ([]byte, error) {
|
|
|
|
header := &baseEventHeader{}
|
|
|
|
|
|
|
|
header.EventLength = -1
|
|
|
|
header.NextPosition = -1
|
|
|
|
|
|
|
|
buffer := bytes.Buffer{}
|
2021-11-02 18:16:32 +08:00
|
|
|
_ = binary.Write(&buffer, common.Endian, header)
|
2021-10-11 14:10:48 +08:00
|
|
|
|
|
|
|
return buffer.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:34:21 +08:00
|
|
|
func (kv *mockWrongHeaderDataKV) GetSize(key string) (int64, error) {
|
|
|
|
return 0, errors.New("wrong header")
|
|
|
|
}
|
|
|
|
|
2021-10-11 14:10:48 +08:00
|
|
|
func newMockWrongHeaderDataKV() kv.DataKV {
|
|
|
|
return &mockWrongHeaderDataKV{}
|
|
|
|
}
|
|
|
|
|
2021-10-11 18:20:30 +08:00
|
|
|
func TestGetBinlogSize(t *testing.T) {
|
2021-10-11 14:10:48 +08:00
|
|
|
memoryKV := memkv.NewMemoryKV()
|
|
|
|
defer memoryKV.Close()
|
|
|
|
|
2021-10-11 18:20:30 +08:00
|
|
|
key := "TestGetBinlogSize"
|
2021-10-11 14:10:48 +08:00
|
|
|
|
|
|
|
var size int64
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// key not in memoryKV
|
2021-10-11 18:20:30 +08:00
|
|
|
size, err = GetBinlogSize(memoryKV, key)
|
2021-10-11 14:10:48 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Zero(t, size)
|
|
|
|
|
|
|
|
// normal binlog key, for example, index binlog
|
|
|
|
indexBuildID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
version := int64(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
collectionID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
partitionID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
segmentID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
fieldID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
indexName := funcutil.GenRandomStr()
|
|
|
|
indexID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
indexParams := make(map[string]string)
|
|
|
|
indexParams["index_type"] = "IVF_FLAT"
|
|
|
|
datas := []*Blob{
|
|
|
|
{
|
|
|
|
Key: "ivf1",
|
|
|
|
Value: []byte{1, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "ivf2",
|
|
|
|
Value: []byte{4, 5, 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "large",
|
|
|
|
Value: []byte(funcutil.RandomString(maxLengthPerRowOfIndexFile + 1)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
codec := NewIndexFileBinlogCodec()
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
serializedBlobs, err := codec.Serialize(indexBuildID, version, collectionID, partitionID, segmentID, fieldID, indexParams, indexName, indexID, datas)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for _, blob := range serializedBlobs {
|
|
|
|
err = memoryKV.Save(blob.Key, string(blob.Value))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-10-11 18:20:30 +08:00
|
|
|
size, err = GetBinlogSize(memoryKV, blob.Key)
|
2021-10-11 14:10:48 +08:00
|
|
|
assert.Nil(t, err)
|
2021-10-26 15:34:21 +08:00
|
|
|
assert.Equal(t, size, int64(len(blob.Value)))
|
2021-10-11 14:10:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cover case that failed to read event header
|
2021-10-11 18:20:30 +08:00
|
|
|
func TestGetBinlogSize_less_header(t *testing.T) {
|
2021-10-11 14:10:48 +08:00
|
|
|
mockKV := newMockLessHeaderDataKV()
|
|
|
|
|
2021-10-11 18:20:30 +08:00
|
|
|
key := "TestGetBinlogSize_less_header"
|
2021-10-11 14:10:48 +08:00
|
|
|
|
2021-10-11 18:20:30 +08:00
|
|
|
_, err := GetBinlogSize(mockKV, key)
|
2021-10-11 14:10:48 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cover case that file not in binlog format
|
2021-10-11 18:20:30 +08:00
|
|
|
func TestGetBinlogSize_not_in_binlog_format(t *testing.T) {
|
2021-10-11 14:10:48 +08:00
|
|
|
mockKV := newMockWrongHeaderDataKV()
|
|
|
|
|
2021-10-11 18:20:30 +08:00
|
|
|
key := "TestGetBinlogSize_not_in_binlog_format"
|
2021-10-11 14:10:48 +08:00
|
|
|
|
2021-10-11 18:20:30 +08:00
|
|
|
_, err := GetBinlogSize(mockKV, key)
|
2021-10-11 14:10:48 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2021-10-12 17:00:34 +08:00
|
|
|
|
|
|
|
func TestEstimateMemorySize(t *testing.T) {
|
|
|
|
memoryKV := memkv.NewMemoryKV()
|
|
|
|
defer memoryKV.Close()
|
|
|
|
|
|
|
|
key := "TestEstimateMemorySize"
|
|
|
|
|
|
|
|
var size int64
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// key not in memoryKV
|
|
|
|
_, err = EstimateMemorySize(memoryKV, key)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// normal binlog key, for example, index binlog
|
|
|
|
indexBuildID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
version := int64(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
collectionID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
partitionID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
segmentID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
fieldID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
indexName := funcutil.GenRandomStr()
|
|
|
|
indexID := UniqueID(uniquegenerator.GetUniqueIntGeneratorIns().GetInt())
|
|
|
|
indexParams := make(map[string]string)
|
|
|
|
indexParams["index_type"] = "IVF_FLAT"
|
|
|
|
datas := []*Blob{
|
|
|
|
{
|
|
|
|
Key: "ivf1",
|
|
|
|
Value: []byte{1, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "ivf2",
|
|
|
|
Value: []byte{4, 5, 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "large",
|
|
|
|
Value: []byte(funcutil.RandomString(maxLengthPerRowOfIndexFile + 1)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
codec := NewIndexFileBinlogCodec()
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
serializedBlobs, err := codec.Serialize(indexBuildID, version, collectionID, partitionID, segmentID, fieldID, indexParams, indexName, indexID, datas)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for _, blob := range serializedBlobs {
|
|
|
|
err = memoryKV.Save(blob.Key, string(blob.Value))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(blob.Value)
|
|
|
|
|
|
|
|
_, _ = readMagicNumber(buf)
|
2021-10-13 13:22:33 +08:00
|
|
|
desc, _ := ReadDescriptorEvent(buf)
|
2021-10-12 17:00:34 +08:00
|
|
|
|
|
|
|
size, err = EstimateMemorySize(memoryKV, blob.Key)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, fmt.Sprintf("%v", desc.Extras[originalSizeKey]), fmt.Sprintf("%v", size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cover case that failed to read event header
|
|
|
|
func TestEstimateMemorySize_less_header(t *testing.T) {
|
|
|
|
mockKV := newMockLessHeaderDataKV()
|
|
|
|
|
|
|
|
key := "TestEstimateMemorySize_less_header"
|
|
|
|
|
|
|
|
_, err := EstimateMemorySize(mockKV, key)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cover case that file not in binlog format
|
|
|
|
func TestEstimateMemorySize_not_in_binlog_format(t *testing.T) {
|
|
|
|
mockKV := newMockWrongHeaderDataKV()
|
|
|
|
|
|
|
|
key := "TestEstimateMemorySize_not_in_binlog_format"
|
|
|
|
|
|
|
|
_, err := EstimateMemorySize(mockKV, key)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockFailedToGetDescDataKV struct {
|
2021-10-12 19:47:08 +08:00
|
|
|
kv.BaseKV
|
2021-10-12 17:00:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (kv *mockFailedToGetDescDataKV) LoadPartial(key string, start, end int64) ([]byte, error) {
|
|
|
|
header := &eventHeader{}
|
|
|
|
header.EventLength = 20
|
|
|
|
headerSize := binary.Size(header)
|
|
|
|
|
|
|
|
if end-start > int64(headerSize) {
|
|
|
|
return nil, errors.New("mock failed to get desc data")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
2021-11-02 18:16:32 +08:00
|
|
|
_ = binary.Write(&buf, common.Endian, header)
|
2021-10-12 17:00:34 +08:00
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:34:21 +08:00
|
|
|
func (kv *mockFailedToGetDescDataKV) GetSize(key string) (int64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:00:34 +08:00
|
|
|
func newMockFailedToGetDescDataKV() *mockFailedToGetDescDataKV {
|
|
|
|
return &mockFailedToGetDescDataKV{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cover case that failed to get descriptor event content
|
|
|
|
func TestEstimateMemorySize_failed_to_load_desc(t *testing.T) {
|
|
|
|
mockKV := newMockFailedToGetDescDataKV()
|
|
|
|
|
|
|
|
key := "TestEstimateMemorySize_failed_to_load_desc"
|
|
|
|
|
|
|
|
_, err := EstimateMemorySize(mockKV, key)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockLessDescDataKV struct {
|
2021-10-12 19:47:08 +08:00
|
|
|
kv.BaseKV
|
2021-10-12 17:00:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (kv *mockLessDescDataKV) LoadPartial(key string, start, end int64) ([]byte, error) {
|
|
|
|
header := &baseEventHeader{}
|
|
|
|
header.EventLength = 20
|
|
|
|
|
|
|
|
buffer := bytes.Buffer{}
|
2021-11-02 18:16:32 +08:00
|
|
|
_ = binary.Write(&buffer, common.Endian, header)
|
2021-10-12 17:00:34 +08:00
|
|
|
|
|
|
|
// no event data
|
|
|
|
return buffer.Bytes(), nil
|
|
|
|
|
|
|
|
/*
|
|
|
|
desc := &descriptorEvent{}
|
|
|
|
desc.ExtraLength = 2
|
|
|
|
desc.ExtraBytes = []byte{1, 2}
|
|
|
|
buffer := bytes.Buffer{}
|
2021-11-02 18:16:32 +08:00
|
|
|
_ = binary.Write(&buffer, common.Endian, desc)
|
2021-10-12 17:00:34 +08:00
|
|
|
// extra not in json format
|
|
|
|
return buffer.Bytes(), nil
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:34:21 +08:00
|
|
|
func (kv *mockLessDescDataKV) GetSize(key string) (int64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:00:34 +08:00
|
|
|
func newMockLessDescDataKV() *mockLessDescDataKV {
|
|
|
|
return &mockLessDescDataKV{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEstimateMemorySize_less_desc_data(t *testing.T) {
|
|
|
|
mockKV := newMockLessDescDataKV()
|
|
|
|
|
|
|
|
key := "TestEstimateMemorySize_less_desc_data"
|
|
|
|
|
|
|
|
_, err := EstimateMemorySize(mockKV, key)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockOriginalSizeDataKV struct {
|
2021-10-12 19:47:08 +08:00
|
|
|
kv.BaseKV
|
2021-10-12 17:00:34 +08:00
|
|
|
impl func(key string, start, end int64) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kv *mockOriginalSizeDataKV) LoadPartial(key string, start, end int64) ([]byte, error) {
|
|
|
|
if kv.impl != nil {
|
|
|
|
return kv.impl(key, start, end)
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:34:21 +08:00
|
|
|
func (kv *mockOriginalSizeDataKV) GetSize(key string) (int64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:00:34 +08:00
|
|
|
func newMockOriginalSizeDataKV() *mockOriginalSizeDataKV {
|
|
|
|
return &mockOriginalSizeDataKV{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEstimateMemorySize_no_original_size(t *testing.T) {
|
|
|
|
mockKV := newMockOriginalSizeDataKV()
|
|
|
|
mockKV.impl = func(key string, start, end int64) ([]byte, error) {
|
|
|
|
desc := &descriptorEvent{}
|
|
|
|
desc.descriptorEventHeader.EventLength = 20
|
|
|
|
desc.descriptorEventData = *newDescriptorEventData()
|
|
|
|
extra := make(map[string]interface{})
|
|
|
|
extra["key"] = "value"
|
|
|
|
extraBytes, _ := json.Marshal(extra)
|
|
|
|
desc.ExtraBytes = extraBytes
|
|
|
|
desc.ExtraLength = int32(len(extraBytes))
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
_ = desc.descriptorEventHeader.Write(&buf)
|
|
|
|
_ = desc.descriptorEventData.Write(&buf)
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key := "TestEstimateMemorySize_no_original_size"
|
|
|
|
|
|
|
|
_, err := EstimateMemorySize(mockKV, key)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEstimateMemorySize_cannot_convert_original_size_to_int(t *testing.T) {
|
|
|
|
mockKV := newMockOriginalSizeDataKV()
|
|
|
|
mockKV.impl = func(key string, start, end int64) ([]byte, error) {
|
|
|
|
desc := &descriptorEvent{}
|
|
|
|
desc.descriptorEventHeader.EventLength = 20
|
|
|
|
desc.descriptorEventData = *newDescriptorEventData()
|
|
|
|
extra := make(map[string]interface{})
|
|
|
|
extra[originalSizeKey] = "value"
|
|
|
|
extraBytes, _ := json.Marshal(extra)
|
|
|
|
desc.ExtraBytes = extraBytes
|
|
|
|
desc.ExtraLength = int32(len(extraBytes))
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
_ = desc.descriptorEventHeader.Write(&buf)
|
|
|
|
_ = desc.descriptorEventData.Write(&buf)
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key := "TestEstimateMemorySize_cannot_convert_original_size_to_int"
|
|
|
|
|
|
|
|
_, err := EstimateMemorySize(mockKV, key)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|