2021-11-17 19:39:17 +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-11-11 12:56:42 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-11-17 19:39:17 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-11-11 12:56:42 +08:00
|
|
|
//
|
2021-11-17 19:39:17 +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-11-11 12:56:42 +08:00
|
|
|
|
|
|
|
package querycoord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2021-12-21 11:57:39 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-11-11 12:56:42 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func defaultChannelAllocatePolicy() ChannelAllocatePolicy {
|
|
|
|
return shuffleChannelsToQueryNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChannelAllocatePolicy helper function definition to allocate dmChannel to queryNode
|
2021-12-21 11:57:39 +08:00
|
|
|
type ChannelAllocatePolicy func(ctx context.Context, reqs []*querypb.WatchDmChannelsRequest, cluster Cluster, metaCache Meta, wait bool, excludeNodeIDs []int64) error
|
2021-11-11 12:56:42 +08:00
|
|
|
|
2021-12-21 11:57:39 +08:00
|
|
|
func shuffleChannelsToQueryNode(ctx context.Context, reqs []*querypb.WatchDmChannelsRequest, cluster Cluster, metaCache Meta, wait bool, excludeNodeIDs []int64) error {
|
|
|
|
if len(reqs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-11 12:56:42 +08:00
|
|
|
for {
|
2021-12-21 11:57:39 +08:00
|
|
|
onlineNodeIDs := cluster.onlineNodeIDs()
|
|
|
|
if len(onlineNodeIDs) == 0 {
|
|
|
|
err := errors.New("no online QueryNode to allocate")
|
|
|
|
log.Error("shuffleChannelsToQueryNode failed", zap.Error(err))
|
2021-11-11 12:56:42 +08:00
|
|
|
if !wait {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-21 11:57:39 +08:00
|
|
|
time.Sleep(shuffleWaitInterval)
|
2021-11-11 12:56:42 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-21 11:57:39 +08:00
|
|
|
var availableNodeIDs []int64
|
2021-11-11 12:56:42 +08:00
|
|
|
nodeID2NumChannels := make(map[int64]int)
|
2021-12-21 11:57:39 +08:00
|
|
|
for _, nodeID := range onlineNodeIDs {
|
|
|
|
// nodeID in excludeNodeIDs
|
|
|
|
if nodeIncluded(nodeID, excludeNodeIDs) {
|
2021-11-11 12:56:42 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-12-21 11:57:39 +08:00
|
|
|
watchedChannelInfos := metaCache.getDmChannelInfosByNodeID(nodeID)
|
|
|
|
nodeID2NumChannels[nodeID] = len(watchedChannelInfos)
|
|
|
|
availableNodeIDs = append(availableNodeIDs, nodeID)
|
2021-11-11 12:56:42 +08:00
|
|
|
}
|
|
|
|
|
2021-12-21 11:57:39 +08:00
|
|
|
if len(availableNodeIDs) > 0 {
|
|
|
|
log.Debug("shuffleChannelsToQueryNode: shuffle channel to available QueryNode", zap.Int64s("available nodeIDs", availableNodeIDs))
|
2021-11-11 12:56:42 +08:00
|
|
|
for _, req := range reqs {
|
2021-12-21 11:57:39 +08:00
|
|
|
sort.Slice(availableNodeIDs, func(i, j int) bool {
|
|
|
|
return nodeID2NumChannels[availableNodeIDs[i]] < nodeID2NumChannels[availableNodeIDs[j]]
|
2021-11-11 12:56:42 +08:00
|
|
|
})
|
2021-12-21 11:57:39 +08:00
|
|
|
selectedNodeID := availableNodeIDs[0]
|
|
|
|
req.NodeID = selectedNodeID
|
|
|
|
nodeID2NumChannels[selectedNodeID]++
|
2021-11-11 12:56:42 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !wait {
|
2021-12-21 11:57:39 +08:00
|
|
|
err := errors.New("no available queryNode to allocate")
|
|
|
|
log.Error("shuffleChannelsToQueryNode failed", zap.Int64s("online nodeIDs", onlineNodeIDs), zap.Int64s("exclude nodeIDs", excludeNodeIDs), zap.Error(err))
|
|
|
|
return err
|
2021-11-11 12:56:42 +08:00
|
|
|
}
|
2021-12-21 11:57:39 +08:00
|
|
|
time.Sleep(shuffleWaitInterval)
|
2021-11-11 12:56:42 +08:00
|
|
|
}
|
|
|
|
}
|