2021-01-19 18:32:57 +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 indexservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2021-03-10 09:56:09 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/indexpb"
|
2021-01-19 18:32:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type metaTable struct {
|
2021-04-12 18:09:28 +08:00
|
|
|
client kv.TxnKV // client of a reliable kv service, i.e. etcd client
|
2021-02-23 11:57:18 +08:00
|
|
|
indexBuildID2Meta map[UniqueID]indexpb.IndexMeta // index build id to index meta
|
2021-01-19 18:32:57 +08:00
|
|
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:09:28 +08:00
|
|
|
func NewMetaTable(kv kv.TxnKV) (*metaTable, error) {
|
2021-01-19 18:32:57 +08:00
|
|
|
mt := &metaTable{
|
|
|
|
client: kv,
|
|
|
|
lock: sync.RWMutex{},
|
|
|
|
}
|
|
|
|
err := mt.reloadFromKV()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTable) reloadFromKV() error {
|
2021-02-02 19:56:04 +08:00
|
|
|
mt.indexBuildID2Meta = make(map[UniqueID]indexpb.IndexMeta)
|
2021-01-19 18:32:57 +08:00
|
|
|
|
|
|
|
_, values, err := mt.client.LoadWithPrefix("indexes")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range values {
|
2021-01-26 09:38:40 +08:00
|
|
|
indexMeta := indexpb.IndexMeta{}
|
2021-01-19 18:32:57 +08:00
|
|
|
err = proto.UnmarshalText(value, &indexMeta)
|
|
|
|
if err != nil {
|
2021-03-30 20:19:30 +08:00
|
|
|
return fmt.Errorf("IndexService metaTable reloadFromKV UnmarshalText indexpb.IndexMeta err:%w", err)
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-02-02 19:56:04 +08:00
|
|
|
mt.indexBuildID2Meta[indexMeta.IndexBuildID] = indexMeta
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// metaTable.lock.Lock() before call this function
|
2021-01-26 09:38:40 +08:00
|
|
|
func (mt *metaTable) saveIndexMeta(meta *indexpb.IndexMeta) error {
|
2021-01-19 18:32:57 +08:00
|
|
|
value := proto.MarshalTextString(meta)
|
|
|
|
|
2021-02-02 19:56:04 +08:00
|
|
|
mt.indexBuildID2Meta[meta.IndexBuildID] = *meta
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-02-02 19:56:04 +08:00
|
|
|
return mt.client.Save("/indexes/"+strconv.FormatInt(meta.IndexBuildID, 10), value)
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
|
2021-02-02 19:56:04 +08:00
|
|
|
func (mt *metaTable) AddIndex(indexBuildID UniqueID, req *indexpb.BuildIndexRequest) error {
|
2021-01-19 18:32:57 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
2021-03-26 10:01:08 +08:00
|
|
|
log.Debug("indexservice add index ...")
|
2021-02-02 19:56:04 +08:00
|
|
|
_, ok := mt.indexBuildID2Meta[indexBuildID]
|
2021-01-19 18:32:57 +08:00
|
|
|
if ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return fmt.Errorf("index already exists with ID = %d", indexBuildID)
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
meta := &indexpb.IndexMeta{
|
2021-03-11 14:14:29 +08:00
|
|
|
State: commonpb.IndexState_Unissued,
|
2021-02-02 19:56:04 +08:00
|
|
|
IndexBuildID: indexBuildID,
|
|
|
|
Req: req,
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
return mt.saveIndexMeta(meta)
|
|
|
|
}
|
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
func (mt *metaTable) BuildIndex(indexBuildID UniqueID) error {
|
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
log.Debug("IndexService update index state")
|
|
|
|
|
|
|
|
meta, ok := mt.indexBuildID2Meta[indexBuildID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("index not exists with ID = %d", indexBuildID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.State != commonpb.IndexState_Unissued {
|
|
|
|
return fmt.Errorf("can not update index state, index with ID = %d state is %d", indexBuildID, meta.State)
|
|
|
|
}
|
|
|
|
meta.State = commonpb.IndexState_InProgress
|
|
|
|
return mt.saveIndexMeta(&meta)
|
|
|
|
}
|
|
|
|
|
2021-02-23 11:57:18 +08:00
|
|
|
func (mt *metaTable) MarkIndexAsDeleted(indexID UniqueID) error {
|
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
|
2021-03-26 10:01:08 +08:00
|
|
|
log.Debug("indexservice", zap.Int64("mark index is deleted", indexID))
|
|
|
|
|
2021-02-23 11:57:18 +08:00
|
|
|
for indexBuildID, meta := range mt.indexBuildID2Meta {
|
|
|
|
if meta.Req.IndexID == indexID {
|
2021-04-16 15:37:13 +08:00
|
|
|
meta.MarkDeleted = true
|
2021-02-23 11:57:18 +08:00
|
|
|
mt.indexBuildID2Meta[indexBuildID] = meta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (mt *metaTable) NotifyBuildIndex(nty *indexpb.NotifyBuildIndexRequest) error {
|
2021-01-19 18:32:57 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
2021-03-26 10:01:08 +08:00
|
|
|
|
|
|
|
log.Debug("indexservice", zap.Int64("notify build index", nty.IndexBuildID))
|
2021-02-02 19:56:04 +08:00
|
|
|
indexBuildID := nty.IndexBuildID
|
|
|
|
meta, ok := mt.indexBuildID2Meta[indexBuildID]
|
2021-01-19 18:32:57 +08:00
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return fmt.Errorf("index not exists with ID = %d", indexBuildID)
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:06:22 +08:00
|
|
|
if nty.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-03-11 14:14:29 +08:00
|
|
|
meta.State = commonpb.IndexState_Failed
|
2021-01-26 09:38:40 +08:00
|
|
|
meta.FailReason = nty.Status.Reason
|
|
|
|
} else {
|
2021-03-11 14:14:29 +08:00
|
|
|
meta.State = commonpb.IndexState_Finished
|
2021-01-26 09:38:40 +08:00
|
|
|
meta.IndexFilePaths = nty.IndexFilePaths
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
return mt.saveIndexMeta(&meta)
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:56:04 +08:00
|
|
|
func (mt *metaTable) GetIndexState(indexBuildID UniqueID) (*indexpb.IndexInfo, error) {
|
2021-01-19 18:32:57 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
2021-01-26 09:38:40 +08:00
|
|
|
ret := &indexpb.IndexInfo{
|
2021-02-02 19:56:04 +08:00
|
|
|
IndexBuildID: indexBuildID,
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-02-02 19:56:04 +08:00
|
|
|
meta, ok := mt.indexBuildID2Meta[indexBuildID]
|
2021-01-19 18:32:57 +08:00
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return ret, fmt.Errorf("index not exists with ID = %d", indexBuildID)
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-04-16 15:37:13 +08:00
|
|
|
if meta.MarkDeleted {
|
2021-03-05 10:15:27 +08:00
|
|
|
return ret, fmt.Errorf("index not exists with ID = %d", indexBuildID)
|
2021-02-23 11:57:18 +08:00
|
|
|
}
|
2021-02-02 19:56:04 +08:00
|
|
|
ret.IndexID = meta.Req.IndexID
|
|
|
|
ret.IndexName = meta.Req.IndexName
|
2021-01-26 09:38:40 +08:00
|
|
|
ret.Reason = meta.FailReason
|
|
|
|
ret.State = meta.State
|
|
|
|
return ret, nil
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
|
2021-02-02 19:56:04 +08:00
|
|
|
func (mt *metaTable) GetIndexFilePathInfo(indexBuildID UniqueID) (*indexpb.IndexFilePathInfo, error) {
|
2021-01-19 18:32:57 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
2021-01-26 09:38:40 +08:00
|
|
|
ret := &indexpb.IndexFilePathInfo{
|
2021-02-02 19:56:04 +08:00
|
|
|
IndexBuildID: indexBuildID,
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-02-02 19:56:04 +08:00
|
|
|
meta, ok := mt.indexBuildID2Meta[indexBuildID]
|
2021-01-19 18:32:57 +08:00
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, fmt.Errorf("index not exists with ID = %d", indexBuildID)
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-04-16 15:37:13 +08:00
|
|
|
if meta.MarkDeleted {
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, fmt.Errorf("index not exists with ID = %d", indexBuildID)
|
2021-02-23 11:57:18 +08:00
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
ret.IndexFilePaths = meta.IndexFilePaths
|
|
|
|
return ret, nil
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
func (mt *metaTable) DeleteIndex(indexBuildID UniqueID) {
|
2021-02-23 11:57:18 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
delete(mt.indexBuildID2Meta, indexBuildID)
|
2021-02-23 11:57:18 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
func (mt *metaTable) getMarkDeleted(limit int) []indexpb.IndexMeta {
|
2021-02-23 11:57:18 +08:00
|
|
|
mt.lock.Lock()
|
|
|
|
defer mt.lock.Unlock()
|
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
log.Debug("IndexService get mark deleted meta")
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
var indexMetas []indexpb.IndexMeta
|
|
|
|
for _, meta := range mt.indexBuildID2Meta {
|
|
|
|
if meta.MarkDeleted && meta.State == commonpb.IndexState_Finished {
|
|
|
|
indexMetas = append(indexMetas, meta)
|
|
|
|
}
|
|
|
|
if len(indexMetas) >= limit {
|
|
|
|
return indexMetas
|
|
|
|
}
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
return indexMetas
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|