2020-12-21 10:06:46 +08:00
|
|
|
package writenode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/kv"
|
|
|
|
pb "github.com/zilliztech/milvus-distributed/internal/proto/writerpb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type metaTable struct {
|
|
|
|
client kv.TxnBase // client of a reliable kv service, i.e. etcd client
|
2020-12-24 20:55:40 +08:00
|
|
|
segID2FlushMeta map[UniqueID]pb.SegmentFlushMeta // segment id to flush meta
|
|
|
|
collID2DdlMeta map[UniqueID]*pb.DDLFlushMeta
|
2020-12-21 10:06:46 +08:00
|
|
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMetaTable(kv kv.TxnBase) (*metaTable, error) {
|
|
|
|
mt := &metaTable{
|
|
|
|
client: kv,
|
|
|
|
lock: sync.RWMutex{},
|
|
|
|
}
|
2020-12-24 20:55:40 +08:00
|
|
|
err := mt.reloadSegMetaFromKV()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mt.reloadDdlMetaFromKV()
|
2020-12-21 10:06:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return mt, nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (mt *metaTable) AppendDDLBinlogPaths(collID UniqueID, paths []string) error {
|
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
2020-12-21 10:06:46 +08:00
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
_, ok := mt.collID2DdlMeta[collID]
|
|
|
|
if !ok {
|
|
|
|
mt.collID2DdlMeta[collID] = &pb.DDLFlushMeta{
|
|
|
|
CollectionID: collID,
|
|
|
|
BinlogPaths: make([]string, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := mt.collID2DdlMeta[collID]
|
|
|
|
meta.BinlogPaths = append(meta.BinlogPaths, paths...)
|
|
|
|
|
|
|
|
return mt.saveDDLFlushMeta(meta)
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:25:16 +08:00
|
|
|
func (mt *metaTable) AppendSegBinlogPaths(tsOpen Timestamp, segmentID UniqueID, fieldID int64, dataPaths []string) error {
|
2020-12-24 20:55:40 +08:00
|
|
|
_, ok := mt.segID2FlushMeta[segmentID]
|
|
|
|
if !ok {
|
2020-12-26 16:28:55 +08:00
|
|
|
err := mt.addSegmentFlush(segmentID, tsOpen)
|
2020-12-24 20:55:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := mt.segID2FlushMeta[segmentID]
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, field := range meta.Fields {
|
|
|
|
if field.FieldID == fieldID {
|
|
|
|
field.BinlogPaths = append(field.BinlogPaths, dataPaths...)
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
newField := &pb.FieldFlushMeta{
|
|
|
|
FieldID: fieldID,
|
|
|
|
BinlogPaths: dataPaths,
|
|
|
|
}
|
|
|
|
meta.Fields = append(meta.Fields, newField)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mt.saveSegFlushMeta(&meta)
|
|
|
|
}
|
|
|
|
|
2020-12-26 16:28:55 +08:00
|
|
|
func (mt *metaTable) CompleteFlush(tsClose Timestamp, segmentID UniqueID) error {
|
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
meta, ok := mt.segID2FlushMeta[segmentID]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("segment not exists with ID = " + strconv.FormatInt(segmentID, 10))
|
|
|
|
}
|
|
|
|
meta.IsClosed = true
|
|
|
|
meta.CloseTime = tsClose
|
|
|
|
|
|
|
|
return mt.saveSegFlushMeta(&meta)
|
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
// metaTable.lock.Lock() before call this function
|
|
|
|
func (mt *metaTable) saveDDLFlushMeta(meta *pb.DDLFlushMeta) error {
|
|
|
|
value := proto.MarshalTextString(meta)
|
|
|
|
|
|
|
|
mt.collID2DdlMeta[meta.CollectionID] = meta
|
|
|
|
|
2021-01-04 14:17:59 +08:00
|
|
|
return mt.client.Save(Params.WriteNodeDDLKvSubPath+strconv.FormatInt(meta.CollectionID, 10), value)
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTable) reloadDdlMetaFromKV() error {
|
|
|
|
mt.collID2DdlMeta = make(map[UniqueID]*pb.DDLFlushMeta)
|
2021-01-04 14:17:59 +08:00
|
|
|
_, values, err := mt.client.LoadWithPrefix(Params.WriteNodeDDLKvSubPath)
|
2020-12-21 10:06:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range values {
|
2020-12-24 20:55:40 +08:00
|
|
|
ddlMeta := &pb.DDLFlushMeta{}
|
|
|
|
err = proto.UnmarshalText(value, ddlMeta)
|
2020-12-21 10:06:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-24 20:55:40 +08:00
|
|
|
mt.collID2DdlMeta[ddlMeta.CollectionID] = ddlMeta
|
2020-12-21 10:06:46 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// metaTable.lock.Lock() before call this function
|
2020-12-24 20:55:40 +08:00
|
|
|
func (mt *metaTable) saveSegFlushMeta(meta *pb.SegmentFlushMeta) error {
|
2020-12-21 10:06:46 +08:00
|
|
|
value := proto.MarshalTextString(meta)
|
|
|
|
|
|
|
|
mt.segID2FlushMeta[meta.SegmentID] = *meta
|
|
|
|
|
2021-01-04 14:17:59 +08:00
|
|
|
return mt.client.Save(Params.WriteNodeSegKvSubPath+strconv.FormatInt(meta.SegmentID, 10), value)
|
2020-12-21 10:06:46 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (mt *metaTable) reloadSegMetaFromKV() error {
|
|
|
|
mt.segID2FlushMeta = make(map[UniqueID]pb.SegmentFlushMeta)
|
|
|
|
|
2021-01-04 14:17:59 +08:00
|
|
|
_, values, err := mt.client.LoadWithPrefix(Params.WriteNodeSegKvSubPath)
|
2020-12-24 20:55:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
flushMeta := pb.SegmentFlushMeta{}
|
|
|
|
err = proto.UnmarshalText(value, &flushMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mt.segID2FlushMeta[flushMeta.SegmentID] = flushMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTable) addSegmentFlush(segmentID UniqueID, timestamp Timestamp) error {
|
2020-12-21 10:06:46 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
_, ok := mt.segID2FlushMeta[segmentID]
|
|
|
|
if ok {
|
|
|
|
return errors.Errorf("segment already exists with ID = " + strconv.FormatInt(segmentID, 10))
|
|
|
|
}
|
|
|
|
meta := pb.SegmentFlushMeta{
|
|
|
|
IsClosed: false,
|
|
|
|
SegmentID: segmentID,
|
2020-12-24 20:55:40 +08:00
|
|
|
OpenTime: timestamp,
|
2020-12-21 10:06:46 +08:00
|
|
|
}
|
2020-12-24 20:55:40 +08:00
|
|
|
return mt.saveSegFlushMeta(&meta)
|
2020-12-21 10:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTable) getFlushCloseTime(segmentID UniqueID) (Timestamp, error) {
|
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
meta, ok := mt.segID2FlushMeta[segmentID]
|
|
|
|
if !ok {
|
|
|
|
return typeutil.ZeroTimestamp, errors.Errorf("segment not exists with ID = " + strconv.FormatInt(segmentID, 10))
|
|
|
|
}
|
|
|
|
return meta.CloseTime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTable) getFlushOpenTime(segmentID UniqueID) (Timestamp, error) {
|
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
meta, ok := mt.segID2FlushMeta[segmentID]
|
|
|
|
if !ok {
|
|
|
|
return typeutil.ZeroTimestamp, errors.Errorf("segment not exists with ID = " + strconv.FormatInt(segmentID, 10))
|
|
|
|
}
|
|
|
|
return meta.OpenTime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTable) checkFlushComplete(segmentID UniqueID) (bool, error) {
|
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
meta, ok := mt.segID2FlushMeta[segmentID]
|
|
|
|
if !ok {
|
|
|
|
return false, errors.Errorf("segment not exists with ID = " + strconv.FormatInt(segmentID, 10))
|
|
|
|
}
|
|
|
|
return meta.IsClosed, nil
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:25:16 +08:00
|
|
|
func (mt *metaTable) getSegBinlogPaths(segmentID UniqueID) (map[int64][]string, error) {
|
2020-12-21 10:06:46 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
|
|
|
|
meta, ok := mt.segID2FlushMeta[segmentID]
|
|
|
|
if !ok {
|
2020-12-24 20:55:40 +08:00
|
|
|
return nil, errors.Errorf("segment not exists with ID = " + strconv.FormatInt(segmentID, 10))
|
2020-12-21 10:06:46 +08:00
|
|
|
}
|
2020-12-28 15:25:16 +08:00
|
|
|
ret := make(map[int64][]string)
|
2020-12-21 10:06:46 +08:00
|
|
|
for _, field := range meta.Fields {
|
2020-12-24 20:55:40 +08:00
|
|
|
ret[field.FieldID] = field.BinlogPaths
|
2020-12-21 10:06:46 +08:00
|
|
|
}
|
2020-12-24 20:55:40 +08:00
|
|
|
return ret, nil
|
2020-12-21 10:06:46 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (mt *metaTable) getDDLBinlogPaths(collID UniqueID) (map[UniqueID][]string, error) {
|
2020-12-21 10:06:46 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
meta, ok := mt.collID2DdlMeta[collID]
|
2020-12-21 10:06:46 +08:00
|
|
|
if !ok {
|
2020-12-24 20:55:40 +08:00
|
|
|
return nil, errors.Errorf("collection not exists with ID = " + strconv.FormatInt(collID, 10))
|
2020-12-21 10:06:46 +08:00
|
|
|
}
|
2020-12-24 20:55:40 +08:00
|
|
|
ret := make(map[UniqueID][]string)
|
|
|
|
ret[meta.CollectionID] = meta.BinlogPaths
|
2020-12-21 10:06:46 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|