2021-11-10 23:55:32 +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-05-21 13:11:21 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-11-10 23:55:32 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-05-21 13:11:21 +08:00
|
|
|
//
|
2021-11-10 23:55:32 +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-05-21 13:11:21 +08:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-12-23 15:06:48 +08:00
|
|
|
// timestampAllocator implements tsoAllocator.
|
2021-10-13 21:44:37 +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-12-06 19:41:44 +08:00
|
|
|
// newTimestampAllocator creates a new timestampAllocator
|
2021-10-13 21:44:37 +08:00
|
|
|
func newTimestampAllocator(ctx context.Context, tso timestampAllocatorInterface, peerID UniqueID) (*timestampAllocator, error) {
|
|
|
|
a := ×tampAllocator{
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-13 21:44:37 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-24 19:29:33 +08:00
|
|
|
// AllocOne allocates a timestamp.
|
2021-10-13 21:44:37 +08:00
|
|
|
func (ta *timestampAllocator) AllocOne() (Timestamp, error) {
|
|
|
|
ret, err := ta.alloc(1)
|
2021-05-21 13:11:21 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return ret[0], nil
|
|
|
|
}
|