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"
|
2022-03-15 21:51:21 +08:00
|
|
|
"strconv"
|
2021-05-21 13:11:21 +08:00
|
|
|
"time"
|
|
|
|
|
2022-10-16 20:49:27 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/commonpb"
|
2022-03-15 21:51:21 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metrics"
|
2021-06-22 16:14:09 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
2022-10-19 10:01:26 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/commonpbutil"
|
2022-11-04 14:25:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
2022-03-15 21:51:21 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/timerecord"
|
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) {
|
2022-03-15 21:51:21 +08:00
|
|
|
tr := timerecord.NewTimeRecorder("applyTimestamp")
|
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{
|
2022-10-19 10:01:26 +08:00
|
|
|
Base: commonpbutil.NewMsgBase(
|
|
|
|
commonpbutil.WithMsgType(commonpb.MsgType_RequestTSO),
|
|
|
|
commonpbutil.WithMsgID(0),
|
|
|
|
commonpbutil.WithSourceID(ta.peerID),
|
|
|
|
),
|
2021-05-21 13:11:21 +08:00
|
|
|
Count: count,
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:33:58 +08:00
|
|
|
resp, err := ta.tso.AllocTimestamp(ctx, req)
|
2022-03-15 21:51:21 +08:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
2022-11-04 14:25:38 +08:00
|
|
|
metrics.ProxyApplyTimestampLatency.WithLabelValues(strconv.FormatInt(paramtable.GetNodeID(), 10)).Observe(float64(tr.ElapseSpan().Milliseconds()))
|
2022-03-15 21:51:21 +08:00
|
|
|
}()
|
2021-05-21 13:11:21 +08:00
|
|
|
|
|
|
|
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
|
2022-10-09 10:06:58 +08:00
|
|
|
ret := make([]Timestamp, cnt)
|
2021-05-21 13:11:21 +08:00
|
|
|
for i := uint32(0); i < cnt; i++ {
|
2022-10-09 10:06:58 +08:00
|
|
|
ret[i] = start + uint64(i)
|
2021-05-21 13:11:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|