2022-09-15 18:48:32 +08:00
|
|
|
package querynode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-10 20:35:39 +08:00
|
|
|
"fmt"
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-02-26 11:31:49 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
|
|
|
|
2022-10-16 20:49:27 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/commonpb"
|
2022-09-27 19:22:54 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2022-09-15 18:48:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2022-11-28 09:53:13 +08:00
|
|
|
"github.com/samber/lo"
|
2022-09-27 19:22:54 +08:00
|
|
|
"go.uber.org/zap"
|
2022-09-15 18:48:32 +08:00
|
|
|
)
|
|
|
|
|
2022-11-28 09:53:13 +08:00
|
|
|
// TransferLoad transfers load segments with shard cluster.
|
2022-09-15 18:48:32 +08:00
|
|
|
func (node *QueryNode) TransferLoad(ctx context.Context, req *querypb.LoadSegmentsRequest) (*commonpb.Status, error) {
|
|
|
|
if len(req.GetInfos()) == 0 {
|
|
|
|
return &commonpb.Status{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
shard := req.GetInfos()[0].GetInsertChannel()
|
2022-11-28 09:53:13 +08:00
|
|
|
segmentIDs := lo.Map(req.GetInfos(), func(info *querypb.SegmentLoadInfo, _ int) int64 {
|
|
|
|
return info.GetSegmentID()
|
|
|
|
})
|
|
|
|
log := log.Ctx(ctx).With(
|
|
|
|
zap.String("shard", shard),
|
|
|
|
zap.Int64s("segmentIDs", segmentIDs),
|
|
|
|
)
|
|
|
|
|
|
|
|
log.Info("LoadSegment start to transfer load with shard cluster")
|
2022-09-15 18:48:32 +08:00
|
|
|
shardCluster, ok := node.ShardClusterService.getShardCluster(shard)
|
|
|
|
if !ok {
|
2022-11-28 09:53:13 +08:00
|
|
|
log.Warn("TransferLoad failed to find shard cluster")
|
2022-09-15 18:48:32 +08:00
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_NotShardLeader,
|
|
|
|
Reason: "shard cluster not found, the leader may have changed",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req.NeedTransfer = false
|
2022-09-27 19:22:54 +08:00
|
|
|
err := shardCluster.LoadSegments(ctx, req)
|
2022-09-15 18:48:32 +08:00
|
|
|
if err != nil {
|
2023-01-10 20:35:39 +08:00
|
|
|
if errors.Is(err, ErrInsufficientMemory) {
|
|
|
|
log.Warn("insufficient memory when shard cluster load segments", zap.Error(err))
|
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_InsufficientMemoryToLoad,
|
|
|
|
Reason: fmt.Sprintf("insufficient memory when shard cluster load segments, err:%s", err.Error()),
|
|
|
|
}, nil
|
|
|
|
}
|
2022-11-28 09:53:13 +08:00
|
|
|
log.Warn("shard cluster failed to load segments", zap.Error(err))
|
2022-09-15 18:48:32 +08:00
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: err.Error(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:53:13 +08:00
|
|
|
log.Info("LoadSegment transfer load done")
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:53:13 +08:00
|
|
|
// TransferRelease transfers release segments with shard cluster.
|
2022-09-15 18:48:32 +08:00
|
|
|
func (node *QueryNode) TransferRelease(ctx context.Context, req *querypb.ReleaseSegmentsRequest) (*commonpb.Status, error) {
|
2022-11-28 09:53:13 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
|
|
|
zap.String("shard", req.GetShard()),
|
|
|
|
zap.Int64s("segmentIDs", req.GetSegmentIDs()),
|
|
|
|
zap.String("scope", req.GetScope().String()),
|
|
|
|
)
|
|
|
|
|
|
|
|
log.Info("ReleaseSegments start to transfer release with shard cluster")
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
shardCluster, ok := node.ShardClusterService.getShardCluster(req.GetShard())
|
|
|
|
if !ok {
|
2022-11-28 09:53:13 +08:00
|
|
|
log.Warn("TransferLoad failed to find shard cluster")
|
2022-09-15 18:48:32 +08:00
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_NotShardLeader,
|
|
|
|
Reason: "shard cluster not found, the leader may have changed",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req.NeedTransfer = false
|
2022-09-27 19:22:54 +08:00
|
|
|
err := shardCluster.ReleaseSegments(ctx, req, false)
|
2022-09-15 18:48:32 +08:00
|
|
|
if err != nil {
|
2022-11-28 09:53:13 +08:00
|
|
|
log.Warn("shard cluster failed to release segments", zap.Error(err))
|
2022-09-15 18:48:32 +08:00
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: err.Error(),
|
|
|
|
}, nil
|
|
|
|
}
|
2022-11-28 09:53:13 +08:00
|
|
|
|
|
|
|
log.Info("ReleaseSegments transfer release done")
|
2022-09-15 18:48:32 +08:00
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
}, nil
|
|
|
|
}
|