2021-12-28 09:44:06 +08:00
|
|
|
// 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
|
2021-05-20 18:38:45 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-28 09:44:06 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-05-20 18:38:45 +08:00
|
|
|
//
|
2021-12-28 09:44:06 +08:00
|
|
|
// 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.
|
2021-05-20 18:38:45 +08:00
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2024-09-19 10:57:12 +08:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2021-05-20 18:38:45 +08:00
|
|
|
"encoding/json"
|
2023-05-29 10:21:28 +08:00
|
|
|
"fmt"
|
2024-09-19 10:57:12 +08:00
|
|
|
"math"
|
2021-10-13 10:22:33 +08:00
|
|
|
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
"go.uber.org/zap"
|
2024-09-19 10:57:12 +08:00
|
|
|
"golang.org/x/exp/maps"
|
2023-09-21 09:45:27 +08:00
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/bloomfilter"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/common"
|
2023-05-29 10:21:28 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
2023-06-20 16:40:42 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2023-12-28 18:10:46 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2024-09-19 10:57:12 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2021-05-20 18:38:45 +08:00
|
|
|
)
|
|
|
|
|
2024-09-19 10:57:12 +08:00
|
|
|
// PrimaryKeyStats contains rowsWithToken data for pk column
|
2022-03-25 14:27:25 +08:00
|
|
|
type PrimaryKeyStats struct {
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
FieldID int64 `json:"fieldID"`
|
|
|
|
Max int64 `json:"max"` // useless, will delete
|
|
|
|
Min int64 `json:"min"` // useless, will delete
|
|
|
|
BFType bloomfilter.BFType `json:"bfType"`
|
|
|
|
BF bloomfilter.BloomFilterInterface `json:"bf"`
|
|
|
|
PkType int64 `json:"pkType"`
|
|
|
|
MaxPk PrimaryKey `json:"maxPk"`
|
|
|
|
MinPk PrimaryKey `json:"minPk"`
|
2022-03-25 14:27:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshal bytes to PrimaryKeyStats
|
|
|
|
func (stats *PrimaryKeyStats) UnmarshalJSON(data []byte) error {
|
|
|
|
var messageMap map[string]*json.RawMessage
|
|
|
|
err := json.Unmarshal(data, &messageMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(*messageMap["fieldID"], &stats.FieldID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.PkType = int64(schemapb.DataType_Int64)
|
2022-03-30 10:15:28 +08:00
|
|
|
if value, ok := messageMap["pkType"]; ok && value != nil {
|
|
|
|
var typeValue int64
|
|
|
|
err = json.Unmarshal(*value, &typeValue)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// valid pkType
|
|
|
|
if typeValue > 0 {
|
|
|
|
stats.PkType = typeValue
|
|
|
|
}
|
2022-03-25 14:27:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch schemapb.DataType(stats.PkType) {
|
|
|
|
case schemapb.DataType_Int64:
|
|
|
|
stats.MaxPk = &Int64PrimaryKey{}
|
|
|
|
stats.MinPk = &Int64PrimaryKey{}
|
|
|
|
|
|
|
|
// Compatible with versions that only support int64 type primary keys
|
|
|
|
err = json.Unmarshal(*messageMap["max"], &stats.Max)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = stats.MaxPk.SetValue(stats.Max)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(*messageMap["min"], &stats.Min)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = stats.MinPk.SetValue(stats.Min)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case schemapb.DataType_VarChar:
|
2022-04-02 17:43:29 +08:00
|
|
|
stats.MaxPk = &VarCharPrimaryKey{}
|
|
|
|
stats.MinPk = &VarCharPrimaryKey{}
|
2023-05-29 10:21:28 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid PK Data Type")
|
2022-03-25 14:27:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if maxPkMessage, ok := messageMap["maxPk"]; ok && maxPkMessage != nil {
|
|
|
|
err = json.Unmarshal(*maxPkMessage, stats.MaxPk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if minPkMessage, ok := messageMap["minPk"]; ok && minPkMessage != nil {
|
|
|
|
err = json.Unmarshal(*minPkMessage, stats.MinPk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
bfType := bloomfilter.BasicBF
|
|
|
|
if bfTypeMessage, ok := messageMap["bfType"]; ok && bfTypeMessage != nil {
|
|
|
|
err := json.Unmarshal(*bfTypeMessage, &bfType)
|
2022-03-25 14:27:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
stats.BFType = bfType
|
|
|
|
}
|
|
|
|
|
|
|
|
if bfMessage, ok := messageMap["bf"]; ok && bfMessage != nil {
|
|
|
|
bf, err := bloomfilter.UnmarshalJSON(*bfMessage, bfType)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Failed to unmarshal bloom filter, use AlwaysTrueBloomFilter instead of return err", zap.Error(err))
|
|
|
|
bf = bloomfilter.AlwaysTrueBloomFilter
|
|
|
|
}
|
|
|
|
stats.BF = bf
|
2022-03-25 14:27:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-29 10:21:28 +08:00
|
|
|
func (stats *PrimaryKeyStats) UpdateByMsgs(msgs FieldData) {
|
|
|
|
switch schemapb.DataType(stats.PkType) {
|
2022-04-02 17:43:29 +08:00
|
|
|
case schemapb.DataType_Int64:
|
|
|
|
data := msgs.(*Int64FieldData).Data
|
2022-03-25 14:27:25 +08:00
|
|
|
if len(data) < 1 {
|
|
|
|
// return error: msgs must has one element at least
|
2023-05-29 10:21:28 +08:00
|
|
|
return
|
2022-03-25 14:27:25 +08:00
|
|
|
}
|
|
|
|
|
2021-10-13 10:22:33 +08:00
|
|
|
b := make([]byte, 8)
|
2022-03-25 14:27:25 +08:00
|
|
|
for _, int64Value := range data {
|
2022-04-02 17:43:29 +08:00
|
|
|
pk := NewInt64PrimaryKey(int64Value)
|
2023-05-29 10:21:28 +08:00
|
|
|
stats.UpdateMinMax(pk)
|
2022-03-25 14:27:25 +08:00
|
|
|
common.Endian.PutUint64(b, uint64(int64Value))
|
2021-10-13 10:22:33 +08:00
|
|
|
stats.BF.Add(b)
|
|
|
|
}
|
2022-04-02 17:43:29 +08:00
|
|
|
case schemapb.DataType_VarChar:
|
|
|
|
data := msgs.(*StringFieldData).Data
|
2022-03-25 14:27:25 +08:00
|
|
|
if len(data) < 1 {
|
|
|
|
// return error: msgs must has one element at least
|
2023-05-29 10:21:28 +08:00
|
|
|
return
|
2022-03-25 14:27:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, str := range data {
|
2022-04-02 17:43:29 +08:00
|
|
|
pk := NewVarCharPrimaryKey(str)
|
2023-05-29 10:21:28 +08:00
|
|
|
stats.UpdateMinMax(pk)
|
2022-04-02 17:43:29 +08:00
|
|
|
stats.BF.AddString(str)
|
2022-03-25 14:27:25 +08:00
|
|
|
}
|
|
|
|
default:
|
2023-09-21 09:45:27 +08:00
|
|
|
// TODO::
|
2021-05-20 18:38:45 +08:00
|
|
|
}
|
2023-05-29 10:21:28 +08:00
|
|
|
}
|
2022-03-25 14:27:25 +08:00
|
|
|
|
2023-05-29 10:21:28 +08:00
|
|
|
func (stats *PrimaryKeyStats) Update(pk PrimaryKey) {
|
|
|
|
stats.UpdateMinMax(pk)
|
|
|
|
switch schemapb.DataType(stats.PkType) {
|
|
|
|
case schemapb.DataType_Int64:
|
|
|
|
data := pk.GetValue().(int64)
|
|
|
|
b := make([]byte, 8)
|
|
|
|
common.Endian.PutUint64(b, uint64(data))
|
|
|
|
stats.BF.Add(b)
|
|
|
|
case schemapb.DataType_VarChar:
|
|
|
|
data := pk.GetValue().(string)
|
|
|
|
stats.BF.AddString(data)
|
|
|
|
default:
|
|
|
|
log.Warn("Update pk stats with invalid data type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updatePk update minPk and maxPk value
|
|
|
|
func (stats *PrimaryKeyStats) UpdateMinMax(pk PrimaryKey) {
|
|
|
|
if stats.MinPk == nil {
|
|
|
|
stats.MinPk = pk
|
|
|
|
} else if stats.MinPk.GT(pk) {
|
|
|
|
stats.MinPk = pk
|
|
|
|
}
|
|
|
|
|
|
|
|
if stats.MaxPk == nil {
|
|
|
|
stats.MaxPk = pk
|
|
|
|
} else if stats.MaxPk.LT(pk) {
|
|
|
|
stats.MaxPk = pk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 10:28:21 +08:00
|
|
|
func NewPrimaryKeyStats(fieldID, pkType, rowNum int64) (*PrimaryKeyStats, error) {
|
|
|
|
if rowNum <= 0 {
|
2024-03-14 05:32:54 +08:00
|
|
|
return nil, merr.WrapErrParameterInvalidMsg("zero or negative row num", rowNum)
|
2023-11-21 10:28:21 +08:00
|
|
|
}
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
|
|
|
|
bfType := paramtable.Get().CommonCfg.BloomFilterType.GetValue()
|
2023-05-29 10:21:28 +08:00
|
|
|
return &PrimaryKeyStats{
|
|
|
|
FieldID: fieldID,
|
|
|
|
PkType: pkType,
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
BFType: bloomfilter.BFTypeFromString(bfType),
|
|
|
|
BF: bloomfilter.NewBloomFilterWithType(
|
|
|
|
uint(rowNum),
|
|
|
|
paramtable.Get().CommonCfg.MaxBloomFalsePositive.GetAsFloat(),
|
|
|
|
bfType),
|
2023-11-21 10:28:21 +08:00
|
|
|
}, nil
|
2023-05-29 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatsWriter writes stats to buffer
|
|
|
|
type StatsWriter struct {
|
|
|
|
buffer []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBuffer returns buffer
|
|
|
|
func (sw *StatsWriter) GetBuffer() []byte {
|
|
|
|
return sw.buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateList writes Stats slice to buffer
|
|
|
|
func (sw *StatsWriter) GenerateList(stats []*PrimaryKeyStats) error {
|
2021-05-20 18:38:45 +08:00
|
|
|
b, err := json.Marshal(stats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sw.buffer = b
|
2023-05-29 10:21:28 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-20 18:38:45 +08:00
|
|
|
|
2023-05-29 10:21:28 +08:00
|
|
|
// Generate writes Stats to buffer
|
|
|
|
func (sw *StatsWriter) Generate(stats *PrimaryKeyStats) error {
|
|
|
|
b, err := json.Marshal(stats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sw.buffer = b
|
2021-05-20 18:38:45 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-29 10:21:28 +08:00
|
|
|
// GenerateByData writes Int64Stats or StringStats from @msgs with @fieldID to @buffer
|
|
|
|
func (sw *StatsWriter) GenerateByData(fieldID int64, pkType schemapb.DataType, msgs FieldData) error {
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
bfType := paramtable.Get().CommonCfg.BloomFilterType.GetValue()
|
2023-05-29 10:21:28 +08:00
|
|
|
stats := &PrimaryKeyStats{
|
|
|
|
FieldID: fieldID,
|
|
|
|
PkType: int64(pkType),
|
enhance: Use Blocked Bloom Filter instead of basic bloom fitler impl. (#33405)
issue: #32995
To speed up the construction and querying of Bloom filters, we chose a
blocked Bloom filter instead of a basic Bloom filter implementation.
WARN: This PR is compatible with old version bf impl, but if fall back
to old milvus version, it may causes bloom filter deserialize failed.
In single Bloom filter test cases with a capacity of 1,000,000 and a
false positive rate (FPR) of 0.001, the blocked Bloom filter is 5 times
faster than the basic Bloom filter in both querying and construction, at
the cost of a 30% increase in memory usage.
- Block BF construct time {"time": "54.128131ms"}
- Block BF size {"size": 3021578}
- Block BF Test cost {"time": "55.407352ms"}
- Basic BF construct time {"time": "210.262183ms"}
- Basic BF size {"size": 2396308}
- Basic BF Test cost {"time": "192.596229ms"}
In multi Bloom filter test cases with a capacity of 100,000, an FPR of
0.001, and 100 Bloom filters, we reuse the primary key locations for all
Bloom filters to avoid repeated hash computations. As a result, the
blocked Bloom filter is also 5 times faster than the basic Bloom filter
in querying.
- Block BF TestLocation cost {"time": "529.97183ms"}
- Basic BF TestLocation cost {"time": "3.197430181s"}
---------
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
2024-05-31 17:49:45 +08:00
|
|
|
BFType: bloomfilter.BFTypeFromString(bfType),
|
|
|
|
BF: bloomfilter.NewBloomFilterWithType(
|
|
|
|
uint(msgs.RowNum()),
|
|
|
|
paramtable.Get().CommonCfg.MaxBloomFalsePositive.GetAsFloat(),
|
|
|
|
bfType),
|
2023-05-29 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
stats.UpdateByMsgs(msgs)
|
|
|
|
return sw.Generate(stats)
|
|
|
|
}
|
|
|
|
|
2021-12-17 18:47:56 +08:00
|
|
|
// StatsReader reads stats
|
2021-05-20 18:38:45 +08:00
|
|
|
type StatsReader struct {
|
|
|
|
buffer []byte
|
|
|
|
}
|
|
|
|
|
2021-12-07 14:53:13 +08:00
|
|
|
// SetBuffer sets buffer
|
2021-05-20 18:38:45 +08:00
|
|
|
func (sr *StatsReader) SetBuffer(buffer []byte) {
|
|
|
|
sr.buffer = buffer
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:27:25 +08:00
|
|
|
// GetInt64Stats returns buffer as PrimaryKeyStats
|
|
|
|
func (sr *StatsReader) GetPrimaryKeyStats() (*PrimaryKeyStats, error) {
|
|
|
|
stats := &PrimaryKeyStats{}
|
2021-10-13 10:22:33 +08:00
|
|
|
err := json.Unmarshal(sr.buffer, &stats)
|
|
|
|
if err != nil {
|
2023-06-20 16:40:42 +08:00
|
|
|
return nil, merr.WrapErrParameterInvalid(
|
|
|
|
"valid JSON",
|
|
|
|
string(sr.buffer),
|
|
|
|
err.Error())
|
2021-10-13 10:22:33 +08:00
|
|
|
}
|
2022-03-25 14:27:25 +08:00
|
|
|
|
2021-10-13 10:22:33 +08:00
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2023-05-29 10:21:28 +08:00
|
|
|
// GetInt64Stats returns buffer as PrimaryKeyStats
|
|
|
|
func (sr *StatsReader) GetPrimaryKeyStatsList() ([]*PrimaryKeyStats, error) {
|
|
|
|
stats := []*PrimaryKeyStats{}
|
|
|
|
err := json.Unmarshal(sr.buffer, &stats)
|
|
|
|
if err != nil {
|
2023-06-20 16:40:42 +08:00
|
|
|
return nil, merr.WrapErrParameterInvalid(
|
|
|
|
"valid JSON",
|
|
|
|
string(sr.buffer),
|
|
|
|
err.Error())
|
2023-05-29 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 10:57:12 +08:00
|
|
|
type BM25Stats struct {
|
|
|
|
rowsWithToken map[uint32]int32 // mapping token => row num include token
|
|
|
|
numRow int64 // total row num
|
|
|
|
numToken int64 // total token num
|
|
|
|
}
|
|
|
|
|
|
|
|
const BM25VERSION int32 = 0
|
|
|
|
|
|
|
|
func NewBM25Stats() *BM25Stats {
|
|
|
|
return &BM25Stats{
|
|
|
|
rowsWithToken: map[uint32]int32{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBM25StatsWithBytes(bytes []byte) (*BM25Stats, error) {
|
|
|
|
stats := NewBM25Stats()
|
|
|
|
err := stats.Deserialize(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) Append(rows ...map[uint32]float32) {
|
|
|
|
for _, row := range rows {
|
|
|
|
for key, value := range row {
|
|
|
|
m.rowsWithToken[key] += 1
|
|
|
|
m.numToken += int64(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.numRow += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) AppendFieldData(datas ...*SparseFloatVectorFieldData) {
|
|
|
|
for _, data := range datas {
|
|
|
|
m.AppendBytes(data.GetContents()...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update BM25Stats by sparse vector bytes
|
|
|
|
func (m *BM25Stats) AppendBytes(datas ...[]byte) {
|
|
|
|
for _, data := range datas {
|
|
|
|
dim := len(data) / 8
|
|
|
|
for i := 0; i < dim; i++ {
|
|
|
|
index := typeutil.SparseFloatRowIndexAt(data, i)
|
|
|
|
value := typeutil.SparseFloatRowValueAt(data, i)
|
|
|
|
m.rowsWithToken[index] += 1
|
|
|
|
m.numToken += int64(value)
|
|
|
|
}
|
|
|
|
m.numRow += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) NumRow() int64 {
|
|
|
|
return m.numRow
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) NumToken() int64 {
|
|
|
|
return m.numToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) Merge(meta *BM25Stats) {
|
|
|
|
for key, value := range meta.rowsWithToken {
|
|
|
|
m.rowsWithToken[key] += value
|
|
|
|
}
|
|
|
|
m.numRow += meta.NumRow()
|
|
|
|
m.numToken += meta.numToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) Minus(meta *BM25Stats) {
|
|
|
|
for key, value := range meta.rowsWithToken {
|
|
|
|
m.rowsWithToken[key] -= value
|
|
|
|
}
|
|
|
|
m.numRow -= meta.numRow
|
|
|
|
m.numToken -= meta.numToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) Clone() *BM25Stats {
|
|
|
|
return &BM25Stats{
|
|
|
|
rowsWithToken: maps.Clone(m.rowsWithToken),
|
|
|
|
numRow: m.numRow,
|
|
|
|
numToken: m.numToken,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) Serialize() ([]byte, error) {
|
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, len(m.rowsWithToken)*8+20))
|
|
|
|
|
|
|
|
if err := binary.Write(buffer, common.Endian, BM25VERSION); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(buffer, common.Endian, m.numRow); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(buffer, common.Endian, m.numToken); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range m.rowsWithToken {
|
|
|
|
if err := binary.Write(buffer, common.Endian, key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(buffer, common.Endian, value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO ADD Serialize Time Metric
|
|
|
|
return buffer.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) Deserialize(bs []byte) error {
|
|
|
|
buffer := bytes.NewBuffer(bs)
|
|
|
|
dim := (len(bs) - 20) / 8
|
|
|
|
var numRow, tokenNum int64
|
|
|
|
var version int32
|
|
|
|
if err := binary.Read(buffer, common.Endian, &version); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(buffer, common.Endian, &numRow); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(buffer, common.Endian, &tokenNum); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []uint32 = make([]uint32, dim)
|
|
|
|
var values []int32 = make([]int32, dim)
|
|
|
|
for i := 0; i < dim; i++ {
|
|
|
|
if err := binary.Read(buffer, common.Endian, &keys[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(buffer, common.Endian, &values[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.numRow += numRow
|
|
|
|
m.numToken += tokenNum
|
|
|
|
for i := 0; i < dim; i++ {
|
|
|
|
m.rowsWithToken[keys[i]] += values[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("test-- deserialize", zap.Int64("numrow", m.numRow), zap.Int64("tokenNum", m.numToken))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) BuildIDF(tf map[uint32]float32) map[uint32]float32 {
|
|
|
|
vector := make(map[uint32]float32)
|
|
|
|
for key, value := range tf {
|
|
|
|
nq := m.rowsWithToken[key]
|
|
|
|
vector[key] = value * float32(math.Log(1+(float64(m.numRow)-float64(nq)+0.5)/(float64(nq)+0.5)))
|
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *BM25Stats) GetAvgdl() float64 {
|
|
|
|
return float64(m.numToken) / float64(m.numRow)
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:27:25 +08:00
|
|
|
// DeserializeStats deserialize @blobs as []*PrimaryKeyStats
|
|
|
|
func DeserializeStats(blobs []*Blob) ([]*PrimaryKeyStats, error) {
|
|
|
|
results := make([]*PrimaryKeyStats, 0, len(blobs))
|
2021-10-20 14:26:35 +08:00
|
|
|
for _, blob := range blobs {
|
2023-06-25 11:32:43 +08:00
|
|
|
if len(blob.Value) == 0 {
|
2021-10-13 10:22:33 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sr := &StatsReader{}
|
|
|
|
sr.SetBuffer(blob.Value)
|
2022-03-25 14:27:25 +08:00
|
|
|
stats, err := sr.GetPrimaryKeyStats()
|
2021-10-13 10:22:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-20 14:26:35 +08:00
|
|
|
results = append(results, stats)
|
2021-10-13 10:22:33 +08:00
|
|
|
}
|
|
|
|
return results, nil
|
2021-05-20 18:38:45 +08:00
|
|
|
}
|
2023-05-29 10:21:28 +08:00
|
|
|
|
|
|
|
func DeserializeStatsList(blob *Blob) ([]*PrimaryKeyStats, error) {
|
2023-06-25 11:32:43 +08:00
|
|
|
if len(blob.Value) == 0 {
|
2023-05-29 10:21:28 +08:00
|
|
|
return []*PrimaryKeyStats{}, nil
|
|
|
|
}
|
|
|
|
sr := &StatsReader{}
|
|
|
|
sr.SetBuffer(blob.Value)
|
|
|
|
stats, err := sr.GetPrimaryKeyStatsList()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stats, nil
|
|
|
|
}
|