2021-10-25 20:23:16 +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 20:23:16 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:35:38 +08:00
|
|
|
//
|
2021-10-25 20:23:16 +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-06-30 10:20:15 +08:00
|
|
|
|
2021-06-22 10:42:07 +08:00
|
|
|
package datacoord
|
2021-01-22 19:43:27 +08:00
|
|
|
|
|
|
|
import (
|
2021-08-27 15:37:56 +08:00
|
|
|
"context"
|
2022-10-22 12:09:28 +08:00
|
|
|
"fmt"
|
2021-01-22 19:43:27 +08:00
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/metrics"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/commonpbutil"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2022-10-22 12:09:28 +08:00
|
|
|
"github.com/samber/lo"
|
2022-04-01 11:33:28 +08:00
|
|
|
"go.uber.org/zap"
|
2021-01-22 19:43:27 +08:00
|
|
|
)
|
|
|
|
|
2021-10-14 15:44:34 +08:00
|
|
|
// Cluster provides interfaces to interact with datanode cluster
|
2021-07-12 11:03:52 +08:00
|
|
|
type Cluster struct {
|
2021-10-14 15:44:34 +08:00
|
|
|
sessionManager *SessionManager
|
|
|
|
channelManager *ChannelManager
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 19:29:31 +08:00
|
|
|
// NewCluster creates a new cluster
|
2021-10-14 15:44:34 +08:00
|
|
|
func NewCluster(sessionManager *SessionManager, channelManager *ChannelManager) *Cluster {
|
2021-07-12 11:03:52 +08:00
|
|
|
c := &Cluster{
|
2021-10-14 15:44:34 +08:00
|
|
|
sessionManager: sessionManager,
|
|
|
|
channelManager: channelManager,
|
2021-07-12 11:03:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:44:34 +08:00
|
|
|
return c
|
2021-06-30 10:20:15 +08:00
|
|
|
}
|
|
|
|
|
2022-02-22 13:15:51 +08:00
|
|
|
// Startup inits the cluster with the given data nodes.
|
2022-03-28 22:33:27 +08:00
|
|
|
func (c *Cluster) Startup(ctx context.Context, nodes []*NodeInfo) error {
|
2021-07-12 11:03:52 +08:00
|
|
|
for _, node := range nodes {
|
2021-10-14 15:44:34 +08:00
|
|
|
c.sessionManager.AddSession(node)
|
2021-07-12 11:03:52 +08:00
|
|
|
}
|
2021-10-14 15:44:34 +08:00
|
|
|
currs := make([]int64, 0, len(nodes))
|
2021-07-12 11:03:52 +08:00
|
|
|
for _, node := range nodes {
|
2021-10-14 15:44:34 +08:00
|
|
|
currs = append(currs, node.NodeID)
|
2021-06-28 13:28:14 +08:00
|
|
|
}
|
2022-03-28 22:33:27 +08:00
|
|
|
return c.channelManager.Startup(ctx, currs)
|
2021-05-26 19:06:56 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:44:34 +08:00
|
|
|
// Register registers a new node in cluster
|
|
|
|
func (c *Cluster) Register(node *NodeInfo) error {
|
|
|
|
c.sessionManager.AddSession(node)
|
2022-03-15 21:51:21 +08:00
|
|
|
err := c.channelManager.AddNode(node.NodeID)
|
|
|
|
if err == nil {
|
|
|
|
metrics.DataCoordNumDataNodes.WithLabelValues().Inc()
|
|
|
|
}
|
|
|
|
return err
|
2021-05-26 19:06:56 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:44:34 +08:00
|
|
|
// UnRegister removes a node from cluster
|
|
|
|
func (c *Cluster) UnRegister(node *NodeInfo) error {
|
|
|
|
c.sessionManager.DeleteSession(node)
|
2022-03-15 21:51:21 +08:00
|
|
|
err := c.channelManager.DeleteNode(node.NodeID)
|
|
|
|
if err == nil {
|
|
|
|
metrics.DataCoordNumDataNodes.WithLabelValues().Dec()
|
|
|
|
}
|
2022-03-25 12:09:25 +08:00
|
|
|
return err
|
2021-05-26 19:06:56 +08:00
|
|
|
}
|
|
|
|
|
2021-12-15 11:58:31 +08:00
|
|
|
// Watch tries to add a channel in datanode cluster
|
2021-10-14 15:44:34 +08:00
|
|
|
func (c *Cluster) Watch(ch string, collectionID UniqueID) error {
|
2021-10-26 20:15:41 +08:00
|
|
|
return c.channelManager.Watch(&channel{Name: ch, CollectionID: collectionID})
|
2021-10-14 15:44:34 +08:00
|
|
|
}
|
2021-05-26 19:06:56 +08:00
|
|
|
|
2022-10-22 12:09:28 +08:00
|
|
|
// Flush sends flush requests to dataNodes specified
|
|
|
|
// which also according to channels where segments are assigned to.
|
|
|
|
func (c *Cluster) Flush(ctx context.Context, nodeID int64, channel string,
|
2022-11-10 22:13:04 +08:00
|
|
|
segments []*datapb.SegmentInfo) error {
|
2022-10-22 12:09:28 +08:00
|
|
|
if !c.channelManager.Match(nodeID, channel) {
|
|
|
|
log.Warn("node is not matched with channel",
|
|
|
|
zap.String("channel", channel),
|
|
|
|
zap.Int64("nodeID", nodeID),
|
|
|
|
)
|
|
|
|
return fmt.Errorf("channel %s is not watched on node %d", channel, nodeID)
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
2022-10-22 12:09:28 +08:00
|
|
|
|
|
|
|
ch := c.channelManager.getChannelByNodeAndName(nodeID, channel)
|
|
|
|
|
|
|
|
getSegmentID := func(segment *datapb.SegmentInfo, _ int) int64 {
|
|
|
|
return segment.GetID()
|
2021-07-12 11:03:52 +08:00
|
|
|
}
|
|
|
|
|
2022-10-22 12:09:28 +08:00
|
|
|
req := &datapb.FlushSegmentsRequest{
|
|
|
|
Base: commonpbutil.NewMsgBase(
|
|
|
|
commonpbutil.WithMsgType(commonpb.MsgType_Flush),
|
2022-11-04 14:25:38 +08:00
|
|
|
commonpbutil.WithSourceID(paramtable.GetNodeID()),
|
2022-11-03 22:47:35 +08:00
|
|
|
commonpbutil.WithTargetID(nodeID),
|
2022-10-22 12:09:28 +08:00
|
|
|
),
|
2022-11-10 22:13:04 +08:00
|
|
|
CollectionID: ch.CollectionID,
|
|
|
|
SegmentIDs: lo.Map(segments, getSegmentID),
|
2021-01-28 11:24:41 +08:00
|
|
|
}
|
2022-10-22 12:09:28 +08:00
|
|
|
|
|
|
|
c.sessionManager.Flush(ctx, nodeID, req)
|
|
|
|
return nil
|
2021-07-12 11:03:52 +08:00
|
|
|
}
|
|
|
|
|
2022-04-01 11:33:28 +08:00
|
|
|
// Import sends import requests to DataNodes whose ID==nodeID.
|
|
|
|
func (c *Cluster) Import(ctx context.Context, nodeID int64, it *datapb.ImportTaskRequest) {
|
|
|
|
c.sessionManager.Import(ctx, nodeID, it)
|
|
|
|
}
|
|
|
|
|
2022-05-25 14:34:00 +08:00
|
|
|
// ReCollectSegmentStats triggers a ReCollectSegmentStats call from session manager.
|
2023-02-27 10:41:46 +08:00
|
|
|
func (c *Cluster) ReCollectSegmentStats(ctx context.Context) error {
|
|
|
|
for _, node := range c.sessionManager.getLiveNodeIDs() {
|
|
|
|
err := c.sessionManager.ReCollectSegmentStats(ctx, node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-05-25 14:34:00 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:44:34 +08:00
|
|
|
// GetSessions returns all sessions
|
|
|
|
func (c *Cluster) GetSessions() []*Session {
|
|
|
|
return c.sessionManager.GetSessions()
|
2021-07-12 11:03:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:44:34 +08:00
|
|
|
// Close releases resources opened in Cluster
|
2021-07-12 11:03:52 +08:00
|
|
|
func (c *Cluster) Close() {
|
2021-10-14 15:44:34 +08:00
|
|
|
c.sessionManager.Close()
|
2023-04-18 17:54:31 +08:00
|
|
|
c.channelManager.Close()
|
2021-02-02 18:53:10 +08:00
|
|
|
}
|