2021-10-25 20:19: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 11:35:38 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-25 20:19:25 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:35:38 +08:00
|
|
|
//
|
2021-10-25 20:19: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-09-10 11:26:00 +08:00
|
|
|
|
2021-06-22 10:42:07 +08:00
|
|
|
package datacoord
|
2021-01-19 12:10:49 +08:00
|
|
|
|
2021-01-22 19:43:27 +08:00
|
|
|
import (
|
2021-02-26 17:44:24 +08:00
|
|
|
"context"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
2021-06-22 16:14:09 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
2021-12-15 20:49:10 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/commonpbutil"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2021-01-22 19:43:27 +08:00
|
|
|
)
|
|
|
|
|
2021-12-23 15:12:21 +08:00
|
|
|
// allocator is the interface that allocating `UniqueID` or `Timestamp`
|
2021-05-28 09:55:21 +08:00
|
|
|
type allocator interface {
|
2021-08-23 17:59:51 +08:00
|
|
|
allocTimestamp(context.Context) (Timestamp, error)
|
|
|
|
allocID(context.Context) (UniqueID, error)
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:35:06 +08:00
|
|
|
// make sure rootCoordAllocator implements allocator interface
|
2021-08-23 17:59:51 +08:00
|
|
|
var _ allocator = (*rootCoordAllocator)(nil)
|
|
|
|
|
|
|
|
// rootCoordAllocator use RootCoord as allocator
|
2021-06-21 17:28:03 +08:00
|
|
|
type rootCoordAllocator struct {
|
2021-08-23 17:59:51 +08:00
|
|
|
types.RootCoord
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-11-01 16:52:19 +08:00
|
|
|
// newRootCoordAllocator gets an allocator from RootCoord
|
2021-08-23 17:59:51 +08:00
|
|
|
func newRootCoordAllocator(rootCoordClient types.RootCoord) allocator {
|
2021-06-21 17:28:03 +08:00
|
|
|
return &rootCoordAllocator{
|
2021-08-23 17:59:51 +08:00
|
|
|
RootCoord: rootCoordClient,
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-11-01 16:50:33 +08:00
|
|
|
// allocTimestamp allocates a Timestamp
|
2021-08-23 17:59:51 +08:00
|
|
|
// invoking RootCoord `AllocTimestamp`
|
|
|
|
func (alloc *rootCoordAllocator) allocTimestamp(ctx context.Context) (Timestamp, error) {
|
|
|
|
resp, err := alloc.AllocTimestamp(ctx, &rootcoordpb.AllocTimestampRequest{
|
2022-10-21 15:57:28 +08:00
|
|
|
Base: commonpbutil.NewMsgBase(
|
|
|
|
commonpbutil.WithMsgType(commonpb.MsgType_RequestTSO),
|
|
|
|
commonpbutil.WithMsgID(0),
|
2022-11-04 14:25:38 +08:00
|
|
|
commonpbutil.WithSourceID(paramtable.GetNodeID()),
|
2022-10-21 15:57:28 +08:00
|
|
|
),
|
2021-01-22 19:43:27 +08:00
|
|
|
Count: 1,
|
|
|
|
})
|
2021-02-23 09:58:06 +08:00
|
|
|
if err = VerifyResponse(resp, err); err != nil {
|
2021-01-22 19:43:27 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return resp.Timestamp, nil
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-11-01 16:54:07 +08:00
|
|
|
// allocID allocates an `UniqueID` from RootCoord, invoking AllocID grpc
|
2021-08-23 17:59:51 +08:00
|
|
|
func (alloc *rootCoordAllocator) allocID(ctx context.Context) (UniqueID, error) {
|
|
|
|
resp, err := alloc.AllocID(ctx, &rootcoordpb.AllocIDRequest{
|
2022-10-21 15:57:28 +08:00
|
|
|
Base: commonpbutil.NewMsgBase(
|
|
|
|
commonpbutil.WithMsgType(commonpb.MsgType_RequestID),
|
|
|
|
commonpbutil.WithMsgID(0),
|
2022-11-04 14:25:38 +08:00
|
|
|
commonpbutil.WithSourceID(paramtable.GetNodeID()),
|
2022-10-21 15:57:28 +08:00
|
|
|
),
|
2021-01-22 19:43:27 +08:00
|
|
|
Count: 1,
|
|
|
|
})
|
2022-02-18 18:29:50 +08:00
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
if err = VerifyResponse(resp, err); err != nil {
|
2021-01-22 19:43:27 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-02-23 09:58:06 +08:00
|
|
|
|
2021-01-22 19:43:27 +08:00
|
|
|
return resp.ID, nil
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|