2021-10-25 19:46:28 +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 19:46:28 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:35:38 +08:00
|
|
|
//
|
2021-10-25 19:46:28 +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-22 19:59:59 +08:00
|
|
|
|
2021-06-22 10:42:07 +08:00
|
|
|
package datacoord
|
2021-02-23 09:58:06 +08:00
|
|
|
|
|
|
|
import (
|
2021-08-12 19:58:08 +08:00
|
|
|
"context"
|
2021-02-23 09:58:06 +08:00
|
|
|
"errors"
|
2021-08-12 19:58:08 +08:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2021-02-23 09:58:06 +08:00
|
|
|
|
2021-08-12 19:58:08 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
2021-11-05 22:25:00 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/tsoutil"
|
2021-02-23 09:58:06 +08:00
|
|
|
)
|
|
|
|
|
2021-08-20 17:50:12 +08:00
|
|
|
// Response response interface for verification
|
2021-02-23 09:58:06 +08:00
|
|
|
type Response interface {
|
|
|
|
GetStatus() *commonpb.Status
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:50:12 +08:00
|
|
|
// VerifyResponse verify grpc Response 1. check error is nil 2. check response.GetStatus() with status success
|
2021-02-23 09:58:06 +08:00
|
|
|
func VerifyResponse(response interface{}, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if response == nil {
|
2021-06-23 12:10:12 +08:00
|
|
|
return errNilResponse
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
switch resp := response.(type) {
|
|
|
|
case Response:
|
2021-08-20 17:50:12 +08:00
|
|
|
// note that resp will not be nil here, since it's still a interface
|
|
|
|
if resp.GetStatus() == nil {
|
|
|
|
return errNilStatusResponse
|
|
|
|
}
|
|
|
|
if resp.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success {
|
|
|
|
return errors.New(resp.GetStatus().GetReason())
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
case *commonpb.Status:
|
2021-08-20 17:50:12 +08:00
|
|
|
if resp == nil {
|
|
|
|
return errNilResponse
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if resp.ErrorCode != commonpb.ErrorCode_Success {
|
2021-08-20 17:50:12 +08:00
|
|
|
return errors.New(resp.GetReason())
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
default:
|
2021-06-23 12:10:12 +08:00
|
|
|
return errUnknownResponseType
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-12 19:58:08 +08:00
|
|
|
|
2021-10-15 20:40:53 +08:00
|
|
|
// FailResponse sets status to failed with reason
|
2021-10-14 15:44:34 +08:00
|
|
|
func FailResponse(status *commonpb.Status, reason string) {
|
|
|
|
status.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
|
|
|
status.Reason = reason
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:58:08 +08:00
|
|
|
// LongTermChecker checks we receive at least one msg in d duration. If not, checker
|
|
|
|
// will print a warn message.
|
|
|
|
type LongTermChecker struct {
|
|
|
|
d time.Duration
|
|
|
|
t *time.Ticker
|
2021-09-13 16:50:57 +08:00
|
|
|
ch chan struct{}
|
2021-08-12 19:58:08 +08:00
|
|
|
warn string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:50:12 +08:00
|
|
|
// NewLongTermChecker create a long term checker specified name, checking interval and warning string to print
|
2021-08-12 19:58:08 +08:00
|
|
|
func NewLongTermChecker(ctx context.Context, name string, d time.Duration, warn string) *LongTermChecker {
|
|
|
|
c := &LongTermChecker{
|
|
|
|
name: name,
|
|
|
|
d: d,
|
|
|
|
warn: warn,
|
2021-09-13 16:50:57 +08:00
|
|
|
ch: make(chan struct{}),
|
2021-08-12 19:58:08 +08:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:50:12 +08:00
|
|
|
// Start starts the check process
|
2021-08-12 19:58:08 +08:00
|
|
|
func (c *LongTermChecker) Start() {
|
|
|
|
c.t = time.NewTicker(c.d)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-09-13 16:50:57 +08:00
|
|
|
case <-c.ch:
|
2021-08-12 19:58:08 +08:00
|
|
|
log.Warn(fmt.Sprintf("long term checker [%s] shutdown", c.name))
|
|
|
|
return
|
|
|
|
case <-c.t.C:
|
|
|
|
log.Warn(c.warn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check reset the time ticker
|
|
|
|
func (c *LongTermChecker) Check() {
|
|
|
|
c.t.Reset(c.d)
|
|
|
|
}
|
2021-09-13 16:50:57 +08:00
|
|
|
|
|
|
|
// Stop stop the checker
|
|
|
|
func (c *LongTermChecker) Stop() {
|
|
|
|
c.t.Stop()
|
|
|
|
close(c.ch)
|
|
|
|
}
|
2021-11-05 22:25:00 +08:00
|
|
|
|
2021-11-08 21:45:00 +08:00
|
|
|
func getTimetravelReverseTime(ctx context.Context, allocator allocator) (*timetravel, error) {
|
2021-11-05 22:25:00 +08:00
|
|
|
ts, err := allocator.allocTimestamp(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pts, _ := tsoutil.ParseTS(ts)
|
|
|
|
ttpts := pts.Add(-timetravelRange)
|
|
|
|
tt := tsoutil.ComposeTS(ttpts.UnixNano()/int64(time.Millisecond), 0)
|
|
|
|
return &timetravel{tt}, nil
|
|
|
|
}
|