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"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2021-03-05 20:41:34 +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-01-22 19:43:27 +08:00
|
|
|
)
|
|
|
|
|
2021-08-23 17:59:51 +08:00
|
|
|
// allocator is the interface for 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-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{
|
2021-01-22 19:43:27 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-04-23 10:15:30 +08:00
|
|
|
MsgType: commonpb.MsgType_RequestTSO,
|
2021-06-23 12:10:12 +08:00
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: 0,
|
2021-01-26 15:14:49 +08:00
|
|
|
SourceID: Params.NodeID,
|
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{
|
2021-01-22 19:43:27 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-04-23 10:15:30 +08:00
|
|
|
MsgType: commonpb.MsgType_RequestID,
|
2021-06-23 12:10:12 +08:00
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: 0,
|
2021-01-26 15:14:49 +08:00
|
|
|
SourceID: Params.NodeID,
|
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
|
|
|
|
}
|
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
|
|
|
}
|