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"
|
2022-09-25 15:56:51 +08:00
|
|
|
|
2022-10-21 15:57:28 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/commonpbutil"
|
2022-09-25 15:56:51 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/metautil"
|
2022-11-04 14:25:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
2021-02-26 17:44:24 +08:00
|
|
|
|
2022-10-16 20:49:27 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/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{
|
2022-10-21 15:57:28 +08:00
|
|
|
Base: commonpbutil.NewMsgBase(
|
|
|
|
commonpbutil.WithMsgType(commonpb.MsgType_RequestID),
|
|
|
|
commonpbutil.WithMsgID(1), // GOOSE TODO
|
|
|
|
commonpbutil.WithTimeStamp(0), // GOOSE TODO
|
2022-11-04 14:25:38 +08:00
|
|
|
commonpbutil.WithSourceID(paramtable.GetNodeID()),
|
2022-10-21 15:57:28 +08:00
|
|
|
),
|
2021-01-24 21:20:11 +08:00
|
|
|
Count: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-06-15 19:11:55 +08:00
|
|
|
|
2022-05-31 18:02:03 +08:00
|
|
|
if resp.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success {
|
|
|
|
return 0, errors.New(resp.GetStatus().GetReason())
|
|
|
|
}
|
|
|
|
|
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{
|
2022-10-21 15:57:28 +08:00
|
|
|
Base: commonpbutil.NewMsgBase(
|
|
|
|
commonpbutil.WithMsgType(commonpb.MsgType_RequestID),
|
2022-11-04 14:25:38 +08:00
|
|
|
commonpbutil.WithSourceID(paramtable.GetNodeID()),
|
2022-10-21 15:57:28 +08:00
|
|
|
),
|
2021-10-13 22:12:32 +08:00
|
|
|
Count: count,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2022-05-31 18:02:03 +08:00
|
|
|
|
|
|
|
if resp.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success {
|
|
|
|
return 0, 0, errors.New(resp.GetStatus().GetReason())
|
|
|
|
}
|
|
|
|
|
2021-10-13 22:12:32 +08:00
|
|
|
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)
|
2022-09-25 15:56:51 +08:00
|
|
|
return metautil.JoinIDPath(ids...), nil
|
2021-05-18 19:45:00 +08:00
|
|
|
}
|