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"
|
2021-06-22 16:14:09 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/metrics"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/commonpbutil"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
|
|
|
"github.com/milvus-io/milvus/pkg/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
|
|
|
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
|
2023-03-09 18:51:52 +08:00
|
|
|
func newTimestampAllocator(tso timestampAllocatorInterface, peerID UniqueID) (*timestampAllocator, error) {
|
2021-10-13 21:44:37 +08:00
|
|
|
a := ×tampAllocator{
|
2021-08-31 10:33:58 +08:00
|
|
|
peerID: peerID,
|
|
|
|
tso: tso,
|
2021-05-21 13:11:21 +08:00
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 18:51:52 +08:00
|
|
|
func (ta *timestampAllocator) alloc(ctx context.Context, count uint32) ([]Timestamp, error) {
|
2022-03-15 21:51:21 +08:00
|
|
|
tr := timerecord.NewTimeRecorder("applyTimestamp")
|
2023-03-13 18:01:53 +08:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
2023-03-09 18:51:52 +08:00
|
|
|
defer cancel()
|
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() {
|
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.
|
2023-03-09 18:51:52 +08:00
|
|
|
func (ta *timestampAllocator) AllocOne(ctx context.Context) (Timestamp, error) {
|
|
|
|
ret, err := ta.alloc(ctx, 1)
|
2021-05-21 13:11:21 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return ret[0], nil
|
|
|
|
}
|