binlog_meta.go 4.5 KB
Newer Older
X
XuanYang-cn 已提交
1 2 3 4 5 6 7 8 9 10
// 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.
11
// GOOSE TODO remove this
X
XuanYang-cn 已提交
12

13 14 15 16 17 18 19
package datanode

import (
	"path"
	"strconv"

	"github.com/golang/protobuf/proto"
X
Xiangyu Wang 已提交
20 21
	"github.com/milvus-io/milvus/internal/kv"
	"github.com/milvus-io/milvus/internal/proto/datapb"
22 23
)

24 25
// binlogMeta persists binlog paths into etcd.
// ddl binlog etcd meta key:
26
//   ${prefix}/${collectionID}/${idx}
27
// segment binlog etcd meta key:
28 29
//   ${prefix}/${segmentID}/${fieldID}/${idx}
type binlogMeta struct {
G
godchen 已提交
30
	client      kv.TxnKV // etcd kv
31 32 33
	idAllocator allocatorInterface
}

G
godchen 已提交
34
func NewBinlogMeta(kv kv.TxnKV, idAllocator allocatorInterface) (*binlogMeta, error) {
35 36 37 38 39 40 41
	mt := &binlogMeta{
		client:      kv,
		idAllocator: idAllocator,
	}
	return mt, nil
}

42 43 44
// genKey gives a valid key string for lists of UniqueIDs:
//  if alloc is true, the returned keys will have a generated-unique ID at the end.
//  if alloc is false, the returned keys will only consist of provided ids.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
func (bm *binlogMeta) genKey(alloc bool, ids ...UniqueID) (key string, err error) {
	if alloc {
		idx, err := bm.idAllocator.allocID()
		if err != nil {
			return "", err
		}
		ids = append(ids, idx)
	}

	idStr := make([]string, len(ids))
	for _, id := range ids {
		idStr = append(idStr, strconv.FormatInt(id, 10))
	}

	key = path.Join(idStr...)
	return
}

63 64 65
// SaveSegmentBinlogMetaTxn stores all fields' binlog paths of a segment in a transaction.
// segment binlog etcd meta key:
//   ${prefix}/${segmentID}/${fieldID}/${idx}
66 67
func (bm *binlogMeta) SaveSegmentBinlogMetaTxn(segmentID UniqueID, field2Path map[UniqueID]string) error {

68
	etcdKey2binlogPath := make(map[string]string, len(field2Path))
69 70 71 72 73 74
	for fieldID, p := range field2Path {
		key, err := bm.genKey(true, segmentID, fieldID)
		if err != nil {
			return err
		}

75
		binlogPath := proto.MarshalTextString(&datapb.SegmentFieldBinlogMeta{
76 77 78
			FieldID:    fieldID,
			BinlogPath: p,
		})
79
		etcdKey2binlogPath[path.Join(Params.SegFlushMetaSubPath, key)] = binlogPath
80
	}
81
	return bm.client.MultiSave(etcdKey2binlogPath)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

func (bm *binlogMeta) getFieldBinlogMeta(segmentID UniqueID,
	fieldID UniqueID) (metas []*datapb.SegmentFieldBinlogMeta, err error) {

	prefix, err := bm.genKey(false, segmentID, fieldID)
	if err != nil {
		return nil, err
	}

	_, vs, err := bm.client.LoadWithPrefix(path.Join(Params.SegFlushMetaSubPath, prefix))
	if err != nil {
		return nil, err
	}

	for _, blob := range vs {
		m := &datapb.SegmentFieldBinlogMeta{}
		if err = proto.UnmarshalText(blob, m); err != nil {
			return nil, err
		}

		metas = append(metas, m)
	}

	return
}

func (bm *binlogMeta) getSegmentBinlogMeta(segmentID UniqueID) (metas []*datapb.SegmentFieldBinlogMeta, err error) {

	prefix, err := bm.genKey(false, segmentID)
	if err != nil {
		return nil, err
	}

	_, vs, err := bm.client.LoadWithPrefix(path.Join(Params.SegFlushMetaSubPath, prefix))
	if err != nil {
		return nil, err
	}

	for _, blob := range vs {
		m := &datapb.SegmentFieldBinlogMeta{}
		if err = proto.UnmarshalText(blob, m); err != nil {
			return nil, err
		}

		metas = append(metas, m)
	}
	return
}

132
// SaveDDLBinlogMetaTxn stores timestamp and ddl binlog path pair into etcd in a transaction.
133 134 135 136
// ddl binlog meta key:
//   ${prefix}/${collectionID}/${idx}
func (bm *binlogMeta) SaveDDLBinlogMetaTxn(collID UniqueID, tsPath string, ddlPath string) error {

137
	uniqueKey, err := bm.genKey(true, collID)
138 139 140
	if err != nil {
		return err
	}
141
	binlogPathPair := proto.MarshalTextString(&datapb.DDLBinlogMeta{
142 143 144 145
		DdlBinlogPath: ddlPath,
		TsBinlogPath:  tsPath,
	})

146
	return bm.client.Save(path.Join(Params.DDLFlushMetaSubPath, uniqueKey), binlogPathPair)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
}

func (bm *binlogMeta) getDDLBinlogMete(collID UniqueID) (metas []*datapb.DDLBinlogMeta, err error) {
	prefix, err := bm.genKey(false, collID)
	if err != nil {
		return nil, err
	}

	_, vs, err := bm.client.LoadWithPrefix(path.Join(Params.DDLFlushMetaSubPath, prefix))
	if err != nil {
		return nil, err
	}

	for _, blob := range vs {
		m := &datapb.DDLBinlogMeta{}
		if err = proto.UnmarshalText(blob, m); err != nil {
			return nil, err
		}

		metas = append(metas, m)
	}
	return
}