2021-05-21 13:11:21 +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.
|
|
|
|
|
2021-06-22 14:40:07 +08:00
|
|
|
package proxy
|
2021-05-21 13:11:21 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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-05-21 13:11:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type TimestampAllocator struct {
|
2021-08-31 10:33:58 +08:00
|
|
|
ctx context.Context
|
|
|
|
tso timestampAllocatorInterface
|
|
|
|
peerID UniqueID
|
2021-05-21 13:11:21 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 10:33:58 +08:00
|
|
|
func NewTimestampAllocator(ctx context.Context, tso timestampAllocatorInterface, peerID UniqueID) (*TimestampAllocator, error) {
|
2021-05-21 13:11:21 +08:00
|
|
|
a := &TimestampAllocator{
|
2021-08-31 10:33:58 +08:00
|
|
|
ctx: ctx,
|
|
|
|
peerID: peerID,
|
|
|
|
tso: tso,
|
2021-05-21 13:11:21 +08:00
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *TimestampAllocator) Alloc(count uint32) ([]Timestamp, error) {
|
2021-06-16 18:35:59 +08:00
|
|
|
ctx, cancel := context.WithTimeout(ta.ctx, 5*time.Second)
|
2021-06-22 16:14:09 +08:00
|
|
|
req := &rootcoordpb.AllocTimestampRequest{
|
2021-05-21 13:11:21 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_RequestTSO,
|
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
SourceID: ta.peerID,
|
|
|
|
},
|
|
|
|
Count: count,
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:33:58 +08:00
|
|
|
resp, err := ta.tso.AllocTimestamp(ctx, req)
|
2021-05-21 13:11:21 +08:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("syncTimestamp Failed:%w", err)
|
|
|
|
}
|
2021-06-09 14:01:48 +08:00
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
|
|
|
return nil, fmt.Errorf("syncTimeStamp Failed:%s", resp.Status.Reason)
|
|
|
|
}
|
2021-05-21 13:11:21 +08:00
|
|
|
start, cnt := resp.Timestamp, resp.Count
|
|
|
|
var ret []Timestamp
|
|
|
|
for i := uint32(0); i < cnt; i++ {
|
|
|
|
ret = append(ret, start+uint64(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *TimestampAllocator) AllocOne() (Timestamp, error) {
|
|
|
|
ret, err := ta.Alloc(1)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return ret[0], nil
|
|
|
|
}
|