2021-10-15 18:03:25 +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-04-19 15:16:33 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-15 18:03:25 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 15:16:33 +08:00
|
|
|
//
|
2021-10-15 18:03:25 +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-04-19 15:16:33 +08:00
|
|
|
|
2021-01-24 21:20:11 +08:00
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
2021-02-26 17:44:24 +08:00
|
|
|
"context"
|
2021-06-15 19:11:55 +08:00
|
|
|
"errors"
|
2021-05-18 19:45:00 +08:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2021-02-26 17:44:24 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
2021-06-22 16:14:09 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
2021-12-20 14:33:47 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2021-01-24 21:20:11 +08:00
|
|
|
)
|
|
|
|
|
2021-03-05 20:41:34 +08:00
|
|
|
type allocatorInterface interface {
|
|
|
|
allocID() (UniqueID, error)
|
2021-10-13 22:12:32 +08:00
|
|
|
allocIDBatch(count uint32) (UniqueID, uint32, error)
|
2021-12-09 11:09:06 +08:00
|
|
|
genKey(ids ...UniqueID) (key string, err error)
|
2021-03-05 20:41:34 +08:00
|
|
|
}
|
2021-05-18 19:45:00 +08:00
|
|
|
|
2021-03-05 20:41:34 +08:00
|
|
|
type allocator struct {
|
2021-06-21 17:28:03 +08:00
|
|
|
rootCoord types.RootCoord
|
2021-03-05 20:41:34 +08:00
|
|
|
}
|
2021-01-24 21:20:11 +08:00
|
|
|
|
2021-10-07 23:50:52 +08:00
|
|
|
// check if allocator implements allocatorInterface
|
2021-05-18 19:45:00 +08:00
|
|
|
var _ allocatorInterface = &allocator{}
|
|
|
|
|
2021-06-21 17:28:03 +08:00
|
|
|
func newAllocator(s types.RootCoord) *allocator {
|
2021-03-05 16:52:45 +08:00
|
|
|
return &allocator{
|
2021-06-21 17:28:03 +08:00
|
|
|
rootCoord: s,
|
2021-01-24 21:20:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:50:52 +08:00
|
|
|
// allocID allocates one ID from rootCoord
|
2021-03-05 16:52:45 +08:00
|
|
|
func (alloc *allocator) allocID() (UniqueID, error) {
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.TODO()
|
2021-06-22 16:14:09 +08:00
|
|
|
resp, err := alloc.rootCoord.AllocID(ctx, &rootcoordpb.AllocIDRequest{
|
2021-01-24 21:20:11 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_RequestID,
|
2021-01-25 18:33:10 +08:00
|
|
|
MsgID: 1, // GOOSE TODO
|
2021-01-24 21:20:11 +08:00
|
|
|
Timestamp: 0, // GOOSE TODO
|
2022-04-24 22:03:44 +08:00
|
|
|
SourceID: Params.DataNodeCfg.GetNodeID(),
|
2021-01-24 21:20:11 +08:00
|
|
|
},
|
|
|
|
Count: 1,
|
|
|
|
})
|
2021-06-15 19:11:55 +08:00
|
|
|
|
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
|
|
|
return 0, errors.New(resp.Status.GetReason())
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:20:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-06-15 19:11:55 +08:00
|
|
|
|
2021-01-24 21:20:11 +08:00
|
|
|
return resp.ID, nil
|
|
|
|
}
|
2021-05-18 19:45:00 +08:00
|
|
|
|
2021-10-13 22:12:32 +08:00
|
|
|
// allocIDBatch allocates IDs in batch from rootCoord
|
|
|
|
func (alloc *allocator) allocIDBatch(count uint32) (UniqueID, uint32, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
resp, err := alloc.rootCoord.AllocID(ctx, &rootcoordpb.AllocIDRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_RequestID,
|
2022-04-24 22:03:44 +08:00
|
|
|
SourceID: Params.DataNodeCfg.GetNodeID(),
|
2021-10-13 22:12:32 +08:00
|
|
|
},
|
|
|
|
Count: count,
|
|
|
|
})
|
|
|
|
|
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
|
|
|
return 0, 0, errors.New(resp.Status.GetReason())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return resp.GetID(), resp.GetCount(), nil
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:45:00 +08:00
|
|
|
// genKey gives a valid key string for lists of UniqueIDs:
|
2021-12-09 11:09:06 +08:00
|
|
|
func (alloc *allocator) genKey(ids ...UniqueID) (string, error) {
|
|
|
|
idx, err := alloc.allocID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-05-18 19:45:00 +08:00
|
|
|
}
|
2021-12-09 11:09:06 +08:00
|
|
|
ids = append(ids, idx)
|
2021-10-12 17:20:33 +08:00
|
|
|
return JoinIDPath(ids...), nil
|
|
|
|
}
|
2021-05-18 19:45:00 +08:00
|
|
|
|
2021-10-12 19:23:39 +08:00
|
|
|
// JoinIDPath joins ids to path format.
|
2021-10-12 17:20:33 +08:00
|
|
|
func JoinIDPath(ids ...UniqueID) string {
|
2021-12-09 12:05:04 +08:00
|
|
|
idStr := make([]string, 0, len(ids))
|
2021-05-18 19:45:00 +08:00
|
|
|
for _, id := range ids {
|
|
|
|
idStr = append(idStr, strconv.FormatInt(id, 10))
|
|
|
|
}
|
2021-10-12 17:20:33 +08:00
|
|
|
return path.Join(idStr...)
|
2021-05-18 19:45:00 +08:00
|
|
|
}
|